📄 src/web3/network/chains.test.ts
D-OPEN SOVEREIGN
1import { describe, it, expect } from 'vitest';
2import { EVM_CHAINS, DEFAULT_CHAIN_ID, getChain, toViemChain } from './chains';
3
4describe('getChain', () => {
5  it('returns the config for a known numeric chain id', () => {
6    const chain = getChain(1116);
7    expect(chain.chainId).toBe(1116);
8    expect(chain.name).toBe('Core Blockchain MainNet');
9  });
10
11  it('accepts a string chain id', () => {
12    const chain = getChain('1114');
13    expect(chain.chainId).toBe(1114);
14  });
15
16  it('throws for an unconfigured chain id', () => {
17    expect(() => getChain(999999)).toThrow('Chain 999999 not configured');
18  });
19
20  it('resolves the default chain id to a valid config', () => {
21    expect(() => getChain(DEFAULT_CHAIN_ID)).not.toThrow();
22  });
23});
24
25describe('toViemChain', () => {
26  it('builds a viem Chain from an EvmChainConfig', () => {
27    const config = EVM_CHAINS[1116];
28    const chain = toViemChain(config);
29
30    expect(chain.id).toBe(config.chainId);
31    expect(chain.name).toBe(config.name);
32    expect(chain.nativeCurrency).toEqual({
33      name: config.symbol,
34      symbol: config.symbol,
35      decimals: config.decimals,
36    });
37    expect(chain.rpcUrls.default.http).toEqual([config.rpc]);
38  });
39
40  it('defaults decimals to 18 when the config omits them', () => {
41    const chain = toViemChain({ ...EVM_CHAINS[1116], decimals: 0 });
42    expect(chain.nativeCurrency.decimals).toBe(18);
43  });
44});
45