Skip to content

Commit

Permalink
refactor: l1 tvl multicall fetch (#40)
Browse files Browse the repository at this point in the history
# 🤖 Linear

Closes ZKS-146

## Description

Replace current impl. of fetching token balances using virtual batching
contract with `Multicall` contract
Delete contracts and files related to old implementation.
Delete foundry related deps and configuration since it's no longer
required
  • Loading branch information
0xnigir1 authored Aug 8, 2024
1 parent 9238bcc commit fd88b1f
Show file tree
Hide file tree
Showing 15 changed files with 47 additions and 441 deletions.
3 changes: 0 additions & 3 deletions .github/actions/setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,3 @@ runs:
with:
node-version: 20
cache: "pnpm"

- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
3 changes: 0 additions & 3 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,3 @@ jobs:

- name: Run Linter
run: pnpm lint

- name: Run Contracts Linter
run: pnpm lint:contracts:check
17 changes: 0 additions & 17 deletions .solhint.json

This file was deleted.

21 changes: 0 additions & 21 deletions .solhint.tests.json

This file was deleted.

19 changes: 0 additions & 19 deletions contracts/foundry.toml

This file was deleted.

4 changes: 0 additions & 4 deletions contracts/remappings.txt

This file was deleted.

50 changes: 0 additions & 50 deletions contracts/src/contracts/TokenBalances.sol

This file was deleted.

9 changes: 0 additions & 9 deletions contracts/src/interfaces/IERC20.sol

This file was deleted.

100 changes: 0 additions & 100 deletions contracts/test/TokenBalances.t.sol

This file was deleted.

14 changes: 0 additions & 14 deletions libs/metrics/src/l1/abis/tokenBalances.abi.ts

This file was deleted.

2 changes: 0 additions & 2 deletions libs/metrics/src/l1/bytecode/index.ts

This file was deleted.

35 changes: 16 additions & 19 deletions libs/metrics/src/l1/l1MetricsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,16 @@ import { Inject, Injectable, LoggerService } from "@nestjs/common";
import { WINSTON_MODULE_NEST_PROVIDER } from "nest-winston";
import {
Address,
ContractConstructorArgs,
encodeFunctionData,
erc20Abi,
formatUnits,
parseAbiParameters,
parseEther,
parseUnits,
zeroAddress,
} from "viem";

import { L1ProviderException } from "@zkchainhub/metrics/exceptions/provider.exception";
import { bridgeHubAbi, sharedBridgeAbi } from "@zkchainhub/metrics/l1/abis";
import { tokenBalancesAbi } from "@zkchainhub/metrics/l1/abis/tokenBalances.abi";
import { tokenBalancesBytecode } from "@zkchainhub/metrics/l1/bytecode";
import { AssetTvl, GasInfo } from "@zkchainhub/metrics/types";
import { IPricingService, PRICING_PROVIDER } from "@zkchainhub/pricing";
import { EvmProviderService } from "@zkchainhub/providers";
Expand Down Expand Up @@ -121,29 +117,30 @@ export class L1MetricsService {

/**
* Fetches the token balances for the given addresses and ETH balance.
* Note: The last balance in the returned array is the ETH balance, so the fetch length should be addresses.length + 1.
* @param addresses - An array of addresses for which to fetch the token balances.
* @returns A promise that resolves to an object containing the ETH balance and an array of address balances.
*/
private async fetchTokenBalances(
addresses: Address[],
): Promise<{ ethBalance: bigint; addressesBalance: bigint[] }> {
const returnAbiParams = parseAbiParameters("uint256[]");
const args: ContractConstructorArgs<typeof tokenBalancesAbi> = [
L1_CONTRACTS.SHARED_BRIDGE,
addresses,
];

const [balances] = await this.evmProviderService.batchRequest(
tokenBalancesAbi,
tokenBalancesBytecode,
args,
returnAbiParams,
);
const balances = await this.evmProviderService.multicall({
contracts: [
...addresses.map((tokenAddress) => {
return {
address: tokenAddress,
abi: erc20Abi,
functionName: "balanceOf",
args: [this.sharedBridge.address],
} as const;
}),
],
allowFailure: false,
});
const ethBalance = await this.evmProviderService.getBalance(this.sharedBridge.address);

assert(balances.length === addresses.length + 1, "Invalid balances length");
assert(balances.length === addresses.length, "Invalid balances length");

return { ethBalance: balances[addresses.length]!, addressesBalance: balances.slice(0, -1) };
return { ethBalance: ethBalance, addressesBalance: balances };
}

//TODO: Implement getBatchesInfo.
Expand Down
Loading

0 comments on commit fd88b1f

Please sign in to comment.