Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use multicall getEthBalance #46

Merged
merged 5 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions libs/metrics/src/l1/l1MetricsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,20 +140,19 @@ export class L1MetricsService {
addresses: Address[],
): Promise<{ ethBalance: bigint; addressesBalance: bigint[] }> {
const multicall3Address = this.evmProviderService.getMulticall3Address();
const contracts = addresses.map((tokenAddress) => {
return {
address: tokenAddress,
abi: erc20Abi,
functionName: "balanceOf",
args: [this.sharedBridge.address],
} as const;
});
let balances: bigint[] = [];

if (multicall3Address) {
balances = await this.evmProviderService.multicall({
contracts: [
...contracts,
...addresses.map((tokenAddress) => {
return {
address: tokenAddress,
abi: erc20Abi,
functionName: "balanceOf",
args: [this.sharedBridge.address],
} as const;
}),
{
address: multicall3Address,
abi: multicall3Abi,
Expand All @@ -164,14 +163,14 @@ export class L1MetricsService {
allowFailure: false,
} as const);
} else {
const [erc20Balances, ethBalance] = await Promise.all([
this.evmProviderService.multicall({
contracts: contracts,
allowFailure: false,
} as const),
balances = await Promise.all([
...addresses.map((tokenAddress) =>
this.evmProviderService.readContract(tokenAddress, erc20Abi, "balanceOf", [
this.sharedBridge.address,
]),
),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any possibility to have one of these calls being throttled by the RPC node err'ing with a 429?

Copy link
Collaborator Author

@0xnigir1 0xnigir1 Aug 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it could but the current amount of tokens is not an issue from what i tested with llama rpc, there's the fine line between adding the pagination overhead when making multiple RPC requests too

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a very edgy case that practically wont exist, however we are gona be having 429 when calling multiple methods, but we expect to tackle that issue with caching and viem fallback client

this.evmProviderService.getBalance(this.sharedBridge.address),
]);
balances = [...erc20Balances, ethBalance];
}

assert(balances.length === addresses.length + 1, "Invalid balances length");
Expand Down
43 changes: 21 additions & 22 deletions libs/metrics/test/unit/l1/l1MetricsService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from "@zkchainhub/metrics/l1/abis";
import { IPricingService, PRICING_PROVIDER } from "@zkchainhub/pricing";
import { EvmProviderService } from "@zkchainhub/providers";
import { MulticallNotFound } from "@zkchainhub/providers/exceptions";
import {
BatchesInfo,
ChainType,
Expand Down Expand Up @@ -253,14 +254,14 @@ describe("L1MetricsService", () => {
expect(mockEvmProviderService.getBalance).not.toHaveBeenCalled();
});

it("fetch ethBalance using getBalance", async () => {
const mockMulticallBalances = [60_841_657_140641n, 135_63005559n]; // Mocked balances
it("return the TVL on L1 Shared Bridge without multicall", async () => {
const mockPrices = { "wrapped-bitcoin": 66_129, "usd-coin": 0.999, ethereum: 3_181.09 }; // Mocked prices

jest.spyOn(mockEvmProviderService, "getMulticall3Address").mockReturnValue(undefined);
jest.spyOn(mockEvmProviderService, "multicall").mockResolvedValue(
mockMulticallBalances,
);
jest.spyOn(mockEvmProviderService, "multicall").mockRejectedValue(MulticallNotFound);
jest.spyOn(mockEvmProviderService, "readContract")
.mockResolvedValueOnce(60_841_657_140641n)
.mockResolvedValueOnce(135_63005559n);
jest.spyOn(mockEvmProviderService, "getBalance").mockResolvedValue(
123_803_824374847279970609n,
);
Expand Down Expand Up @@ -307,23 +308,21 @@ describe("L1MetricsService", () => {
decimals: 8,
},
]);
expect(mockEvmProviderService.multicall).toHaveBeenCalledWith({
contracts: [
{
address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
abi: erc20Abi,
functionName: "balanceOf",
args: [L1_CONTRACTS.SHARED_BRIDGE],
},
{
address: "0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599",
abi: erc20Abi,
functionName: "balanceOf",
args: [L1_CONTRACTS.SHARED_BRIDGE],
},
],
allowFailure: false,
});
expect(mockEvmProviderService.multicall).not.toHaveBeenCalled();
expect(mockEvmProviderService.readContract).toHaveBeenNthCalledWith(
1,
"0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
erc20Abi,
"balanceOf",
[L1_CONTRACTS.SHARED_BRIDGE],
);
expect(mockEvmProviderService.readContract).toHaveBeenNthCalledWith(
2,
"0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599",
erc20Abi,
"balanceOf",
[L1_CONTRACTS.SHARED_BRIDGE],
);
expect(mockPricingService.getTokenPrices).toHaveBeenCalledWith([
"ethereum",
"usd-coin",
Expand Down