Skip to content

Commit

Permalink
Improve gas balance validation
Browse files Browse the repository at this point in the history
  • Loading branch information
jmrossy committed Nov 9, 2023
1 parent 476a04f commit b1fd854
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/components/toast/IgpDetailsToast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function IgpDetailsToast({ tokenName, igpFee }) {
return (
<div>
Cross-chain transfers require a small fee of {igpFee} {tokenName} to fund delivery transaction
costs. Your {tokenName} balance is insufficient. Try bridging {igpFee} fewer {tokenName}.{' '}
costs. Your {tokenName} balance is insufficient.{' '}
<a className="underline" href={links.gasDocs} target="_blank" rel="noopener noreferrer">
Learn More
</a>
Expand Down
36 changes: 15 additions & 21 deletions src/features/transfer/TransferTokenForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -510,9 +510,11 @@ function validateFormValues(

if (!tokenCaip19Id) return { tokenCaip19Id: 'Token required' };
const { address: tokenAddress } = parseCaip19Id(tokenCaip19Id);
if (!isZeroishAddress(tokenAddress) && !isValidAddress(tokenAddress))
const tokenMetadata = getToken(tokenCaip19Id);
if (!tokenMetadata || (!isZeroishAddress(tokenAddress) && !isValidAddress(tokenAddress)))
return { tokenCaip19Id: 'Invalid token' };

const originProtocol = getProtocolType(originCaip2Id);
const destProtocol = getProtocolType(destinationCaip2Id);
if (!isValidAddress(recipientAddress, destProtocol))
return { recipientAddress: 'Invalid recipient' };
Expand All @@ -528,30 +530,22 @@ function validateFormValues(
if (sendValue.gt(balances.senderTokenBalance)) return { amount: 'Insufficient balance' };
// Ensure balances can cover IGP fees
const igpWeiAmount = new BigNumber(igpQuote?.weiAmount || 0);
const requiredNativeBalance = isRouteFromNative(route)
? sendValue.plus(igpWeiAmount)
: igpWeiAmount;

const nativeDecimals = getChainMetadata(originCaip2Id)?.nativeToken?.decimals || 18;
const requiredNativeBalance =
isRouteFromNative(route) || originProtocol === ProtocolType.Cosmos
? sendValue.plus(igpWeiAmount)
: igpWeiAmount;

const nativeToken = getChainMetadata(originCaip2Id)?.nativeToken;
const nativeDecimals = nativeToken?.decimals || 18;
const gasTokenSymbol =
originProtocol === ProtocolType.Cosmos
? tokenMetadata.symbol
: nativeToken?.symbol || 'native token';
const igpAmountPretty = fromWei(igpWeiAmount, nativeDecimals);

const originProtocol = getProtocolType(originCaip2Id);
// Hardcode case of Cosmos, where Neutron is assumed to be the
// only case for now.
if (originProtocol === ProtocolType.Cosmos) {
// Assume that only TIA is being bridged out of Neutron and
// that the IGP fee is in TIA
const requiredTiaBalance = sendValue.plus(igpWeiAmount);

if (requiredTiaBalance.gt(balances.senderTokenBalance)) {
toastIgpDetails(igpAmountPretty, 'TIA');
return { amount: 'Insufficient TIA for gas' };
}
}

if (requiredNativeBalance.gt(balances.senderNativeBalance)) {
toastIgpDetails(igpAmountPretty);
return { amount: 'Insufficient native token for gas' };
return { amount: `Insufficient ${gasTokenSymbol} for gas` };
}
} else {
// Validate balances for ERC721-like tokens
Expand Down

0 comments on commit b1fd854

Please sign in to comment.