Skip to content

Commit

Permalink
fix swap decimal input support
Browse files Browse the repository at this point in the history
  • Loading branch information
temptemp3 committed Dec 23, 2023
1 parent 28bd84b commit 33589bb
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions app/app-sandbox/src/components/SwapForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -324,26 +324,32 @@ const SwapForm: React.FC<SwapFormProps> = () => {
if (swapDirection) {
const { tokenA } = debouncedValue;
if (!tokenA) return;
const tokenN = bigNumberify(tokenA);
if (tokenN.gt(bigNumberify(allowance))) return;
const amount = bigNumberToBigInt(
parseWithDecimals(tokenA, token.decimals)
const tokenBn = new BigNumber(tokenA);
const tokenAtomic = tokenBn.multipliedBy(
new BigNumber(10).pow(token.decimals)
);
v_Swap(ctcInfo, activeAccount.address, amount, swapDirection).then(
([a, b]) => {
const tokenBi = bigNumberToBigInt(bigNumberify(tokenAtomic.toFixed(0)));
const allowanceBi = bigNumberToBigInt(bigNumberify(allowance));
console.log({ tokenBi, allowanceBi });
swap(ctcInfo, activeAccount.address, tokenBi, swapDirection).then(
({ returnValue }) => {
const [a, b] = returnValue;
setOutput(formatWithDecimals(bigNumberify(b), ntoken.decimals));
}
);
} else {
const { tokenB } = debouncedValue;
if (!tokenB) return;
const tokenN = bigNumberify(tokenB);
if (tokenN.gt(bigNumberify(allowance))) return; // token number is greater than allowance do approval
const amount = bigNumberToBigInt(
parseWithDecimals(tokenB, token.decimals)
const tokenBn = new BigNumber(tokenB);
const tokenAtomic = tokenBn.multipliedBy(
new BigNumber(10).pow(token.decimals)
);
v_Swap(ctcInfo, activeAccount.address, amount, swapDirection).then(
([a, b]) => {
const tokenBi = bigNumberToBigInt(bigNumberify(tokenAtomic.toFixed(0)));
const allowanceBi = bigNumberToBigInt(bigNumberify(allowance));
console.log({ allowanceBi, tokenBi });
swap(ctcInfo, activeAccount.address, tokenBi, swapDirection).then(
({ returnValue }) => {
const [a, b] = returnValue;
setOutput(formatWithDecimals(bigNumberify(a), ntoken.decimals));
}
);
Expand Down

0 comments on commit 33589bb

Please sign in to comment.