Skip to content

Commit

Permalink
code review observations
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianscatularo committed Aug 15, 2023
1 parent 427a0b0 commit 906de6b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 21 deletions.
8 changes: 6 additions & 2 deletions src/components/Transfer/Source.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ function Source() {
);
/* End pandle token check */
const { decimals = 0, isNativeAsset = false } = parsedTokenAccount || {};
const isBelowMinimum = useMinimumAmountGuard({
const { isBelowMinimum, minimum } = useMinimumAmountGuard({
amount,
sourceChain,
decimals,
Expand Down Expand Up @@ -268,7 +268,11 @@ function Source() {
onChange={handleAmountChange}
disabled={isTransferDisabled || shouldLockFields}
error={isBelowMinimum}
helperText={isBelowMinimum ? "Amount is below minimum" : ""}
helperText={
isBelowMinimum
? `Amount sent is too small. The amount must be equal or greater than ${minimum}.`
: ""
}
onMaxClick={
uiAmountString && !parsedTokenAccount.isNativeAsset
? handleMaxClick
Expand Down
53 changes: 34 additions & 19 deletions src/hooks/useMinimumAmountGuard.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
import { useMemo } from "react";
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 getDivider(decimals: number) {
return Math.pow(10, decimals);
}

const EIGHT_DECIMALS = 8;

function getAdjustedDecimals(
chainId: ChainId,
isNativeAsset: boolean,
Expand All @@ -25,6 +15,23 @@ function getAdjustedDecimals(
: decimals;
}

function getMinimum(divider: number, adjustedDecimals: number) {
return (1 / divider).toFixed(adjustedDecimals);
}

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

const EIGHT_DECIMALS = 8;

export type MinimumAmountGuardArgs = {
amount: string;
sourceChain: ChainId;
Expand All @@ -38,13 +45,21 @@ export default function useMinimumAmountGuard({
decimals = 0,
isNativeAsset = false,
}: MinimumAmountGuardArgs) {
const adjustedDecimals = useMemo(
() => getAdjustedDecimals(sourceChain, isNativeAsset, decimals),
[sourceChain, isNativeAsset, decimals]
);
const divider = useMemo(
() => getDivider(adjustedDecimals),
[adjustedDecimals]
);
const isBelowMinimum = useMemo(
() =>
checkIfIsBelowMinimum(
amount,
getAdjustedDecimals(sourceChain, isNativeAsset, decimals)
),
[amount, sourceChain, isNativeAsset, decimals]
() => checkIfIsBelowMinimum(amount, divider),
[amount, divider]
);
const minimum = useMemo(
() => getMinimum(divider, adjustedDecimals),
[divider, adjustedDecimals]
);
return isBelowMinimum;
return { isBelowMinimum, minimum };
}

0 comments on commit 906de6b

Please sign in to comment.