Skip to content

Commit

Permalink
feat: rework networks input (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
irubido authored Jan 19, 2024
1 parent 2eff1ee commit 3caf657
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 71 deletions.
26 changes: 0 additions & 26 deletions packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Artifact, HardhatRuntimeEnvironment } from "hardhat/types";
import { Config, Domain } from "@buildwithsygma/sygma-sdk-core";
import { HardhatPluginError } from "hardhat/plugins";
import Web3, {
ContractAbi,
Transaction,
Expand All @@ -10,7 +9,6 @@ import Web3, {
} from "web3";
import {
getConfigEnvironmentVariable,
getDeploymentNetworks,
getNetworkChainId,
mapNetworkArgs,
sumedFees,
Expand Down Expand Up @@ -44,30 +42,6 @@ export class MultichainHardhatRuntimeEnvironmentField {
await config.init(originChainId, environment);

this.domains = config.getDomains();
const domainChainIds = this.domains.map(({ chainId }) => chainId);

const deploymentNetworks = getDeploymentNetworks(this.hre);
const deploymentNetworksInfo = await Promise.all(
deploymentNetworks.map(async (name) => {
const chainId = await getNetworkChainId(name, this.hre);
return { name, chainId };
})
);

const missedRoutes: typeof deploymentNetworksInfo = [];
deploymentNetworksInfo.forEach(({ chainId, name }) => {
if (!domainChainIds.includes(chainId))
missedRoutes.push({ chainId, name });
});
if (missedRoutes.length)
throw new HardhatPluginError(
"@chainsafe/hardhat-plugin-multichain-deploy",
`Unavailable Networks in Deployment: The following networks from 'deploymentNetworks' are not routed in Sygma for the '${environment}' environment: ${missedRoutes
.map(({ chainId, name }) => `${name}(${chainId})`)
.join(", ")
.replace(/, ([^,]*)$/, " and $1")}\n` +
`Please adjust your 'deploymentNetworks' to align with the supported routes in this environment. For details on supported networks, refer to the Sygma documentation.`
);

this.isValidated = true;
}
Expand Down
29 changes: 1 addition & 28 deletions packages/plugin/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { extendConfig, extendEnvironment } from "hardhat/config";
import { HardhatPluginError, lazyObject } from "hardhat/plugins";
import { lazyObject } from "hardhat/plugins";
import {
HardhatConfig,
HardhatUserConfig,
Expand All @@ -23,33 +23,6 @@ extendConfig(
multichainConfig.environment = Environment.TESTNET;
}

if (
!multichainConfig.deploymentNetworks ||
!multichainConfig.deploymentNetworks.length
) {
console.warn(
"Warning: Missing Deployment Networks - It appears that you have not provided the Deployment Networks. To avoid potential issues, it is recommended that you supply these values. If they are not provided, you will be required to enter them manually as parameters. Please ensure that the necessary information is included to facilitate a smoother process."
);
multichainConfig.deploymentNetworks = [];
}

/** Validates that all networks in 'deploymentNetworks' are defined in 'config.networks'. */
const missedNetworks: string[] = [];
const configNetworkKeys = Object.keys(config.networks);
multichainConfig.deploymentNetworks.forEach((networkName) => {
if (!configNetworkKeys.includes(networkName))
missedNetworks.push(networkName);
});
if (missedNetworks.length)
throw new HardhatPluginError(
"@chainsafe/hardhat-plugin-multichain-deploy",
`Missing Configuration for Deployment Networks: ${missedNetworks
.join(", ")
.replace(/, ([^,]*)$/, " and $1")}\n` +
`The above networks are listed in your 'deploymentNetworks' but they are not defined in 'config.networks'. ` +
`Please ensure each of these networks is properly configured in the 'config.networks' section of your configuration.`
);

config.multichain = multichainConfig as MultichainConfig;
}
);
Expand Down
1 change: 0 additions & 1 deletion packages/plugin/src/type-extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ declare module "hardhat/types/config" {

export interface MultichainConfig {
environment: Environment;
deploymentNetworks: string[];
}

export interface HardhatUserConfig {
Expand Down
15 changes: 7 additions & 8 deletions packages/plugin/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ export async function getNetworkChainId(
return chainID;
}

export function getDeploymentNetworks(
hre: HardhatRuntimeEnvironment
): string[] {
return hre.config.multichain.deploymentNetworks;
}

export function sumedFees(fees: Numbers[]): string {
const sumOfFees = fees.reduce(
(previous, current) => BigInt(previous) + BigInt(current),
Expand Down Expand Up @@ -79,15 +73,20 @@ export function mapNetworkArgs<Abi extends ContractAbi = any>(
const initDatas: Bytes[] = [];

Object.keys(networkArgs).map((networkName) => {
//checks if network args
//checks if destination networks name is valid
const matchingDomain = domains.find(
(domain) => domain.name === networkName
);
if (matchingDomain) deployDomainIDs.push(BigInt(matchingDomain.id));
else {
throw new HardhatPluginError(
"@chainsafe/hardhat-plugin-multichain-deploy",
`Unavailable Networks in networkArgs: The following network ${networkName} is not supported as destination network.`
`Unavailable Networks in networkArgs: The following network ${networkName} is not supported as destination network.
Available networks: ${domains
.map((domain): string => `${domain.name}`)
.join(", ")
.replace(/, ([^,]*)$/, "")}\n
`
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const config: HardhatUserConfig = {
},
multichain: {
environment: Environment.TESTNET,
deploymentNetworks: ["sepolia"],
},
};

Expand Down
7 changes: 0 additions & 7 deletions packages/plugin/test/project.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@ describe("Integration tests examples", function () {
describe("HardhatConfig extension", function () {
useEnvironment("hardhat-project");

it("Should add the multichain.deploymentNetworks to the config", function () {
assert.deepEqual(
this.hre.config.multichain.deploymentNetworks,
["sepolia"]
);
});

it("Should add the multichain.environment to the config", function () {
assert.deepEqual(
this.hre.config.multichain.environment,
Expand Down

0 comments on commit 3caf657

Please sign in to comment.