Skip to content

Commit

Permalink
chore: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
npty committed Feb 12, 2024
1 parent 584be9b commit 6fe4438
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 48 deletions.
61 changes: 13 additions & 48 deletions packages/axelar-local-dev/src/exportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,17 @@

import { ethers } from 'ethers';
import { setJSON } from './utils';
import { Network, NetworkOptions, networks } from './Network';
import { RelayData, RelayerMap, relay } from './relay';
import { createNetwork, forkNetwork, getNetwork, listen, setupNetwork, stopAll } from './networkUtils';
import { networks } from './Network';
import { RelayerMap, relay } from './relay';
import { createNetwork, forkNetwork, listen, setupNetwork, stopAll } from './networkUtils';
import { testnetInfo } from './info';
import { EvmRelayer } from './relay/EvmRelayer';
import { getChainArray } from '@axelar-network/axelar-chains-config';
import { registerRemoteITS } from './its';
import { CloneLocalOptions, CreateLocalOptions, SetupLocalOptions } from './types';

let interval: any;

export interface CreateLocalOptions {
chainOutputPath?: string;
accountsToFund?: string[];
fundAmount?: string;
chains?: string[];
relayInterval?: number;
port?: number;
relayers?: RelayerMap;
afterRelay?: (relayData: RelayData) => void;
callback?: (network: Network, info: any) => Promise<void>;
}

export interface SetupLocalOptions {
rpcUrls: string[];
chains: string[];
seed?: string;
relayInterval?: number;
afterRelay?: (relayData: RelayData) => void;
callback?: (network: Network, info: any) => Promise<void>;
chainOutputPath?: string;
}

export interface CloneLocalOptions {
chainOutputPath?: string;
accountsToFund?: string[];
fundAmount?: string;
env?: string | any;
chains?: string[];
relayInterval?: number;
port?: number;
networkOptions?: NetworkOptions;
relayers?: RelayerMap;
afterRelay?: (relayData: RelayData) => void;
callback?: (network: Network, info: any) => Promise<null>;
}

const defaultEvmRelayer = new EvmRelayer();

let relaying = false;
Expand Down Expand Up @@ -123,7 +88,7 @@ export async function createAndExport(options: CreateLocalOptions = {}) {
}

export async function setupAndExport(options: SetupLocalOptions) {
const { afterRelay, callback, rpcUrls, chainOutputPath, chains, relayInterval, seed } = options;
const { afterRelay, callback, chainOutputPath, chains, relayInterval, seed } = options;

if (chains.length < 2) {
console.log('At least 2 chains are required to setup and export');
Expand All @@ -140,17 +105,17 @@ export async function setupAndExport(options: SetupLocalOptions) {
};

const localChains: Record<string, any>[] = [];
for (let i = 0; i < rpcUrls.length; i++) {
const chain = await setupNetwork(rpcUrls[i], { seed, name: chains[i] });
for (let i = 0; i < chains.length; i++) {
const network = await setupNetwork(chains[i].rpcUrl, { seed, name: chains[i].name });

const info = chain.getCloneInfo() as any;
info.rpc = rpcUrls[i];
localChains.push(info);
const networkInfo = network.getCloneInfo() as any;
networkInfo.rpc = chains[i].rpcUrl;
localChains.push(networkInfo);

if (_options.callback) await _options.callback(chain, info);
if (Object.keys(chain.tokens).length > 0) {
if (_options.callback) await _options.callback(network, networkInfo);
if (Object.keys(network.tokens).length > 0) {
// Check if there is a USDC token.
const alias = Object.keys(chain.tokens).find((alias) => alias.toLowerCase().includes('usdc'));
const alias = Object.keys(network.tokens).find((alias) => alias.toLowerCase().includes('usdc'));

// If there is no USDC token, return.
if (!alias) return;
Expand Down
42 changes: 42 additions & 0 deletions packages/axelar-local-dev/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Network, NetworkOptions } from './Network';
import { RelayData, RelayerMap } from './relay';

export interface CreateLocalOptions {
chainOutputPath?: string;
accountsToFund?: string[];
fundAmount?: string;
chains?: string[];
relayInterval?: number;
port?: number;
relayers?: RelayerMap;
afterRelay?: (relayData: RelayData) => void;
callback?: (network: Network, info: any) => Promise<void>;
}

export type SetupChain = {
rpcUrl: string;
name: string;
};

export interface SetupLocalOptions {
chains: SetupChain[];
seed?: string;
relayInterval?: number;
afterRelay?: (relayData: RelayData) => void;
callback?: (network: Network, info: any) => Promise<void>;
chainOutputPath?: string;
}

export interface CloneLocalOptions {
chainOutputPath?: string;
accountsToFund?: string[];
fundAmount?: string;
env?: string | any;
chains?: string[];
relayInterval?: number;
port?: number;
networkOptions?: NetworkOptions;
relayers?: RelayerMap;
afterRelay?: (relayData: RelayData) => void;
callback?: (network: Network, info: any) => Promise<null>;
}

0 comments on commit 6fe4438

Please sign in to comment.