Skip to content

Commit

Permalink
Merge pull request #9 from oraichain/develop
Browse files Browse the repository at this point in the history
Feature: Add check minimum bridge fee and update contracts
  • Loading branch information
perfogic authored Jul 31, 2024
2 parents af4eeac + 490ccdc commit 2927f8e
Show file tree
Hide file tree
Showing 14 changed files with 164 additions and 147 deletions.
20 changes: 20 additions & 0 deletions assets/icons/token.tsx

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion components/page/bridge/components/inputBridge/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ArrowDownIcon } from "@/assets/icons/arrow";
import { SelectOptionIcon } from "@/assets/icons/network";
import SelectCommon from "@/components/commons/select";
import { AMOUNT_BALANCE_ENTRIES_UNIVERSAL_SWAP } from "@/constants/config";
import { TonNetwork } from "@/constants/networks";
import { TonNetwork } from "@/constants/ton";
import { OraichainTokenList, TonTokenList } from "@/constants/tokens";
import { numberWithCommas } from "@/helper/number";
import { useCoinGeckoPrices } from "@/hooks/useCoingecko";
Expand Down
4 changes: 3 additions & 1 deletion components/page/bridge/constants.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { toNano } from "@ton/core";

const FWD_AMOUNT = toNano(0.1);
const FWD_AMOUNT = toNano(0.15);
const TON_MESSAGE_VALID_UNTIL = 100000;
const BRIDGE_TON_TO_ORAI_MINIMUM_GAS = toNano(1);
const EXTERNAL_MESSAGE_FEE = toNano(0.01);
const MINIMUM_BRIDGE_PER_USD = 0.001;

export {
FWD_AMOUNT,
TON_MESSAGE_VALID_UNTIL,
BRIDGE_TON_TO_ORAI_MINIMUM_GAS,
EXTERNAL_MESSAGE_FEE,
MINIMUM_BRIDGE_PER_USD,
};
3 changes: 2 additions & 1 deletion components/page/bridge/hooks/useGetFee.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { network, TonNetwork } from "@/constants/networks";
import { TonNetwork } from "@/constants/ton";
import { network } from "@/constants/networks";
import { TokenType, TonTokenList } from "@/constants/tokens";
import { useAuthOraiAddress } from "@/stores/authentication/selector";
import { BigDecimal, toDisplay } from "@oraichain/oraidex-common";
Expand Down
64 changes: 42 additions & 22 deletions components/page/bridge/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@ import { OraiIcon } from "@/assets/icons/token";
import Loader from "@/components/commons/loader/Loader";
import ConnectButton from "@/components/layout/connectButton";
import { TON_SCAN } from "@/constants/config";
import mixpanel from "mixpanel-browser";
import {
TON_ADDRESS_CONTRACT,
TonInteractionContract,
TonNetwork,
network,
} from "@/constants/networks";
import { TonNetwork } from "@/constants/ton";
import { TON_ZERO_ADDRESS, TonInteractionContract } from "@/constants/contract";
import { network } from "@/constants/networks";
import {
OraichainTokenList,
TokenType,
Expand Down Expand Up @@ -61,9 +57,11 @@ import {
FWD_AMOUNT,
TON_MESSAGE_VALID_UNTIL,
BRIDGE_TON_TO_ORAI_MINIMUM_GAS,
MINIMUM_BRIDGE_PER_USD,
} from "./constants";
import { getMixPanelClient } from "@/libs/mixpanel";
import { useTonConnectUI } from "@tonconnect/ui-react";
import { useCoinGeckoPrices } from "@/hooks/useCoingecko";

const Bridge = () => {
const oraiAddress = useAuthOraiAddress();
Expand Down Expand Up @@ -91,6 +89,7 @@ const Bridge = () => {
jettonWalletAddress: null,
});
const [deductNativeAmount, setDeductNativeAmount] = useState(0n);
const { data: prices } = useCoinGeckoPrices();

const destinationAddress =
toNetwork.id === NetworkList.oraichain.id
Expand All @@ -109,7 +108,7 @@ const Bridge = () => {
useEffect(() => {
if (
toNetwork.id == NetworkList.oraichain.id &&
token?.contractAddress === TON_ADDRESS_CONTRACT
token?.contractAddress === TON_ZERO_ADDRESS
) {
setDeductNativeAmount(BRIDGE_TON_TO_ORAI_MINIMUM_GAS);
return;
Expand All @@ -128,7 +127,7 @@ const Bridge = () => {
const client = new TonClient({
endpoint,
});
if (token?.contractAddress === TON_ADDRESS_CONTRACT) {
if (token?.contractAddress === TON_ZERO_ADDRESS) {
setDeductNativeAmount(BRIDGE_TON_TO_ORAI_MINIMUM_GAS);
setTokenInfo({
jettonWalletAddress: "",
Expand Down Expand Up @@ -164,7 +163,7 @@ const Bridge = () => {
const bridgeAdapter =
TonInteractionContract[TonNetwork.Mainnet].bridgeAdapter;

if (token.contractAddress === TON_ADDRESS_CONTRACT) {
if (token.contractAddress === TON_ZERO_ADDRESS) {
const balance = await client.getBalance(Address.parse(bridgeAdapter));

return {
Expand Down Expand Up @@ -194,6 +193,16 @@ const Bridge = () => {
const handleCheckBalanceBridgeOfOraichain = async (token: TokenType) => {
try {
if (token) {
if (!token.contractAddress) {
const data = await window.client.getBalance(
network.CW_TON_BRIDGE,
token.denom
);
return {
balance: data.amount,
};
}

const tx = await window.client.queryContractSmart(
token.contractAddress,
{
Expand Down Expand Up @@ -226,6 +235,19 @@ const Bridge = () => {
return toDisplay(balance || 0, token.decimal || CW20_DECIMALS);
};

const validatePrice = (token: TokenType, amount: number) => {
const usdPrice = new BigDecimal(amount || 0)
.mul(prices?.[token?.coingeckoId] || 0)
.toNumber();

const minimumAmount =
Math.ceil((MINIMUM_BRIDGE_PER_USD * amount * 1000) / usdPrice) / 1000;

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

const handleBridgeFromTon = async () => {
try {
if (!oraiAddress) {
Expand All @@ -240,6 +262,8 @@ const Bridge = () => {
throw "Not valid!";
}

validatePrice(token, amount);

setLoading(true);

const tokenInOrai = OraichainTokenList.find(
Expand All @@ -250,21 +274,19 @@ const Bridge = () => {
tokenInOrai
);

if (Number(balanceMax) < Number(amount)) {
if (
Number(balanceMax) < Number(amount) &&
token.contractAddress !== TON_ZERO_ADDRESS
) {
setLoading(false);
throw `The bridge contract does not have enough balance to process this bridge transaction. Wanted ${amount} ${token.symbol}, have ${balanceMax} ${token.symbol}`;
}

if (Number(amount) < 100) {
throw Error("Minimum bridge is 100 USDT");
}

const bridgeAdapterAddress = Address.parse(
TonInteractionContract[tonNetwork].bridgeAdapter
);
const fmtAmount = new BigDecimal(10).pow(token.decimal).mul(amount);
const isNativeTon: boolean =
token.contractAddress === TON_ADDRESS_CONTRACT;
const isNativeTon: boolean = token.contractAddress === TON_ZERO_ADDRESS;
const toAddress: string = isNativeTon
? bridgeAdapterAddress.toString()
: tokenInfo.jettonWalletAddress?.toString();
Expand Down Expand Up @@ -370,6 +392,8 @@ const Bridge = () => {
throw "Not valid!";
}

validatePrice(token, amount);

setLoading(true);

const tokenInTon = TonTokenList(TonNetwork.Mainnet).find(
Expand All @@ -390,15 +414,11 @@ const Bridge = () => {
tokenInTon?.decimal || CW20_DECIMALS
);

if (displayBalance < Number(amount)) {
if (displayBalance < Number(amount) && token.contractAddress !== null) {
setLoading(false);
throw `The bridge contract does not have enough balance to process this bridge transaction. Wanted ${amount} ${token.symbol}, have ${displayBalance} ${token.symbol}`;
}

if (Number(amount) < 100) {
throw Error("Minimum bridge is 100 USDT");
}

const tonBridgeClient = new TonbridgeBridgeClient(
window.client,
oraiAddress,
Expand Down
11 changes: 10 additions & 1 deletion constants/chainInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ import {
WRAP_TRON_TRX_CONTRACT,
defaultBech32Config,
} from "@oraichain/oraidex-common";
import { TonNetworkICon } from "@/assets/icons/network";
import { TON_DENOM } from "./tokens";

export const OraiToken: BridgeAppCurrency = {
coinDenom: "ORAI",
Expand Down Expand Up @@ -86,12 +88,18 @@ export const oraichainNetwork: CustomChainInfo = {
},
bech32Config: defaultBech32Config("orai"),
feeCurrencies: [OraiToken],

Icon: OraiIcon,
IconLight: OraiIcon,
features: ["ibc-transfer", "cosmwasm", "wasmd_0.24+"],
currencies: [
OraiToken,
{
coinDenom: "TON",
coinGeckoId: "the-open-network",
coinMinimalDenom: TON_DENOM,
coinDecimals: 9,
Icon: TonNetworkICon,
},
{
coinDenom: "ATOM",
coinGeckoId: "cosmos",
Expand Down Expand Up @@ -173,6 +181,7 @@ export const oraichainNetwork: CustomChainInfo = {
coinDecimals: 18,
// Icon: MilkyIcon
},

{
coinDenom: "MILKY",
coinGeckoId: "milky-token",
Expand Down
33 changes: 33 additions & 0 deletions constants/contract.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { TonNetwork } from "./ton";

export const TonInteractionContract = {
[TonNetwork.Mainnet]: {
lightClient: "EQDzy_POlimFDyzrHd3OQsb9sZCngyG3O7Za4GRFzM-rrO93",
whitelist: "EQATDM6mfPZjPDMD9TVa6D9dlbmAKY5w6xOJiTXJ9Nqj_dsu",
bridgeAdapter: "EQC-aFP0rJXwTgKZQJPbPfTSpBFc8wxOgKHWD9cPvOl_DnaY",
},
[TonNetwork.Testnet]: {
lightClient: "",
whitelist: "EQD2xPIqdeggqtP3q852Y-7yD-RRHi12Zy7M4iUx4-7q0E1",
bridgeAdapter: "EQDZfQX89gMo3HAiW1tSK9visb2gouUvDCt6PODo3qkXKeox",
},
};

export const TON_ZERO_ADDRESS =
"EQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAM9c";

export const TonTokensContract = {
[TonNetwork.Mainnet]: {
usdt: "EQCxE6mUtQJKFnGfaROTKOt1lZbDiiX1kCixRv7Nw2Id_sDs",
ton: TON_ZERO_ADDRESS,
},
[TonNetwork.Testnet]: {
usdt: "EQA5FnPP13uZPJQq7aj6UHLEukJJZSZW053cU1Wu6R6BpYYB",
ton: TON_ZERO_ADDRESS,
},
};

export const CW_TON_BRIDGE =
"orai159l8l9c5ckhqpuwdfgs9p4v599nqt3cjlfahalmtrhfuncnec2ms5mz60e";
export const TOKEN_FACTORY =
"orai1wuvhex9xqs3r539mvc6mtm7n20fcj3qr2m0y9khx6n5vtlngfzes3k0rq9";
48 changes: 4 additions & 44 deletions constants/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,58 +4,18 @@ import {
ROUTER_V2_CONTRACT,
} from "@oraichain/oraidex-common";
import { oraichainNetwork } from "./chainInfo";

export enum TonNetwork {
Mainnet = "mainnet",
Testnet = "testnet",
}

export const TonInteractionContract = {
[TonNetwork.Mainnet]: {
lightClient: "EQDt5RAUICxUeHaNicwspH8obI__z3X0UHy6vv1xhpi3AbfT",
whitelist: "EQATDM6mfPZjPDMD9TVa6D9dlbmAKY5w6xOJiTXJ9Nqj_dsu",
bridgeAdapter: "EQC-aFP0rJXwTgKZQJPbPfTSpBFc8wxOgKHWD9cPvOl_DnaY",
},
[TonNetwork.Testnet]: {
lightClient: "",
whitelist: "EQD2xPIqdeggqtP3q852Y-7yD-RRHi12Zy7M4iUx4-7q0E1",
bridgeAdapter: "EQDZfQX89gMo3HAiW1tSK9visb2gouUvDCt6PODo3qkXKeox",
},
};

export const TON_ADDRESS_CONTRACT =
"EQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAM9c";

export const TonTokensContract = {
[TonNetwork.Mainnet]: {
usdt: "EQBynBO23ywHy_CgarY9NK9FTz0yDsG82PtcbSTQgGoXwiuA",
// btc: "EQDcBkGHmC4pTf34x3Gm05XvepO5w60DNxZ-XT4I6-UGG5L5",
// dai: "EQDo_ZJyQ_YqBzBwbVpMmhbhIddKtRP99HugZJ14aFscxi7B",
// usdc: "EQB-MPwrd1G6WKNkLz_VnV6WqBDd142KMQv-g1O-8QUA3728",
// ton: TON_ADDRESS_CONTRACT,
},
[TonNetwork.Testnet]: {
usdt: "EQA5FnPP13uZPJQq7aj6UHLEukJJZSZW053cU1Wu6R6BpYYB",
// btc: "",
// dai: "",
// usdc: "",
// ton: null,
},
};

export const CW20_TON_CONTRACT =
"orai1v5msmzjhyrf0285fyhfwg7uxk4yzdhrn6srvf8jf27dz8uuvu3mstj78qt";
import { CW_TON_BRIDGE, TOKEN_FACTORY } from "./contract";

export const network = {
...oraichainNetwork,
prefix: oraichainNetwork.bech32Config.bech32PrefixAccAddr,
denom: "orai",
coinType: oraichainNetwork.bip44.coinType,
fee: { gasPrice: "0.00506", amount: "1518", gas: "2000000" }, // 0.000500 ORAI
explorer: "https://scan.orai.io",
router: ROUTER_V2_CONTRACT,
oracle: ORACLE_CONTRACT,
multicall: MULTICALL_CONTRACT,
explorer: "https://scan.orai.io",
CW_TON_BRIDGE:
"orai159l8l9c5ckhqpuwdfgs9p4v599nqt3cjlfahalmtrhfuncnec2ms5mz60e",
CW_TON_BRIDGE: CW_TON_BRIDGE,
TOKEN_FACTORY: TOKEN_FACTORY,
};
Loading

0 comments on commit 2927f8e

Please sign in to comment.