Skip to content

Commit

Permalink
Merge pull request #169 from bnb-chain/fix/balance1205
Browse files Browse the repository at this point in the history
feat: Fix balance issue
  • Loading branch information
wenty22 authored Dec 5, 2024
2 parents 545a906 + 034cc4f commit c92f2f2
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 22 deletions.
12 changes: 12 additions & 0 deletions .release/.changeset/pre.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"mode": "pre",
"tag": "alpha",
"initialVersions": {
"@bnb-chain/canonical-bridge-sdk": "0.4.2",
"@bnb-chain/canonical-bridge-widget": "0.5.6"
},
"changesets": [
"rare-poets-rhyme",
"silent-mirrors-drop"
]
}
5 changes: 5 additions & 0 deletions .release/.changeset/rare-poets-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bnb-chain/canonical-bridge-widget": patch
---

Fix token balance issue
5 changes: 5 additions & 0 deletions .release/.changeset/silent-mirrors-drop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bnb-chain/canonical-bridge-widget": patch
---

Add token address to token element
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export function useTransferConfig() {
137: ['cUSDCv3'],
42161: ['cUSDCv3'],
43114: ['BNB'],
7565164: ['So11111111111111111111111111111111111111112'],
},
},
bridgedTokenGroups: [
Expand Down
12 changes: 12 additions & 0 deletions packages/canonical-bridge-widget/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# @bnb-chain/canonical-bridge-widget

## 0.5.7-alpha.1

### Patch Changes

- Add token address to token element

## 0.5.7-alpha.0

### Patch Changes

- Fix token balance issue

## 0.5.6

### 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.6",
"version": "0.5.7-alpha.1",
"description": "canonical bridge widget",
"author": "bnb-chain",
"private": false,
Expand Down
4 changes: 4 additions & 0 deletions packages/canonical-bridge-widget/src/core/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,7 @@ export const MIN_FEE = 0.0001;
export const REFETCH_INTERVAL = 8 * 1000;

export const MIN_SOL_TO_ENABLED_TX = 0.05;

export const EVM_NATIVE_TOKEN_ADDRESS = '0x0000000000000000000000000000000000000000';

export const SOLANA_NATIVE_TOKEN_ADDRESS = '11111111111111111111111111111111';
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export function ChooseTokenModal(props: ChooseTokenModalProps) {
iconUrl={item.icon}
isActive={isActive}
isDisabled={isDisabled}
data-address={item.address?.toLowerCase()}
incompatibleTooltip={formatMessage({
id: 'select-modal.token.incompatible.tooltip',
})}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ function useSortedTokens() {
});

const tmpTokens = tokens.map((item) => {
const balance = balances[item.displaySymbol];
const balance = balances[item.address?.toLowerCase()];
const price = getTokenPrice(item);

let value: number | undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ export function useTokenBalance() {
const tokenBalances = useAppSelector((state) => state.aggregator.tokenBalances);

const getTokenBalance = useCallback(
(params: IBridgeToken | { symbol?: string } = {}) => {
const tokenSymbol = (params as IBridgeToken).displaySymbol ?? params.symbol;
(params: IBridgeToken | { address?: string } = {}) => {
const tokenAddress = (params as IBridgeToken).address ?? params.address;

if (tokenSymbol) {
return tokenBalances[tokenSymbol.toUpperCase()];
if (tokenAddress) {
return tokenBalances[tokenAddress.toLowerCase()];
}
},
[tokenBalances],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ChainType, IBridgeToken } from '@/modules/aggregator/types';
import { ERC20_TOKEN } from '@/core/contract/abi';
import { isChainOrTokenCompatible } from '@/modules/aggregator/shared/isChainOrTokenCompatible';
import { isSameAddress } from '@/core/utils/address';
import { EVM_NATIVE_TOKEN_ADDRESS, SOLANA_NATIVE_TOKEN_ADDRESS } from '@/core/constants';

export async function getTokenBalances({
chainType,
Expand Down Expand Up @@ -99,15 +100,17 @@ async function getEvmTokenBalances({
values.map((value, index) => {
const token = tokens[index];

const symbol = token.displaySymbol?.toUpperCase();
balances[symbol] =
const address = token.address?.toLowerCase();
balances[address] =
typeof value === 'undefined' ? undefined : formatUnits(BigInt(value), token.decimals);
});
}

if (nativeTokenRes.status === 'fulfilled') {
const symbol = chain.nativeCurrency.symbol?.toUpperCase();
balances[symbol] = formatUnits(BigInt(nativeTokenRes.value), chain.nativeCurrency.decimals);
balances[EVM_NATIVE_TOKEN_ADDRESS] = formatUnits(
BigInt(nativeTokenRes.value),
chain.nativeCurrency.decimals,
);
}

return balances;
Expand Down Expand Up @@ -162,15 +165,15 @@ async function getTronTokenBalances({
);

if (token) {
balances[token.displaySymbol.toUpperCase()] = formatUnits(
balances[token.address?.toLowerCase()] = formatUnits(
BigInt(tokenInfo.balance),
token.decimals,
);
}
});

tokens.forEach((t) => {
const key = t.displaySymbol.toUpperCase();
const key = t.address.toLowerCase();
balances[key] = balances[key] ?? '0';
});

Expand Down Expand Up @@ -211,20 +214,17 @@ async function getSolanaTokenBalances({

const token = tokens.find((t) => isSameAddress(t.address, accountInfo.mint.toBase58()));
if (token) {
balances[token.displaySymbol.toUpperCase()] = formatUnits(
accountInfo.amount,
token.decimals,
);
balances[token.address?.toLowerCase()] = formatUnits(accountInfo.amount, token.decimals);
}
});
}

if (nativeTokenRes.status === 'fulfilled') {
balances['SOL'] = String(nativeTokenRes.value / LAMPORTS_PER_SOL);
balances[SOLANA_NATIVE_TOKEN_ADDRESS] = String(nativeTokenRes.value / LAMPORTS_PER_SOL);
}

tokens.forEach((t) => {
const key = t.displaySymbol.toUpperCase();
const key = t.address?.toLowerCase();
balances[key] = balances[key] ?? '0';
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const InputValidationMessage = () => {

useEffect(() => {
const balance = getTokenBalance({
symbol: selectedToken?.displaySymbol,
address: selectedToken?.address?.toLowerCase(),
});

const balanceResult = validateInput({
Expand All @@ -52,14 +52,14 @@ export const InputValidationMessage = () => {
sendValue,
dispatch,
estimatedAmount,
minMaxSendAmt?.min,
minMaxSendAmt?.max,
minMaxSendAmt.min,
minMaxSendAmt.max,
selectedToken?.decimals,
selectedToken?.isPegged,
transferActionInfo,
validateInput,
getTokenBalance,
selectedToken?.displaySymbol,
selectedToken?.address,
]);

const errorMsg = balanceInputError ?? error?.text;
Expand Down

0 comments on commit c92f2f2

Please sign in to comment.