Skip to content

Commit

Permalink
Encoding parameters instead of manual checking
Browse files Browse the repository at this point in the history
  • Loading branch information
KyrylR committed Feb 8, 2024
1 parent bb74e64 commit b9611de
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 23 deletions.
13 changes: 4 additions & 9 deletions src/deployer/MinimalContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,7 @@ import { isFullyQualifiedName } from "hardhat/utils/contract-names";

import { Linker } from "./Linker";

import {
catchError,
checkType,
fillParameters,
getChainId,
getInterfaceOnlyWithConstructor,
getSignerHelper,
} from "../utils";
import { catchError, fillParameters, getChainId, getInterfaceOnlyWithConstructor, getSignerHelper } from "../utils";

import { MigrateError } from "../errors";

Expand Down Expand Up @@ -81,8 +74,10 @@ export class MinimalContract {
private async _createDeployTransaction(args: any[], txOverrides: Overrides): Promise<ContractDeployTxWithName> {
const factory = new ethers.ContractFactory(this._interface, this._bytecode);

const coder = new ethers.AbiCoder();
factory.interface.deploy.inputs.forEach((input, idx) => {
checkType(args[idx], input.type, input.name);
// Try to encode the arguments before sending the deployment transaction
coder.encode([input], [args[idx]]);
});

return {
Expand Down
13 changes: 0 additions & 13 deletions src/utils/common.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { join } from "path";
import { realpathSync, existsSync } from "fs";

import { MigrateError } from "../errors";
import { UNKNOWN_CONTRACT_NAME } from "../constants";

export function resolvePathToFile(path: string, file: string = ""): string {
Expand All @@ -18,18 +17,6 @@ export function getInstanceNameFromClass(instance: any): string {
return className === undefined ? UNKNOWN_CONTRACT_NAME : className.replace("__factory", "");
}

export function checkType(value: any, type: string, name: string): void {
const types = type.split("|").map((t) => t.trim());

if (types.some((t) => t === "any" || typeof value === t)) {
return;
}

throw new MigrateError(
`Invalid value for "${name}". Expected type: ${type}. Received value: ${value} of type ${typeof value}.`,
);
}

function parseClassName(classDefinitionString: string) {
// Regular expression to match the class name
const classNameRegex = /class\s+([^\s]+)\s*\{/;
Expand Down
2 changes: 1 addition & 1 deletion test/integration/deployer/base-contract-interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe("deployer", () => {
const signer = (await deployer.getSigner()) as any;

await expect(deployer.deploy(ConstructorWithArguments__factory, [signer], {})).to.be.rejectedWith(
`Deployer.deploy(): MinimalContract.deploy(): MinimalContract._createDeployTransaction(): Invalid value for "_value". Expected type: uint256. Received value: [object Object] of type object.`,
`Deployer.deploy(): MinimalContract.deploy(): MinimalContract._createDeployTransaction(): invalid BigNumberish value (argument="value", value="<SignerWithAddress 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266>", code=INVALID_ARGUMENT, version=6.10.0)`,
);
});

Expand Down

0 comments on commit b9611de

Please sign in to comment.