Skip to content

Commit

Permalink
Enforce minimum amount for evm token transfers
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianscatularo committed Aug 9, 2023
1 parent 7013e88 commit 46937e6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/components/Transfer/Source.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import { RootState } from "../../store";
import useTransferControl from "../../hooks/useTransferControl";
import transferRules from "../../config/transferRules";
import useRoundTripTransfer from "../../hooks/useRoundTripTransfer";
import useMinimumAmountGuard from "../../hooks/useMinimumAmountGuard";

const useStyles = makeStyles((theme) => ({
chainSelectWrapper: {
Expand Down Expand Up @@ -169,7 +170,7 @@ function Source() {
isPandle
);
/* End pandle token check */

const isBelowMinimum = useMinimumAmountGuard();
return (
<>
<StepDescription>
Expand Down Expand Up @@ -260,6 +261,8 @@ function Source() {
value={amount}
onChange={handleAmountChange}
disabled={isTransferDisabled || shouldLockFields}
error={isBelowMinimum}
helperText={isBelowMinimum ? "Amount is below minimum" : ""}
onMaxClick={
uiAmountString && !parsedTokenAccount.isNativeAsset
? handleMaxClick
Expand Down
43 changes: 43 additions & 0 deletions src/hooks/useMinimumAmountGuard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import type { RootState } from "../store";
import { useMemo } from "react";
import { useSelector } from "react-redux";
import { ChainId, isEVMChain } from "@certusone/wormhole-sdk";

function checkIfIsBelowMinimum(amount: string, decimals: number) {
try {
const divider = Math.pow(10, decimals);
const floatAmount = parseFloat(amount);
const intAmount = floatAmount * divider;
return Math.trunc(intAmount) <= 0;
} catch (err: any) {
console.error(err);
return true;
}
}

function getAdjustedDecimals(
chainId: ChainId,
isNativeAsset: boolean,
decimals: number
) {
return isEVMChain(chainId) && !isNativeAsset && decimals > 8
? decimals - 8
: decimals;
}

export default function useMinimumAmountGuard() {
const {
amount,
sourceChain,
sourceParsedTokenAccount: { decimals = 0, isNativeAsset = false } = {},
} = useSelector((state: RootState) => state.transfer);
const isBelowMinimum = useMemo(
() =>
checkIfIsBelowMinimum(
amount,
getAdjustedDecimals(sourceChain, isNativeAsset, decimals)
),
[amount, sourceChain, isNativeAsset, decimals]
);
return isBelowMinimum;
}

0 comments on commit 46937e6

Please sign in to comment.