Skip to content

Commit

Permalink
support mem pools (perf upgrade)
Browse files Browse the repository at this point in the history
  • Loading branch information
nohaapav committed Nov 21, 2024
1 parent fe72e80 commit 6ee889f
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 17 deletions.
2 changes: 1 addition & 1 deletion integration-tests/xcm-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"test:e2e": "jest e2e.spec.ts"
},
"devDependencies": {
"@acala-network/chopsticks-testing": "^1.0.1"
"@acala-network/chopsticks-testing": "1.0.1"
},
"dependencies": {
"@galacticcouncil/xcm-cfg": "^5.4.2",
Expand Down
22 changes: 22 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@galacticcouncil/math-stableswap": "^1.0.0",
"@galacticcouncil/math-xyk": "^1.0.0",
"@thi.ng/cache": "^2.1.35",
"@thi.ng/memoize": "^4.0.2",
"bignumber.js": "^9.1.0",
"lodash.clonedeep": "^4.5.0"
},
Expand Down
32 changes: 23 additions & 9 deletions packages/sdk/src/pool/PoolClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { ApiPromise } from '@polkadot/api';
import { UnsubscribePromise, VoidFn } from '@polkadot/api-base/types';

import { memoize1 } from '@thi.ng/memoize';

import { HYDRADX_OMNIPOOL_ADDRESS } from '../consts';
import { BalanceClient } from '../client';
import { Asset, PoolBase, PoolFees, PoolType } from '../types';
Expand All @@ -9,9 +11,13 @@ import { BigNumber } from '../utils/bignumber';
export abstract class PoolClient extends BalanceClient {
protected pools: PoolBase[] = [];
protected subs: VoidFn[] = [];
private loaded = false;
private assets: Map<string, Asset> = new Map([]);

private memPools = memoize1((x: number) => {
console.log(this.getPoolType(), 'mem pools', x, '✅');
return this.getPools();
});

constructor(api: ApiPromise) {
super(api);
}
Expand All @@ -32,13 +38,14 @@ export abstract class PoolClient extends BalanceClient {
this.assets = new Map(assets.map((asset: Asset) => [asset.id, asset]));
}

async getMemPools(): Promise<PoolBase[]> {
return this.memPools(1);
}

async getPools(): Promise<PoolBase[]> {
if (this.loaded) {
return this.augmentedPools;
}
console.log(this.getPoolType(), 'getPools', '✅');
this.pools = await this.loadPools();
this.subs = await this.subscribe();
this.loaded = true;
return this.augmentedPools;
}

Expand Down Expand Up @@ -95,7 +102,14 @@ export abstract class PoolClient extends BalanceClient {
}

private subscribeTokensPoolBalance(pool: PoolBase): UnsubscribePromise {
// Balance of shared token is stored in omnipool, not in stablepool, skip balance update otherwise getting 0
/**
* Skip balance update for shared token in stablepool as balance is
* stored in omnipool instead
*
* @param p - asset pool
* @param t - pool token
* @returns true if pool id different than token, otherwise false (shared token)
*/
const isNotStableswap = (p: PoolBase, t: string) => p.id !== t;
return this.subscribeTokenBalance(
pool.address,
Expand Down Expand Up @@ -125,7 +139,7 @@ export abstract class PoolClient extends BalanceClient {
* Check if pool valid. Only XYK pools are being verified as those are
* considered permissionless.
*
* @param pool - pool
* @param pool - asset pool
* @returns true if pool valid & assets known by registry, otherwise false
*/
private isValidPool(pool: PoolBase): boolean {
Expand All @@ -137,8 +151,8 @@ export abstract class PoolClient extends BalanceClient {
/**
* Augment pool tokens with asset metadata
*
* @param pool - pool
* @returns pool with augmented tokens
* @param pool - asset pool
* @returns asset pool with augmented tokens
*/
private withMetadata(pool: PoolBase) {
pool.tokens = pool.tokens.map((t) => {
Expand Down
13 changes: 10 additions & 3 deletions packages/sdk/src/pool/PoolService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { ApiPromise } from '@polkadot/api';
import { SubmittableExtrinsic } from '@polkadot/api/promise/types';

import { memoize1 } from '@thi.ng/memoize';

import { LbpPoolClient } from './lbp/LbpPoolClient';
import { OmniPoolClient } from './omni/OmniPoolClient';
import { XykPoolClient } from './xyk/XykPoolClient';
Expand Down Expand Up @@ -38,6 +40,11 @@ export class PoolService implements IPoolService {

protected onChainAssets: Asset[] = [];

private memRegistry = memoize1((x: number) => {
console.log('PoolService mem registry', x, '✅');
return this.syncRegistry();
});

constructor(api: ApiPromise) {
this.api = api;
this.assetClient = new AssetClient(this.api);
Expand Down Expand Up @@ -69,22 +76,22 @@ export class PoolService implements IPoolService {

async getPools(includeOnly: PoolType[]): Promise<PoolBase[]> {
if (!this.isRegistrySynced) {
await this.syncRegistry();
await this.memRegistry(1);
}

if (includeOnly.length == 0) {
const pools = await Promise.all(
this.clients
.filter((client) => client.isSupported())
.map((client) => client.getPools())
.map((client) => client.getMemPools())
);
return pools.flat();
}

const pools = await Promise.all(
this.clients
.filter((client) => includeOnly.some((t) => t === client.getPoolType()))
.map((client) => client.getPools())
.map((client) => client.getMemPools())
);
return pools.flat();
}
Expand Down
7 changes: 3 additions & 4 deletions packages/sdk/test/script/examples/router/getPools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ import { ApiUrl, PolkadotExecutor } from '../../executor';
import { PoolService } from '../../../../src/pool';
import { TradeRouter } from '../../../../src/api';

class GetAllAssetsExample extends PolkadotExecutor {
class GetPoolsExample extends PolkadotExecutor {
async script(api: ApiPromise): Promise<any> {
const poolService = new PoolService(api);
const router = new TradeRouter(poolService);
await router.getPools();
return [];
return router.getPools();
}
}

new GetAllAssetsExample(ApiUrl.HydraDx, 'Get pools', true).run();
new GetPoolsExample(ApiUrl.HydraDx, 'Get pools', true).run();
42 changes: 42 additions & 0 deletions packages/sdk/test/script/examples/router/onBlockPoolChange.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { ApiPromise } from '@polkadot/api';
import { ApiUrl, PolkadotExecutor } from '../../executor';
import { PoolService } from '../../../../src/pool';
import { TradeRouter } from '../../../../src/api';

class GetOnBlockPoolChangeExample extends PolkadotExecutor {
async script(api: ApiPromise): Promise<any> {
const poolService = new PoolService(api);
const router = new TradeRouter(poolService);

const result = await Promise.all([
router.getBestSpotPrice('5', '0'),
router.getBestSpotPrice('5', '0'),
router.getBestSpotPrice('5', '0'),
router.getBestSpotPrice('5', '0'),
router.getBestSpotPrice('5', '0'),
]);
console.log('Call spot 5x simultaneously', result);

api.rpc.chain.subscribeNewHeads(async (_lastHeader) => {
console.log('==================');
router.getPools().then((p) => {
p.forEach((o) => {
o.tokens.forEach((t) => {
if (t.id === '1') {
// Checking LRNA balance change
console.log(t.id, t.balance.toString());
}
});
});
});
});

return [];
}
}

new GetOnBlockPoolChangeExample(
ApiUrl.HydraDx,
'Get on block change',
true
).run();

0 comments on commit 6ee889f

Please sign in to comment.