Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
nohaapav committed Nov 20, 2024
1 parent 07e437b commit dec1c87
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
13 changes: 9 additions & 4 deletions packages/sdk/src/client/BalanceClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,39 +59,44 @@ export class BalanceClient extends PolkadotApiClient {
async subscribeTokenBalance(
address: string,
assets: Asset[],
onChange: (token: string, balance: BigNumber) => void
onChange: (balances: [string, BigNumber][]) => void
): UnsubscribePromise {
const supported = assets
.filter((a) => a.type !== 'Erc20')
.filter((a) => a.id !== SYSTEM_ASSET_ID);

const callArgs = supported.map((a) => [address, a.id]);
return this.api.query.tokens.accounts.multi(callArgs, (balances) => {
const result: [string, BigNumber][] = [];
balances.forEach((data, i) => {
const freeBalance = this.calculateFreeBalance(data);
const token = callArgs[i][1];
onChange(token, freeBalance);
console.log(address, token, freeBalance.toString());
result.push([token, freeBalance]);
});
onChange(result);
});
}

async subscribeErc20Balance(
address: string,
assets: Asset[],
onChange: (token: string, balance: BigNumber) => void
onChange: (balances: [string, BigNumber][]) => void
): UnsubscribePromise {
const supported = assets.filter((a) => a.type === 'Erc20');

const getErc20Balance = async () => {
const result: [string, BigNumber][] = [];
const balances: [string, BigNumber][] = await Promise.all(
supported.map(async (token: Asset) => [
token.id,
await this.getErc20Balance(address, token.id),
])
);
balances.forEach(([token, balance]) => {
onChange(token, balance);
result.push([token, balance]);
});
onChange(result);
};

await getErc20Balance();
Expand Down
27 changes: 21 additions & 6 deletions packages/sdk/src/pool/PoolClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,24 +90,25 @@ export abstract class PoolClient extends BalanceClient {
private subscribeSystemPoolBalance(pool: PoolBase): UnsubscribePromise {
return this.subscribeSystemBalance(
pool.address,
this.updateBalanceCallback(pool, () => true)
this.updateBalanceCallback(pool)
);
}

private subscribeTokensPoolBalance(pool: PoolBase): UnsubscribePromise {
// Balance of shared token is stored in omnipool, not in stablepool, skip balance update otherwise getting 0
const isNotStableswap = (p: PoolBase, t: string) => p.id !== t;
return this.subscribeTokenBalance(
pool.address,
pool.tokens,
this.updateBalanceCallback(pool, isNotStableswap)
this.updateBalancesCallback(pool, isNotStableswap)
);
}

private subscribeErc20PoolBalance(pool: PoolBase): UnsubscribePromise {
return this.subscribeErc20Balance(
pool.address,
pool.tokens,
this.updateBalanceCallback(pool, () => true)
this.updateBalancesCallback(pool, () => true)
);
}

Expand All @@ -116,7 +117,7 @@ export abstract class PoolClient extends BalanceClient {
return this.subscribeTokenBalance(
HYDRADX_OMNIPOOL_ADDRESS,
[sharedAsset!],
this.updateBalanceCallback(pool, () => true)
this.updateBalancesCallback(pool, () => true)
);
}

Expand Down Expand Up @@ -150,13 +151,27 @@ export abstract class PoolClient extends BalanceClient {
return pool;
}

private updateBalanceCallback(
private updateBalancesCallback(
pool: PoolBase,
canUpdate: (pool: PoolBase, token: string) => boolean
) {
return function (balances: [string, BigNumber][]) {
balances.forEach(([token, balance]) => {
const tokenIndex = pool.tokens.findIndex((t) => t.id == token);
if (tokenIndex >= 0 && canUpdate(pool, token)) {
pool.tokens[tokenIndex].balance = balance.toString();
}
});
/* pool.tokens.forEach((t) => {
console.log(pool.type, pool.address, t.id, t.balance.toString());
}); */
};
}

private updateBalanceCallback(pool: PoolBase) {
return function (token: string, balance: BigNumber) {
const tokenIndex = pool.tokens.findIndex((t) => t.id == token);
if (tokenIndex >= 0 && canUpdate(pool, token)) {
if (tokenIndex >= 0) {
pool.tokens[tokenIndex].balance = balance.toString();
}
};
Expand Down
3 changes: 2 additions & 1 deletion packages/sdk/test/script/examples/router/getPools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class GetAllAssetsExample extends PolkadotExecutor {
async script(api: ApiPromise): Promise<any> {
const poolService = new PoolService(api);
const router = new TradeRouter(poolService);
return router.getPools();
await router.getPools();
return [];
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/test/script/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export abstract class PolkadotExecutor {
})
.finally(() => {
console.timeEnd('Execution time:');
api.disconnect();
//api.disconnect();
resolve('');
});
});
Expand Down

0 comments on commit dec1c87

Please sign in to comment.