import chai from 'chai'; import chaiSubset from 'chai-subset'; import { getEvmNetworkToChainIdMap, getNetwork, getNetworkName, lookupAliasesByNetworkName, lookupEvmChainIdByNetwork, lookupEvmChainNetworkById, } from '../../src/util'; import { EthereumNetwork } from '../../src/types'; chai.use(chaiSubset); const { expect } = chai; describe('chainIdMaps', () => { context('lookupEvmChainNetworkById', () => { it('should return the network name for an ethereum chain ID', () => { expect(lookupEvmChainNetworkById(51)).to.equal('xdcApothem'); expect(lookupEvmChainNetworkById(420)).to.equal('optimismGoerli'); }); it('should return null for an unmapped chain id', () => { expect(lookupEvmChainNetworkById(0)).to.be.null; }); }); context('lookupEvmChainIdByNetwork', () => { it('should return the chainId for an ethereum chain ID', () => { expect(lookupEvmChainIdByNetwork('xdcApothem')).to.equal('51'); expect(lookupEvmChainIdByNetwork('optimismGoerli')).to.equal('420'); }); it('should return null for an unmapped chain network', () => { expect(lookupEvmChainIdByNetwork('devnet')).to.be.null; }); it('should return null for a non-existent chain', () => { expect(lookupEvmChainIdByNetwork('devnet')).to.be.null; }); }); context('getNetworkToChainIdMap', () => { it('should return inverted map ( network name to chain ID )', () => { const map = getEvmNetworkToChainIdMap(); expect(map.xdcApothem).to.equal(51); expect(map.xdcMainnet).to.equal(50); expect(map.mainnet).to.equal(1); }); }); context('getNetworkName', () => { context('with a non-numeric network name', () => { it('should return the input name', () => { expect(getNetworkName('test')).to.equal('test'); }); }); context('with a numeric network name', () => { context('with the network found in the chainIdMap', () => { it('should return the chainIdMap entry', () => { expect(getNetworkName('11155111')).to.equal('sepolia'); }); }); context('with the network found in the chainIdMap', () => { it('should return the input name', () => { expect(getNetworkName('1115512342342')).to.equal('1115512342342'); }); }); }); }); context('getNetwork', () => { context('with a name found in the networks map', () => { it('should return the network object', () => { expect(getNetwork('sepolia')).to.deep.eq({ url: 'https://sepolia.infura.io/v3/', chainId: 11155111, symbol: 'ETH', network: 'Sepolia', }); }); }); context('with a name not found in the networks map', () => { it('should return undefined', () => { expect(getNetwork('bscMainnet')).to.eq(undefined); }); }); }); context('lookupAliasesByNetworkName', () => { it('should return two names for networks with a single alias', () => { expect(lookupAliasesByNetworkName('fantom')).to.deep.equal(['fantomMainnet', 'fantom']); expect(lookupAliasesByNetworkName('fantomMainnet')).to.deep.equal(['fantomMainnet', 'fantom']); }); it('should work correctly for networks with multiple aliases', () => { expect(lookupAliasesByNetworkName('mainnet')).to.deep.equal(['mainnet', 'homestead', 'ethereum']); expect(lookupAliasesByNetworkName('homestead')).to.deep.equal(['mainnet', 'homestead', 'ethereum']); expect(lookupAliasesByNetworkName('ethereum')).to.deep.equal(['mainnet', 'homestead', 'ethereum']); }); it('should work correctly for networks with no aliases', () => { expect(lookupAliasesByNetworkName('fantomTestnet')).to.deep.equal(['fantomTestnet']); }); it('should be case-insensitive', () => { expect(lookupAliasesByNetworkName('fantommainnet' as EthereumNetwork)).to.deep.equal(['fantomMainnet', 'fantom']); }); it('should return an empty array if the network is not found at all', () => { expect(lookupAliasesByNetworkName('invalid-network' as EthereumNetwork)).to.deep.equal([]); }); }); });