Skip to content
This repository was archived by the owner on Jun 16, 2025. It is now read-only.

Commit 80d2ea5

Browse files
environment setup utils
1 parent 61faa77 commit 80d2ea5

File tree

10 files changed

+188
-0
lines changed

10 files changed

+188
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"guardianSetIndex": 3,
3+
"evm": {
4+
"operatingChains": [],
5+
"networks": [
6+
{
7+
"description": "Ethereum",
8+
"externalId": 1,
9+
"chainId": 2,
10+
"rpc": "https://eth.llamarpc.com"
11+
},
12+
{
13+
"description": "Arbitrum",
14+
"externalId": 42161,
15+
"chainId": 23,
16+
"rpc": "https://arbitrum.llamarpc.com"
17+
},
18+
{
19+
"description": "Optimism",
20+
"externalId": 10,
21+
"chainId": 24,
22+
"rpc": "https://optimism-rpc.publicnode.com"
23+
},
24+
{
25+
"description": "Base",
26+
"externalId": 8453,
27+
"chainId": 30,
28+
"rpc": "https://mainnet.base.org"
29+
}
30+
]
31+
},
32+
"solana": {
33+
"networks": [
34+
{
35+
"description": "Solana",
36+
"chainId": 1,
37+
"rpc": "https://solana.llamarpc.com"
38+
}
39+
]
40+
}
41+
}

deployment/config/testnet/contracts.json

Whitespace-only changes.

deployment/config/testnet/matching-engine.json

Whitespace-only changes.

deployment/config/testnet/token-router.json

Whitespace-only changes.

deployment/helpers/env.ts

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import fs from "fs";
2+
import { ChainId } from "@certusone/wormhole-sdk";
3+
4+
/**
5+
* Types:
6+
*/
7+
export type ChainInfo = {
8+
name: string;
9+
chainId: ChainId;
10+
rpc: string;
11+
externalId?: string;
12+
};
13+
14+
export type Deployment = {
15+
chainId: ChainId;
16+
address: string;
17+
};
18+
19+
export type Ecosystem = {
20+
guardianSetIndex: number;
21+
evm: {
22+
operatingChains?: number[];
23+
networks: ChainInfo[];
24+
},
25+
solana: {
26+
networks: ChainInfo[];
27+
}
28+
};
29+
30+
export type ContractsJson = Record<string, Deployment[]>;
31+
32+
interface ChainConfig {
33+
chainId: ChainId;
34+
}
35+
36+
export const env = getEnv("ENV");
37+
export const contracts = loadContracts();
38+
export const ecosystemChains = loadEcosystem();
39+
40+
function loadJson<T>(filename: string): T {
41+
const fileContent = fs.readFileSync(
42+
`./config/${env}/${filename}.json`
43+
);
44+
45+
return JSON.parse(fileContent.toString()) as T;
46+
}
47+
48+
function loadContracts<T extends ContractsJson>() {
49+
return loadJson<T>("contracts");
50+
}
51+
52+
function loadEcosystem(): Ecosystem {
53+
return loadJson<Ecosystem>("ecosystem");
54+
}
55+
56+
export function getEnv(env: string): string {
57+
const v = process.env[env];
58+
if (!v) {
59+
throw Error(`Env var not set: ${env}`);
60+
}
61+
return v;
62+
}
63+
64+
export async function getChainConfig<T extends ChainConfig>(filename: string, chainId: ChainId): Promise<T> {
65+
const scriptConfig: T[] = await loadJson(filename);
66+
67+
const chainConfig = scriptConfig.find((x) => x.chainId == chainId);
68+
69+
if (!chainConfig) {
70+
throw Error(`Failed to find chain config for chain ${chainId}`);
71+
}
72+
73+
return chainConfig;
74+
}
75+
76+
export async function getContractAddress(contractName: string, chainId: ChainId): Promise<string> {
77+
const contract = contracts[contractName]?.find((c) => c.chainId === chainId)?.address;
78+
79+
if (!contract) {
80+
throw new Error(`No ${contractName} contract found for chain ${chainId}`);
81+
}
82+
83+
return contract;
84+
}
85+
86+
export function writeDeployedContract(chain: ChainId, contractName: string, address: string) {
87+
const contracts = loadContracts();
88+
if (!contracts[contractName]) {
89+
contracts[contractName] = [];
90+
}
91+
contracts[contractName].push({ chainId: chain, address: process.env[contractName]! });
92+
fs.writeFileSync(
93+
`./config/${env}/contracts.json`,
94+
JSON.stringify(contracts),
95+
{ flag: "w" }
96+
);
97+
}

deployment/helpers/evm.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { ecosystemChains } from "./index";
2+
3+
export function evmOperatingChains() {
4+
const { operatingChains } = ecosystemChains.evm;
5+
if (Array.isArray(operatingChains) && operatingChains.length >= 1) {
6+
return ecosystemChains.evm.networks.filter((x) => {
7+
return operatingChains.includes(x.chainId);
8+
});
9+
}
10+
return ecosystemChains.evm.networks;
11+
};
12+
13+
14+
// export async function getSigner(chain: ChainInfo): Promise<ethers.Signer> {
15+
// const provider = getProvider(chain);
16+
// const privateKey = loadPrivateKey();
17+
18+
// if (privateKey === "ledger") {
19+
// if (process.env.LEDGER_BIP32_PATH === undefined) {
20+
// throw new Error(`Missing BIP32 derivation path.
21+
// With ledger devices the path needs to be specified in env var 'LEDGER_BIP32_PATH'.`);
22+
// }
23+
// const { LedgerSigner } = await import("@xlabs-xyz/ledger-signer");
24+
// return LedgerSigner.create(provider, process.env.LEDGER_BIP32_PATH);
25+
// }
26+
27+
// const signer = new ethers.Wallet(privateKey, provider);
28+
// return signer;
29+
// }
30+
31+
// export function getProvider(
32+
// chain: ChainInfo
33+
// ): ethers.providers.StaticJsonRpcProvider {
34+
// const providerRpc = loadChains().find((x: any) => x.chainId == chain.chainId)?.rpc || "";
35+
36+
// if (!providerRpc) {
37+
// throw new Error("Failed to find a provider RPC for chain " + chain.chainId);
38+
// }
39+
40+
// let provider = new ethers.providers.StaticJsonRpcProvider(
41+
// providerRpc,
42+
// );
43+
44+
// return provider;
45+
// }
46+

deployment/helpers/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from "./env";
2+
export * from "./evm";
3+
export * from "./solana";

deployment/helpers/solana.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const one = 1;

deployment/scripts/evm/deploy-matching-engine.ts

Whitespace-only changes.

deployment/scripts/evm/deploy-token-router.ts

Whitespace-only changes.

0 commit comments

Comments
 (0)