-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enforce minimum amount for evm token transfers
- Loading branch information
1 parent
7013e88
commit 46937e6
Showing
2 changed files
with
47 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |