Skip to content

Commit

Permalink
fix swap transaction, and arc200 balance conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
temptemp3 committed Jan 8, 2025
1 parent ad4ef9b commit c5c1cff
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 49 deletions.
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"react-toastify": "^10.0.4",
"react-use": "^17.5.1",
"styled-components": "^6.1.8",
"ulujs": "^0.12.0",
"ulujs": "^0.12.1",
"usehooks-ts": "^2.15.0"
},
"devDependencies": {
Expand Down
47 changes: 26 additions & 21 deletions src/components/PoolAdd/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1141,8 +1141,11 @@ const Swap = () => {
.then((accInfo: any) => {
const balance = accInfo.amount;
const minBalance = accInfo["min-balance"];
const availableBalance = balance - minBalance;
setBalance((availableBalance / 1e6).toLocaleString());
const availableBalance = Math.max(0, balance - (minBalance - 0.1));
const balanceStr = new BigNumber(availableBalance)
.dividedBy(new BigNumber(10).pow(6))
.toFixed(6);
setBalance(balanceStr);
});
} else if (wrappedTokenId !== 0 && !isNaN(wrappedTokenId)) {
algodClient
Expand All @@ -1165,12 +1168,11 @@ const Swap = () => {
ci.arc200_balanceOf(activeAccount.address).then(
(arc200_balanceOfR: any) => {
if (arc200_balanceOfR.success) {
setBalance(
(
Number(arc200_balanceOfR.returnValue) /
10 ** token.decimals
).toLocaleString()
);
const balanceBn = new BigNumber(arc200_balanceOfR.returnValue);
const balanceStr = balanceBn
.dividedBy(new BigNumber(10).pow(token.decimals))
.toFixed(token.decimals);
setBalance(balanceStr);
}
}
);
Expand All @@ -1192,8 +1194,11 @@ const Swap = () => {
.then((accInfo: any) => {
const balance = accInfo.amount;
const minBalance = accInfo["min-balance"];
const availableBalance = balance - minBalance;
setBalance2((availableBalance / 1e6).toLocaleString());
const availableBalance = Math.max(0, balance - (minBalance - 0.1));
const balanceStr = new BigNumber(availableBalance)
.dividedBy(new BigNumber(10).pow(6))
.toFixed(6);
setBalance2(balanceStr);
});
} else if (wrappedTokenId !== 0 && !isNaN(wrappedTokenId)) {
algodClient
Expand All @@ -1215,12 +1220,11 @@ const Swap = () => {
ci.arc200_balanceOf(activeAccount.address).then(
(arc200_balanceOfR: any) => {
if (arc200_balanceOfR.success) {
setBalance2(
(
Number(arc200_balanceOfR.returnValue) /
10 ** token2.decimals
).toLocaleString()
);
const balanceBn = new BigNumber(arc200_balanceOfR.returnValue);
const balanceStr = balanceBn
.dividedBy(new BigNumber(10).pow(token2.decimals))
.toFixed(token2.decimals);
setBalance2(balanceStr);
}
}
);
Expand Down Expand Up @@ -1331,7 +1335,7 @@ const Swap = () => {
console.log({ A, B, acc, pool });

const swapR = await ci.deposit(acc.addr, pool.poolId, A, B, [], {
debug: true
debug: true,
});

if (!swapR.success) {
Expand Down Expand Up @@ -1455,26 +1459,27 @@ const Swap = () => {
}, 1000);
return () => clearTimeout(timeout);
}, [progress]);

const findTokenInfo = (token: ARC200TokenI, tokens2: any[]) => {
if (!token || !tokens2) return undefined;
return tokens2.find(
(t) => t.contractId === token.tokenId || t.tokenId === String(token.tokenId)
(t) =>
t.contractId === token.tokenId || t.tokenId === String(token.tokenId)
);
};

const [tokAInfo, setTokAInfo] = useState<any>();
useEffect(() => {
if (!token || !tokens2) return;
const tokA = findTokenInfo(token, tokens2)
const tokA = findTokenInfo(token, tokens2);
if (!tokA) return;
setTokAInfo(tokA);
}, [token, tokens2]);

const [tokBInfo, setTokBInfo] = useState<any>();
useEffect(() => {
if (!token2 || !tokens2) return;
const tokB = findTokenInfo(token2, tokens2)
const tokB = findTokenInfo(token2, tokens2);
if (!tokB) return;
setTokBInfo(tokB);
}, [token2, tokens2]);
Expand Down
40 changes: 21 additions & 19 deletions src/components/Swap/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ const Swap = () => {

const fee = useMemo(() => {
if (!info || !token) return "0";
const amount = new BigNumber(fromAmount.replace(/,/g, ''));
const amount = new BigNumber(fromAmount.replace(/,/g, ""));
if (amount.isNaN()) return "0";
return amount
.multipliedBy(info?.protoInfo.totFee)
Expand All @@ -748,9 +748,12 @@ const Swap = () => {

const expectedOutcome = useMemo(() => {
if (!rate || !fromAmount) return;
const amount = new BigNumber(fromAmount.replace(/,/g, ''));
const amount = new BigNumber(fromAmount.replace(/,/g, ""));
if (amount.isNaN()) return;
return amount.multipliedBy(rate).decimalPlaces(token2?.decimals || 6, BigNumber.ROUND_DOWN).toNumber();
return amount
.multipliedBy(rate)
.decimalPlaces(token2?.decimals || 6, BigNumber.ROUND_DOWN)
.toNumber();
}, [rate, fromAmount, token2?.decimals]);

console.log("expectedOutcome", expectedOutcome);
Expand Down Expand Up @@ -782,7 +785,7 @@ const Swap = () => {
);
ci.setFee(4000);
if (pool.tokA === tokenId(token)) {
const fromAmountBN = new BigNumber(fromAmount.replace(/,/g, ''));
const fromAmountBN = new BigNumber(fromAmount.replace(/,/g, ""));
if (fromAmountBN.isNaN()) return;
const fromAmountBI = BigInt(
fromAmountBN
Expand All @@ -803,7 +806,7 @@ const Swap = () => {
}
});
} else if (pool.tokB === tokenId(token)) {
const fromAmountBN = new BigNumber(fromAmount.replace(/,/g, ''));
const fromAmountBN = new BigNumber(fromAmount.replace(/,/g, ""));
if (fromAmountBN.isNaN()) return;
const fromAmountBI = BigInt(
fromAmountBN
Expand Down Expand Up @@ -854,7 +857,7 @@ const Swap = () => {
);
ci.setFee(4000);
if (pool.tokA === tokenId(token2)) {
const toAmountBN = new BigNumber(toAmount.replace(/,/g, ''));
const toAmountBN = new BigNumber(toAmount.replace(/,/g, ""));
if (toAmountBN.isNaN()) return;
const toAmountBI = BigInt(
toAmountBN
Expand All @@ -881,7 +884,7 @@ const Swap = () => {
}
);
} else if (pool.tokA === tokenId(token)) {
const toAmountBN = new BigNumber(toAmount.replace(/,/g, ''));
const toAmountBN = new BigNumber(toAmount.replace(/,/g, ""));
if (toAmountBN.isNaN()) return;
const toAmountBI = BigInt(
toAmountBN
Expand Down Expand Up @@ -1014,12 +1017,12 @@ const Swap = () => {
ci.arc200_balanceOf(activeAccount.address).then(
(arc200_balanceOfR: any) => {
if (arc200_balanceOfR.success) {
setBalance(
(
Number(arc200_balanceOfR.returnValue) /
10 ** token.decimals
).toLocaleString()
);
const arc200_balanceOf = arc200_balanceOfR.returnValue;
const balanceBn = new BigNumber(arc200_balanceOf);
const balanceStr = balanceBn
.dividedBy(new BigNumber(10).pow(token.decimals))
.toFixed(token.decimals);
setBalance(balanceStr);
}
}
);
Expand Down Expand Up @@ -1064,12 +1067,11 @@ const Swap = () => {
ci.arc200_balanceOf(activeAccount.address).then(
(arc200_balanceOfR: any) => {
if (arc200_balanceOfR.success) {
setBalance2(
(
Number(arc200_balanceOfR.returnValue) /
10 ** token2.decimals
).toLocaleString()
);
const balanceBn = new BigNumber(arc200_balanceOfR.returnValue);
const balanceStr = balanceBn
.dividedBy(new BigNumber(10).pow(token2.decimals))
.toFixed(token2.decimals);
setBalance2(balanceStr);
}
}
);
Expand Down

0 comments on commit c5c1cff

Please sign in to comment.