Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions src/config/tokens/index.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import { usdcEvm, usdcNear, usdcSol, usdcChains, usdcAptos } from "@/config/tokens/usdc";
import { usdtAptos, usdtEvm, usdtNear, usdtSol, usdtTron, usdtChains } from "@/config/tokens/usdt";

export const evmBalancesTokens = (() => {
const evmTokenChains = [
usdcEvm.chains,
usdtEvm.chains,
];
export const evmBalancesTokens: { chain_id: number; tokens: string[]; decimals: number[]; }[] = (() => {
const map: any = {};
usdcEvm.chains.forEach((chain: any) => {
if (map[chain.chainName]) {
map[chain.chainName].tokens.push(chain.contractAddress);
} else {
map[chain.chainName] = {
chain_id: chain.chainId,
tokens: [chain.contractAddress]
};
}
});
usdtEvm.chains.forEach((chain: any) => {
if (map[chain.chainName]) {
map[chain.chainName].tokens.push(chain.contractAddress);
} else {
map[chain.chainName] = {
chain_id: chain.chainId,
tokens: [chain.contractAddress]
};
}
});
for (const chains of evmTokenChains) {
chains.forEach((chain: any) => {
if (map[chain.chainName]) {
map[chain.chainName].tokens.push(chain.contractAddress);
map[chain.chainName].decimals.push(chain.decimals);
map[chain.chainName].symbols.push(chain.symbol);
} else {
map[chain.chainName] = {
chain_id: chain.chainId,
tokens: [chain.contractAddress],
decimals: [chain.decimals],
symbols: [chain.symbol],
};
}
});
}

return Object.values(map);
})();
Expand Down
71 changes: 41 additions & 30 deletions src/hooks/use-evm-balances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,43 @@ export default function useEvmBalances(auto = false) {
const _balances: any = {};
const _data = res.data.data;

const setBalances = (__data: any) => {
let usdcBalance = Big(0);
let usdtBalance = Big(0);

Object.entries(__data).forEach(([key, item]: any) => {
if (!item) return;
const currentTokenChain = evmBalancesTokens.find((token) => Number(token.chain_id) === Number(key));
item.forEach((sl: any) => {
const currentTokenIndex = currentTokenChain?.tokens?.map?.((address) => address.toLowerCase())?.indexOf?.(sl.address.toLowerCase());
let currentTokenDecimals = 6;
if (currentTokenChain && typeof currentTokenIndex === "number" && currentTokenIndex > -1) {
currentTokenDecimals = currentTokenChain.decimals[currentTokenIndex];
}
const _balance = Big(sl.balance).div(10 ** currentTokenDecimals);
if (usdcAddresses.includes(sl.address)) {
usdcBalance = usdcBalance.plus(_balance);
}
if (usdtAddresses.includes(sl.address)) {
usdtBalance = usdtBalance.plus(_balance);
}
_balances[sl.address] = _balance.toString();
});
});

if (wallet?.account) {
balancesStore.set({
evmBalances: {
..._balances,
usdcBalance: usdcBalance.toString(),
usdtBalance: usdtBalance.toString()
}
});
}
};

setBalances(_data);

const unsupportedChainIds = evmBalancesTokens.map((token: any) => Object.keys(_data).includes(token.chain_id.toString()) ? null : token.chain_id).filter(Boolean);
const unsupportedBalances: any = {};
// get unsupported tokens balances from provider
Expand Down Expand Up @@ -69,8 +106,9 @@ export default function useEvmBalances(auto = false) {
return _result;
});

const balances = await Promise.all(balancePromises);
unsupportedBalances[_token.chain_id] = balances;
const balances = await Promise.allSettled(balancePromises);
const validBalances = balances.filter((balance) => balance.status === "fulfilled").map((balance) => balance.value);
unsupportedBalances[_token.chain_id] = validBalances;
}
}

Expand All @@ -80,34 +118,7 @@ export default function useEvmBalances(auto = false) {
}
}

let usdcBalance = Big(0);
let usdtBalance = Big(0);

Object.entries(_data).forEach(([key, item]: any) => {
if (!item) return;
item.forEach((sl: any) => {
const _balance = Big(sl.balance).div(
10 ** (Number(key) === 56 ? 18 : 6)
);
if (usdcAddresses.includes(sl.address)) {
usdcBalance = usdcBalance.plus(_balance);
}
if (usdtAddresses.includes(sl.address)) {
usdtBalance = usdtBalance.plus(_balance);
}
_balances[sl.address] = _balance.toString();
});
});

if (wallet?.account) {
balancesStore.set({
evmBalances: {
..._balances,
usdcBalance: usdcBalance.toString(),
usdtBalance: usdtBalance.toString()
}
});
}
setBalances(_data);

setLoading(false);
} catch (error) {
Expand Down
6 changes: 3 additions & 3 deletions src/sections/wallet/total.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default function Total() {
if (!key.includes("Balances")) return;
const chainType = key.split("Balances")[0];
const currentChain = chainTypes[chainType];
const currentTokenWithChains = stablecoinWithChains[chainType][walletStore.selectedToken];
const currentTokenWithChains = stablecoinWithChains[chainType]?.[walletStore.selectedToken];
_balanceSummaries[chainType] = {
balance: Big(0),
balanceString: "0.00",
Expand Down Expand Up @@ -104,7 +104,7 @@ export default function Total() {
_balanceSummariesListWithBalance.length,
finalPercentages.map(percent => percent + "%").join(" ")
];
}, [balancesStore]);
}, [balancesStore, walletStore.selectedToken]);

return (
<div className="flex flex-col justify-center items-center border-b border-[#EDF0EF] pb-[40px] mt-[20px]">
Expand All @@ -113,7 +113,7 @@ export default function Total() {
<div className="">
Total {walletStore.selectedToken}
</div>
{total && Big(total).gt(0) ? (
{total && typeof total !== "undefined" ? (
<Amount
amount={total}
className="mt-[4px]"
Expand Down
7 changes: 7 additions & 0 deletions src/services/oneclick/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ class OneClickService {
} catch (error) { }
res.data.priceImpact = numberRemoveEndZero(Big(priceImpact).toFixed(4));

const fromTokenSymbol = params.fromToken.symbol === "USD₮0" ? "USDT" : params.fromToken.symbol;
const toTokenSymbol = params.toToken.symbol === "USD₮0" ? "USDT" : params.toToken.symbol;
res.data.exchangeRate = "1";
if (fromTokenSymbol !== toTokenSymbol) {
res.data.exchangeRate = numberRemoveEndZero(Big(res.data.quote?.amountOutFormatted || 0).div(res.data.quote?.amountInFormatted || 1).toFixed(params.toToken.decimals, 0));
}

try {
// const bridgeFee = BridgeFee.reduce((acc, item) => {
// return acc.plus(Big(item.fee).div(100));
Expand Down
6 changes: 4 additions & 2 deletions src/utils/format/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const formatNumber = (
isShort?: boolean;
isShortUppercase?: boolean;
round?: Big.RoundingMode;
isLessPrecision?: boolean;
}
): any => {
const {
Expand All @@ -33,7 +34,8 @@ export const formatNumber = (
isZeroPrecision,
isShort,
isShortUppercase,
round = Big.roundHalfUp
round = Big.roundHalfUp,
isLessPrecision = true,
} = options || {};

const isValid = () => {
Expand Down Expand Up @@ -65,7 +67,7 @@ export const formatNumber = (
};
}

if (Big(value).lt(Big(10).pow(-precision))) {
if (isLessPrecision && Big(value).lt(Big(10).pow(-precision))) {
if (isSimple) {
return `< ${prefix}${Big(10).pow(-precision).toFixed(precision, round)}`;
}
Expand Down
32 changes: 26 additions & 6 deletions src/views/bridge/components/result/oneclick.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const ResultOneClick = (props: any) => {
bridgeFee: totalBridgeFeeLabel,
bridgeFeeValue: 0,
netFee: 0,
exchangeRate: 1,
slippage,
});
return;
Expand All @@ -51,6 +52,7 @@ const ResultOneClick = (props: any) => {
bridgeFee: totalBridgeFeeLabel,
bridgeFeeValue: totalBridgeFeeValue,
netFee: _quoteData?.fees?.destinationGasFeeUsd,
exchangeRate: formatNumber(_quoteData?.exchangeRate, 6, true, { round: Big.roundDown }),
slippage,
});
}, { wait: 500 });
Expand All @@ -71,6 +73,12 @@ const ResultOneClick = (props: any) => {
return Big(energySourceGasFee).minus(_quoteData?.quoteParam?.needsEnergyAmount || 0).toFixed(0);
}, [isFromTron, _quoteData]);

const isExchangeToken = useMemo(() => {
const fromTokenSymbol = _quoteData?.quoteParam?.fromToken?.symbol === "USD₮0" ? "USDT" : _quoteData?.quoteParam?.fromToken?.symbol;
const toTokenSymbol = _quoteData?.quoteParam?.toToken?.symbol === "USD₮0" ? "USDT" : _quoteData?.quoteParam?.toToken?.symbol;
return fromTokenSymbol && toTokenSymbol && fromTokenSymbol !== toTokenSymbol;
}, [_quoteData]);

return (
<AnimatePresence>
{
Expand All @@ -82,12 +90,24 @@ const ResultOneClick = (props: any) => {
animate={{ height: "auto", opacity: 1 }}
exit={{ height: 0, opacity: 0 }}
>
<ResultFeeItem
label="Net fee"
loading={bridgeStore.quotingMap.get(Service.OneClick)}
>
{fees?.netFee}
</ResultFeeItem>
{
isExchangeToken ? (
<ResultFeeItem
label="Exchange Rate"
loading={bridgeStore.quotingMap.get(Service.OneClick)}
isFormat={false}
>
1 {_quoteData?.quoteParam.fromToken.symbol} ~ {fees?.exchangeRate} {_quoteData?.quoteParam.toToken.symbol}
</ResultFeeItem>
) : (
<ResultFeeItem
label="Net fee"
loading={bridgeStore.quotingMap.get(Service.OneClick)}
>
{fees?.netFee}
</ResultFeeItem>
)
}
<ResultFeeItem
label={(
<>
Expand Down