From fb7c5c48d8c0031d690053e1182ea60e42dec4de Mon Sep 17 00:00:00 2001 From: Pavol Noha Date: Fri, 22 Nov 2024 14:37:17 +0100 Subject: [PATCH] evict mem pool on registry sync --- packages/sdk/src/pool/PoolClient.ts | 39 +++++++++++++++++----- packages/sdk/src/pool/PoolService.ts | 2 +- packages/sdk/src/pool/xyk/XykPoolClient.ts | 6 ++-- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/packages/sdk/src/pool/PoolClient.ts b/packages/sdk/src/pool/PoolClient.ts index f1384943..a77f06af 100644 --- a/packages/sdk/src/pool/PoolClient.ts +++ b/packages/sdk/src/pool/PoolClient.ts @@ -11,10 +11,12 @@ import { BigNumber } from '../utils/bignumber'; export abstract class PoolClient extends BalanceClient { protected pools: PoolBase[] = []; protected subs: VoidFn[] = []; + private assets: Map = new Map([]); + private mem: number = 0; - private memPools = memoize1((x: number) => { - console.log(this.getPoolType(), 'mem pools', x, '✅'); + private memPools = memoize1((mem: number) => { + console.log(this.getPoolType(), 'mem pools', mem, '✅'); return this.getPools(); }); @@ -34,28 +36,43 @@ export abstract class PoolClient extends BalanceClient { .map((p) => this.withMetadata(p)); } + /** + * Update registry assets, evict mempool + * + * @param assets - registry assets + */ async withAssets(assets: Asset[]) { this.assets = new Map(assets.map((asset: Asset) => [asset.id, asset])); + this.mem = this.mem + 1; } async getMemPools(): Promise { - return this.memPools(1); + return this.memPools(this.mem); } async getPools(): Promise { - console.log(this.getPoolType(), 'getPools', '✅'); + this.unsubscribe(); this.pools = await this.loadPools(); this.subs = await this.subscribe(); + const type = this.getPoolType(); + console.log(type, `pools(${this.augmentedPools.length})`, '✅'); + console.log(type, `subs(${this.subs.length})`, '✅'); return this.augmentedPools; } private async subscribe() { const subs = this.augmentedPools.map(async (pool: PoolBase) => { - const poolSubs = [ - await this.subscribePoolChange(pool), - await this.subscribeSystemPoolBalance(pool), - await this.subscribeTokensPoolBalance(pool), - ]; + const poolSubs = [await this.subscribeTokensPoolBalance(pool)]; + + try { + const subChange = await this.subscribePoolChange(pool); + poolSubs.push(subChange); + } catch (e) {} + + if (this.hasSystemAsset(pool)) { + const subSystem = await this.subscribeSystemPoolBalance(pool); + poolSubs.push(subSystem); + } if (this.hasErc20Asset(pool)) { const subErc20 = await this.subscribeErc20PoolBalance(pool); @@ -75,6 +92,10 @@ export abstract class PoolClient extends BalanceClient { return subsriptions.flat(); } + private hasSystemAsset(pool: PoolBase) { + return pool.tokens.some((t) => t.id === '0'); + } + private hasShareAsset(pool: PoolBase) { return pool.type === PoolType.Stable && pool.id; } diff --git a/packages/sdk/src/pool/PoolService.ts b/packages/sdk/src/pool/PoolService.ts index 8d5ffd46..64cf90c1 100644 --- a/packages/sdk/src/pool/PoolService.ts +++ b/packages/sdk/src/pool/PoolService.ts @@ -41,7 +41,7 @@ export class PoolService implements IPoolService { protected onChainAssets: Asset[] = []; private memRegistry = memoize1((x: number) => { - console.log('PoolService mem registry', x, '✅'); + console.log('Registry mem sync', x, '✅'); return this.syncRegistry(); }); diff --git a/packages/sdk/src/pool/xyk/XykPoolClient.ts b/packages/sdk/src/pool/xyk/XykPoolClient.ts index b933af90..3405632d 100644 --- a/packages/sdk/src/pool/xyk/XykPoolClient.ts +++ b/packages/sdk/src/pool/xyk/XykPoolClient.ts @@ -65,10 +65,8 @@ export class XykPoolClient extends PoolClient { return PoolType.XYK; } - protected subscribePoolChange(pool: PoolBase): UnsubscribePromise { - return this.api.query.xyk.poolAssets(pool.address, (_states) => { - //do nothing - }); + protected subscribePoolChange(_pool: PoolBase): UnsubscribePromise { + throw new Error('Pool change subscription not supported!'); } private getExchangeFee(): PoolFee {