-
Notifications
You must be signed in to change notification settings - Fork 34
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 (#293)
* Enforce minimum amount for evm token transfers * code review observations * fix decimals to the max supported for evm chains if the decimals are higher * [skip ci] - run prettier * code review observations * [skip ci] - cosmetic changes * disable next button if the amount is below minimum * [skip ci] - bump version
- Loading branch information
1 parent
73442db
commit 049538f
Showing
3 changed files
with
81 additions
and
3 deletions.
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
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,65 @@ | ||
import { useMemo } from "react"; | ||
import { ChainId, isEVMChain } from "@certusone/wormhole-sdk"; | ||
|
||
export type MinimumAmountGuardArgs = { | ||
amount: string; | ||
sourceChain: ChainId; | ||
decimals: number; | ||
isNativeAsset: boolean; | ||
}; | ||
|
||
const EIGHT_DECIMALS = 8; | ||
|
||
function getMultiplier(decimals: number) { | ||
return Math.pow(10, decimals); | ||
} | ||
|
||
function getAdjustedDecimals( | ||
chainId: ChainId, | ||
isNativeAsset: boolean, | ||
decimals: number | ||
) { | ||
return isEVMChain(chainId) && !isNativeAsset && decimals > EIGHT_DECIMALS | ||
? EIGHT_DECIMALS // max decimals supported on evm chains | ||
: decimals; | ||
} | ||
|
||
function getMinimum(divider: number, adjustedDecimals: number) { | ||
return (1 / divider).toFixed(adjustedDecimals); | ||
} | ||
|
||
function checkIfIsBelowMinimum(amount: string, multiplier: number) { | ||
try { | ||
const floatAmount = parseFloat(amount); | ||
const intAmount = floatAmount * multiplier; | ||
return Math.trunc(intAmount) <= 0; | ||
} catch (err: any) { | ||
console.error(err); | ||
return true; | ||
} | ||
} | ||
|
||
export default function useMinimumAmountGuard({ | ||
amount, | ||
sourceChain, | ||
decimals = 0, | ||
isNativeAsset = false, | ||
}: MinimumAmountGuardArgs) { | ||
const adjustedDecimals = useMemo( | ||
() => getAdjustedDecimals(sourceChain, isNativeAsset, decimals), | ||
[sourceChain, isNativeAsset, decimals] | ||
); | ||
const multiplier = useMemo( | ||
() => getMultiplier(adjustedDecimals), | ||
[adjustedDecimals] | ||
); | ||
const isBelowMinimum = useMemo( | ||
() => checkIfIsBelowMinimum(amount, multiplier), | ||
[amount, multiplier] | ||
); | ||
const minimum = useMemo( | ||
() => getMinimum(multiplier, adjustedDecimals), | ||
[multiplier, adjustedDecimals] | ||
); | ||
return { isBelowMinimum, minimum }; | ||
} |