Skip to content

Commit

Permalink
encodeInitData as part of deploy function
Browse files Browse the repository at this point in the history
  • Loading branch information
irubido committed Jan 24, 2024
1 parent 52e87fe commit 65a3aa0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 30 deletions.
29 changes: 2 additions & 27 deletions packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import { Artifact, HardhatRuntimeEnvironment } from "hardhat/types";
import { HardhatRuntimeEnvironment } from "hardhat/types";
import { Config, Domain } from "@buildwithsygma/sygma-sdk-core";
import Web3, {
ContractAbi,
Transaction,
Bytes,
utils,
PayableCallOptions,
ContractMethodInputParameters,
AbiFunctionFragment,
} from "web3";
import { vars } from "hardhat/config";
import { HardhatPluginError } from "hardhat/internal/core/errors";
import {
getConfigEnvironmentVariable,
getNetworkChainId,
Expand Down Expand Up @@ -53,32 +49,11 @@ export class MultichainHardhatRuntimeEnvironmentField {
this.isValidated;
}

public encodeInitData<Abi extends ContractAbi = any>(
artifact: Artifact,
initMethodName: string,
initMethodArgs: ContractMethodInputParameters<Abi>
): Bytes {
const initMethodAbiFragment: AbiFunctionFragment | undefined =
artifact.abi.find(
(fragment: AbiFunctionFragment) => fragment.name === initMethodName
) as AbiFunctionFragment | undefined;
if (!initMethodAbiFragment)
throw new HardhatPluginError(
"@chainsafe/hardhat-plugin-multichain-deploy",
`InitMethod ${initMethodName} not foud in ABI`
);

return this.web3!.eth.abi.encodeFunctionCall(
initMethodAbiFragment,
initMethodArgs
);
}

/**
* @param contractName name of the contract
* @param networkArgs record key is name of the networks on which contract is being deployed
* @param args contract contructor args
* @param initData optional encoded initilize method, can be encoded with encodeInitData
* @param initData optional init method details, method name and args required
* @param salt optional or generated by default from randombytes(32)
* @param isUniquePerChain optional
* @param customNonPayableTxOptions non payable options for web3 deploy.method.send(), payable summed fees are always calculated by the method
Expand Down
6 changes: 5 additions & 1 deletion packages/plugin/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import {
ContractAbi,
ContractConstructorArgs,
ContractMethodInputParameters,
MatchPrimitiveType,
NonPayableCallOptions,
} from "web3";

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

Expand Down
30 changes: 28 additions & 2 deletions packages/plugin/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import assert from "assert";
import { HardhatRuntimeEnvironment } from "hardhat/types";
import { Domain, Environment } from "@buildwithsygma/sygma-sdk-core";
import {
AbiFallbackFragment,
Bytes,
ContractAbi,
ContractMethodInputParameters,
FMT_BYTES,
FMT_NUMBER,
HttpProvider,
Expand Down Expand Up @@ -49,6 +51,23 @@ export function sumedFees(fees: Numbers[]): string {
return sumOfFees.toString();
}

export function encodeInitData<Abi extends ContractAbi>(
abi: Abi,
initMethodName: string,
initMethodArgs: ContractMethodInputParameters<Abi>
): Bytes {
const initMethodAbiFragment = (
abi as unknown as Array<AbiFallbackFragment>
).find((fragment) => fragment.name === initMethodName);
if (!initMethodAbiFragment)
throw new HardhatPluginError(
"@chainsafe/hardhat-plugin-multichain-deploy",
`InitMethod ${initMethodName} not foud in ABI`
);

return eth.abi.encodeFunctionCall(initMethodAbiFragment, initMethodArgs);
}

export function mapNetworkArgs<Abi extends ContractAbi = any>(
contractAbi: Abi,
networkArgs: NetworkArguments<Abi>,
Expand Down Expand Up @@ -111,9 +130,16 @@ export function mapNetworkArgs<Abi extends ContractAbi = any>(
//no constructorAbi and no args
constructorArgs.push("0x");
}
const networkInitData = networkArgs[networkName].initData;
if (networkInitData !== undefined) {
const { initMethodName, initMethodArgs } = networkInitData;
const initData = encodeInitData(
contractAbi,
initMethodName,
initMethodArgs
);

if (networkArgs[networkName].initData) {
initDatas.push(networkArgs[networkName].initData as Bytes);
initDatas.push(initData);
} else {
initDatas.push(hexToBytes("0x"));
}
Expand Down

0 comments on commit 65a3aa0

Please sign in to comment.