From 8935601c7c4dfac4d76cdac412d39c5c74180ba7 Mon Sep 17 00:00:00 2001 From: Nicole O'Brien Date: Tue, 5 Sep 2023 09:29:12 +0100 Subject: [PATCH 1/2] 547 refactor IToken and ITokenWithBalance (#548) * create base token constant * replace getCoinType with BASE_TOKEN_ID * refactor: to use ITokenWithBalance * TokenWithBalance into token information popup * feat: token with balance on slider * fix: use of token with balance * fix type checks * fix * move actions to correct folder * remove utils folder --------- Co-authored-by: Mark Nardi --- .../modals/AccountSwitcherModal.svelte | 6 +- .../BurnNativeTokensConfirmationPopup.svelte | 4 +- .../popups/BurnNativeTokensPopup.svelte | 8 +- .../popups/TokenInformationPopup.svelte | 16 ++-- .../views/InputTokenAmountView.svelte | 28 ++++-- .../send-flow/views/SelectTokenView.svelte | 15 +-- .../atoms/BalanceSummarySection.svelte | 4 +- .../components/inputs/TokenAmountInput.svelte | 7 +- .../inputs/TokenAmountWithSliderInput.svelte | 25 ++--- .../components/inputs/TokenDropdown.svelte | 96 ------------------- .../shared/src/components/inputs/index.js | 1 - .../labels/TogglableTokenBalanceLabel.svelte | 6 +- .../src/components/labels/TokenLabel.svelte | 23 +++++ .../shared/src/components/labels/index.js | 1 + .../modals/TokenActionsMenuModal.svelte | 14 +-- .../src/components/molecules/TokenList.svelte | 18 ++-- .../molecules/TransactionAssetSection.svelte | 16 +++- .../menu-items/AccountSwitcherMenuItem.svelte | 8 +- .../src/components/tiles/ActivityTile.svelte | 4 +- .../components/tiles/TokenAmountTile.svelte | 7 +- .../tiles/TokenAvailableBalanceTile.svelte | 4 +- .../account/stores/selected-account.store.ts | 2 +- .../getMarketAmountFromTokenValue.ts | 4 +- .../getMarketPriceForToken.ts | 4 +- .../src/lib/core/market/actions/index.ts | 2 + packages/shared/src/lib/core/market/index.ts | 1 - .../shared/src/lib/core/market/utils/index.ts | 2 - .../getAccountTokensForSelectedAccount.ts | 10 +- .../lib/core/token/actions/getTokenBalance.ts | 8 ++ .../src/lib/core/token/actions/index.ts | 1 + .../interfaces/account-tokens.interface.ts | 6 +- .../core/token/interfaces/token.interface.ts | 4 - .../stores/selected-account-tokens.store.ts | 11 ++- .../src/lib/core/token/utils/sortTokens.ts | 16 ++-- .../core/token/utils/validateTokenAmount.ts | 4 +- 35 files changed, 170 insertions(+), 216 deletions(-) delete mode 100644 packages/shared/src/components/inputs/TokenDropdown.svelte create mode 100644 packages/shared/src/components/labels/TokenLabel.svelte rename packages/shared/src/lib/core/market/{utils => actions}/getMarketAmountFromTokenValue.ts (81%) rename packages/shared/src/lib/core/market/{utils => actions}/getMarketPriceForToken.ts (59%) delete mode 100644 packages/shared/src/lib/core/market/utils/index.ts create mode 100644 packages/shared/src/lib/core/token/actions/getTokenBalance.ts diff --git a/packages/desktop/components/modals/AccountSwitcherModal.svelte b/packages/desktop/components/modals/AccountSwitcherModal.svelte index 74c31ed63a..2e4f94be74 100644 --- a/packages/desktop/components/modals/AccountSwitcherModal.svelte +++ b/packages/desktop/components/modals/AccountSwitcherModal.svelte @@ -3,7 +3,7 @@ import { sumBalanceForAccounts } from '@core/account' import { selectedAccount } from '@core/account/stores' import { formatCurrency, localize } from '@core/i18n' - import { getMarketAmountFromTokenValue } from '@core/market/utils' + import { getMarketAmountFromTokenValue } from '@core/market/actions' import { getBaseToken } from '@core/profile/actions' import { activeProfile, visibleActiveAccounts } from '@core/profile/stores' import { formatTokenAmountBestMatch } from '@core/token' @@ -15,7 +15,7 @@ export let modal: Modal = undefined $: totalBalance = sumBalanceForAccounts($visibleActiveAccounts) - $: ({ baseCoin } = $selectedAccountTokens[$activeProfile?.network.id]) + $: baseCoin = $selectedAccountTokens[$activeProfile?.network.id]?.baseCoin async function scrollToSelectedAccount(): Promise { await tick() @@ -55,7 +55,7 @@ {formatTokenAmountBestMatch(totalBalance, getBaseToken())} - {formatCurrency(getMarketAmountFromTokenValue(totalBalance, baseCoin))} + {baseCoin ? formatCurrency(getMarketAmountFromTokenValue(totalBalance, baseCoin)) : undefined} diff --git a/packages/desktop/components/popups/BurnNativeTokensConfirmationPopup.svelte b/packages/desktop/components/popups/BurnNativeTokensConfirmationPopup.svelte index 4e702f4ffb..d4cbc90956 100644 --- a/packages/desktop/components/popups/BurnNativeTokensConfirmationPopup.svelte +++ b/packages/desktop/components/popups/BurnNativeTokensConfirmationPopup.svelte @@ -4,13 +4,13 @@ import { handleError } from '@core/error/handlers' import { localize } from '@core/i18n' import { checkActiveProfileAuth } from '@core/profile/actions' - import { IToken, formatTokenAmountBestMatch } from '@core/token' + import { ITokenWithBalance, formatTokenAmountBestMatch } from '@core/token' import { burnToken } from '@core/wallet' import { PopupId, closePopup, openPopup } from '@desktop/auxiliary/popup' import { Button, ButtonVariant, FontWeight, Text, TextHint, TextType } from '@ui' import { onMount } from 'svelte' - export let token: IToken + export let token: ITokenWithBalance export let rawAmount: string export let _onMount: (..._: any[]) => Promise = async () => {} diff --git a/packages/desktop/components/popups/BurnNativeTokensPopup.svelte b/packages/desktop/components/popups/BurnNativeTokensPopup.svelte index 6dcec73118..2744bf5c05 100644 --- a/packages/desktop/components/popups/BurnNativeTokensPopup.svelte +++ b/packages/desktop/components/popups/BurnNativeTokensPopup.svelte @@ -1,10 +1,10 @@ - -{#if token} -
- - {#if isDropdownOpen && !isReadonly} - - {/if} -
-{/if} - - diff --git a/packages/shared/src/components/inputs/index.js b/packages/shared/src/components/inputs/index.js index 9441626a93..241aa93bd9 100644 --- a/packages/shared/src/components/inputs/index.js +++ b/packages/shared/src/components/inputs/index.js @@ -26,5 +26,4 @@ export { default as TogglableButton } from './TogglableButton.svelte' export { default as Toggle } from './Toggle.svelte' export { default as TokenAmountInput } from './TokenAmountInput.svelte' export { default as TokenAmountWithSliderInput } from './TokenAmountWithSliderInput.svelte' -export { default as TokenDropdown } from './TokenDropdown.svelte' export { default as UnitInput } from './UnitInput.svelte' diff --git a/packages/shared/src/components/labels/TogglableTokenBalanceLabel.svelte b/packages/shared/src/components/labels/TogglableTokenBalanceLabel.svelte index 2449f76435..21a20b30a6 100644 --- a/packages/shared/src/components/labels/TogglableTokenBalanceLabel.svelte +++ b/packages/shared/src/components/labels/TogglableTokenBalanceLabel.svelte @@ -1,10 +1,10 @@ + +{#if token} +
+ +
+ + {token.metadata?.name ?? token.id} + +
+
+{/if} diff --git a/packages/shared/src/components/labels/index.js b/packages/shared/src/components/labels/index.js index 0ddee3b7b0..0302cf589a 100644 --- a/packages/shared/src/components/labels/index.js +++ b/packages/shared/src/components/labels/index.js @@ -1,3 +1,4 @@ export { default as AccountLabel } from './AccountLabel' export { default as ColorLabel } from './ColorLabel' export { default as TogglableTokenBalanceLabel } from './TogglableTokenBalanceLabel' +export { default as TokenLabel } from './TokenLabel' diff --git a/packages/shared/src/components/modals/TokenActionsMenuModal.svelte b/packages/shared/src/components/modals/TokenActionsMenuModal.svelte index 22812b9fef..1302ae2069 100644 --- a/packages/shared/src/components/modals/TokenActionsMenuModal.svelte +++ b/packages/shared/src/components/modals/TokenActionsMenuModal.svelte @@ -1,17 +1,17 @@ {#if baseCoinTransfer || tokenTransfer || nft || aliasId} - {#if tokenTransfer?.token} - + {#if tokenTransfer?.token && tokenWithBalance} + {:else if nft} {:else if aliasId} {/if} - {#if baseCoinTransfer?.token && Number(baseCoinTransfer?.rawAmount) > 0} + {#if baseCoinTransfer?.token && baseTokenWithBalance && Number(baseCoinTransfer?.rawAmount) > 0} diff --git a/packages/shared/src/components/molecules/menu-items/AccountSwitcherMenuItem.svelte b/packages/shared/src/components/molecules/menu-items/AccountSwitcherMenuItem.svelte index f308964c14..fb731fe5d9 100644 --- a/packages/shared/src/components/molecules/menu-items/AccountSwitcherMenuItem.svelte +++ b/packages/shared/src/components/molecules/menu-items/AccountSwitcherMenuItem.svelte @@ -3,7 +3,7 @@ import { setSelectedAccount } from '@core/account/actions' import { selectedAccount } from '@core/account/stores' import { formatCurrency } from '@core/i18n' - import { getMarketAmountFromTokenValue } from '@core/market/utils' + import { getMarketAmountFromTokenValue } from '@core/market/actions' import { getBaseToken } from '@core/profile/actions' import { activeProfile } from '@core/profile/stores' import { formatTokenAmountBestMatch } from '@core/token' @@ -14,7 +14,7 @@ export let onClick: () => unknown export let id: string = '' - $: ({ baseCoin } = $selectedAccountTokens[$activeProfile?.network?.id]) + $: baseCoin = $selectedAccountTokens[$activeProfile?.network?.id]?.baseCoin function onAccountClick(accountIndex: number): void { setSelectedAccount(accountIndex) @@ -46,7 +46,9 @@ color="blue-500" classes="{account.index === $selectedAccount?.index ? '' : 'opacity-50'} text-right" > - {formatCurrency(getMarketAmountFromTokenValue(Number(account.balances.baseCoin.total), baseCoin))} + {baseCoin + ? formatCurrency(getMarketAmountFromTokenValue(Number(account.balances.baseCoin.total), baseCoin)) + : ''} diff --git a/packages/shared/src/components/tiles/ActivityTile.svelte b/packages/shared/src/components/tiles/ActivityTile.svelte index f3697e48a0..3ad968bc57 100644 --- a/packages/shared/src/components/tiles/ActivityTile.svelte +++ b/packages/shared/src/components/tiles/ActivityTile.svelte @@ -1,7 +1,7 @@ @@ -37,7 +37,7 @@ {/if} - {#if storageDeposit || giftStorageDeposit} + {#if storageDeposit || (giftStorageDeposit && !isToLayer2)}
{localize('general.storageDeposit')} @@ -56,20 +56,12 @@ />
{/if} - {#if estimatedGasFee} + {#if transactionFee}
- {localize('general.estimatedFee')} + {localize('general.transactionFee')}
- {formatTokenAmountBestMatch(Number(estimatedGasFee), getBaseToken())} -
- {/if} - {#if maxGasFee} -
-
- {localize('general.maxFees')} -
- {formatTokenAmountBestMatch(Number(maxGasFee), getBaseToken())} + {formatTokenAmountBestMatch(Number(transactionFee), getBaseToken())}
{/if} {#if selectedExpirationPeriod} diff --git a/packages/desktop/views/dashboard/send-flow/views/components/StardustTransactionSummary.svelte b/packages/desktop/views/dashboard/send-flow/views/components/StardustTransactionSummary.svelte index 0455faf6ee..4f8a6686e8 100644 --- a/packages/desktop/views/dashboard/send-flow/views/components/StardustTransactionSummary.svelte +++ b/packages/desktop/views/dashboard/send-flow/views/components/StardustTransactionSummary.svelte @@ -2,14 +2,12 @@ import { selectedAccount } from '@core/account/stores' import { getStorageDepositFromOutput } from '@core/activity/utils/helper' import { localize } from '@core/i18n' - import { getGasFeesForLayer1ToLayer2Transaction } from '@core/layer-2/actions' - import { getNetwork, isEvmChain } from '@core/network' + import { getActiveNetworkId, isEvmChain } from '@core/network' import { INft } from '@core/nfts/interfaces' import { selectedAccountTokens } from '@core/token/stores' import { TimePeriod } from '@core/utils/enums' import { Output, SendFlowParameters, TokenTransferData } from '@core/wallet' import { SendFlowType, updateSendFlowParameters } from '@core/wallet/stores' - import { BigIntLike } from '@ethereumjs/util' import { AddInputButton, OptionalInput, TransactionAssetSection } from '@ui' import { onMount } from 'svelte' import StardustTransactionDetails from './StardustTransactionDetails.svelte' @@ -28,7 +26,6 @@ disableToggleGift, } = sendFlowParameters - let baseCoinTransfer: TokenTransferData let storageDeposit: number let tagInput: OptionalInput let metadataInput: OptionalInput @@ -38,6 +35,7 @@ $: isTransferring = !!$selectedAccount.isTransferring $: updateSendFlowOnChange(expirationDate, timelockDate, giftStorageDeposit, tag, metadata) + $: storageDeposit = getStorageDepositFromOutput(output) const isToLayer2 = destinationNetworkId && isEvmChain(destinationNetworkId) disableGiftingStorageDeposit(isToLayer2) @@ -67,17 +65,6 @@ } } - let estimatedGasFee: BigIntLike | undefined = undefined - let maxGasFee: BigIntLike | undefined = undefined - async function setGasVariables(sendFlowParameters: SendFlowParameters): Promise { - if (isToLayer2) { - const gasFees = await getGasFeesForLayer1ToLayer2Transaction(sendFlowParameters) - estimatedGasFee = gasFees.estimatedGasFee - maxGasFee = gasFees.maxGasFee - } - } - $: void setGasVariables(sendFlowParameters) - function disableGiftingStorageDeposit(isToLayer2: boolean) { if (isToLayer2) { disableToggleGift = true @@ -85,24 +72,47 @@ } } - function setBaseCoinAndStorageDeposit(output: Output, maxGasFee: BigIntLike | undefined): void { - storageDeposit = getStorageDepositFromOutput(output) - baseCoinTransfer = { - token: $selectedAccountTokens?.[getNetwork().getMetadata().id].baseCoin, - rawAmount: String(Number(output.amount) - storageDeposit - Number(maxGasFee ?? 0)), - } - } - $: setBaseCoinAndStorageDeposit(output, maxGasFee) + $: transactionFee = isToLayer2 + ? sendFlowParameters.type === SendFlowType.BaseCoinTransfer + ? String(Number(output.amount) - Number(sendFlowParameters.baseCoinTransfer.rawAmount)) + : output.amount + : 0 - function getTransactionAsset(sendFlowParameters: SendFlowParameters): { - tokenTransfer?: TokenTransferData + function getTransactionAssets( + output: Output, + sendFlowParameters: SendFlowParameters + ): { nft?: INft + tokenTransfer?: TokenTransferData + baseCoinTransfer?: TokenTransferData } { - return { - ...(sendFlowParameters.type === SendFlowType.TokenTransfer && { - tokenTransfer: sendFlowParameters.tokenTransfer, - }), - ...(sendFlowParameters.type === SendFlowType.NftTransfer && { nft: sendFlowParameters.nft }), + const baseCoin = $selectedAccountTokens?.[getActiveNetworkId()].baseCoin + + if (sendFlowParameters.type === SendFlowType.BaseCoinTransfer) { + return { + baseCoinTransfer: { + token: baseCoin, + rawAmount: isToLayer2 + ? sendFlowParameters.baseCoinTransfer.rawAmount + : String(Number(output.amount) - storageDeposit), + }, + } + } else { + const baseCoinTransfer = { + token: baseCoin, + rawAmount: isToLayer2 ? '0' : String(Number(output.amount) - storageDeposit), + } + if (sendFlowParameters.type === SendFlowType.TokenTransfer) { + return { + tokenTransfer: sendFlowParameters.tokenTransfer, + baseCoinTransfer, + } + } else { + return { + nft: sendFlowParameters.nft, + baseCoinTransfer, + } + } } } @@ -117,13 +127,13 @@ } onMount(() => { - setBaseCoinAndStorageDeposit(output, maxGasFee) + storageDeposit = getStorageDepositFromOutput(output) selectedExpirationPeriod = getInitialExpirationDate(!!expirationDate, !!storageDeposit, giftStorageDeposit) })
- +