Skip to content

Commit

Permalink
add deduct fee if it is from native ton
Browse files Browse the repository at this point in the history
  • Loading branch information
perfogic committed Jul 19, 2024
1 parent 403bda6 commit 55d1c78
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
25 changes: 23 additions & 2 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 Down Expand Up @@ -93,11 +95,30 @@ const InputBridge: FC<{
})}
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(deductNativeAmount)
.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 && onChangeAmount(amount > 0 ? amount : 0);

onChangeAmount &&
// @dev: still need to validate whether amount > display balance
onChangeAmount(
foreseeBalance >= 0 && amount >= 0 ? amount : 0
);
setCoe(coeff);
}}
>
Expand Down
8 changes: 7 additions & 1 deletion components/page/bridge/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,11 @@ 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 };
export {
FWD_AMOUNT,
TON_MESSAGE_VALID_UNTIL,
BRIDGE_TON_TO_ORAI_MINIMUM_GAS,
EXTERNAL_MESSAGE_FEE,
};
22 changes: 16 additions & 6 deletions components/page/bridge/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,17 @@ const Bridge = () => {
token,
});

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 @@ -105,7 +116,6 @@ const Bridge = () => {
const client = new TonClient({
endpoint,
});

if (token?.contractAddress === TON_ADDRESS_CONTRACT) {
setDeductNativeAmount(BRIDGE_TON_TO_ORAI_MINIMUM_GAS);
setTokenInfo({
Expand Down Expand Up @@ -279,17 +289,17 @@ const Bridge = () => {
0
).toBoc();

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

const tx = await connector.sendTransaction({
validUntil: TON_MESSAGE_VALID_UNTIL,
messages: [
{
address: toAddress, // dia chi token
amount: gasAmount, // gas
payload: Base64.encode(
isNativeTon
? getNativeBridgePayload()
: getOtherBridgeTokenPayload()
),
payload: Base64.encode(boc),
},
],
});
Expand Down

0 comments on commit 55d1c78

Please sign in to comment.