Skip to content

Commit

Permalink
refactor: migrate fetch contract balances at micro block to v2 endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
tmrdlt committed Mar 21, 2024
1 parent 2831a43 commit fb964dc
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 19 deletions.
7 changes: 5 additions & 2 deletions src/clients/mdw-client.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,14 @@ export type AccountBalance = {
contract: ContractAddress;
};

export type BalancesV1 = {
amounts: Record<AccountAddress, string>;
export type ContractBalance = {
account_id: AccountAddress;
amount: string;
block_hash: MicroBlockHash;
contract_id: ContractAddress;
height: string;
last_log_idx: string;
last_tx_hash: TxHash;
};

export type MdwMicroBlock = {
Expand Down
20 changes: 10 additions & 10 deletions src/clients/mdw-client.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from '../lib/utils';
import {
AccountBalance,
BalancesV1,
ContractBalance,
Contract,
ContractLog,
MdwMicroBlock,
Expand Down Expand Up @@ -45,6 +45,15 @@ export class MdwClientService {
);
}

getContractBalancesAtMicroBlockHash(
contractAddress: ContractAddress,
microBlockHash: MicroBlockHash,
): Promise<ContractBalance[]> {
return this.getAllPages<ContractBalance>(
`/v2/aex9/${contractAddress}/balances/?block_hash=${microBlockHash}&${this.defaultParams}`,
);
}

getAccountBalanceForContractAtMicroBlockHash(
contractAddress: ContractAddress,
accountAddress: AccountAddress,
Expand All @@ -61,15 +70,6 @@ export class MdwClientService {
);
}

getContractBalancesAtMicroBlockHashV1(
contractAddress: ContractAddress,
microBlockHash: MicroBlockHash,
): Promise<BalancesV1> {
return this.get<BalancesV1>(
`/aex9/balances/hash/${microBlockHash}/${contractAddress}?${this.defaultParams}`,
);
}

async getKeyBlockMicroBlocks(
hashOrKbi: KeyBlockHash | number,
): Promise<MdwMicroBlock[]> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const mockMdwClientService = {
getContract: jest.fn(),
getMicroBlock: jest.fn(),
getContractLogsUntilCondition: jest.fn(),
getContractBalancesAtMicroBlockHashV1: jest.fn(),
getContractBalancesAtMicroBlockHash: jest.fn(),
getAccountBalanceForContractAtMicroBlockHash: jest.fn(),
};

Expand Down Expand Up @@ -100,8 +100,8 @@ describe('PairLiquidityInfoHistoryImporterService', () => {
pairContractLog1,
pairContractLog2,
]);
mockMdwClientService.getContractBalancesAtMicroBlockHashV1.mockResolvedValue(
{ amounts: { ak_a: '1', ak_b: '1' } },
mockMdwClientService.getContractBalancesAtMicroBlockHash.mockResolvedValue(
[{ amount: '1' }, { amount: '1' }],
);
mockMdwClientService.getAccountBalanceForContractAtMicroBlockHash.mockResolvedValue(
{ amount: '1' },
Expand Down
8 changes: 4 additions & 4 deletions src/tasks/pair-liquidity-info-history-importer.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable, Logger } from '@nestjs/common';
import { MdwClientService } from '../clients/mdw-client.service';
import { PairService, PairWithTokens } from '../database/pair.service';
import { isEqual, orderBy, uniqWith, values } from 'lodash';
import { isEqual, orderBy, uniqWith } from 'lodash';
import { PairLiquidityInfoHistoryService } from '../database/pair-liquidity-info-history.service';
import {
ContractAddress,
Expand Down Expand Up @@ -223,12 +223,12 @@ export class PairLiquidityInfoHistoryImporterService {
) {
// Total supply is the sum of all amounts of the pair contract's balances
const pairBalances =
await this.mdwClientService.getContractBalancesAtMicroBlockHashV1(
await this.mdwClientService.getContractBalancesAtMicroBlockHash(
pairWithTokens.address as ContractAddress,
block.hash,
);
const totalSupply = values(pairBalances.amounts)
.map((amount) => BigInt(amount))
const totalSupply = pairBalances
.map((contractBalance) => BigInt(contractBalance.amount))
.reduce((a, b) => a + b, 0n);

// reserve0 is the balance of the pair contract's account of token0
Expand Down

0 comments on commit fb964dc

Please sign in to comment.