From 617ce8827adb4e8b7788049785ad0f1974b21b55 Mon Sep 17 00:00:00 2001 From: Pavol Noha Date: Sat, 18 Nov 2023 12:07:47 +0100 Subject: [PATCH] Fix subs balance, cleanup logs --- packages/sdk/src/client/BalanceClient.ts | 4 +-- packages/sdk/src/pool/PoolClient.ts | 33 +++++++++++------------- packages/sdk/src/pool/PoolService.ts | 2 -- 3 files changed, 17 insertions(+), 22 deletions(-) diff --git a/packages/sdk/src/client/BalanceClient.ts b/packages/sdk/src/client/BalanceClient.ts index 609cb307..4e8d1221 100644 --- a/packages/sdk/src/client/BalanceClient.ts +++ b/packages/sdk/src/client/BalanceClient.ts @@ -72,10 +72,10 @@ export class BalanceClient extends PolkadotApiClient { .filter((t) => t !== SYSTEM_ASSET_ID) .map((t) => [address, t]); return this.api.query.tokens.accounts.multi(tokenAccArgs, (balances) => { - balances.forEach(({ free, reserved, frozen }, i) => { + balances.forEach(({ free, frozen }, i) => { const freeBalance = this.calculateFreeBalance( free.toString(), - reserved.toString(), + ZERO.toFixed(), frozen.toString() ); const token = tokenAccArgs[i][1]; diff --git a/packages/sdk/src/pool/PoolClient.ts b/packages/sdk/src/pool/PoolClient.ts index 30bed0f7..4edbe5bc 100644 --- a/packages/sdk/src/pool/PoolClient.ts +++ b/packages/sdk/src/pool/PoolClient.ts @@ -24,11 +24,9 @@ export abstract class PoolClient extends BalanceClient { if (this.poolsLoaded) { return this.pools; } - console.time(`Load ${this.getPoolType()}`); this.pools = await this.loadPools(); this.subs = await this.subscribe(); this.poolsLoaded = true; - console.timeEnd(`Load ${this.getPoolType()}`); return this.pools; } @@ -36,14 +34,15 @@ export abstract class PoolClient extends BalanceClient { const subs = this.pools.map(async (pool: PoolBase) => { const poolSubs = [ await this.subscribePoolChange(pool), - await this.tokenSubs(pool), - await this.systemSubs(pool), + await this.subscribeTokensPoolBalance(pool), + await this.subscribeSystemPoolBalance(pool), ]; if (this.hasShareAsset(pool)) { - const sub = await this.shareSubs(pool); + const sub = await this.subscribeSharePoolBalance(pool); poolSubs.push(sub); } + this.subscribeLog(pool); return poolSubs; }); @@ -51,6 +50,11 @@ export abstract class PoolClient extends BalanceClient { return subsriptions.flat(); } + private subscribeLog(pool: PoolBase) { + const poolAddr = pool.address.substring(0, 10).concat('...'); + console.log(`${pool.type} [${poolAddr}] balance subscribed`); + } + unsubscribe() { this.subs.forEach((unsub) => { unsub(); @@ -61,35 +65,33 @@ export abstract class PoolClient extends BalanceClient { return pool.type === PoolType.Stable && pool.id; } - private tokenSubs(pool: PoolBase): UnsubscribePromise { + private subscribeTokensPoolBalance(pool: PoolBase): UnsubscribePromise { return this.subscribeTokenBalance( pool.address, pool.tokens.map((t) => t.id), - this.updateBalanceCallback(pool, 'tokens', (p, t) => p.id !== t) + this.updateBalanceCallback(pool, (p, t) => p.id !== t) ); } - private shareSubs(pool: PoolBase): UnsubscribePromise { + private subscribeSharePoolBalance(pool: PoolBase): UnsubscribePromise { return this.subscribeTokenBalance( HYDRADX_OMNIPOOL_ADDRESS, [pool.id!], - this.updateBalanceCallback(pool, 'share', () => true) + this.updateBalanceCallback(pool, () => true) ); } - private systemSubs(pool: PoolBase): UnsubscribePromise { + private subscribeSystemPoolBalance(pool: PoolBase): UnsubscribePromise { return this.subscribeSystemBalance( pool.address, - this.updateBalanceCallback(pool, 'system', () => true) + this.updateBalanceCallback(pool, () => true) ); } private updateBalanceCallback( pool: PoolBase, - type: string, canUpdate: (pool: PoolBase, token: string) => boolean ) { - this.updateBalanceLog(pool, type); return function (token: string, balance: BigNumber) { const tokenIndex = pool.tokens.findIndex((t) => t.id == token); if (tokenIndex >= 0 && canUpdate(pool, token)) { @@ -97,9 +99,4 @@ export abstract class PoolClient extends BalanceClient { } }; } - - private updateBalanceLog(pool: PoolBase, type: string) { - const poolAddr = pool.address.substring(0, 10).concat('...'); - console.log(`${pool.type} [${poolAddr}] ${type} balance subscribed`); - } } diff --git a/packages/sdk/src/pool/PoolService.ts b/packages/sdk/src/pool/PoolService.ts index 66b11574..88c5a736 100644 --- a/packages/sdk/src/pool/PoolService.ts +++ b/packages/sdk/src/pool/PoolService.ts @@ -44,10 +44,8 @@ export class PoolService implements IPoolService { async getPools(includeOnly: PoolType[]): Promise { if (!this.metadataLoaded) { - console.time('Load metadata'); this.metadata = await this.assetClient.getOnChainMetadata(); this.metadataLoaded = true; - console.timeEnd('Load metadata'); } if (includeOnly.length == 0) {