Skip to content

Commit

Permalink
fix: conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
quangdz1704 committed Jul 19, 2024
2 parents f2cf1c6 + 3bd9472 commit 5cc95f2
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 50 deletions.
64 changes: 45 additions & 19 deletions components/page/bridge/components/inputBridge/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import {
import classNames from "classnames";
import { Dispatch, FC, SetStateAction, useRef, useState } from "react";
import NumberFormat from "react-number-format";
import { fromNano } from "@ton/core";
import styles from "./index.module.scss";
import { EXTERNAL_MESSAGE_FEE } from "../../constants";

export type NetworkType = "Oraichain" | "Ton";

Expand All @@ -33,6 +35,7 @@ const InputBridge: FC<{
setToken: Dispatch<any>;
txtSearch: string;
setTxtSearch: Dispatch<SetStateAction<string>>;
deductNativeAmount: bigint;
}> = ({
networkTo = "Oraichain",
disabled = false,
Expand All @@ -43,6 +46,7 @@ const InputBridge: FC<{
setToken,
txtSearch,
setTxtSearch,
deductNativeAmount,
}) => {
const amounts = useAmountsCache();
const amountsTon = useTonAmountsCache();
Expand Down Expand Up @@ -81,25 +85,47 @@ const InputBridge: FC<{
{token?.symbol}
</div>
<div className={styles.percentWrapper}>
{AMOUNT_BALANCE_ENTRIES_UNIVERSAL_SWAP.map(([coeff, text]) => (
<button
disabled={!token}
key={coeff}
className={classNames(styles.percent, {
activePercent: coe === coeff,
})}
onClick={(event) => {
event.stopPropagation();
onChangeAmount &&
onChangeAmount(
new BigDecimal(coeff).mul(displayBalance).toNumber()
);
setCoe(coeff);
}}
>
{text}
</button>
))}
{AMOUNT_BALANCE_ENTRIES_UNIVERSAL_SWAP.map(([coeff, text]) => {
return (
<button
disabled={!token}
key={coeff}
className={classNames(styles.percent, {
activePercent: coe === coeff,
})}
onClick={(event) => {
event.stopPropagation();
const formattedDeductNativeAmount =
fromNano(deductNativeAmount);

// @dev: if choose 100% then minus with fee for execute ton tx
const amount = new BigDecimal(coeff)
.mul(displayBalance)
.sub(coeff == 1 ? formattedDeductNativeAmount : 0n)
.sub(
deductNativeAmount > 0n
? fromNano(EXTERNAL_MESSAGE_FEE)
: 0n
)
.toNumber();

const foreseeBalance = new BigDecimal(displayBalance)
.sub(formattedDeductNativeAmount)
.sub(amount)
.toNumber();

onChangeAmount &&
// @dev: still need to validate whether amount > display balance
onChangeAmount(
foreseeBalance >= 0 && amount >= 0 ? amount : 0
);
setCoe(coeff);
}}
>
{text}
</button>
);
})}
</div>
</div>
<div className={styles.content}>
Expand Down
13 changes: 13 additions & 0 deletions components/page/bridge/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { toNano } from "@ton/core";

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

export {
FWD_AMOUNT,
TON_MESSAGE_VALID_UNTIL,
BRIDGE_TON_TO_ORAI_MINIMUM_GAS,
EXTERNAL_MESSAGE_FEE,
};
43 changes: 31 additions & 12 deletions components/page/bridge/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ import {
initToNetwork,
useFillNetwork,
} from "@/hooks/useFillNetwork";
import {
FWD_AMOUNT,
TON_MESSAGE_VALID_UNTIL,
BRIDGE_TON_TO_ORAI_MINIMUM_GAS,
} from "./constants";

const Bridge = () => {
const oraiAddress = useAuthOraiAddress();
Expand All @@ -83,6 +88,7 @@ const Bridge = () => {
const [tokenInfo, setTokenInfo] = useState({
jettonWalletAddress: null,
});
const [deductNativeAmount, setDeductNativeAmount] = useState(0n);

const destinationAddress =
toNetwork.id === NetworkList.oraichain.id
Expand All @@ -98,6 +104,17 @@ const Bridge = () => {
setToNetwork,
});

useEffect(() => {
if (
toNetwork.id == NetworkList.oraichain.id &&
token?.contractAddress === TON_ADDRESS_CONTRACT
) {
setDeductNativeAmount(BRIDGE_TON_TO_ORAI_MINIMUM_GAS);
return;
}
setDeductNativeAmount(0n);
}, [toNetwork, token]);

// @dev: this function will changed based on token minter address (which is USDT, USDC, bla bla bla)
useEffect(() => {
try {
Expand All @@ -109,8 +126,8 @@ const Bridge = () => {
const client = new TonClient({
endpoint,
});

if (token?.contractAddress === TON_ADDRESS_CONTRACT) {
setDeductNativeAmount(BRIDGE_TON_TO_ORAI_MINIMUM_GAS);
setTokenInfo({
jettonWalletAddress: "",
});
Expand All @@ -128,6 +145,7 @@ const Bridge = () => {
setTokenInfo({
jettonWalletAddress,
});
setDeductNativeAmount(0n);
})();
} catch (error) {
console.log("error :>>", error);
Expand Down Expand Up @@ -245,9 +263,9 @@ const Bridge = () => {
? bridgeAdapterAddress.toString()
: tokenInfo.jettonWalletAddress?.toString();
const oraiAddressBech32 = fromBech32(oraiAddress).data;
const toAmount = isNativeTon
? fmtAmount.add(toNano(1)).toString()
: toNano(1).toString();
const gasAmount = isNativeTon
? fmtAmount.add(BRIDGE_TON_TO_ORAI_MINIMUM_GAS).toString()
: BRIDGE_TON_TO_ORAI_MINIMUM_GAS.toString();
const timeout = BigInt(Math.floor(new Date().getTime() / 1000) + 3600);
const memo = beginCell().endCell();

Expand All @@ -270,7 +288,7 @@ const Bridge = () => {
JettonWallet.buildSendTransferPacket(
Address.parse(tonAddress),
{
fwdAmount: toNano("0.1"),
fwdAmount: FWD_AMOUNT,
jettonAmount: BigInt(fmtAmount.toString()),
jettonMaster: Address.parse(token.contractAddress),
remoteReceiver: oraiAddress,
Expand All @@ -281,17 +299,17 @@ const Bridge = () => {
0
).toBoc();

const boc = isNativeTon
? getNativeBridgePayload()
: getOtherBridgeTokenPayload();

const tx = await connector.sendTransaction({
validUntil: 100000,
validUntil: TON_MESSAGE_VALID_UNTIL,
messages: [
{
address: toAddress, // dia chi token
amount: toAmount, // gas
payload: Base64.encode(
isNativeTon
? getNativeBridgePayload()
: getOtherBridgeTokenPayload()
),
amount: gasAmount, // gas
payload: Base64.encode(boc),
},
],
});
Expand Down Expand Up @@ -481,6 +499,7 @@ const Bridge = () => {
tonNetwork={tonNetwork}
setToken={setToken}
networkTo={toNetwork.id as NetworkType}
deductNativeAmount={deductNativeAmount}
/>
</div>
<div className={styles.destination}>
Expand Down
2 changes: 1 addition & 1 deletion constants/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const TonInteractionContract = {
[TonNetwork.Mainnet]: {
lightClient: "EQDt5RAUICxUeHaNicwspH8obI__z3X0UHy6vv1xhpi3AbfT",
whitelist: "EQATDM6mfPZjPDMD9TVa6D9dlbmAKY5w6xOJiTXJ9Nqj_dsu",
bridgeAdapter: "EQArWlaBgdGClwJrAkQjQP_8zxIK_bdgbH-6qdl4f5JEfo3r",
bridgeAdapter: "EQC-aFP0rJXwTgKZQJPbPfTSpBFc8wxOgKHWD9cPvOl_DnaY",
},
[TonNetwork.Testnet]: {
lightClient: "",
Expand Down
36 changes: 18 additions & 18 deletions constants/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ export const OraichainTokenList: TokenType[] = [
coingeckoId: "usd-coin",
decimal: 6,
},
{
name: "Ton",
symbol: "TON",
Icon: TonNetworkICon,
contractAddress: CW20_TON_CONTRACT,
denom: "cw20_ton",
coingeckoId: "the-open-network",
decimal: 6,
},
// {
// name: "Ton",
// symbol: "TON",
// Icon: TonNetworkICon,
// contractAddress: CW20_TON_CONTRACT,
// denom: "cw20_ton",
// coingeckoId: "the-open-network",
// decimal: 6,
// },
];

export const TonTokenList = (network: TonNetwork): TokenType[] => [
Expand All @@ -62,13 +62,13 @@ export const TonTokenList = (network: TonNetwork): TokenType[] => [
coingeckoId: "usd-coin",
decimal: 6,
},
{
name: "Ton",
symbol: "TON",
Icon: TonNetworkICon,
contractAddress: TonTokensContract[network].ton,
denom: "ton",
coingeckoId: "the-open-network",
decimal: 9,
},
// {
// name: "Ton",
// symbol: "TON",
// Icon: TonNetworkICon,
// contractAddress: TonTokensContract[network].ton,
// denom: "ton",
// coingeckoId: "the-open-network",
// decimal: 9,
// },
];

0 comments on commit 5cc95f2

Please sign in to comment.