Skip to content

Commit

Permalink
fix: fix incorrect balances
Browse files Browse the repository at this point in the history
  • Loading branch information
yeager-eren committed Nov 20, 2024
1 parent 2085bb4 commit d0d149f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
1 change: 0 additions & 1 deletion widget/embedded/src/components/TokenList/TokenList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ export function TokenList(props: PropTypes) {
)
);
};

const renderList = () => {
return (
<VirtualizedList
Expand Down
13 changes: 8 additions & 5 deletions widget/embedded/src/store/slices/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ import {

type WalletAddress = string;
type TokenAddress = string;
type TokenSymbol = string;
type BlockchainId = string;
/** format: `BlockchainId-TokenAddress` */
export type AssetKey = `${BlockchainId}-${TokenAddress}`;
/** format: `BlockchainId-TokenAddress-WalletAddress` */
export type BalanceKey = `${BlockchainId}-${TokenAddress}-${WalletAddress}`;
/** format: `BlockchainId-TokenAddress-TokenSymbol` */
export type AssetKey = `${BlockchainId}-${TokenAddress}-${TokenSymbol}`;
/** format: `BlockchainId-TokenAddress-TokenSymbol-WalletAddress` */
export type BalanceKey =
`${BlockchainId}-${TokenAddress}-${TokenSymbol}-${WalletAddress}`;

export type BalanceState = {
[key: BalanceKey]: Balance;
};
Expand Down Expand Up @@ -268,6 +271,7 @@ export const createWalletsSlice: StateCreator<
createBalanceKey(wallet.address, {
address: asset.address,
blockchain: wallet.chain,
symbol: asset.symbol,
}) === key
);

Expand All @@ -283,7 +287,6 @@ export const createWalletsSlice: StateCreator<
);
}
});

set({
_balances: nextBalancesState,
_aggregatedBalances: nextAggregatedBalanceState,
Expand Down
21 changes: 9 additions & 12 deletions widget/embedded/src/store/utils/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,30 @@ import BigNumber from 'bignumber.js';
import { ZERO } from '../../constants/numbers';

/**
* output format: BlockchainId-TokenAddress
* Note: We need to use `symbol` as well since native coins and cosmos blockchains don't have `address`
* output format: BlockchainId-TokenAddress-TokenSymbol
*/
export function createAssetKey(
asset: Pick<Asset, 'address' | 'blockchain'>
): AssetKey {
return `${asset.blockchain}-${asset.address}`;
export function createAssetKey(asset: Asset): AssetKey {
return `${asset.blockchain}-${asset.address}-${asset.symbol}`;
}

/**
* output format: BlockchainId-TokenAddress-WalletAddress
* output format: BlockchainId-TokenAddress-TokenSymbol-WalletAddress
*/
export function createBalanceKey(
accountAddress: string,
asset: Pick<Asset, 'address' | 'blockchain'>
asset: Asset
): BalanceKey {
const assetKey = createAssetKey(asset);
return `${assetKey}-${accountAddress}`;
}

export function extractAssetFromBalanceKey(
key: BalanceKey
): Pick<Asset, 'address' | 'blockchain'> {
const [assetChain, assetAddress] = key.split('-');

export function extractAssetFromBalanceKey(key: BalanceKey): Asset {
const [assetChain, assetAddress, assetSymbol] = key.split('-');
return {
address: assetAddress,
blockchain: assetChain,
symbol: assetSymbol,
};
}

Expand Down

0 comments on commit d0d149f

Please sign in to comment.