From 4f3d0beed1ed486684bc9543430315c2ed2f2dbc Mon Sep 17 00:00:00 2001 From: wenty22 Date: Wed, 4 Dec 2024 10:20:04 +0800 Subject: [PATCH] feat: Solve the cost calculation issue of solana --- .release/.changeset/poor-news-hear.md | 5 ++++ .release/.changeset/pre.json | 11 +++++++++ packages/canonical-bridge-widget/CHANGELOG.md | 6 +++++ packages/canonical-bridge-widget/package.json | 2 +- .../src/core/utils/address.ts | 2 +- .../deBridge/hooks/useGetDeBridgeFees.ts | 23 +++++++++++++++++++ 6 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 .release/.changeset/poor-news-hear.md create mode 100644 .release/.changeset/pre.json diff --git a/.release/.changeset/poor-news-hear.md b/.release/.changeset/poor-news-hear.md new file mode 100644 index 00000000..7feb5f67 --- /dev/null +++ b/.release/.changeset/poor-news-hear.md @@ -0,0 +1,5 @@ +--- +"@bnb-chain/canonical-bridge-widget": patch +--- + +Add address info to token list element diff --git a/.release/.changeset/pre.json b/.release/.changeset/pre.json new file mode 100644 index 00000000..3e141b23 --- /dev/null +++ b/.release/.changeset/pre.json @@ -0,0 +1,11 @@ +{ + "mode": "pre", + "tag": "alpha", + "initialVersions": { + "@bnb-chain/canonical-bridge-sdk": "0.4.2", + "@bnb-chain/canonical-bridge-widget": "0.5.5" + }, + "changesets": [ + "poor-news-hear" + ] +} diff --git a/packages/canonical-bridge-widget/CHANGELOG.md b/packages/canonical-bridge-widget/CHANGELOG.md index 0915dd58..4a5b647d 100644 --- a/packages/canonical-bridge-widget/CHANGELOG.md +++ b/packages/canonical-bridge-widget/CHANGELOG.md @@ -1,5 +1,11 @@ # @bnb-chain/canonical-bridge-widget +## 0.5.6-alpha.0 + +### Patch Changes + +- Add address info to token list element + ## 0.5.5 ### Patch Changes diff --git a/packages/canonical-bridge-widget/package.json b/packages/canonical-bridge-widget/package.json index 83a29389..8a5b3b1e 100644 --- a/packages/canonical-bridge-widget/package.json +++ b/packages/canonical-bridge-widget/package.json @@ -1,6 +1,6 @@ { "name": "@bnb-chain/canonical-bridge-widget", - "version": "0.5.5", + "version": "0.5.6-alpha.0", "description": "canonical bridge widget", "author": "bnb-chain", "private": false, diff --git a/packages/canonical-bridge-widget/src/core/utils/address.ts b/packages/canonical-bridge-widget/src/core/utils/address.ts index 89bf1b60..6147ccef 100644 --- a/packages/canonical-bridge-widget/src/core/utils/address.ts +++ b/packages/canonical-bridge-widget/src/core/utils/address.ts @@ -50,7 +50,7 @@ export function isEvmAddress(address?: string) { return !!address && /^0x[a-f0-9]{40}$/i.test(address); } -export function isNativeToken(tokenAddress: string, chainType: ChainType = 'evm') { +export function isNativeToken(tokenAddress?: string, chainType: ChainType = 'evm') { if (chainType === 'solana') { return tokenAddress === '11111111111111111111111111111111'; } diff --git a/packages/canonical-bridge-widget/src/modules/aggregator/adapters/deBridge/hooks/useGetDeBridgeFees.ts b/packages/canonical-bridge-widget/src/modules/aggregator/adapters/deBridge/hooks/useGetDeBridgeFees.ts index 4b4783f2..705ba422 100644 --- a/packages/canonical-bridge-widget/src/modules/aggregator/adapters/deBridge/hooks/useGetDeBridgeFees.ts +++ b/packages/canonical-bridge-widget/src/modules/aggregator/adapters/deBridge/hooks/useGetDeBridgeFees.ts @@ -15,6 +15,7 @@ import { useGetTokenBalance } from '@/core/contract/hooks/useGetTokenBalance'; import { ERC20_TOKEN } from '@/core/contract/abi'; import { useNativeCurrency } from '@/modules/aggregator/hooks/useNativeCurrency'; import { useSolanaBalance } from '@/modules/wallet/hooks/useSolanaBalance'; +import { isNativeToken } from '@/core/utils/address'; export interface IFeeDetails { value: string; @@ -213,6 +214,7 @@ export const useGetDeBridgeFees = () => { dispatch(setRouteError({ deBridge: 'Failed to get gas fee' })); isFailedToGetGas = true; } + const result = feeList.reduce((acc: { [key: string]: number }, item) => { const symbol = item.symbol; const value = Number(item.value); @@ -220,6 +222,26 @@ export const useGetDeBridgeFees = () => { acc[symbol] += value; return acc; }, {} as { [key: string]: number }); + + if ( + fromChain?.chainType === 'solana' && + isNativeToken(selectedToken?.deBridge?.raw?.address, fromChain?.chainType) && + nativeToken && + nativeTokenBalance + ) { + const totalNativeTokenCost = Number(result[nativeToken]) + Number(sendValue); + const userNativeTokenBalance = Number(nativeTokenBalance.formatted); + + if (userNativeTokenBalance < totalNativeTokenCost) { + dispatch( + setRouteError({ + deBridge: `Insufficient ${nativeToken} to cover protocol fee and market maker gas costs`, + }), + ); + isDisplayError = true; + } + } + const resultString = Object.keys(result) .map((symbol) => { return `${formatFeeAmount(result[symbol])} ${symbol}`; @@ -234,6 +256,7 @@ export const useGetDeBridgeFees = () => { }, }), ); + return { summary: resultString ? resultString : '--', breakdown: feeBreakdown,