Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
feat(balances): Chunk balance calculations (#1880)
Browse files Browse the repository at this point in the history
  • Loading branch information
maximebedard committed Dec 8, 2022
1 parent e8bdd5c commit a4885b3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 23 deletions.
21 changes: 14 additions & 7 deletions src/position/template/app-token.template.position-fetcher.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Inject } from '@nestjs/common';
import { BigNumberish, Contract } from 'ethers/lib/ethers';
import _ from 'lodash';
import { compact, intersection, isArray, partition, sortBy, sum } from 'lodash';

import { drillBalance } from '~app-toolkit';
Expand Down Expand Up @@ -324,15 +325,21 @@ export abstract class AppTokenTemplatePositionFetcher<
async getRawBalances(_address: string): Promise<RawAppTokenBalance[]> {
const multicall = this.appToolkit.getMulticall(this.network);
const address = await this.getAccountAddress(_address);
const appTokens = await this.getPositionsForBalances();
if (address === ZERO_ADDRESS) return [];

return Promise.all(
appTokens.map(async appToken => ({
key: this.appToolkit.getPositionKey(appToken),
balance: (await this.getBalancePerToken({ multicall, address, appToken })).toString(),
})),
);
const appTokens = await this.getPositionsForBalances();
let results: RawAppTokenBalance[] = [];
for (const batch of _.chunk(appTokens, 100).values()) {
results = results.concat(
await Promise.all(
batch.map(async appToken => ({
key: this.appToolkit.getPositionKey(appToken),
balance: (await this.getBalancePerToken({ multicall, address, appToken })).toString(),
})),
),
);
}
return results;
}

async drillRawBalances(balances: RawAppTokenBalance[]): Promise<AppTokenPositionBalance<V>[]> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Inject } from '@nestjs/common';
import { BigNumberish, Contract } from 'ethers/lib/ethers';
import { compact, sumBy } from 'lodash';
import _, { compact, sumBy } from 'lodash';

import { drillBalance } from '~app-toolkit';
import { APP_TOOLKIT, IAppToolkit } from '~app-toolkit/app-toolkit.interface';
Expand Down Expand Up @@ -246,23 +246,34 @@ export abstract class ContractPositionTemplatePositionFetcher<
async getRawBalances(_address: string): Promise<RawContractPositionBalance[]> {
const multicall = this.appToolkit.getMulticall(this.network);
const address = await this.getAccountAddress(_address);
const contractPositions = await this.getPositionsForBalances();
if (address === ZERO_ADDRESS) return [];

return Promise.all(
contractPositions.map(async contractPosition => {
const contract = multicall.wrap(this.getContract(contractPosition.address));
const balancesRaw = await this.getTokenBalancesPerPosition({ address, contract, contractPosition, multicall });

return {
key: this.appToolkit.getPositionKey(contractPosition),
tokens: contractPosition.tokens.map((token, i) => ({
key: this.appToolkit.getPositionKey(token),
balance: balancesRaw[i]?.toString() ?? '0',
})),
};
}),
);
const contractPositions = await this.getPositionsForBalances();
let results: RawContractPositionBalance[] = [];
for (const batch of _.chunk(contractPositions, 100).values()) {
results = results.concat(
await Promise.all(
batch.map(async contractPosition => {
const contract = multicall.wrap(this.getContract(contractPosition.address));
const balancesRaw = await this.getTokenBalancesPerPosition({
address,
contract,
contractPosition,
multicall,
});

return {
key: this.appToolkit.getPositionKey(contractPosition),
tokens: contractPosition.tokens.map((token, i) => ({
key: this.appToolkit.getPositionKey(token),
balance: balancesRaw[i]?.toString() ?? '0',
})),
};
}),
),
);
}
return results;
}

async drillRawBalances(balances: RawContractPositionBalance[]): Promise<ContractPositionBalance<V>[]> {
Expand Down

0 comments on commit a4885b3

Please sign in to comment.