Skip to content

Commit

Permalink
feat: add new chains to EvmChain. (#1142)
Browse files Browse the repository at this point in the history
* feat: add new chains to EvmChain.
  • Loading branch information
b4rtaz authored Jun 5, 2023
1 parent c085091 commit a9ba709
Show file tree
Hide file tree
Showing 5 changed files with 252 additions and 31 deletions.
7 changes: 7 additions & 0 deletions .changeset/good-ducks-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@moralisweb3/common-evm-utils': patch
'@moralisweb3/streams': patch
'moralis': patch
---

Added new chains to the `EvmChain` class: `ARBITRUM_TESTNET`, `RONIN`, `OPTIMISM` and `AVALANCHE_TESTNET`.
17 changes: 9 additions & 8 deletions packages/common/evmUtils/scripts/fetch-chaindata.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,30 @@

import axios from 'axios';
import fs from 'fs';
import { Core, CoreProvider } from '@moralisweb3/common-core';
import { EvmChain } from '@moralisweb3/common-evm-utils';

const url = 'https://chainid.network/chains.json';
const DATASET_URL = 'https://chainid.network/chains.json';
const SKIP_CHAIN_IDS = [
2020, // Ronin, the dataset contains an invalid data for this chain.
];

async function fetchChainData() {
const response = await axios.get(url);
const response = await axios.get(DATASET_URL);
return response.data;
}

function getSupportedChainIds() {
CoreProvider.setDefault(Core.create());

const networks = Object.getOwnPropertyNames(EvmChain);
return networks.filter((name) => name.toUpperCase() === name).map((name) => EvmChain[name].decimal);
return EvmChain.values()
.map((chain) => chain.decimal)
.filter((chainId) => !SKIP_CHAIN_IDS.includes(chainId));
}

function generateFile(items) {
const tsPath = '../src/data/chaindata.ts';
const content = `/* eslint-disable no-template-curly-in-string */
import { EvmChainListDataEntry } from './types';
// source: ${url}
// source: ${DATASET_URL}
export const chainList: EvmChainListDataEntry[] = ${JSON.stringify(items, null, 2)};`;
fs.writeFileSync(tsPath, content, 'utf-8');
}
Expand Down
104 changes: 104 additions & 0 deletions packages/common/evmUtils/src/data/chaindata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export const chainList: EvmChainListDataEntry[] = [
'https://goerli.infura.io/v3/${INFURA_API_KEY}',
'wss://goerli.infura.io/v3/${INFURA_API_KEY}',
'https://rpc.goerli.mudit.blog/',
'https://ethereum-goerli.publicnode.com',
],
faucets: [
'http://fauceth.komputing.org?chain=5&address=${ADDRESS}',
Expand All @@ -78,6 +79,28 @@ export const chainList: EvmChainListDataEntry[] = [
},
],
},
{
name: 'Optimism',
chain: 'ETH',
rpc: ['https://mainnet.optimism.io/'],
faucets: [],
nativeCurrency: {
name: 'Ether',
symbol: 'ETH',
decimals: 18,
},
infoURL: 'https://optimism.io',
shortName: 'oeth',
chainId: 10,
networkId: 10,
explorers: [
{
name: 'etherscan',
url: 'https://optimistic.etherscan.io',
standard: 'EIP3091',
},
],
},
{
name: 'Cronos Mainnet Beta',
chain: 'CRO',
Expand Down Expand Up @@ -153,6 +176,7 @@ export const chainList: EvmChainListDataEntry[] = [
'https://data-seed-prebsc-2-s2.binance.org:8545',
'https://data-seed-prebsc-1-s3.binance.org:8545',
'https://data-seed-prebsc-2-s3.binance.org:8545',
'https://bsc-testnet.publicnode.com',
],
faucets: ['https://testnet.binance.org/faucet-smart'],
nativeCurrency: {
Expand Down Expand Up @@ -228,6 +252,30 @@ export const chainList: EvmChainListDataEntry[] = [
},
],
},
{
name: 'Fantom Testnet',
chain: 'FTM',
rpc: ['https://rpc.testnet.fantom.network', 'https://fantom-testnet.publicnode.com'],
faucets: ['https://faucet.fantom.network'],
nativeCurrency: {
name: 'Fantom',
symbol: 'FTM',
decimals: 18,
},
infoURL: 'https://docs.fantom.foundation/quick-start/short-guide#fantom-testnet',
shortName: 'tftm',
chainId: 4002,
networkId: 4002,
icon: 'fantom',
explorers: [
{
name: 'ftmscan',
url: 'https://testnet.ftmscan.com',
icon: 'ftmscan',
standard: 'EIP3091',
},
],
},
{
name: 'Arbitrum One',
chainId: 42161,
Expand Down Expand Up @@ -268,6 +316,29 @@ export const chainList: EvmChainListDataEntry[] = [
],
},
},
{
name: 'Avalanche Fuji Testnet',
chain: 'AVAX',
icon: 'avax',
rpc: ['https://api.avax-test.network/ext/bc/C/rpc', 'https://avalanche-fuji-c-chain.publicnode.com'],
faucets: ['https://faucet.avax-test.network/'],
nativeCurrency: {
name: 'Avalanche',
symbol: 'AVAX',
decimals: 18,
},
infoURL: 'https://cchain.explorer.avax-test.network',
shortName: 'Fuji',
chainId: 43113,
networkId: 1,
explorers: [
{
name: 'snowtrace',
url: 'https://testnet.snowtrace.io',
standard: 'EIP3091',
},
],
},
{
name: 'Avalanche C-Chain',
chain: 'AVAX',
Expand Down Expand Up @@ -306,6 +377,7 @@ export const chainList: EvmChainListDataEntry[] = [
'https://matic-mumbai.chainstacklabs.com',
'https://rpc-mumbai.maticvigil.com',
'https://matic-testnet-archive-rpc.bwarelabs.com',
'https://polygon-mumbai-bor.publicnode.com',
],
faucets: ['https://faucet.polygon.technology/'],
nativeCurrency: {
Expand All @@ -325,6 +397,38 @@ export const chainList: EvmChainListDataEntry[] = [
},
],
},
{
name: 'Arbitrum Goerli',
title: 'Arbitrum Goerli Rollup Testnet',
chainId: 421613,
shortName: 'arb-goerli',
chain: 'ETH',
networkId: 421613,
nativeCurrency: {
name: 'Arbitrum Goerli Ether',
symbol: 'AGOR',
decimals: 18,
},
rpc: ['https://goerli-rollup.arbitrum.io/rpc/'],
faucets: [],
infoURL: 'https://arbitrum.io/',
explorers: [
{
name: 'Arbitrum Goerli Rollup Explorer',
url: 'https://goerli-rollup-explorer.arbitrum.io',
standard: 'EIP3091',
},
],
parent: {
type: 'L2',
chain: 'eip155-5',
bridges: [
{
url: 'https://bridge.arbitrum.io/',
},
],
},
},
{
name: 'Sepolia',
title: 'Ethereum Testnet Sepolia',
Expand Down
71 changes: 53 additions & 18 deletions packages/common/evmUtils/src/dataTypes/EvmChain/EvmChain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,24 +97,59 @@ describe('EvmChain', () => {
expect(chainA.equals(chainB)).toBeFalsy();
});

/**
* External data
*/
it('should get the chain name', () => {
const chain = EvmChain.create('0x1');

expect(chain.name).toBe('Ethereum Mainnet');
expect(chain.rpcUrls).toStrictEqual([
'https://mainnet.infura.io/v3/${INFURA_API_KEY}',
'wss://mainnet.infura.io/ws/v3/${INFURA_API_KEY}',
'https://api.mycryptoapi.com/eth',
'https://cloudflare-eth.com',
'https://ethereum.publicnode.com',
]);
expect(chain.currency).toStrictEqual({
decimals: 18,
name: 'Ether',
symbol: 'ETH',
it('returns correct chain ID', () => {
expect(EvmChain.ETHEREUM.apiHex).toBe('0x1');
expect(EvmChain.GOERLI.apiHex).toBe('0x5');
expect(EvmChain.SEPOLIA.apiHex).toBe('0xaa36a7');
expect(EvmChain.BSC.apiHex).toBe('0x38');
expect(EvmChain.BSC_TESTNET.apiHex).toBe('0x61');
expect(EvmChain.POLYGON.apiHex).toBe('0x89');
expect(EvmChain.MUMBAI.apiHex).toBe('0x13881');
expect(EvmChain.FANTOM.apiHex).toBe('0xfa');
expect(EvmChain.FANTOM_TESTNET.apiHex).toBe('0xfa2');
expect(EvmChain.AVALANCHE.apiHex).toBe('0xa86a');
expect(EvmChain.AVALANCHE_TESTNET.apiHex).toBe('0xa869');
expect(EvmChain.CRONOS.apiHex).toBe('0x19');
expect(EvmChain.RONIN.apiHex).toBe('0x7e4');
expect(EvmChain.ARBITRUM.apiHex).toBe('0xa4b1');
expect(EvmChain.ARBITRUM_TESTNET.apiHex).toBe('0x66eed');
expect(EvmChain.OPTIMISM.apiHex).toBe('0xa');
expect(EvmChain.PALM.apiHex).toBe('0x2a15c308d');
});

describe('metadata', () => {
// We skip RONIN because the source dataset doesn't have it.
const supportedChains = EvmChain.values().filter((chain) => chain.apiHex !== '0x7e4');

for (const chain of supportedChains) {
it(`returns metadata for ${chain.apiHex}`, () => {
expect(chain.name).toBeDefined();
expect((chain.rpcUrls as string[]).length > 0).toBeTruthy();
expect(chain.currency).toBeDefined();
expect(chain.currency?.decimals).toBeDefined();
expect(chain.currency?.name).toBeDefined();
expect(chain.currency?.symbol).toBeDefined();
expect(chain.explorer).toBeDefined();
expect(chain.explorer?.url).toBeDefined();
});
}

it('returns correct metadata for Ethereum', () => {
const chain = EvmChain.create('0x1');

expect(chain.name).toBe('Ethereum Mainnet');
expect(chain.rpcUrls).toStrictEqual([
'https://mainnet.infura.io/v3/${INFURA_API_KEY}',
'wss://mainnet.infura.io/ws/v3/${INFURA_API_KEY}',
'https://api.mycryptoapi.com/eth',
'https://cloudflare-eth.com',
'https://ethereum.publicnode.com',
]);
expect(chain.currency).toStrictEqual({
decimals: 18,
name: 'Ether',
symbol: 'ETH',
});
});
});
});
Loading

1 comment on commit a9ba709

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

Test coverage

Title Lines Statements Branches Functions
api-utils Coverage: 20%
20.6% (61/296) 20.48% (17/83) 19.04% (12/63)
auth Coverage: 89%
92.45% (98/106) 83.33% (20/24) 86.66% (26/30)
evm-api Coverage: 96%
96.87% (93/96) 66.66% (6/9) 95.23% (60/63)
common-aptos-utils Coverage: 4%
4.56% (151/3306) 4.49% (25/556) 5.53% (45/813)
common-evm-utils Coverage: 66%
66.25% (1559/2353) 27.41% (261/952) 46.26% (434/938)
sol-api Coverage: 100%
100% (40/40) 66.66% (6/9) 100% (15/15)
common-sol-utils Coverage: 69%
69.04% (203/294) 51.42% (18/35) 55.67% (54/97)
common-streams-utils Coverage: 90%
90.73% (1204/1327) 73.63% (363/493) 82.07% (444/541)
streams Coverage: 88%
88.2% (576/653) 68.81% (64/93) 88.02% (125/142)

Please sign in to comment.