From 7de9cffd3347b9e6e7b2461fb062e34168eefc78 Mon Sep 17 00:00:00 2001 From: Tom Beckenham <34339192+tombeckenham@users.noreply.github.com> Date: Sat, 30 Nov 2024 12:16:57 +1100 Subject: [PATCH 1/2] Include coin in storage evaluation Fixes #225 --- src/background/controller/wallet.ts | 9 ++++++--- src/background/service/storage-evaluator.ts | 3 ++- src/ui/utils/useStorageCheck.ts | 14 +++++++++----- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/background/controller/wallet.ts b/src/background/controller/wallet.ts index 9bc4dc0b..ef863905 100644 --- a/src/background/controller/wallet.ts +++ b/src/background/controller/wallet.ts @@ -50,7 +50,7 @@ import { openInternalPageInTab } from 'ui/utils/webapi'; import { pk2PubKey, seed2PubKey, formPubKey } from '../../ui/utils/modules/passkey'; import { fclTestnetConfig, fclMainnetConfig } from '../fclConfig'; -import type { CoinItem } from '../service/coinList'; +import { type CoinItem } from '../service/coinList'; import DisplayKeyring from '../service/keyring/display'; import type { NFTData, NFTModel, StorageInfo, WalletResponse } from '../service/networkModel'; import type { ConnectedSite } from '../service/permission'; @@ -4016,10 +4016,12 @@ export class WalletController extends BaseController { // Check the storage status checkStorageStatus = async ({ transferAmount, + coin, movingBetweenEVMAndFlow, }: { - transferAmount?: number; - movingBetweenEVMAndFlow?: boolean; + transferAmount?: number; // amount in coins + coin?: string; // coin name + movingBetweenEVMAndFlow?: boolean; // are we moving between EVM and Flow? } = {}): Promise<{ isStorageSufficient: boolean; isStorageSufficientAfterAction: boolean; @@ -4030,6 +4032,7 @@ export class WalletController extends BaseController { await this.storageEvaluator.evaluateStorage( address!, transferAmount, + coin, movingBetweenEVMAndFlow ); return { diff --git a/src/background/service/storage-evaluator.ts b/src/background/service/storage-evaluator.ts index 18110b73..8aa2a17d 100644 --- a/src/background/service/storage-evaluator.ts +++ b/src/background/service/storage-evaluator.ts @@ -17,6 +17,7 @@ export class StorageEvaluator { async evaluateStorage( address: string, sendAmount?: number, + coin?: string, movingBetweenEVMAndFlow?: boolean ): Promise { // Get storage info from openapi service @@ -32,7 +33,7 @@ export class StorageEvaluator { if (sendAmount !== undefined) { // This is the amount of flow that will be used by the transaction const flowUsed = - sendAmount + + (coin === 'flow' ? sendAmount : 0) + (movingBetweenEVMAndFlow ? StorageEvaluator.FIXED_MOVE_FEE : 0) + StorageEvaluator.AVERAGE_TX_FEE; diff --git a/src/ui/utils/useStorageCheck.ts b/src/ui/utils/useStorageCheck.ts index 4254bcfd..9ce9f625 100644 --- a/src/ui/utils/useStorageCheck.ts +++ b/src/ui/utils/useStorageCheck.ts @@ -12,12 +12,14 @@ interface StorageCheckResult { } interface UseStorageCheckProps { - transferAmount?: number | undefined; + transferAmount?: number; + coin?: string; movingBetweenEVMAndFlow?: boolean; } export const useStorageCheck = ({ - transferAmount, - movingBetweenEVMAndFlow, + transferAmount, // amount in coins + coin, // coin name + movingBetweenEVMAndFlow = false, // are we moving between EVM and Flow? }: UseStorageCheckProps = {}): StorageCheckResult => { const wallet = useWallet(); @@ -26,6 +28,7 @@ export const useStorageCheck = ({ undefined ); const [storageInfo, setStorageInfo] = useState(undefined); + // Check general storage status const checkStorageStatus = useCallback(async (): Promise<{ sufficient: boolean; @@ -35,7 +38,8 @@ export const useStorageCheck = ({ try { const { isStorageSufficient, isStorageSufficientAfterAction, storageInfo } = await wallet.checkStorageStatus({ - transferAmount, + transferAmount: transferAmount, + coin, movingBetweenEVMAndFlow, }); @@ -52,7 +56,7 @@ export const useStorageCheck = ({ storageInfo: { available: 0, used: 0, capacity: 0 }, }; // Default to true to not block transactions on error } - }, [movingBetweenEVMAndFlow, transferAmount, wallet]); + }, [movingBetweenEVMAndFlow, transferAmount, wallet, coin]); // Initial storage check useEffect(() => { From 5cae8bc70d5f91edc1db94d2de1fd8fcc65acf0f Mon Sep 17 00:00:00 2001 From: Tom Beckenham <34339192+tombeckenham@users.noreply.github.com> Date: Sat, 30 Nov 2024 12:29:12 +1100 Subject: [PATCH 2/2] Checks coin in all transfers Fixes #225 --- src/ui/views/EvmMove/MoveFromChild/index.tsx | 1 + src/ui/views/EvmMove/MoveFromEvm/index.tsx | 1 + src/ui/views/EvmMove/MoveFromFlow/index.tsx | 1 + src/ui/views/EvmMove/MoveFromParent/index.tsx | 1 + src/ui/views/NFT/SendNFT/MovefromParent.tsx | 1 + src/ui/views/Send/SendEth/EvmConfirmation.tsx | 1 + src/ui/views/Send/SendEth/ToEthConfirmation.tsx | 1 + src/ui/views/Send/TransferConfirmation.tsx | 1 + 8 files changed, 8 insertions(+) diff --git a/src/ui/views/EvmMove/MoveFromChild/index.tsx b/src/ui/views/EvmMove/MoveFromChild/index.tsx index 821a24b5..a665fd8f 100644 --- a/src/ui/views/EvmMove/MoveFromChild/index.tsx +++ b/src/ui/views/EvmMove/MoveFromChild/index.tsx @@ -94,6 +94,7 @@ const MoveFromChild = (props: TransferConfirmationProps) => { const [minAmount, setMinAmount] = useState(0.001); const { sufficient: isSufficient, sufficientAfterAction } = useStorageCheck({ transferAmount: Number(amount) || 0, + coin: currentCoin, movingBetweenEVMAndFlow: true, }); diff --git a/src/ui/views/EvmMove/MoveFromEvm/index.tsx b/src/ui/views/EvmMove/MoveFromEvm/index.tsx index 0dbce8bf..53f1c915 100644 --- a/src/ui/views/EvmMove/MoveFromEvm/index.tsx +++ b/src/ui/views/EvmMove/MoveFromEvm/index.tsx @@ -79,6 +79,7 @@ const MoveFromEvm = (props: TransferConfirmationProps) => { const { sufficient: isSufficient, sufficientAfterAction } = useStorageCheck({ transferAmount: Number(amount) || 0, + coin: currentCoin, movingBetweenEVMAndFlow: true, }); diff --git a/src/ui/views/EvmMove/MoveFromFlow/index.tsx b/src/ui/views/EvmMove/MoveFromFlow/index.tsx index 67ed66b8..a0a4d1b2 100644 --- a/src/ui/views/EvmMove/MoveFromFlow/index.tsx +++ b/src/ui/views/EvmMove/MoveFromFlow/index.tsx @@ -95,6 +95,7 @@ const MoveFromFlow = (props: TransferConfirmationProps) => { const { sufficient: isSufficient, sufficientAfterAction } = useStorageCheck({ transferAmount: Number(amount) || 0, + coin: currentCoin, movingBetweenEVMAndFlow: true, }); diff --git a/src/ui/views/EvmMove/MoveFromParent/index.tsx b/src/ui/views/EvmMove/MoveFromParent/index.tsx index 3d6b33aa..58d39317 100644 --- a/src/ui/views/EvmMove/MoveFromParent/index.tsx +++ b/src/ui/views/EvmMove/MoveFromParent/index.tsx @@ -94,6 +94,7 @@ const MoveFromParent = (props: TransferConfirmationProps) => { const [minAmount, setMinAmount] = useState(0.001); const { sufficient: isSufficient, sufficientAfterAction } = useStorageCheck({ transferAmount: Number(amount) || 0, + coin: currentCoin, movingBetweenEVMAndFlow: true, }); diff --git a/src/ui/views/NFT/SendNFT/MovefromParent.tsx b/src/ui/views/NFT/SendNFT/MovefromParent.tsx index 02578235..83bddd08 100644 --- a/src/ui/views/NFT/SendNFT/MovefromParent.tsx +++ b/src/ui/views/NFT/SendNFT/MovefromParent.tsx @@ -36,6 +36,7 @@ const MovefromParent = (props: SendNFTConfirmationProps) => { const [selectedAccount, setSelectedChildAccount] = useState(null); const { sufficient: isSufficient, sufficientAfterAction } = useStorageCheck({ transferAmount: 0, + coin: 'flow', movingBetweenEVMAndFlow: selectedAccount ? isValidEthereumAddress(selectedAccount!['address']) : false, diff --git a/src/ui/views/Send/SendEth/EvmConfirmation.tsx b/src/ui/views/Send/SendEth/EvmConfirmation.tsx index a70a1e5f..9ed7b1ee 100644 --- a/src/ui/views/Send/SendEth/EvmConfirmation.tsx +++ b/src/ui/views/Send/SendEth/EvmConfirmation.tsx @@ -38,6 +38,7 @@ const ToEthConfirmation = (props: ToEthConfirmationProps) => { const movingBetweenEVMAndFlow = true; const { sufficient: isSufficient, sufficientAfterAction } = useStorageCheck({ transferAmount, + coin: props.data?.coinInfo?.coin, movingBetweenEVMAndFlow, }); diff --git a/src/ui/views/Send/SendEth/ToEthConfirmation.tsx b/src/ui/views/Send/SendEth/ToEthConfirmation.tsx index d29cd64b..f610a158 100644 --- a/src/ui/views/Send/SendEth/ToEthConfirmation.tsx +++ b/src/ui/views/Send/SendEth/ToEthConfirmation.tsx @@ -33,6 +33,7 @@ const ToEthConfirmation = (props: ToEthConfirmationProps) => { const [count, setCount] = useState(0); const { sufficient: isSufficient, sufficientAfterAction } = useStorageCheck({ transferAmount: 0, + coin: props.data?.coinInfo?.coin, movingBetweenEVMAndFlow: true, }); diff --git a/src/ui/views/Send/TransferConfirmation.tsx b/src/ui/views/Send/TransferConfirmation.tsx index 81736a9d..6f499bc8 100644 --- a/src/ui/views/Send/TransferConfirmation.tsx +++ b/src/ui/views/Send/TransferConfirmation.tsx @@ -38,6 +38,7 @@ const TransferConfirmation = (props: TransferConfirmationProps) => { const { sufficient: isSufficient, sufficientAfterAction: isSufficientAfterAction } = useStorageCheck({ transferAmount, + coin: props.data?.coinInfo?.coin, movingBetweenEVMAndFlow, });