generated from ChainSafe/yarn-workspaces-typescript-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: tool interfaces aka fasad (#10)
* fix mistakes * small config validation and refactor * fixes and improvements * add sygma sdk package * fix lock file * implement environment config * implement initialization validation * implement deployMultichain mock * test and small fixes * `yarn.lock` fix? * with `yarn` `--immutable`? * basic readme * Readme with secret sauce * fix ethereumjs-abi ? * remove resolutions * return `resolutions`, updated `yarn.lock` * add `skipLibCheck` to `tsconfig.json` * refactor initialization * small typings improve on `deployMultichain` * lint fix * fixes * fix `yarn.lock` * improve README.md
- Loading branch information
Showing
14 changed files
with
4,698 additions
and
212 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
70 changes: 70 additions & 0 deletions
70
packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { HardhatRuntimeEnvironment } from "hardhat/types"; | ||
import { Config } from "@buildwithsygma/sygma-sdk-core"; | ||
import { HardhatPluginError } from "hardhat/plugins"; | ||
import { ContractConstructorArgs, ContractAbi } from "web3"; | ||
import { | ||
getConfigEnvironmentVariable, | ||
getDeploymentNetworks, | ||
getNetworkChainId, | ||
} from "./utils"; | ||
|
||
export class MultichainHardhatRuntimeEnvironmentField { | ||
private isValidated: boolean = false; | ||
|
||
public constructor(private readonly hre: HardhatRuntimeEnvironment) {} | ||
|
||
private async validateConfig(): Promise<void> { | ||
const originChainId = await getNetworkChainId( | ||
this.hre.network.name, | ||
this.hre | ||
); | ||
const environment = getConfigEnvironmentVariable(this.hre); | ||
|
||
const config = new Config(); | ||
await config.init(originChainId, environment); | ||
const domainChainIds = config.getDomains().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; | ||
} | ||
|
||
public async deployMultichain<Abi extends ContractAbi = any>( | ||
nameOrBytecode: string, | ||
args: ContractConstructorArgs<Abi>, | ||
options?: Object | ||
): Promise<string> { | ||
if (!this.isValidated) await this.validateConfig(); | ||
|
||
const bytcode = await this.hre.artifacts | ||
.readArtifact(nameOrBytecode) | ||
.then((artifact) => artifact.bytecode) | ||
.catch(() => nameOrBytecode); | ||
|
||
// temp to silence eslint | ||
console.log(args, options, bytcode); | ||
|
||
return "0x00"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,64 @@ | ||
import path from "path"; | ||
import { extendConfig, extendEnvironment } from "hardhat/config"; | ||
import { lazyObject } from "hardhat/plugins"; | ||
import { HardhatConfig, HardhatUserConfig } from "hardhat/types"; | ||
import { HardhatPluginError, lazyObject } from "hardhat/plugins"; | ||
import { | ||
HardhatConfig, | ||
HardhatUserConfig, | ||
MultichainConfig, | ||
} from "hardhat/types"; | ||
import { Environment } from "@buildwithsygma/sygma-sdk-core"; | ||
|
||
import { MultichainHardhatRuntimeEnvironmentField } from "./MultichainHardhatRuntimeEnvironmentField"; | ||
|
||
import { ExampleHardhatRuntimeEnvironmentField } from "./ExampleHardhatRuntimeEnvironmentField"; | ||
// This import is needed to let the TypeScript compiler know that it should include your type | ||
// extensions in your npm package's types file. | ||
import "./type-extensions"; | ||
|
||
extendConfig( | ||
(config: HardhatConfig, userConfig: Readonly<HardhatUserConfig>) => { | ||
// We apply our default config here. Any other kind of config resolution | ||
// or normalization should be placed here. | ||
// | ||
// `config` is the resolved config, which will be used during runtime and | ||
// you should modify. | ||
// `userConfig` is the config as provided by the user. You should not modify | ||
// it. | ||
// | ||
// If you extended the `HardhatConfig` type, you need to make sure that | ||
// executing this function ensures that the `config` object is in a valid | ||
// state for its type, including its extensions. For example, you may | ||
// need to apply a default value, like in this example. | ||
const userPath = userConfig.paths?.newPath; | ||
|
||
let newPath: string; | ||
if (userPath === undefined) { | ||
newPath = path.join(config.paths.root, "newPath"); | ||
} else { | ||
if (path.isAbsolute(userPath)) { | ||
newPath = userPath; | ||
} else { | ||
// We resolve relative paths starting from the project's root. | ||
// Please keep this convention to avoid confusion. | ||
newPath = path.normalize(path.join(config.paths.root, userPath)); | ||
} | ||
const multichainConfig = Object.assign({}, userConfig.multichain); | ||
|
||
if (!multichainConfig.environment) { | ||
console.warn( | ||
"Warning: Missing 'environment' setting. Defaulting to ", | ||
Environment.TESTNET | ||
); | ||
multichainConfig.environment = Environment.TESTNET; | ||
} | ||
|
||
config.paths.newPath = newPath; | ||
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; | ||
} | ||
); | ||
|
||
extendEnvironment((hre) => { | ||
// We add a field to the Hardhat Runtime Environment here. | ||
// We use lazyObject to avoid initializing things until they are actually | ||
// needed. | ||
hre.example = lazyObject(() => new ExampleHardhatRuntimeEnvironmentField()); | ||
hre.multichain = lazyObject( | ||
() => new MultichainHardhatRuntimeEnvironmentField(hre) | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,30 @@ | ||
// If your plugin extends types from another plugin, you should import the plugin here. | ||
|
||
// To extend one of Hardhat's types, you need to import the module where it has been defined, and redeclare it. | ||
import "hardhat/types/config"; | ||
import "hardhat/types/runtime"; | ||
|
||
import { ExampleHardhatRuntimeEnvironmentField } from "./ExampleHardhatRuntimeEnvironmentField"; | ||
import { Environment } from "@buildwithsygma/sygma-sdk-core"; | ||
import { MultichainHardhatRuntimeEnvironmentField } from "./MultichainHardhatRuntimeEnvironmentField"; | ||
|
||
declare module "hardhat/types/config" { | ||
// This is an example of an extension to one of the Hardhat config values. | ||
/** | ||
* Typings for config that can be used for using by hardhat. | ||
*/ | ||
|
||
export interface MultichainConfig { | ||
environment: Environment; | ||
deploymentNetworks: string[]; | ||
} | ||
|
||
// We extend the UserConfig type, which represents the config as written | ||
// by the users. Things are normally optional here. | ||
export interface ProjectPathsUserConfig { | ||
newPath?: string; | ||
export interface HardhatUserConfig { | ||
multichain?: Partial<MultichainConfig>; | ||
} | ||
|
||
// We also extend the Config type, which represents the configuration | ||
// after it has been resolved. This is the type used during the execution | ||
// of tasks, tests and scripts. | ||
// Normally, you don't want things to be optional here. As you can apply | ||
// default values using the extendConfig function. | ||
export interface ProjectPathsConfig { | ||
newPath: string; | ||
export interface HardhatConfig { | ||
multichain: MultichainConfig; | ||
} | ||
} | ||
|
||
declare module "hardhat/types/runtime" { | ||
// This is an example of an extension to the Hardhat Runtime Environment. | ||
// This new field will be available in tasks' actions, scripts, and tests. | ||
export interface HardhatRuntimeEnvironment { | ||
example: ExampleHardhatRuntimeEnvironmentField; | ||
multichain: MultichainHardhatRuntimeEnvironmentField; | ||
} | ||
} |
Oops, something went wrong.