From 3e12b365c40053cb6af68bd10d7f0d582896fa1b Mon Sep 17 00:00:00 2001 From: Tuditi <45079109+Tuditi@users.noreply.github.com> Date: Fri, 8 Sep 2023 16:18:23 +0200 Subject: [PATCH] chore: send flow type to enum (#532) * Adjust network recipient selection * Integrate token unwrapping * Polish code for review * Fix type checker * Cleanup code * Fix imports * Add gas fee support for native token unwrapping * Only allow unwrapping for appropriate token types * Cleanup code * chore: move SendFlowType into separate file * chore: consistent casing * chore: cleanup logic * fix: update imports * fix: import --------- Co-authored-by: Matthew Maxwell Co-authored-by: Mark Nardi --- .../popups/TokenInformationPopup.svelte | 4 ++-- .../views/CollectiblesDetailsView.svelte | 4 ++-- .../views/InputTokenAmountView.svelte | 23 ++++++++----------- .../send-flow/views/SelectTokenView.svelte | 2 +- .../components/EvmTransactionSummary.svelte | 3 +-- .../StardustTransactionSummary.svelte | 4 ++-- .../actions/canAccountMakeEvmTransaction.ts | 2 +- .../fallback-estimated-gas.constant.ts | 2 +- .../deconstructLedgerVerificationProps.ts | 3 ++- .../createEvmChainToEvmChainTransaction.ts | 2 +- ...ateEvmChainToStardustNetworkTransaction.ts | 2 +- .../shared/src/lib/core/wallet/enums/index.ts | 1 + .../core/wallet/enums/send-flow-type.enum.ts | 6 +++++ .../stores/send-flow-parameter.store.ts | 9 +------- .../wallet/tests/getOutputParameters.test.ts | 2 +- .../wallet/types/send-flow-parameter.type.ts | 2 +- .../getNetworkIdFromSendFlowParameters.ts | 2 +- .../core/wallet/utils/getOutputParameters.ts | 2 +- .../utils/getTokenIdFromSendFlowParameters.ts | 2 +- .../getTokenStandardFromSendFlowParameters.ts | 2 +- 20 files changed, 38 insertions(+), 41 deletions(-) create mode 100644 packages/shared/src/lib/core/wallet/enums/send-flow-type.enum.ts diff --git a/packages/desktop/components/popups/TokenInformationPopup.svelte b/packages/desktop/components/popups/TokenInformationPopup.svelte index 0053342247..3b1439a78f 100644 --- a/packages/desktop/components/popups/TokenInformationPopup.svelte +++ b/packages/desktop/components/popups/TokenInformationPopup.svelte @@ -45,8 +45,8 @@ const sendFlowType = token.id === BASE_TOKEN_ID ? SendFlowType.BaseCoinTransfer : SendFlowType.TokenTransfer setSendFlowParameters({ type: sendFlowType, - [sendFlowType === SendFlowType.BaseCoinTransfer ? 'baseCoinTransfer' : 'tokenTransfer']: { - token: token, + [sendFlowType]: { + token, }, }) diff --git a/packages/desktop/views/dashboard/collectibles/views/CollectiblesDetailsView.svelte b/packages/desktop/views/dashboard/collectibles/views/CollectiblesDetailsView.svelte index 9385e640ea..391e4ff8fb 100644 --- a/packages/desktop/views/dashboard/collectibles/views/CollectiblesDetailsView.svelte +++ b/packages/desktop/views/dashboard/collectibles/views/CollectiblesDetailsView.svelte @@ -13,9 +13,9 @@ import { getBaseToken } from '@core/profile/actions' import { collectiblesRouter } from '@core/router/routers' import { formatTokenAmountPrecise } from '@core/token' - import { getBech32AddressFromAddressTypes, getHexAddressFromAddressTypes } from '@core/wallet' + import { getBech32AddressFromAddressTypes, getHexAddressFromAddressTypes, SendFlowType } from '@core/wallet' import { getTimeDifference } from '@core/utils' - import { SendFlowType, setSendFlowParameters } from '@core/wallet/stores' + import { setSendFlowParameters } from '@core/wallet/stores' import { PopupId, openPopup } from '@desktop/auxiliary/popup' import { Button, Modal, NftMedia, Pane, Text } from '@ui' import { FontWeight, TextType } from '@ui/enums' diff --git a/packages/desktop/views/dashboard/send-flow/views/InputTokenAmountView.svelte b/packages/desktop/views/dashboard/send-flow/views/InputTokenAmountView.svelte index 48f553601d..1a3b5baa90 100644 --- a/packages/desktop/views/dashboard/send-flow/views/InputTokenAmountView.svelte +++ b/packages/desktop/views/dashboard/send-flow/views/InputTokenAmountView.svelte @@ -13,18 +13,15 @@ let rawAmount: string let amount: string let unit: string - const tokenKey = $sendFlowParameters.type === SendFlowType.TokenTransfer ? 'tokenTransfer' : 'baseCoinTransfer' - if ( - $sendFlowParameters.type === SendFlowType.BaseCoinTransfer || - $sendFlowParameters.type === SendFlowType.TokenTransfer - ) { + const sendFlowType = $sendFlowParameters.type + if (sendFlowType === SendFlowType.BaseCoinTransfer || sendFlowType === SendFlowType.TokenTransfer) { token = getTokenFromSelectedAccountTokens( - $sendFlowParameters[tokenKey].token?.id, - $sendFlowParameters[tokenKey].token?.networkId + $sendFlowParameters[sendFlowType].token?.id, + $sendFlowParameters[sendFlowType].token?.networkId ) - rawAmount = $sendFlowParameters[tokenKey].rawAmount - unit = $sendFlowParameters[tokenKey].unit || getUnitFromTokenMetadata(token?.metadata) + rawAmount = $sendFlowParameters[sendFlowType].rawAmount + unit = $sendFlowParameters[sendFlowType].unit || getUnitFromTokenMetadata(token?.metadata) } $: tokenBalance = getTokenBalance(token?.id, token?.networkId) @@ -43,8 +40,8 @@ updateSendFlowParameters({ type: $sendFlowParameters.type, - [tokenKey]: { - token: $sendFlowParameters[tokenKey].token, + [sendFlowType]: { + token, rawAmount, unit, }, @@ -58,8 +55,8 @@ function onBackClick(): void { updateSendFlowParameters({ type: $sendFlowParameters.type, - [tokenKey]: { - token: $sendFlowParameters[tokenKey].token, + [sendFlowType]: { + token, rawAmount: undefined, unit, }, diff --git a/packages/desktop/views/dashboard/send-flow/views/SelectTokenView.svelte b/packages/desktop/views/dashboard/send-flow/views/SelectTokenView.svelte index 7516474217..308378c3b7 100644 --- a/packages/desktop/views/dashboard/send-flow/views/SelectTokenView.svelte +++ b/packages/desktop/views/dashboard/send-flow/views/SelectTokenView.svelte @@ -123,7 +123,7 @@ setSendFlowParameters({ ...previousSharedParameters, type: sendFlowType, - [sendFlowType === SendFlowType.BaseCoinTransfer ? 'baseCoinTransfer' : 'tokenTransfer']: { + [sendFlowType]: { token: selectedToken, }, }) diff --git a/packages/desktop/views/dashboard/send-flow/views/components/EvmTransactionSummary.svelte b/packages/desktop/views/dashboard/send-flow/views/components/EvmTransactionSummary.svelte index 3bcf365a52..e31766ad05 100644 --- a/packages/desktop/views/dashboard/send-flow/views/components/EvmTransactionSummary.svelte +++ b/packages/desktop/views/dashboard/send-flow/views/components/EvmTransactionSummary.svelte @@ -5,8 +5,7 @@ calculateMaxGasFeeFromTransactionData, } from '@core/layer-2' import { INft } from '@core/nfts' - import { SendFlowParameters, TokenTransferData } from '@core/wallet' - import { SendFlowType } from '@core/wallet/stores' + import { SendFlowParameters, SendFlowType, TokenTransferData } from '@core/wallet' import { TransactionAssetSection } from '@ui' import EvmTransactionDetails from './EvmTransactionDetails.svelte' 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 4f8a6686e8..80da8a0bdb 100644 --- a/packages/desktop/views/dashboard/send-flow/views/components/StardustTransactionSummary.svelte +++ b/packages/desktop/views/dashboard/send-flow/views/components/StardustTransactionSummary.svelte @@ -6,8 +6,8 @@ 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 { Output, SendFlowParameters, SendFlowType, TokenTransferData } from '@core/wallet' + import { updateSendFlowParameters } from '@core/wallet/stores' import { AddInputButton, OptionalInput, TransactionAssetSection } from '@ui' import { onMount } from 'svelte' import StardustTransactionDetails from './StardustTransactionDetails.svelte' diff --git a/packages/shared/src/lib/core/layer-2/actions/canAccountMakeEvmTransaction.ts b/packages/shared/src/lib/core/layer-2/actions/canAccountMakeEvmTransaction.ts index 8e10a077c7..a4126145fc 100644 --- a/packages/shared/src/lib/core/layer-2/actions/canAccountMakeEvmTransaction.ts +++ b/packages/shared/src/lib/core/layer-2/actions/canAccountMakeEvmTransaction.ts @@ -1,6 +1,6 @@ import { NetworkId } from '@core/network/types' import { isEvmChain } from '@core/network/utils' -import { SendFlowType } from '@core/wallet/stores' +import { SendFlowType } from '@core/wallet/enums' import { getEvmChainGasPrice, getLayer2AccountBalanceForToken } from '../stores' diff --git a/packages/shared/src/lib/core/layer-2/constants/fallback-estimated-gas.constant.ts b/packages/shared/src/lib/core/layer-2/constants/fallback-estimated-gas.constant.ts index f29aa71fc1..5ef9fb995f 100644 --- a/packages/shared/src/lib/core/layer-2/constants/fallback-estimated-gas.constant.ts +++ b/packages/shared/src/lib/core/layer-2/constants/fallback-estimated-gas.constant.ts @@ -1,4 +1,4 @@ -import { SendFlowType } from '@core/wallet/stores' +import { SendFlowType } from '@core/wallet/enums' /** * We need a fallback estimated gas, since we cannot estimate calls to the magic contract if there is no balance on the sender address. diff --git a/packages/shared/src/lib/core/ledger/helpers/deconstructLedgerVerificationProps.ts b/packages/shared/src/lib/core/ledger/helpers/deconstructLedgerVerificationProps.ts index 7c4a6b994c..786ee021ac 100644 --- a/packages/shared/src/lib/core/ledger/helpers/deconstructLedgerVerificationProps.ts +++ b/packages/shared/src/lib/core/ledger/helpers/deconstructLedgerVerificationProps.ts @@ -1,5 +1,6 @@ import { formatTokenAmountDefault } from '@core/token' -import { SendFlowType, sendFlowParameters } from '@core/wallet/stores' +import { sendFlowParameters } from '@core/wallet/stores' +import { SendFlowType } from '@core/wallet/enums' import { get } from 'svelte/store' import { PopupProps } from '../../../../../../desktop/lib/auxiliary/popup/types' diff --git a/packages/shared/src/lib/core/wallet/actions/send/createEvmChainToEvmChainTransaction.ts b/packages/shared/src/lib/core/wallet/actions/send/createEvmChainToEvmChainTransaction.ts index 6e422a10e6..9bae07fe2e 100644 --- a/packages/shared/src/lib/core/wallet/actions/send/createEvmChainToEvmChainTransaction.ts +++ b/packages/shared/src/lib/core/wallet/actions/send/createEvmChainToEvmChainTransaction.ts @@ -9,7 +9,7 @@ import { getErc20TransferSmartContractData } from '@core/layer-2/utils' import { TokenStandard } from '@core/token/enums' import { IToken } from '@core/token/interfaces' -import { SendFlowType } from '../../stores' +import { SendFlowType } from '../../enums' import { SendFlowParameters } from '../../types' export function createEvmChainToEvmChainTransaction( diff --git a/packages/shared/src/lib/core/wallet/actions/send/createEvmChainToStardustNetworkTransaction.ts b/packages/shared/src/lib/core/wallet/actions/send/createEvmChainToStardustNetworkTransaction.ts index f91c11a538..83dd9476e5 100644 --- a/packages/shared/src/lib/core/wallet/actions/send/createEvmChainToStardustNetworkTransaction.ts +++ b/packages/shared/src/lib/core/wallet/actions/send/createEvmChainToStardustNetworkTransaction.ts @@ -10,7 +10,7 @@ import { buildAssetAllowance } from '@core/layer-2/utils' import { ETHEREUM_COIN_TYPE } from '@core/network/constants' import { IChain } from '@core/network/interfaces' -import { SendFlowType } from '../../stores' +import { SendFlowType } from '../../enums' import { SendFlowParameters } from '../../types' import { buildTransferredAssetFromSendFlowParameters } from '../../utils' diff --git a/packages/shared/src/lib/core/wallet/enums/index.ts b/packages/shared/src/lib/core/wallet/enums/index.ts index d907b9f8cb..7d36e0b23e 100644 --- a/packages/shared/src/lib/core/wallet/enums/index.ts +++ b/packages/shared/src/lib/core/wallet/enums/index.ts @@ -1,3 +1,4 @@ export * from './irc27-version.enum' export * from './return-strategy.enum' +export * from './send-flow-type.enum' export * from './subject-type.enum' diff --git a/packages/shared/src/lib/core/wallet/enums/send-flow-type.enum.ts b/packages/shared/src/lib/core/wallet/enums/send-flow-type.enum.ts new file mode 100644 index 0000000000..f29678b451 --- /dev/null +++ b/packages/shared/src/lib/core/wallet/enums/send-flow-type.enum.ts @@ -0,0 +1,6 @@ +export enum SendFlowType { + BaseCoinTransfer = 'baseCoinTransfer', + TokenTransfer = 'tokenTransfer', + TokenUnwrap = 'tokenUnwrap', + NftTransfer = 'nftTransfer', +} diff --git a/packages/shared/src/lib/core/wallet/stores/send-flow-parameter.store.ts b/packages/shared/src/lib/core/wallet/stores/send-flow-parameter.store.ts index f64f5960f8..e7744d38d2 100644 --- a/packages/shared/src/lib/core/wallet/stores/send-flow-parameter.store.ts +++ b/packages/shared/src/lib/core/wallet/stores/send-flow-parameter.store.ts @@ -1,14 +1,7 @@ import { writable, Writable } from 'svelte/store' +import { SendFlowType } from '../enums' import type { SendFlowParameters } from '../types' -// TODO: Move enum -export enum SendFlowType { - BaseCoinTransfer = 'BaseCoinTransfer', - TokenTransfer = 'TokenTransfer', - TokenUnwrap = 'tokenUnwrap', - NftTransfer = 'NftTransfer', -} - export const sendFlowParameters: Writable = writable(undefined) export function setSendFlowParameters(payload: SendFlowParameters): void { diff --git a/packages/shared/src/lib/core/wallet/tests/getOutputParameters.test.ts b/packages/shared/src/lib/core/wallet/tests/getOutputParameters.test.ts index 06c39a0009..21bc00e3f7 100644 --- a/packages/shared/src/lib/core/wallet/tests/getOutputParameters.test.ts +++ b/packages/shared/src/lib/core/wallet/tests/getOutputParameters.test.ts @@ -6,7 +6,7 @@ import { getOutputParameters } from '../utils' import { ReturnStrategy, SubjectType } from '../enums' import { IToken, IPersistedToken } from '@core/token/interfaces' import { TokenStandard, VerifiedStatus } from '@core/token/enums' -import { SendFlowType } from '../stores' +import { SendFlowType } from '../enums' import { SendFlowParameters } from '../types' const PERSISTED_ASSET_SHIMMER: IPersistedToken = { diff --git a/packages/shared/src/lib/core/wallet/types/send-flow-parameter.type.ts b/packages/shared/src/lib/core/wallet/types/send-flow-parameter.type.ts index b4d9682a19..13b8148336 100644 --- a/packages/shared/src/lib/core/wallet/types/send-flow-parameter.type.ts +++ b/packages/shared/src/lib/core/wallet/types/send-flow-parameter.type.ts @@ -2,7 +2,7 @@ import { NetworkId } from '@core/network' import { INft } from '@core/nfts' import { IToken } from '@core/token/interfaces' import type { Subject } from '@core/wallet' -import { SendFlowType } from '@core/wallet/stores' +import { SendFlowType } from '@core/wallet/enums' export type SendFlowParameters = TokenSendFlowParameters | NftSendFlowParameters | BaseSendFlowParameters diff --git a/packages/shared/src/lib/core/wallet/utils/getNetworkIdFromSendFlowParameters.ts b/packages/shared/src/lib/core/wallet/utils/getNetworkIdFromSendFlowParameters.ts index 028bbe0311..d961213cbe 100644 --- a/packages/shared/src/lib/core/wallet/utils/getNetworkIdFromSendFlowParameters.ts +++ b/packages/shared/src/lib/core/wallet/utils/getNetworkIdFromSendFlowParameters.ts @@ -1,5 +1,5 @@ import { NetworkId } from '@core/network/types' -import { SendFlowType } from '../stores' +import { SendFlowType } from '../enums' import { SendFlowParameters } from '../types' export function getNetworkIdFromSendFlowParameters(params: SendFlowParameters): NetworkId | undefined { diff --git a/packages/shared/src/lib/core/wallet/utils/getOutputParameters.ts b/packages/shared/src/lib/core/wallet/utils/getOutputParameters.ts index de16a04023..376b663e45 100644 --- a/packages/shared/src/lib/core/wallet/utils/getOutputParameters.ts +++ b/packages/shared/src/lib/core/wallet/utils/getOutputParameters.ts @@ -5,7 +5,7 @@ import { BASE_TOKEN_ID } from '@core/token' import { Converter, convertDateToUnixTimestamp } from '@core/utils' import { SendFlowParameters, Subject } from '@core/wallet/types' import { ReturnStrategy } from '../enums' -import { SendFlowType } from '../stores' +import { SendFlowType } from '../enums' export async function getOutputParameters( sendFlowParameters: SendFlowParameters, diff --git a/packages/shared/src/lib/core/wallet/utils/getTokenIdFromSendFlowParameters.ts b/packages/shared/src/lib/core/wallet/utils/getTokenIdFromSendFlowParameters.ts index 3f68837cdc..47e5aa4bd3 100644 --- a/packages/shared/src/lib/core/wallet/utils/getTokenIdFromSendFlowParameters.ts +++ b/packages/shared/src/lib/core/wallet/utils/getTokenIdFromSendFlowParameters.ts @@ -1,4 +1,4 @@ -import { SendFlowType } from '../stores' +import { SendFlowType } from '../enums' import { SendFlowParameters } from '../types' export function getTokenIdFromSendFlowParameters(params: SendFlowParameters): string | undefined { diff --git a/packages/shared/src/lib/core/wallet/utils/getTokenStandardFromSendFlowParameters.ts b/packages/shared/src/lib/core/wallet/utils/getTokenStandardFromSendFlowParameters.ts index 865ddbb443..544b6c2498 100644 --- a/packages/shared/src/lib/core/wallet/utils/getTokenStandardFromSendFlowParameters.ts +++ b/packages/shared/src/lib/core/wallet/utils/getTokenStandardFromSendFlowParameters.ts @@ -1,4 +1,4 @@ -import { SendFlowType } from '../stores' +import { SendFlowType } from '../enums' import { SendFlowParameters } from '../types' export function getTokenStandardFromSendFlowParameters(params: SendFlowParameters): string | undefined {