Skip to content

Commit

Permalink
evict mem pool on registry sync
Browse files Browse the repository at this point in the history
  • Loading branch information
nohaapav committed Nov 22, 2024
1 parent 7486f4f commit fb7c5c4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
39 changes: 30 additions & 9 deletions packages/sdk/src/pool/PoolClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import { BigNumber } from '../utils/bignumber';
export abstract class PoolClient extends BalanceClient {
protected pools: PoolBase[] = [];
protected subs: VoidFn[] = [];

private assets: Map<string, Asset> = 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();
});

Expand All @@ -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<PoolBase[]> {
return this.memPools(1);
return this.memPools(this.mem);
}

async getPools(): Promise<PoolBase[]> {
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);
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/pool/PoolService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand Down
6 changes: 2 additions & 4 deletions packages/sdk/src/pool/xyk/XykPoolClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit fb7c5c4

Please sign in to comment.