-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1087 from oraidex/feat/add-fee-sol-bridge
Feat/add fee sol bridge
- Loading branch information
Showing
2 changed files
with
111 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |