bridge_ui: improve amount safety check

fixes https://github.com/certusone/wormhole/issues/364

Change-Id: Ic90b850cae9635e55320d8304fd8dd16dcaad2b7
This commit is contained in:
Evan Gray 2021-08-31 19:12:48 -04:00
parent 3e4b673bb5
commit e70db48ef7
1 changed files with 31 additions and 17 deletions

View File

@ -77,23 +77,37 @@ export const selectTransferIsRedeeming = (state: RootState) =>
// safety checks // safety checks
// TODO: could make this return a string with a user informative message // TODO: could make this return a string with a user informative message
export const selectTransferIsSourceComplete = (state: RootState) => export const selectTransferIsSourceComplete = (state: RootState) => {
!!state.transfer.sourceChain && try {
!!state.transfer.sourceParsedTokenAccount && return (
!!state.transfer.amount && !!state.transfer.sourceChain &&
(state.transfer.sourceChain !== CHAIN_ID_SOLANA || !!state.transfer.sourceParsedTokenAccount &&
!!state.transfer.sourceParsedTokenAccount.publicKey) && !!state.transfer.amount &&
!!state.transfer.sourceParsedTokenAccount.uiAmountString && (state.transfer.sourceChain !== CHAIN_ID_SOLANA ||
// TODO: make safe with too many decimals !!state.transfer.sourceParsedTokenAccount.publicKey) &&
parseUnits( !!state.transfer.sourceParsedTokenAccount.uiAmountString &&
state.transfer.amount, !!state.transfer.sourceParsedTokenAccount.decimals &&
state.transfer.sourceParsedTokenAccount.decimals state.transfer.sourceParsedTokenAccount.decimals > 0 && // TODO: more advanced NFT check
).lte( // may trigger error: fractional component exceeds decimals
parseUnits( parseUnits(
state.transfer.sourceParsedTokenAccount.uiAmountString, state.transfer.amount,
state.transfer.sourceParsedTokenAccount.decimals 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 // TODO: check wrapped asset exists or is native transfer
export const selectTransferIsTargetComplete = (state: RootState) => export const selectTransferIsTargetComplete = (state: RootState) =>