Skip to content

Commit

Permalink
Fix subs balance, cleanup logs
Browse files Browse the repository at this point in the history
  • Loading branch information
nohaapav committed Nov 18, 2023
1 parent e4daf32 commit 617ce88
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 22 deletions.
4 changes: 2 additions & 2 deletions packages/sdk/src/client/BalanceClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
33 changes: 15 additions & 18 deletions packages/sdk/src/pool/PoolClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,37 @@ 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;
}

async subscribe() {
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;
});

const subsriptions = await Promise.all(subs);
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();
Expand All @@ -61,45 +65,38 @@ 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)) {
pool.tokens[tokenIndex].balance = balance.toString();
}
};
}

private updateBalanceLog(pool: PoolBase, type: string) {
const poolAddr = pool.address.substring(0, 10).concat('...');
console.log(`${pool.type} [${poolAddr}] ${type} balance subscribed`);
}
}
2 changes: 0 additions & 2 deletions packages/sdk/src/pool/PoolService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,8 @@ export class PoolService implements IPoolService {

async getPools(includeOnly: PoolType[]): Promise<PoolBase[]> {
if (!this.metadataLoaded) {
console.time('Load metadata');
this.metadata = await this.assetClient.getOnChainMetadata();
this.metadataLoaded = true;
console.timeEnd('Load metadata');
}

if (includeOnly.length == 0) {
Expand Down

0 comments on commit 617ce88

Please sign in to comment.