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

Add deployment arguments validation before the contract deployment #72

Closed
wants to merge 10 commits into from
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Version 2.1.2

* Added deployment arguments validation before the contract deployment.

## Version 2.1.1

* Fixed a bug related to the shallow copying and mutation of arguments within the `getMethodString` function.
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@solarity/hardhat-migrate",
"version": "2.1.1",
"version": "2.1.2",
"description": "Automatic deployment and verification of smart contracts",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
Expand Down
8 changes: 8 additions & 0 deletions src/deployer/MinimalContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ export class MinimalContract {
return;
}

const factory = new ethers.ContractFactory(this._interface, this._bytecode);

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

VerificationProcessor.saveVerificationFunction({
contractAddress,
contractName: contractName,
Expand Down
1 change: 1 addition & 0 deletions src/utils/common.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { join } from "path";
import { realpathSync, existsSync } from "fs";

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

export function resolvePathToFile(path: string, file: string = ""): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ contract PayableReceive {
receive() external payable {
emit Received(msg.sender, msg.value);
}

function pay() external payable {
emit Received(msg.sender, msg.value);
}
}
19 changes: 19 additions & 0 deletions test/integration/deployer/base-contract-interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useEnvironment } from "../../helpers";
import {
ConstructorWithArguments__factory,
PayableConstructor__factory,
PayableReceive__factory,
} from "../../fixture-projects/hardhat-project-typechain-ethers/typechain-types";

import { Deployer } from "../../../src/deployer/Deployer";
Expand Down Expand Up @@ -41,10 +42,28 @@ describe("deployer", () => {
expect(await ethersProvider!.provider.getBalance(contract.getAddress())).to.equal(toPay);
});

it("should not deploy contract with invalid constructor arguments", async function () {
const signer = (await deployer.getSigner()) as any;

await expect(deployer.deploy(ConstructorWithArguments__factory, [signer], {})).to.be.rejectedWith(
`Deployer.deploy(): MinimalContract.deploy(): MinimalContract._createDeployTransaction(): invalid BigNumberish value (argument="value", value="<SignerWithAddress 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266>", code=INVALID_ARGUMENT, version=6.10.0)`,
);
});

it("should revert if artifact is not a contract", async function () {
await expect(deployer.deploy({} as any, [], {})).to.be.rejectedWith(
"Deployer.deploy(): Unknown Contract Factory Type",
);
});

it("should be able to customize overrides for function calls", async function () {
const contract = await deployer.deploy(PayableReceive__factory);

const toPay = 100n;

await contract.pay({ value: toPay.toString() });

expect(await ethersProvider!.provider.getBalance(contract.getAddress())).to.equal(toPay);
});
});
});
Loading