Skip to content

Commit

Permalink
Merge pull request #14 from oraichain/feat/usdc
Browse files Browse the repository at this point in the history
Feat/usdc
  • Loading branch information
perfogic authored Sep 9, 2024
2 parents ba3b615 + 1e48768 commit 9714b10
Show file tree
Hide file tree
Showing 13 changed files with 267 additions and 175 deletions.
5 changes: 3 additions & 2 deletions components/page/bridge/components/inputBridge/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,12 @@ const InputBridge: FC<{
}

let networkList = TonTokenList(tonNetwork);
if (networkTo === NetworkList.ton.id) networkList = OraichainTokenList;
if (networkTo === NetworkList.ton.id)
networkList = OraichainTokenList(tonNetwork);
if (networkTo === NetworkList["osmosis-1"].id) {
const tokenList =
networkFrom === NetworkList.oraichain.id
? OraichainTokenList
? OraichainTokenList(tonNetwork)
: TonTokenList(tonNetwork);
networkList = filterByCoingeckoId(tokenList);
}
Expand Down
34 changes: 23 additions & 11 deletions components/page/bridge/hooks/useGetFee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,39 @@ import { useAuthOraiAddress } from "@/stores/authentication/selector";
import { BigDecimal } from "@oraichain/oraidex-common";
import { TonbridgeBridgeClient } from "@oraichain/tonbridge-contracts-sdk";
import { useEffect, useState } from "react";
import { useWalletsTonCache } from "@/stores/token/selector";

const useGetFee = ({ token }: { token: TokenType }) => {
const oraiAddress = useAuthOraiAddress();
const [bridgeFee, setBridgeFee] = useState(0);
const [tokenFee, setTokenFee] = useState(0);
const network = getNetworkConfig(Environment.Mainnet);
const network = getNetworkConfig(process.env.NEXT_PUBLIC_ENV as Environment);
const walletsTon = useWalletsTonCache();

useEffect(() => {
(async () => {
try {
if (token) {
const tokenInTon = TonTokenList(Environment.Mainnet).find(
(tk) => tk.coingeckoId === token.coingeckoId
);
const tokenInTon = TonTokenList(
process.env.NEXT_PUBLIC_ENV as Environment
).find((tk) => tk.coingeckoId === token.coingeckoId);
if (!tokenInTon) {
return;
}
const walletTon = walletsTon[tokenInTon.denom];
if (!walletTon) {
return;
}

const tonBridgeClient = new TonbridgeBridgeClient(
window.client,
oraiAddress,
network.CW_TON_BRIDGE
);

// TODO: change to jetton wallet address of bridge adapter instead
const tokenFeeConfig = await tonBridgeClient.tokenFee({
remoteTokenDenom: tokenInTon?.contractAddress,
remoteTokenDenom: walletTon,
});

if (tokenFeeConfig) {
Expand All @@ -52,26 +59,31 @@ const useGetFee = ({ token }: { token: TokenType }) => {
}
}
})();
}, [token, oraiAddress]);
}, [token, oraiAddress, walletsTon]);

useEffect(() => {
(async () => {
if (token) {
const tokenInTon = TonTokenList(Environment.Mainnet).find(
(tk) => tk.coingeckoId === token.coingeckoId
);
const tokenInTon = TonTokenList(
process.env.NEXT_PUBLIC_ENV as Environment
).find((tk) => tk.coingeckoId === token.coingeckoId);
if (!tokenInTon) {
return;
}

const walletTon = walletsTon[tokenInTon.denom];
if (!walletTon) {
return;
}

const tonBridgeClient = new TonbridgeBridgeClient(
window.client,
oraiAddress,
network.CW_TON_BRIDGE
);

const config = await tonBridgeClient.pairMapping({
key: tokenInTon?.contractAddress,
key: walletTon,
});
const pairMapping = config.pair_mapping;

Expand All @@ -80,7 +92,7 @@ const useGetFee = ({ token }: { token: TokenType }) => {
);
}
})();
}, [token, oraiAddress]);
}, [token, oraiAddress, walletsTon]);

return {
bridgeFee,
Expand Down
108 changes: 31 additions & 77 deletions components/page/bridge/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ import {
formatDisplayNumber,
numberWithCommas,
} from "@/helper/number";
import { useAmountsCache, useTonAmountsCache } from "@/stores/token/selector";
import {
useAmountsCache,
useTonAmountsCache,
useWalletsTonCache,
} from "@/stores/token/selector";
import useGetStateData from "./hooks/useGetStateData";
import {
initFromNetwork,
Expand Down Expand Up @@ -101,6 +105,7 @@ const Bridge = () => {

const amounts = useAmountsCache();
const amountsTon = useTonAmountsCache();
const walletsTon = useWalletsTonCache();
const { balances: sentBalance, getChanelStateData } = useGetStateData();
const [loading, setLoading] = useState(false);
const [amount, setAmount] = useState(null);
Expand All @@ -110,7 +115,6 @@ const Bridge = () => {
const [tokenInfo, setTokenInfo] = useState({
jettonWalletAddress: null,
});
const [bridgeJettonWallets, setBridgeJettonWallets] = useState<string[]>();

const [deductNativeAmount, setDeductNativeAmount] = useState(0n);
const { data: prices } = useCoinGeckoPrices();
Expand All @@ -134,40 +138,6 @@ const Bridge = () => {
setToNetwork,
});

useEffect(() => {
try {
(async () => {
const values = TonTokenList(tonNetwork).values();
const endpoint = await getHttpEndpoint();
const client = new TonClient({
endpoint,
});
const bridgeAdapter = TonInteractionContract[tonNetwork].bridgeAdapter;
const jettonWallet = await Promise.all(
[...values].map((value) => {
if (value.contractAddress === TON_ZERO_ADDRESS)
return Address.parse(TON_ZERO_ADDRESS);
const jettonMinter = client.open(
JettonMinter.createFromAddress(
Address.parse(value.contractAddress)
)
);
return jettonMinter.getWalletAddress(Address.parse(bridgeAdapter));
})
);
const allJettonWallet = jettonWallet.reduce((acc, cur, idx) => {
acc.push(cur.toString());
return acc;
}, [] as string[]);
console.log({ allJettonWallet });
console.log({ bridgeAdapter });
setBridgeJettonWallets(allJettonWallet);
})();
} catch (error) {
console.log("error :>>", error);
}
}, []);

useEffect(() => {
if (
toNetwork.id == NetworkList.oraichain.id &&
Expand All @@ -185,41 +155,23 @@ const Bridge = () => {
(async () => {
if (fromNetwork.id !== NetworkList.ton.id || !token) return;

// get the decentralized RPC endpoint
const endpoint = await getHttpEndpoint();
const client = new TonClient({
endpoint,
});
if (token?.contractAddress === TON_ZERO_ADDRESS) {
setDeductNativeAmount(BRIDGE_TON_TO_ORAI_MINIMUM_GAS);
setTokenInfo({
jettonWalletAddress: "",
jettonWalletAddress: walletsTon[token.denom],
});
return;
}

const jettonMinter = JettonMinter.createFromAddress(
Address.parse(token.contractAddress)
);

const jettonMinterContract = client.open(jettonMinter);
const [jettonWalletAddress, bridgeJettonWalletAddress] =
await Promise.all([
jettonMinterContract.getWalletAddress(Address.parse(tonAddress)),
jettonMinterContract.getWalletAddress(
Address.parse(TonInteractionContract[tonNetwork].bridgeAdapter)
),
]);
console.log({ bridgeJettonWalletAddress });
setTokenInfo({
jettonWalletAddress,
jettonWalletAddress: walletsTon[token.denom],
});
setDeductNativeAmount(0n);
})();
} catch (error) {
console.log("error :>>", error);
}
}, [token]); // toNetwork, tonAddress
}, [token, walletsTon]); // toNetwork, tonAddress

const handleCheckBalanceBridgeOfTonNetwork = async (token: TokenType) => {
try {
Expand All @@ -238,17 +190,12 @@ const Bridge = () => {
};
}

const jettonMinter = JettonMinter.createFromAddress(
Address.parse(token.contractAddress)
);
const jettonMinterContract = client.open(jettonMinter);
const jettonWalletAddress = await jettonMinterContract.getWalletAddress(
Address.parse(bridgeAdapter)
const jettonWallet = JettonWallet.createFromAddress(
Address.parse(walletsTon[token.denom])
);
const jettonWallet = JettonWallet.createFromAddress(jettonWalletAddress);
const jettonWalletContract = client.open(jettonWallet);
const balance = await jettonWalletContract.getBalance();
console.log({ bridgeAdapter, jettonMinter, jettonWallet, balance });
console.log({ bridgeAdapter, jettonWallet, balance });
return {
balance: balance.amount,
};
Expand Down Expand Up @@ -339,11 +286,21 @@ const Bridge = () => {
.mul(prices?.[token?.coingeckoId] || 0)
.toNumber();

const minimumAmount =
Math.ceil((MINIMUM_BRIDGE_PER_USD * amount * 100) / usdPrice) / 100;
let totalFee =
Number.parseFloat(
numberWithCommas(bridgeFee || 0, undefined, {
maximumFractionDigits: CW20_DECIMALS,
})
) +
Number.parseFloat(
formatDisplayNumber(
new BigDecimal(tokenFee).mul(amount || 0).toNumber(),
DECIMAL_TOKEN_FEE
)
);

if (amount < minimumAmount) {
throw Error(`Minimum bridge is ${minimumAmount} ${token.symbol}`);
if (amount < totalFee) {
throw Error(`Minimum bridge is ${totalFee} ${token.symbol}`);
}
};

Expand All @@ -361,9 +318,9 @@ const Bridge = () => {

setLoading(true);

const tokenInOrai = OraichainTokenList.find(
(tk) => tk.coingeckoId === token.coingeckoId
);
const tokenInOrai = OraichainTokenList(
process.env.NEXT_PUBLIC_ENV as Environment
).find((tk) => tk.coingeckoId === token.coingeckoId);
const balanceMax = await checkBalanceBridgeByNetwork(
NetworkList.ton.id,
tokenInOrai
Expand Down Expand Up @@ -645,11 +602,8 @@ const Bridge = () => {
const tokenInTon = TonTokenList(tonNetwork).find(
(tk) => tk.coingeckoId === token.coingeckoId
);
const index = TonTokenList(tonNetwork).findIndex(
(tk) => tk.coingeckoId === token.coingeckoId
);

const bridgeJettonWallet = bridgeJettonWallets[index];
const bridgeJettonWallet = walletsTon[tokenInTon.denom];
if (!bridgeJettonWallet) throw "Bridge wallet not found!";

const balanceMax = (sentBalance || []).find(
(b) => b.native.denom === bridgeJettonWallet
Expand Down
10 changes: 5 additions & 5 deletions constants/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ export const TonInteractionContract = {
},
[Environment.Testnet]: {
lightClient: "",
whitelist: "EQD2xPIqdeggqtP3q852Y-7yD-RRHi12Zy7M4iUx4-7q0E1",
bridgeAdapter: "EQDZfQX89gMo3HAiW1tSK9visb2gouUvDCt6PODo3qkXKeox",
whitelist: "",
bridgeAdapter: "",
},
};

export const TonTokensContract = {
[Environment.Mainnet]: {
usdt: "EQCxE6mUtQJKFnGfaROTKOt1lZbDiiX1kCixRv7Nw2Id_sDs",
jUSDC: "EQB-MPwrd1G6WKNkLz_VnV6WqBDd142KMQv-g1O-8QUA3728",
// jUSDT: "EQBynBO23ywHy_CgarY9NK9FTz0yDsG82PtcbSTQgGoXwiuA",
ton: TON_ZERO_ADDRESS,
},
Expand All @@ -33,9 +34,8 @@ export const TonTokensContract = {
ton: TON_ZERO_ADDRESS,
},
[Environment.Testnet]: {
usdt: "EQA5FnPP13uZPJQq7aj6UHLEukJJZSZW053cU1Wu6R6BpYYB",
jUSDT: "EQA5FnPP13uZPJQq7aj6UHLEukJJZSZW053cU1Wu6R6BpYYB",
ton: TON_ZERO_ADDRESS,
usdt: "",
ton: "",
},
};

Expand Down
2 changes: 1 addition & 1 deletion constants/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { oraichainNetwork } from "./chainInfo";
import { CwInteractionContract } from "./contract";
import { Environment } from "./ton";

export const getNetworkConfig = (env: Environment) => {
export const getNetworkConfig = (env: Environment = Environment.Mainnet) => {
return {
...oraichainNetwork,
prefix: oraichainNetwork.bech32Config.bech32PrefixAccAddr,
Expand Down
Loading

0 comments on commit 9714b10

Please sign in to comment.