Changeset View
Changeset View
Standalone View
Standalone View
cashtab/src/components/Etokens/Token/index.tsx
| Show All 24 Lines | import { | ||||
| getAgoraMinBuyError, | getAgoraMinBuyError, | ||||
| } from 'validation'; | } from 'validation'; | ||||
| import BigNumber from 'bignumber.js'; | import BigNumber from 'bignumber.js'; | ||||
| import { | import { | ||||
| formatDate, | formatDate, | ||||
| getFormattedFiatPrice, | getFormattedFiatPrice, | ||||
| getAgoraSpotPriceXec, | getAgoraSpotPriceXec, | ||||
| decimalizedTokenQtyToLocaleFormat, | decimalizedTokenQtyToLocaleFormat, | ||||
| toFormattedXec, | |||||
| } from 'formatting'; | } from 'formatting'; | ||||
| import TokenIcon from 'components/Etokens/TokenIcon'; | import TokenIcon from 'components/Etokens/TokenIcon'; | ||||
| import { explorer } from 'config/explorer'; | import { explorer } from 'config/explorer'; | ||||
| import { token as tokenConfig } from 'config/token'; | import { token as tokenConfig } from 'config/token'; | ||||
| import { isValidCashAddress } from 'ecashaddrjs'; | import { isValidCashAddress } from 'ecashaddrjs'; | ||||
| import appConfig from 'config/app'; | import appConfig from 'config/app'; | ||||
| import { getUserLocale } from 'helpers'; | import { getUserLocale } from 'helpers'; | ||||
| import { | import { | ||||
| ▲ Show 20 Lines • Show All 108 Lines • ▼ Show 20 Lines | |||||
| } from 'ecash-agora'; | } from 'ecash-agora'; | ||||
| import OrderBook from 'components/Agora/OrderBook'; | import OrderBook from 'components/Agora/OrderBook'; | ||||
| import Collection, { | import Collection, { | ||||
| OneshotSwiper, | OneshotSwiper, | ||||
| OneshotOffer, | OneshotOffer, | ||||
| } from 'components/Agora/Collection'; | } from 'components/Agora/Collection'; | ||||
| import { CashtabCachedTokenInfo } from 'config/CashtabCache'; | import { CashtabCachedTokenInfo } from 'config/CashtabCache'; | ||||
| import { confirmRawTx } from 'components/Send/helpers'; | import { confirmRawTx } from 'components/Send/helpers'; | ||||
| import { FIRMA } from 'constants/tokens'; | import { FIRMA, XECX_SWEEPER_ADDRESS } from 'constants/tokens'; | ||||
| const Token: React.FC = () => { | const Token: React.FC = () => { | ||||
| const ContextValue = useContext(WalletContext); | const ContextValue = useContext(WalletContext); | ||||
| if (!isWalletContextLoaded(ContextValue)) { | if (!isWalletContextLoaded(ContextValue)) { | ||||
| // Confirm we have all context required to load the page | // Confirm we have all context required to load the page | ||||
| return null; | return null; | ||||
| } | } | ||||
| const { | const { | ||||
| ▲ Show 20 Lines • Show All 253 Lines • ▼ Show 20 Lines | const Token: React.FC = () => { | ||||
| }; | }; | ||||
| const [formData, setFormData] = | const [formData, setFormData] = | ||||
| useState<TokenScreenFormData>(emptyFormData); | useState<TokenScreenFormData>(emptyFormData); | ||||
| const [isCalculatingRedeemFirma, setIsCalculatingRedeemFirma] = | const [isCalculatingRedeemFirma, setIsCalculatingRedeemFirma] = | ||||
| useState<boolean>(false); | useState<boolean>(false); | ||||
| const [xecxSweeperBalanceSats, setXecxSweeperBalanceSats] = useState< | |||||
| null | bigint | |||||
| >(null); | |||||
| const userLocale = getUserLocale(navigator); | const userLocale = getUserLocale(navigator); | ||||
| /** | |||||
| * Convenience method to compartmentalize comparison of state | |||||
| * variables that require calculations | |||||
| * | |||||
| * TODO dynamically size modals | |||||
| */ | |||||
| const getRedeemXecxModalHeight = () => { | |||||
| const askedQtyStr = Number(agoraPartialTokenQty).toFixed(2); | |||||
| const actualQtyStr = decimalizeTokenAmount( | |||||
| previewedAgoraPartial!.offeredAtoms().toString(), | |||||
| decimals as SlpDecimals, | |||||
| ); | |||||
| const offeredXecxSats = previewedAgoraPartial!.offeredAtoms(); | |||||
| let baseHeight = 175; | |||||
| if ( | |||||
| xecxSweeperBalanceSats !== null && | |||||
| offeredXecxSats > xecxSweeperBalanceSats | |||||
| ) { | |||||
| // If we need to show the "redemption may take 24 hrs" notice | |||||
| baseHeight += 125; | |||||
| } | |||||
| if (askedQtyStr !== actualQtyStr) { | |||||
| // If we need to show the "agora values differ" notice | |||||
| baseHeight += 125; | |||||
| } | |||||
| return baseHeight; | |||||
| }; | |||||
| const getAgoraPartialActualPrice = () => { | const getAgoraPartialActualPrice = () => { | ||||
| if (previewedAgoraPartial === null) { | if (previewedAgoraPartial === null) { | ||||
| return; | return; | ||||
| } | } | ||||
| // Due to encoding limitations of agora offers, the actual price may vary depending on how | // Due to encoding limitations of agora offers, the actual price may vary depending on how | ||||
| // much of an offer the buyer accepts | // much of an offer the buyer accepts | ||||
| // Calculate the actual price by determining the price per token for the minimum buy of the created preview offer | // Calculate the actual price by determining the price per token for the minimum buy of the created preview offer | ||||
| ▲ Show 20 Lines • Show All 140 Lines • ▼ Show 20 Lines | const addTokenToCashtabCache = async (tokenId: string) => { | ||||
| cashtabCache.tokens.set(tokenId, cachedInfoWithGroupTokenId); | cashtabCache.tokens.set(tokenId, cachedInfoWithGroupTokenId); | ||||
| updateCashtabState('cashtabCache', cashtabCache); | updateCashtabState('cashtabCache', cashtabCache); | ||||
| } catch (err) { | } catch (err) { | ||||
| console.error(`Error getting token details for ${tokenId}`, err); | console.error(`Error getting token details for ${tokenId}`, err); | ||||
| setChronikQueryError(true); | setChronikQueryError(true); | ||||
| } | } | ||||
| }; | }; | ||||
| const getXecxSweeperBalance = async () => { | |||||
| let sweeperUtxos; | |||||
| try { | |||||
| sweeperUtxos = (await chronik.address(XECX_SWEEPER_ADDRESS).utxos()) | |||||
| .utxos; | |||||
| const balanceSats = sweeperUtxos | |||||
| .map(utxo => utxo.sats) | |||||
| .reduce((prev, curr) => prev + curr, 0n); | |||||
| setXecxSweeperBalanceSats(balanceSats); | |||||
| } catch (err) { | |||||
| // If there is some error in getting the utxo set of the sweeper address, | |||||
| // we will not be able to get the balance, and Cashtab will simply not show | |||||
| // this information | |||||
| console.error(`Error getting XECX Sweeper balance`, err); | |||||
| } | |||||
| }; | |||||
| useEffect(() => { | |||||
| if (tokenId === appConfig.vipTokens.xecx.tokenId) { | |||||
| // Get XECX sweeper balance when user is on xecx token page | |||||
| getXecxSweeperBalance(); | |||||
| } | |||||
| }, [tokenId]); | |||||
| useEffect(() => { | useEffect(() => { | ||||
| if ( | if ( | ||||
| typeof tokenId === 'undefined' || | typeof tokenId === 'undefined' || | ||||
| typeof tokenType === 'undefined' || | typeof tokenType === 'undefined' || | ||||
| !cachedInfoLoaded | !cachedInfoLoaded | ||||
| ) { | ) { | ||||
| return; | return; | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 1,714 Lines • ▼ Show 20 Lines | return ( | ||||
| setShowConfirmListNft(false) | setShowConfirmListNft(false) | ||||
| } | } | ||||
| showCancelButton | showCancelButton | ||||
| description={`This will create a sell offer. Your NFT is only transferred if your full price is paid. The price is fixed in XEC. If your NFT is not purchased, you can cancel or renew your listing at any time.`} | description={`This will create a sell offer. Your NFT is only transferred if your full price is paid. The price is fixed in XEC. If your NFT is not purchased, you can cancel or renew your listing at any time.`} | ||||
| height={275} | height={275} | ||||
| /> | /> | ||||
| )} | )} | ||||
| {showConfirmListPartialSlp && | {showConfirmListPartialSlp && | ||||
| tokenId === appConfig.vipTokens.xecx.tokenId && | |||||
| previewedAgoraPartial !== null && ( | |||||
| <Modal | |||||
| title={`Redeem ${decimalizedTokenQtyToLocaleFormat( | |||||
| decimalizeTokenAmount( | |||||
| previewedAgoraPartial | |||||
| .offeredAtoms() | |||||
| .toString(), | |||||
| decimals as SlpDecimals, | |||||
| ), | |||||
| userLocale, | |||||
| )} XECX?`} | |||||
| disabled={previewedAgoraPartialUnacceptable} | |||||
| handleOk={listAlpPartial} | |||||
| handleCancel={() => { | |||||
| setPreviewedAgoraPartial(null); | |||||
| }} | |||||
| showCancelButton | |||||
| height={getRedeemXecxModalHeight()} | |||||
| > | |||||
| {decimalizeTokenAmount( | |||||
| previewedAgoraPartial | |||||
| .offeredAtoms() | |||||
| .toString(), | |||||
| decimals as SlpDecimals, | |||||
| ) !== | |||||
| Number(agoraPartialTokenQty).toFixed(2) && ( | |||||
| <AgoraPreviewTable> | |||||
| <AgoraPreviewRow> | |||||
| <AgoraPreviewLabel> | |||||
| Requested qty:{' '} | |||||
| </AgoraPreviewLabel> | |||||
| <AgoraPreviewCol> | |||||
| {Number( | |||||
| agoraPartialTokenQty, | |||||
| ).toLocaleString(userLocale, { | |||||
| minimumFractionDigits: 2, | |||||
| maximumFractionDigits: 2, | |||||
| })}{' '} | |||||
| XECX | |||||
| </AgoraPreviewCol> | |||||
| </AgoraPreviewRow> | |||||
| <AgoraPreviewRow> | |||||
| <AgoraPreviewLabel> | |||||
| Actual qty:{' '} | |||||
| </AgoraPreviewLabel> | |||||
| <AgoraPreviewCol> | |||||
| {decimalizedTokenQtyToLocaleFormat( | |||||
| decimalizeTokenAmount( | |||||
| previewedAgoraPartial | |||||
| .offeredAtoms() | |||||
| .toString(), | |||||
| decimals as SlpDecimals, | |||||
| ), | |||||
| userLocale, | |||||
| )}{' '} | |||||
| XECX | |||||
| </AgoraPreviewCol> | |||||
| </AgoraPreviewRow> | |||||
| <AgoraPreviewRow> | |||||
| <AgoraPreviewLabel> | |||||
| Delta:{' '} | |||||
| </AgoraPreviewLabel> | |||||
| <AgoraPreviewCol> | |||||
| {( | |||||
| Number( | |||||
| agoraPartialTokenQty, | |||||
| ) - | |||||
| Number( | |||||
| decimalizeTokenAmount( | |||||
| previewedAgoraPartial | |||||
| .offeredAtoms() | |||||
| .toString(), | |||||
| decimals as SlpDecimals, | |||||
| ), | |||||
| ) | |||||
| ).toFixed(2)} | |||||
| </AgoraPreviewCol> | |||||
| </AgoraPreviewRow> | |||||
| <Info> | |||||
| Note: Actual qty does not exactly | |||||
| match requested qty due to agora | |||||
| encoding. | |||||
| </Info> | |||||
| </AgoraPreviewTable> | |||||
| )} | |||||
| <AgoraPreviewTable> | |||||
| {previewedAgoraPartialUnacceptable && ( | |||||
| <Alert noWordBreak> | |||||
| Error in agora encoding: | |||||
| unacceptable offer created. Adjust | |||||
| redeem amount. | |||||
| </Alert> | |||||
| )} | |||||
| <AgoraPreviewRow> | |||||
| <AgoraPreviewLabel> | |||||
| You receive:{' '} | |||||
| </AgoraPreviewLabel> | |||||
| <AgoraPreviewCol> | |||||
| {decimalizedTokenQtyToLocaleFormat( | |||||
| decimalizeTokenAmount( | |||||
| previewedAgoraPartial | |||||
| .offeredAtoms() | |||||
| .toString(), | |||||
| decimals as SlpDecimals, | |||||
| ), | |||||
| userLocale, | |||||
| )}{' '} | |||||
| XEC | |||||
| </AgoraPreviewCol> | |||||
| </AgoraPreviewRow> | |||||
| {xecxSweeperBalanceSats !== null && | |||||
| previewedAgoraPartial.offeredAtoms() > | |||||
| xecxSweeperBalanceSats && ( | |||||
| <Alert noWordBreak> | |||||
| ⚠️ XECX redemption larger than | |||||
| hot wallet balance of{' '} | |||||
| {toFormattedXec( | |||||
| Number( | |||||
| xecxSweeperBalanceSats, | |||||
| ), | |||||
| userLocale, | |||||
| )}{' '} | |||||
| XEC. Execution may take up to 24 | |||||
| hours. | |||||
| </Alert> | |||||
| )} | |||||
| </AgoraPreviewTable> | |||||
| </Modal> | |||||
| )} | |||||
| {showConfirmListPartialSlp && | |||||
| tokenId !== appConfig.vipTokens.xecx.tokenId && | |||||
| (formData.tokenListPrice !== '' || | (formData.tokenListPrice !== '' || | ||||
| tokenId === FIRMA.tokenId) && | tokenId === FIRMA.tokenId) && | ||||
| previewedAgoraPartial !== null && ( | previewedAgoraPartial !== null && ( | ||||
| <Modal | <Modal | ||||
| title={`List ${tokenTicker}?`} | title={`List ${tokenTicker}?`} | ||||
| disabled={previewedAgoraPartialUnacceptable} | disabled={previewedAgoraPartialUnacceptable} | ||||
| handleOk={ | handleOk={ | ||||
| isAlp ? listAlpPartial : listSlpPartial | isAlp ? listAlpPartial : listSlpPartial | ||||
| ▲ Show 20 Lines • Show All 1,362 Lines • Show Last 20 Lines | |||||