Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: encodeInitData #30

Merged
merged 4 commits into from
Jan 25, 2024
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
19 changes: 3 additions & 16 deletions packages/plugin/src/MultichainHardhatRuntimeEnvironmentField.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
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,
} from "web3";
Expand Down Expand Up @@ -47,26 +46,14 @@ export class MultichainHardhatRuntimeEnvironmentField {

this.domains = config.getDomains();

this.isValidated = true;
}

public static encodeInitData(
artifact: Artifact,
initMethodName: string,
initMethodArgs: string[]
): Bytes {
//TODO
// const contract = new Contract(artifact.abi);
// const encodedInitMethod = contract.methods[initMethodName](initMethodArgs).encodeABI();
console.log(artifact, initMethodArgs, initMethodName);
return utils.hexToBytes("0x");
this.isValidated;
}

/**
* @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
7 changes: 6 additions & 1 deletion packages/plugin/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import {
ContractAbi,
ContractConstructorArgs,
ContractMethods,
MatchPrimitiveType,
NonPayableCallOptions,
} from "web3";

export interface NetworkArguments<Abi extends ContractAbi = any> {
[network: string]: {
args?: ContractConstructorArgs<Abi>;
initData?: string;
initData?: {
initMethodName: keyof ContractMethods<Abi>;
//impossible to type unless we do something like this.getInitMethod(artifact, methodName).encode(args);
initMethodArgs: unknown[];
};
};
}

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

export function encodeInitData<Abi extends ContractAbi>(
abi: Abi,
initMethodName: string,
initMethodArgs: unknown[]
): 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 +129,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
Loading