• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hicommonwealth / commonwealth / 13497108514

24 Feb 2025 11:36AM UTC coverage: 46.449% (+0.08%) from 46.365%
13497108514

Pull #11078

github

web-flow
Merge 56f84aad3 into beadf67b7
Pull Request #11078: Improvements to Community Homepages

1317 of 3113 branches covered (42.31%)

Branch coverage included in aggregate %.

2490 of 5083 relevant lines covered (48.99%)

38.03 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

2.7
/libs/model/src/services/tokenBalanceCache/providers/get_eth_balances.ts
1
import { logger } from '@hicommonwealth/core';
2
import { ChainNodeInstance } from '@hicommonwealth/model';
3
import { Balances } from '../types';
4
import {
5
  evmBalanceFetcherBatching,
6
  evmOffChainRpcBatching,
7
  evmRpcRequest,
8
  mapNodeToBalanceFetcherContract,
9
} from '../util';
10

11
const log = logger(import.meta);
29✔
12

13
export type GetEthBalancesOptions = {
14
  chainNode: ChainNodeInstance;
15
  addresses: string[];
16
  batchSize?: number;
17
};
18

19
export async function __getEthBalances(options: GetEthBalancesOptions) {
20
  if (options.addresses.length === 0) {
×
21
    return {};
×
22
  }
23

24
  const rpcEndpoint = options.chainNode.private_url || options.chainNode.url;
×
25
  if (options.addresses.length === 1) {
×
26
    return await getEthBalance(
×
27
      options.chainNode.eth_chain_id!,
28
      rpcEndpoint,
29
      options.addresses[0],
30
    );
31
  }
32

33
  const balanceFetcherContract = mapNodeToBalanceFetcherContract(
×
34
    options.chainNode.eth_chain_id,
35
  );
36
  if (balanceFetcherContract) {
×
37
    return await getOnChainBatchEthBalances(
×
38
      options.chainNode.eth_chain_id!,
39
      rpcEndpoint,
40
      options.addresses,
41
      options.batchSize,
42
    );
43
  } else {
44
    return await getOffChainBatchEthBalances(
×
45
      options.chainNode.eth_chain_id!,
46
      rpcEndpoint,
47
      options.addresses,
48
      options.batchSize,
49
    );
50
  }
51
}
52

53
async function getOnChainBatchEthBalances(
54
  evmChainId: number,
55
  rpcEndpoint: string,
56
  addresses: string[],
57
  batchSize = 1000,
×
58
): Promise<Balances> {
59
  const { balances } = await evmBalanceFetcherBatching(
×
60
    {
61
      evmChainId,
62
      url: rpcEndpoint,
63
    },
64
    {
65
      batchSize,
66
    },
67
    addresses,
68
  );
69

70
  return balances;
×
71
}
72

73
async function getOffChainBatchEthBalances(
74
  evmChainId: number,
75
  rpcEndpoint: string,
76
  addresses: string[],
77
  batchSize = 1000,
×
78
): Promise<Balances> {
79
  const { balances } = await evmOffChainRpcBatching(
×
80
    {
81
      evmChainId,
82
      url: rpcEndpoint,
83
    },
84
    {
85
      method: 'eth_getBalance',
86
      getParams: (address, _tokenAddress) => {
87
        return address;
×
88
      },
89
      batchSize,
90
    },
91
    addresses,
92
  );
93
  return balances;
×
94
}
95

96
async function getEthBalance(
97
  evmChainId: number,
98
  rpcEndpoint: string,
99
  address: string,
100
): Promise<Balances> {
101
  const requestBody = {
×
102
    method: 'eth_getBalance',
103
    params: [address, 'latest'],
104
    id: 1,
105
    jsonrpc: '2.0',
106
  };
107

108
  const errorMsg =
109
    `Eth balance fetch failed for address ${address} ` +
×
110
    `on evm chain id ${evmChainId}`;
111

112
  const data = await evmRpcRequest(rpcEndpoint, requestBody, errorMsg);
×
113
  if (!data) return {};
×
114

115
  if (data.error) {
×
116
    log.error(errorMsg, data.error);
×
117
    return {};
×
118
  } else {
119
    return {
×
120
      [address]: BigInt(data.result).toString(),
121
    };
122
  }
123
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc