Skip to content

Commit

Permalink
Display backend minSend amounts in frontend validation messages
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlaprade committed Apr 2, 2022
1 parent 25db292 commit 5ce1542
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 19 deletions.
1 change: 1 addition & 0 deletions frontend/src/components/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export type ApiError = { error: string };
export interface TokenInfoWithMinSendAmount extends TokenInfo {
minSendAmount: string;
}
// Omit the TokenList.tokens type so we can override it with our own.
export interface TokenListWithMinSendAmount extends Omit<TokenList, 'tokens'> {
nativeTokenMinSendAmount: string;
tokens: TokenInfoWithMinSendAmount[];
Expand Down
62 changes: 49 additions & 13 deletions frontend/src/pages/AccountSend.vue
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ function useSendForm() {
tokens: tokenOptions,
umbra,
userAddress,
relayer,
} = useWalletStore();
// Helpers
Expand Down Expand Up @@ -292,21 +293,56 @@ function useSendForm() {
}
const isNativeToken = (address: string) => getAddress(address) === NATIVE_TOKEN.value.address;
const getMinSendAmount = (tokenAddress: string) => {
const getNativeTokenMinSendAmountFallback = (chainId: number): number => {
switch (chainId) {
case 137: return 1.0; // Polygon
default: return 0.01; // everything else
}
}
const getTokenMinSendAmountFallback = (
tokenAddress: string,
chainId: number
): number => {
switch (chainId) {
case 137: // Polygon
if (tokenAddress === '0x7ceB23fD6bC0adD59E62ac25578270cFf1b9f619') {
return 0.001; // weth token minimum
} else {
return 3; // stablecoin token minimum
}
default:
// Mainnet, Rinkeby, and other networks have higher ETH and stablecoin minimums
// due to higher fees
return 100
}
}
const getMinSendAmount = (tokenAddress: string): number => {
const chainId = BigNumber.from(currentChain.value?.chainId).toNumber();
// Polygon
if (chainId === 137) {
if (isNativeToken(tokenAddress)) {
return 1.0;
} else if (tokenAddress === '0x7ceB23fD6bC0adD59E62ac25578270cFf1b9f619') {
return 0.001; // weth token minimum
} else {
return 3; // stablecoin token minimum
}
if (isNativeToken(tokenAddress)) {
const defaultNativeMinSend = getNativeTokenMinSendAmountFallback(chainId);
const relayerMinSend = relayer.value?.nativeTokenMinSendAmount;
const dynamicMinSend = relayerMinSend && Number(formatUnits(relayerMinSend, 18));
return dynamicMinSend || defaultNativeMinSend;
} else {
const relayerTokenInfo = relayer.value?.tokens.filter(
(token) => token.address === tokenAddress
)[0];
const relayerMinSend = relayerTokenInfo?.minSendAmount && Number(
formatUnits(
parseUnits(relayerTokenInfo?.minSendAmount, 'wei')
)
);
const defaultTokenMinSend = getTokenMinSendAmountFallback(tokenAddress, chainId);
// TODO Rather than have a global fallback like this, we should probably just add a
// token Enum type so that the TS compiler will ensure we've covered all cases. This
// would also have the benefit of making it fairly mechanical to add support for new
// tokens.
const globalFallbackAmount = 100;
return relayerMinSend || defaultTokenMinSend || globalFallbackAmount;
}
// Mainnet, Rinkeby, and other networks have higher ETH and stablecoin minimums due to higher fees
if (isNativeToken(tokenAddress)) return 0.01;
else return 100; // stablecoin token minimum
};
function isValidTokenAmount(val: string | undefined) {
Expand Down
10 changes: 4 additions & 6 deletions frontend/src/utils/relayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
* @notice Class for managing relayed withdrawal transactions
*/

import { JsonRpcProvider, BigNumber } from 'src/utils/ethers';
import { JsonRpcProvider, BigNumber, parseUnits } from 'src/utils/ethers';
import {
ConfirmedITXStatusResponse,
FeeEstimateResponse,
ITXStatusResponse,
Provider,
TokenInfo,
TokenInfoWithMinSendAmount,
RelayResponse,
TokenListResponse,
WithdrawalInputs,
Expand All @@ -17,7 +17,7 @@ import {
export class ITXRelayer {
constructor(
readonly baseUrl: string,
readonly tokens: TokenInfo[],
readonly tokens: TokenInfoWithMinSendAmount[],
readonly chainId: number,
readonly nativeTokenMinSendAmount: BigNumber | undefined,
) {}
Expand All @@ -34,9 +34,7 @@ export class ITXRelayer {
if ('error' in data) {
console.warn(`Could not fetch tokens from relayer: ${data.error}`);
} else {
try {
nativeMinSend = BigNumber.from(data.nativeTokenMinSendAmount);
} catch (_error) {}
nativeMinSend = parseUnits(data.nativeTokenMinSendAmount, 'wei');
}

// TODO pre-parse token minSendAmounts?
Expand Down

0 comments on commit 5ce1542

Please sign in to comment.