Skip to content

Commit

Permalink
feat: Solve the cost calculation issue of solana
Browse files Browse the repository at this point in the history
  • Loading branch information
wenty22 committed Dec 4, 2024
1 parent c67315b commit 4f3d0be
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .release/.changeset/poor-news-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bnb-chain/canonical-bridge-widget": patch
---

Add address info to token list element
11 changes: 11 additions & 0 deletions .release/.changeset/pre.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
6 changes: 6 additions & 0 deletions packages/canonical-bridge-widget/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/canonical-bridge-widget/package.json
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/canonical-bridge-widget/src/core/utils/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -213,13 +214,34 @@ 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);
if (symbol && !acc[symbol]) acc[symbol] = 0;
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}`;
Expand All @@ -234,6 +256,7 @@ export const useGetDeBridgeFees = () => {
},
}),
);

return {
summary: resultString ? resultString : '--',
breakdown: feeBreakdown,
Expand Down

0 comments on commit 4f3d0be

Please sign in to comment.