Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 25 additions & 26 deletions packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@ import { Artifact, HardhatRuntimeEnvironment } from "hardhat/types";
import { Config, Domain } from "@buildwithsygma/sygma-sdk-core";
import { HardhatPluginError } from "hardhat/plugins";
import Web3, {
ContractConstructorArgs,
ContractAbi,
MatchPrimitiveType,
Transaction,
Bytes,
utils,
PayableCallOptions,
NonPayableCallOptions,
} from "web3";
import {
getConfigEnvironmentVariable,
Expand All @@ -19,11 +16,12 @@ import {
sumedFees,
} from "./utils";
import { AdapterABI } from "./adapterABI";
import { DeployOptions, NetworkArguments } from "./types";

export class MultichainHardhatRuntimeEnvironmentField {
private isValidated: boolean = false;
private domains: Domain[] = [];
private web3: Web3 | null;
private readonly web3: Web3 | null;

public constructor(private readonly hre: HardhatRuntimeEnvironment) {
const provider = this.hre.network.provider;
Expand Down Expand Up @@ -97,24 +95,28 @@ export class MultichainHardhatRuntimeEnvironmentField {
*/
public async deployMultichain<Abi extends ContractAbi = any>(
contractName: string,
networkArgs: Record<
string,
{
args: ContractConstructorArgs<Abi>;
initData?: string;
}
>,
options?: {
salt?: MatchPrimitiveType<"bytes32", unknown>;
isUniquePerChain?: boolean;
customNonPayableTxOptions?: NonPayableCallOptions;
}
networkArgs: NetworkArguments<Abi>,
options?: DeployOptions
): Promise<Transaction | void> {
const artifact = this.hre.artifacts.readArtifactSync(contractName);

return this.deployMultichainBytecode(
artifact.bytecode,
artifact.abi as unknown as Abi,
networkArgs,
options
);
}

public async deployMultichainBytecode<Abi extends ContractAbi = any>(
contractBytecode: string,
contractAbi: Abi,
networkArgs: NetworkArguments<Abi>,
options?: DeployOptions
): Promise<Transaction | void> {
if (!this.isValidated) await this.validateConfig();
if (!this.web3) return;

const artifact = this.hre.artifacts.readArtifactSync(contractName);

//optional params
const salt = options?.salt ?? utils.randomBytes(32);
const isUniquePerChain = options?.isUniquePerChain ?? false;
Expand All @@ -126,16 +128,15 @@ export class MultichainHardhatRuntimeEnvironmentField {
);

const { constructorArgs, initDatas, deployDomainIDs } = mapNetworkArgs(
artifact,
contractBytecode,
contractAbi,
networkArgs,
this.domains
);

const deployBytecode = artifact.bytecode;

const fees = await adapterContract.methods
.calculateDeployFee(
deployBytecode,
contractBytecode,
this.gasLimit,
salt,
isUniquePerChain,
Expand All @@ -154,9 +155,9 @@ export class MultichainHardhatRuntimeEnvironmentField {
};
}

const tx = await adapterContract.methods
return adapterContract.methods
.deploy(
deployBytecode,
contractBytecode,
this.gasLimit,
salt,
isUniquePerChain,
Expand All @@ -166,7 +167,5 @@ export class MultichainHardhatRuntimeEnvironmentField {
fees
)
.send(payableTxOptions);

return tx;
}
}
19 changes: 19 additions & 0 deletions packages/plugin/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {
ContractAbi,
ContractConstructorArgs,
MatchPrimitiveType,
NonPayableCallOptions,
} from "web3";

export interface NetworkArguments<Abi extends ContractAbi = any> {
[network: string]: {
args: ContractConstructorArgs<Abi>;
initData?: string;
};
}

export interface DeployOptions {
salt?: MatchPrimitiveType<"bytes32", unknown>;
isUniquePerChain?: boolean;
customNonPayableTxOptions?: NonPayableCallOptions;
}
13 changes: 6 additions & 7 deletions packages/plugin/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from "assert";
import { Artifact, HardhatRuntimeEnvironment } from "hardhat/types";
import { HardhatRuntimeEnvironment } from "hardhat/types";
import { Domain, Environment } from "@buildwithsygma/sygma-sdk-core";
import {
Bytes,
Expand Down Expand Up @@ -56,7 +56,8 @@ export function sumedFees(fees: Numbers[]): string {
}

export function mapNetworkArgs<Abi extends ContractAbi = any>(
artifact: Artifact,
contractBytecode: string,
contractAbi: Abi,
networkArgs: Record<
string,
{
Expand All @@ -71,7 +72,7 @@ export function mapNetworkArgs<Abi extends ContractAbi = any>(
initDatas: Bytes[];
} {
const { bytesToHex, hexToBytes } = utils;
const contract = new Contract(artifact.abi);
const contract = new Contract(contractAbi);

const deployDomainIDs: bigint[] = [];
const constructorArgs: string[] = [];
Expand All @@ -92,15 +93,13 @@ export function mapNetworkArgs<Abi extends ContractAbi = any>(

const encodedDeployMethod = contract
.deploy({
data: artifact.bytecode,
data: contractBytecode,
arguments: networkArgs[networkName].args,
})
.encodeABI();

const argsInBytes = bytesToHex(
hexToBytes(encodedDeployMethod).slice(
hexToBytes(artifact.bytecode).length
)
hexToBytes(encodedDeployMethod).slice(hexToBytes(contractBytecode).length)
);

constructorArgs.push(argsInBytes);
Expand Down