Skip to content

Commit

Permalink
feat: encodeInitData (#30)
Browse files Browse the repository at this point in the history
* encodeInitData

* encodeInitData as part of deploy function

* fix types

Signed-off-by: Marin Petrunic <[email protected]>

* args to unknown[]

---------

Signed-off-by: Marin Petrunic <[email protected]>
Co-authored-by: Marin Petrunic <[email protected]>
  • Loading branch information
irubido and mpetrunic authored Jan 25, 2024
1 parent 9c4b7b1 commit 50758ca
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
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

0 comments on commit 50758ca

Please sign in to comment.