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

hicommonwealth / commonwealth / 14870193493

06 May 2025 09:35PM UTC coverage: 43.081% (-0.6%) from 43.654%
14870193493

Pull #12056

github

web-flow
Merge 7a94be300 into 2bbbdfe0a
Pull Request #12056: Judged contest fixes

1685 of 4323 branches covered (38.98%)

Branch coverage included in aggregate %.

4 of 16 new or added lines in 4 files covered. (25.0%)

156 existing lines in 14 files now uncovered.

3050 of 6668 relevant lines covered (45.74%)

37.71 hits per line

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

1.85
/libs/model/src/services/tokenBalanceCache/tokenBalanceCache.ts
1
import { logger, stats } from '@hicommonwealth/core';
2
import { BalanceSourceType } from '@hicommonwealth/shared';
3
import { getCosmosBalances, getEvmBalances } from './providers';
4
import { getSolanaBalances } from './providers/getSolanaBalances';
5
import { getSuiBalances } from './providers/getSuiBalances';
6
import {
7
  Balances,
8
  GetBalancesOptions,
9
  GetCosmosBalancesOptions,
10
  GetErcBalanceOptions,
11
  GetEvmBalancesOptions,
12
  GetSPLBalancesOptions,
13
  GetSuiNativeBalanceOptions,
14
  GetSuiTokenBalanceOptions,
15
} from './types';
16

17
const log = logger(import.meta);
33✔
18

19
/**
20
 * This is the main function through which all balances should be fetched.
21
 * This function supports all balance sources and is fully compatible with Redis caching.
22
 */
23
export async function getBalances(
24
  options: GetBalancesOptions,
25
  ttl?: number,
26
): Promise<Balances> {
UNCOV
27
  if (options.addresses.length === 0) return {};
×
28

29
  let balances: Balances = {};
×
30

UNCOV
31
  try {
×
UNCOV
32
    if (
×
33
      options.balanceSourceType === BalanceSourceType.CosmosNative ||
×
34
      options.balanceSourceType === BalanceSourceType.CW20 ||
35
      options.balanceSourceType === BalanceSourceType.CW721
36
    ) {
UNCOV
37
      balances = await getCosmosBalances(options, ttl);
×
UNCOV
38
    } else if (
×
39
      options.balanceSourceType == BalanceSourceType.SPL ||
×
40
      options.balanceSourceType == BalanceSourceType.SOLNFT
41
    ) {
UNCOV
42
      balances = await getSolanaBalances(options, ttl);
×
UNCOV
43
    } else if (
×
44
      options.balanceSourceType === BalanceSourceType.SuiNative ||
×
45
      options.balanceSourceType === BalanceSourceType.SuiToken
46
    ) {
UNCOV
47
      balances = await getSuiBalances(
×
48
        options as GetSuiNativeBalanceOptions | GetSuiTokenBalanceOptions,
49
        ttl,
50
      );
51
    } else {
UNCOV
52
      balances = await getEvmBalances(options as GetEvmBalancesOptions, ttl);
×
53
    }
54
  } catch (e) {
55
    const chainId =
UNCOV
56
      options.balanceSourceType === BalanceSourceType.SPL
×
57
        ? 'solana'
58
        : options.balanceSourceType === BalanceSourceType.SuiNative ||
×
59
            options.balanceSourceType === BalanceSourceType.SuiToken
60
          ? (options as GetSuiNativeBalanceOptions | GetSuiTokenBalanceOptions)
61
              .sourceOptions.suiNetwork
62
          : (options as GetEvmBalancesOptions).sourceOptions.evmChainId ||
×
63
            (options as GetCosmosBalancesOptions).sourceOptions.cosmosChainId;
64

65
    const contractAddress =
UNCOV
66
      (options as GetSPLBalancesOptions).mintAddress ||
×
67
      (options as GetErcBalanceOptions).sourceOptions.contractAddress ||
68
      (options as GetSuiNativeBalanceOptions).sourceOptions.objectId ||
69
      (options.balanceSourceType === BalanceSourceType.SuiToken
×
70
        ? (options as GetSuiTokenBalanceOptions).sourceOptions.coinType
71
        : undefined);
72

73
    const msg =
UNCOV
74
      `Failed to fetch balance(s) for ${options.addresses.length} address(es)` +
×
75
      `on chain ${chainId}${contractAddress ? ' for contract ' : ''}${
×
76
        contractAddress || ''
×
77
      }`;
78

UNCOV
79
    log.error(msg, e instanceof Error ? e : undefined, {
×
80
      fingerprint: `TBC: ${chainId} - ${contractAddress}`,
81
      addresses: options.addresses.slice(0, 5),
82
      contractAddress: (options as GetErcBalanceOptions).sourceOptions
83
        ?.contractAddress,
84
      evmChainId: (options as GetEvmBalancesOptions).sourceOptions?.evmChainId,
85
      cosmosChainId: (options as GetCosmosBalancesOptions).sourceOptions
86
        ?.cosmosChainId,
87
      suiNetwork: (
88
        options as GetSuiNativeBalanceOptions | GetSuiTokenBalanceOptions
89
      ).sourceOptions?.suiNetwork,
90
      objectId: (options as GetSuiNativeBalanceOptions).sourceOptions?.objectId,
91
      coinType:
92
        options.balanceSourceType === BalanceSourceType.SuiToken
×
93
          ? (options as GetSuiTokenBalanceOptions).sourceOptions.coinType
94
          : undefined,
95
    });
96
  }
97

UNCOV
98
  stats().incrementBy(
×
99
    'tbc.successful.balance.fetch',
100
    Object.keys(balances).length,
101
    {
102
      balance_source_type: options.balanceSourceType,
103
    },
104
  );
UNCOV
105
  return balances;
×
106
}
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

© 2026 Coveralls, Inc