Skip to content

Commit

Permalink
Merge pull request #1087 from oraidex/feat/add-fee-sol-bridge
Browse files Browse the repository at this point in the history
Feat/add fee sol bridge
  • Loading branch information
haunv3 authored Dec 20, 2024
2 parents e9d3b3a + 21f47d7 commit 85bbf35
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 12 deletions.
37 changes: 25 additions & 12 deletions src/pages/Balance/TransferConvertToken/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import useGetFee from '../hooks/useGetFee';
import useTonBridgeHandler, { EXTERNAL_MESSAGE_FEE } from '../hooks/useTonBridgeHandler';
import { TonChainId } from 'context/ton-provider';
import { network } from 'config/networks';
import useGetFeeSol from '../hooks/useGetFeeSol';

interface TransferConvertProps {
token: TokenItemType;
Expand Down Expand Up @@ -169,14 +170,19 @@ const TransferConvertToken: FC<TransferConvertProps> = ({
toNetwork: toNetworkChainId
});

const { solFee, isOraichainToSol, isSolToOraichain } = useGetFeeSol({
originalFromToken: token,
toChainId: toNetworkChainId,
amountToken: convertAmount
});

const { deductNativeAmount, checkBalanceBridgeByNetwork } = useTonBridgeHandler({
token,
fromNetwork: token.chainId,
toNetwork: toNetworkChainId
});

// bridge fee & relayer fee
let bridgeFee = fromTokenFee + toTokenFee;
let bridgeFee = fromTokenFee + toTokenFee + solFee.relayerFee + solFee.tokenFeeAmount;

const isFromOraichainToBitcoin = token.chainId === 'Oraichain' && toNetworkChainId === ('bitcoin' as any);
const isFromBitcoinToOraichain = token.chainId === ('bitcoin' as string) && toNetworkChainId === 'Oraichain';
Expand Down Expand Up @@ -241,7 +247,14 @@ const TransferConvertToken: FC<TransferConvertProps> = ({

return (
<div className={styles.bridgeFee}>
{bridgeFeeTon ? (
{isSolToOraichain || isOraichainToSol ? (
<>
Bridge fee:{' '}
<span>
{solFee.totalFee} {token.name}
</span>
</>
) : bridgeFeeTon ? (
<>
Bridge fee:{' '}
<span>
Expand All @@ -261,7 +274,7 @@ const TransferConvertToken: FC<TransferConvertProps> = ({
{tonTokenFee} {token.name}{' '}
</span>
</div>
) : null}{' '}
) : null}
{relayerFeeTokenFee > 0 ? (
<div className={styles.relayerFee}>
- Relayer fee:{' '}
Expand All @@ -270,11 +283,13 @@ const TransferConvertToken: FC<TransferConvertProps> = ({
{relayerFeeTokenFee} {token.name}{' '}
</span>
</div>
) : null}{' '}
- Received amount:
) : null}
- Received amount:{' '}
<span>
{' '}
{(receivedAmount > 0 ? receivedAmount : 0).toFixed(6)} {token.name}
{(isSolToOraichain || isOraichainToSol ? solFee.sendAmount : receivedAmount > 0 ? receivedAmount : 0).toFixed(
6
)}{' '}
{token.name}
</span>
{!!toDisplayBTCFee && (
<>
Expand Down Expand Up @@ -303,8 +318,6 @@ const TransferConvertToken: FC<TransferConvertProps> = ({

const isBTCLegacy = token?.contractAddress === BTC_CONTRACT;

console.log({ networks });

return (
<div className={classNames(styles.tokenFromGroup, styles.small)} style={{ flexWrap: 'wrap' }}>
<div className={styles.tokenSubAmouts}>
Expand Down Expand Up @@ -494,10 +507,10 @@ const TransferConvertToken: FC<TransferConvertProps> = ({
btcChains.find((chain) => chain.chainId !== token.chainId)
) {
const isValidateFeeTon = bridgeFeeTon ? convertAmount < bridgeFeeTon : false;
const isSolBridge = token.chainId === solChainId || toNetworkChainId === solChainId;
// const isSolBridge = token.chainId === solChainId || toNetworkChainId === solChainId;
const isBridgeBitcoin = token.chainId === ('bitcoin' as any) || toNetworkChainId === ('bitcoin' as any);
const isDisabled =
isSolBridge ||
// isSolBridge ||
isBridgeBitcoin ||
transferLoading ||
!addressTransfer ||
Expand Down
86 changes: 86 additions & 0 deletions src/pages/Balance/hooks/useGetFeeSol.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { BigDecimal, toDisplay, TokenItemType, toAmount, solChainId } from '@oraichain/oraidex-common';
import axios from 'axios';
import { flattenTokens } from 'config/bridgeTokens';
import useConfigReducer from 'hooks/useConfigReducer';
import { useDebounce } from 'hooks/useDebounce';
import { useEffect, useState } from 'react';
import { useQuery } from '@tanstack/react-query';

export enum Direction {
SOLANA_TO_ORAI = 'solana_to_orai',
ORAI_TO_SOLANA = 'orai_to_solana'
}

export enum SUPPORT_TOKEN {
ORAI = 'orai',
MAX = 'max'
}

const useGetFeeSol = ({
originalFromToken,
toChainId,
amountToken
}: {
amountToken: string;
originalFromToken: TokenItemType;
toChainId: string;
}) => {
const isSolToOraichain = originalFromToken.chainId === solChainId;
const isOraichainToSol = toChainId === solChainId;

const debouncedAmountToken = useDebounce(amountToken, 800);
const defaultSolFee = {
sendAmount: 0,
tokenFeeAmount: 0,
relayerFee: 0,
totalFee: 0
};

const [solFee, setSolFee] = useState(defaultSolFee);

useEffect(() => {
(async () => {
try {
if (!debouncedAmountToken || (!isSolToOraichain && !isOraichainToSol)) {
if (!!solFee.sendAmount) {
setSolFee(defaultSolFee);
}
return;
}

const amount: string = toAmount(debouncedAmountToken, originalFromToken.decimals).toString();
const direction: Direction = toChainId === solChainId ? Direction.ORAI_TO_SOLANA : Direction.SOLANA_TO_ORAI;
const supportedToken: SUPPORT_TOKEN =
originalFromToken.coinGeckoId === 'oraichain-token' ? SUPPORT_TOKEN.ORAI : SUPPORT_TOKEN.MAX;

const baseURL = `https://solana-relayer.orai.io`;
const url: string = `${baseURL}/fee?direction=${direction}&amount=${amount}&supportedToken=${supportedToken}`;
const { data } = await axios.get(url);
const originalToToken = flattenTokens.find(
(flat) => flat.coinGeckoId === originalFromToken.coinGeckoId && flat.chainId === toChainId
);
const totalFeeSol = new BigDecimal(data?.oraichainFee ?? data?.solanaFee)
.add(data.tokenFeeAmount)
.div(10 ** originalToToken.decimals)
.toNumber();

setSolFee({
sendAmount: toDisplay(data.sendAmount, originalToToken.decimals),
tokenFeeAmount: toDisplay(data.tokenFeeAmount, originalToToken.decimals),
relayerFee: toDisplay(data?.oraichainFee ?? data?.solanaFee, originalToToken.decimals),
totalFee: totalFeeSol
});
} catch (error) {
console.log({ error });
}
})();
}, [originalFromToken, toChainId, debouncedAmountToken]);

return {
solFee,
isSolToOraichain,
isOraichainToSol
};
};

export default useGetFeeSol;

0 comments on commit 85bbf35

Please sign in to comment.