Skip to content

Commit

Permalink
Fix subscribe balance duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
nohaapav committed Mar 12, 2024
1 parent 543f0b4 commit c764a46
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions packages/xcm-sdk/src/Wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ export class Wallet {
observer: (balances: AssetAmount[]) => void
): Promise<Subscription> {
const chainConfig = this.configService.getChainConfig(chain);

const evmClient = this.getEvmClient(chain);
const evmReconciler = this.getEvmReconciler(chain);
const substrate = await this.getSubstrateService(chain);
Expand All @@ -158,22 +157,25 @@ export class Wallet {
substrate,
});

const observables = chainConfig
.getAssetsConfigs()
.map(async (assetConfig) => {
const { asset, balance } = assetConfig;
const { chain } = chainConfig;
const assetId = chain.getBalanceAssetId(asset);
const isErc20 = isH160Address(assetId.toString());
const evmAddr = isErc20
? await evmReconciler.toEvmAddress(address, substrate.api)
: address;
const balanceConfig = balance.build({
address: evmAddr,
asset: assetId,
});
return balanceAdapter.subscribe(asset, balanceConfig);
const configs = chainConfig.getAssetsConfigs();
const uniqueConfigs = [
...new Map(configs.map((cfg) => [cfg.asset, cfg])).values(),
];

const observables = uniqueConfigs.map(async (assetConfig) => {
const { asset, balance } = assetConfig;
const { chain } = chainConfig;
const assetId = chain.getBalanceAssetId(asset);
const isErc20 = isH160Address(assetId.toString());
const evmAddr = isErc20
? await evmReconciler.toEvmAddress(address, substrate.api)
: address;
const balanceConfig = balance.build({
address: evmAddr,
asset: assetId,
});
return balanceAdapter.subscribe(asset, balanceConfig);
});
const ob = await Promise.all(observables);
const observable = combineLatest(ob);
return observable.pipe(debounceTime(500)).subscribe(observer);
Expand Down

0 comments on commit c764a46

Please sign in to comment.