From e70db48ef73b63b1db7093a465844e445f006e13 Mon Sep 17 00:00:00 2001 From: Evan Gray Date: Tue, 31 Aug 2021 19:12:48 -0400 Subject: [PATCH] bridge_ui: improve amount safety check fixes https://github.com/certusone/wormhole/issues/364 Change-Id: Ic90b850cae9635e55320d8304fd8dd16dcaad2b7 --- bridge_ui/src/store/selectors.ts | 48 +++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/bridge_ui/src/store/selectors.ts b/bridge_ui/src/store/selectors.ts index 654e24f0..05233a45 100644 --- a/bridge_ui/src/store/selectors.ts +++ b/bridge_ui/src/store/selectors.ts @@ -77,23 +77,37 @@ export const selectTransferIsRedeeming = (state: RootState) => // safety checks // TODO: could make this return a string with a user informative message -export const selectTransferIsSourceComplete = (state: RootState) => - !!state.transfer.sourceChain && - !!state.transfer.sourceParsedTokenAccount && - !!state.transfer.amount && - (state.transfer.sourceChain !== CHAIN_ID_SOLANA || - !!state.transfer.sourceParsedTokenAccount.publicKey) && - !!state.transfer.sourceParsedTokenAccount.uiAmountString && - // TODO: make safe with too many decimals - parseUnits( - state.transfer.amount, - state.transfer.sourceParsedTokenAccount.decimals - ).lte( - parseUnits( - state.transfer.sourceParsedTokenAccount.uiAmountString, - state.transfer.sourceParsedTokenAccount.decimals - ) - ); +export const selectTransferIsSourceComplete = (state: RootState) => { + try { + return ( + !!state.transfer.sourceChain && + !!state.transfer.sourceParsedTokenAccount && + !!state.transfer.amount && + (state.transfer.sourceChain !== CHAIN_ID_SOLANA || + !!state.transfer.sourceParsedTokenAccount.publicKey) && + !!state.transfer.sourceParsedTokenAccount.uiAmountString && + !!state.transfer.sourceParsedTokenAccount.decimals && + state.transfer.sourceParsedTokenAccount.decimals > 0 && // TODO: more advanced NFT check + // may trigger error: fractional component exceeds decimals + parseUnits( + state.transfer.amount, + state.transfer.sourceParsedTokenAccount.decimals + ).gt(0) && + // may trigger error: fractional component exceeds decimals + parseUnits( + state.transfer.amount, + state.transfer.sourceParsedTokenAccount.decimals + ).lte( + parseUnits( + state.transfer.sourceParsedTokenAccount.uiAmountString, + state.transfer.sourceParsedTokenAccount.decimals + ) + ) + ); + } catch (e) { + return false; + } +}; // TODO: check wrapped asset exists or is native transfer export const selectTransferIsTargetComplete = (state: RootState) =>