Skip to content

Commit

Permalink
Synchronize chains package (#864)
Browse files Browse the repository at this point in the history
* Update chains package

* Changeset
  • Loading branch information
JayJay1024 authored Nov 5, 2024
1 parent bb002a8 commit 91dd052
Show file tree
Hide file tree
Showing 12 changed files with 183 additions and 161 deletions.
5 changes: 5 additions & 0 deletions .changeset/grumpy-carrots-remember.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@helixbridge/chains": minor
---

Synchronize helixconf updates
4 changes: 2 additions & 2 deletions packages/chains/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"preset": "@helixbridge/jest-presets/node"
},
"dependencies": {
"@helixbridge/helixconf": "^1.1.1",
"viem": "2.18.0"
"@helixbridge/helixconf": "1.1.18",
"viem": "^2.21.19"
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { berachainTestnet as berachainTestnetViem } from "viem/chains";
import { Chain } from "../types";
import { berachainTestnetbArtio as beraViem } from "viem/chains";

export const berachainTestnet: Chain = {
...berachainTestnetViem,
export const bera: Chain = {
...beraViem,
network: "bera",
logo: "https://raw.githubusercontent.com/helix-bridge/helix-ui/main/packages/assets/images/chains/bera.png",
};
7 changes: 5 additions & 2 deletions packages/chains/src/chains/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export * from "./astar-zkevm";
export * from "./avalanche";
export * from "./base";
export * from "./base-sepolia";
export * from "./berachain-testnet";
export * from "./bera";
export * from "./blast";
export * from "./bsc";
export * from "./crab";
Expand All @@ -14,7 +14,8 @@ export * from "./gnosis";
export * from "./linea";
export * from "./mantle";
export * from "./moonbeam";
export * from "./morph-sepolia";
export * from "./morph";
export * from "./morph-testnet";
export * from "./optimism";
export * from "./polygon";
export * from "./polygon-zkevm";
Expand All @@ -23,3 +24,5 @@ export * from "./sepolia";
export * from "./taiko-hekla";
export * from "./zksync";
export * from "./zksync-sepolia";
export * from "./zircuit";
export * from "./zircuit-sepolia";
8 changes: 8 additions & 0 deletions packages/chains/src/chains/morph-testnet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { morphHolesky } from "viem/chains";
import { Chain } from "../types";

export const morphTestnet: Chain = {
...morphHolesky,
network: "morph-testnet",
logo: "https://raw.githubusercontent.com/helix-bridge/helix-ui/main/packages/assets/images/chains/morph.png",
};
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { morphSepolia as morphSepoliaViem } from "viem/chains";
import { Chain } from "../types";
import { morph as morphViem } from "viem/chains";

export const morphSepolia: Chain = {
...morphSepoliaViem,
export const morph: Chain = {
...morphViem,
network: "morph",
logo: "https://raw.githubusercontent.com/helix-bridge/helix-ui/main/packages/assets/images/chains/morph.png",
};
9 changes: 9 additions & 0 deletions packages/chains/src/chains/zircuit-sepolia.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Chain } from "../types";

import { zircuitTestnet } from "viem/chains";

export const zircuitSepolia: Chain = {
...zircuitTestnet,
network: "zircuit-sepolia",
logo: "https://raw.githubusercontent.com/helix-bridge/helix-ui/main/packages/assets/images/chains/zircuit.png",
};
9 changes: 9 additions & 0 deletions packages/chains/src/chains/zircuit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Chain } from "../types";

import { zircuit as zircuitViem } from "viem/chains";

export const zircuit: Chain = {
...zircuitViem,
network: "zircuit",
logo: "https://raw.githubusercontent.com/helix-bridge/helix-ui/main/packages/assets/images/chains/zircuit.png",
};
12 changes: 9 additions & 3 deletions packages/chains/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ export enum ChainID {
OPTIMISM = 10,
GNOSIS = 100,
BLAST = 81457,
BERA = 80085,
BERA = 80084,
TAIKO_HEKLA = 167009,
ASTAR_ZKEVM = 3776,
MORPH = 2710,
MORPH = 2818,
MORPH_TESTNET = 2810,
MOONBEAM = 1284,
AVALANCHE = 43_114,
ZIRCUIT_SEPOLIA = 48899,
ZIRCUIT = 48900,
}
/* eslint-enable no-unused-vars */

Expand Down Expand Up @@ -59,9 +62,12 @@ export type Network =
| "bera"
| "taiko-hekla"
| "morph"
| "morph-testnet"
| "moonbeam"
| "avalanche"
| "bsc";
| "bsc"
| "zircuit"
| "zircuit-sepolia";

export interface Chain extends ViemChain {
id: ChainID;
Expand Down
29 changes: 15 additions & 14 deletions packages/chains/src/utils/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import { HelixChain } from "@helixbridge/helixconf";
import { getChainByIdOrNetwork, getChains } from ".";
import { ChainID } from "../types";
import { Network } from "../types";

const numberOfChains = Object.keys(ChainID).length / 2;
const chains = getChains();

test(`The number of chains should be equal to ${numberOfChains}`, () => {
expect(chains.length).toBe(numberOfChains);
test("An equal number of chains should be configured", () => {
expect(getChains().length).toBe(HelixChain.chains().length);
});

describe.each(getChains())("$name", ({ id, network }) => {
test(`The 'network' should be configured correctly`, () => {
expect(network).toBe(HelixChain.get(id)?.code);
});

describe.each(HelixChain.chains().map(({ id, code, name }) => ({ id, code, name })))("$name", ({ id, code }) => {
test(`getChainByIdOrNetwork(${id}) should return the correct chain`, () => {
expect(getChainByIdOrNetwork(id)?.network).toBe(network);
expect(getChainByIdOrNetwork(Number(id))?.network).toBe(code);
});
test(`getChainByIdOrNetwork('${network}') should return the correct chain`, () => {
expect(getChainByIdOrNetwork(network)?.id).toBe(id);
test(`getChainByIdOrNetwork('${code}') should return the correct chain`, () => {
expect(getChainByIdOrNetwork(code as Network)?.id).toBe(Number(id));
});

const logo = getChainByIdOrNetwork(Number(id))?.logo;
if (logo) {
test(`${logo} should be available`, async () => {
const res = await fetch(logo);
expect(res.status).not.toBe(404);
});
}
});
159 changes: 95 additions & 64 deletions packages/chains/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
avalanche,
base,
baseSepolia,
berachainTestnet,
bera,
blast,
bsc,
crab,
Expand All @@ -15,17 +15,20 @@ import {
linea,
mantle,
moonbeam,
morphSepolia,
morph,
morphTestnet,
optimism,
polygon,
polygonZkEvm,
scroll,
sepolia,
taikoHekla,
zircuit,
zircuitSepolia,
zkSync,
zksyncSepolia,
} from "../chains";
import { type Chain, ChainID, type Network } from "../types";
import { ChainID, type Network } from "../types";

export function getChains() {
return [
Expand All @@ -35,7 +38,7 @@ export function getChains() {
avalanche,
baseSepolia,
base,
berachainTestnet,
bera,
blast,
bsc,
crab,
Expand All @@ -45,7 +48,7 @@ export function getChains() {
linea,
mantle,
moonbeam,
morphSepolia,
morphTestnet,
optimism,
polygonZkEvm,
polygon,
Expand All @@ -54,67 +57,95 @@ export function getChains() {
taikoHekla,
zksyncSepolia,
zkSync,
zircuit,
zircuitSepolia,
];
}

const CHAIN_ID_2_CHAIN: Record<ChainID, Chain> = {
[ChainID.ARBITRUM_SEPOLIA]: arbitrumSepolia,
[ChainID.ARBITRUM]: arbitrum,
[ChainID.ASTAR_ZKEVM]: astarZkEVM,
[ChainID.AVALANCHE]: avalanche,
[ChainID.BASE_SEPOLIA]: baseSepolia,
[ChainID.BASE]: base,
[ChainID.BERA]: berachainTestnet,
[ChainID.BLAST]: blast,
[ChainID.BSC]: bsc,
[ChainID.CRAB]: crab,
[ChainID.DARWINIA]: darwinia,
[ChainID.ETHEREUM]: ethereum,
[ChainID.GNOSIS]: gnosis,
[ChainID.LINEA]: linea,
[ChainID.MANTLE]: mantle,
[ChainID.MOONBEAM]: moonbeam,
[ChainID.MORPH]: morphSepolia,
[ChainID.OPTIMISM]: optimism,
[ChainID.POLYGON_ZKEVM]: polygonZkEvm,
[ChainID.POLYGON]: polygon,
[ChainID.SCROLL]: scroll,
[ChainID.SEPOLIA]: sepolia,
[ChainID.TAIKO_HEKLA]: taikoHekla,
[ChainID.ZKSYNC_SEPOLIA]: zksyncSepolia,
[ChainID.ZKSYNC]: zkSync,
};

const NETWORK_2_CHAIN: Record<Network, Chain> = {
"arbitrum-sepolia": arbitrumSepolia,
arbitrum: arbitrum,
"astar-zkevm": astarZkEVM,
avalanche: avalanche,
"base-sepolia": baseSepolia,
base: base,
bera: berachainTestnet,
blast: blast,
bsc: bsc,
"crab-dvm": crab,
"darwinia-dvm": darwinia,
ethereum: ethereum,
gnosis: gnosis,
linea: linea,
mantle: mantle,
moonbeam: moonbeam,
morph: morphSepolia,
op: optimism,
"polygon-zkEvm": polygonZkEvm,
polygon: polygon,
scroll: scroll,
sepolia: sepolia,
"taiko-hekla": taikoHekla,
"zksync-sepolia": zksyncSepolia,
zksync: zkSync,
};

export function getChainByIdOrNetwork(chainIdOrNetwork: ChainID | Network | null | undefined) {
return chainIdOrNetwork
? CHAIN_ID_2_CHAIN[chainIdOrNetwork as ChainID] ?? NETWORK_2_CHAIN[chainIdOrNetwork as Network]
: undefined;
switch (chainIdOrNetwork) {
case ChainID.ARBITRUM:
case "arbitrum":
return arbitrum;
case ChainID.ARBITRUM_SEPOLIA:
case "arbitrum-sepolia":
return arbitrumSepolia;
case ChainID.ASTAR_ZKEVM:
case "astar-zkevm":
return astarZkEVM;
case ChainID.AVALANCHE:
case "avalanche":
return avalanche;
case ChainID.BASE:
case "base":
return base;
case ChainID.BASE_SEPOLIA:
case "base-sepolia":
return baseSepolia;
case ChainID.BERA:
case "bera":
return bera;
case ChainID.BLAST:
case "blast":
return blast;
case ChainID.BSC:
case "bsc":
return bsc;
case ChainID.CRAB:
case "crab-dvm":
return crab;
case ChainID.DARWINIA:
case "darwinia-dvm":
return darwinia;
case ChainID.ETHEREUM:
case "ethereum":
return ethereum;
case ChainID.GNOSIS:
case "gnosis":
return gnosis;
case ChainID.LINEA:
case "linea":
return linea;
case ChainID.MANTLE:
case "mantle":
return mantle;
case ChainID.MOONBEAM:
case "moonbeam":
return moonbeam;
case ChainID.MORPH:
case "morph":
return morph;
case ChainID.MORPH_TESTNET:
case "morph-testnet":
return morphTestnet;
case ChainID.OPTIMISM:
case "op":
return optimism;
case ChainID.POLYGON:
case "polygon":
return polygon;
case ChainID.SCROLL:
case "scroll":
return scroll;
case ChainID.SEPOLIA:
case "sepolia":
return sepolia;
case ChainID.TAIKO_HEKLA:
case "taiko-hekla":
return taikoHekla;
case ChainID.ZKSYNC:
case "zksync":
return zkSync;
case ChainID.ZKSYNC_SEPOLIA:
case "zksync-sepolia":
return zksyncSepolia;
case ChainID.ZIRCUIT:
case "zircuit":
return zircuit;
case ChainID.ZIRCUIT_SEPOLIA:
case "zircuit-sepolia":
return zircuitSepolia;
default:
return;
}
}
Loading

0 comments on commit 91dd052

Please sign in to comment.