This is using Hardhat with ethers.js:
My test wants to deploy a contract and then I want to send ether to it:
const { ethers } = require("hardhat");
describe("HandleEther contract", function () {
let factory;
let contract;
let owner;
let addr1;
beforeEach(async function () {
factory = await ethers.getContractFactory("HandleEther");
[owner, addr1] = await ethers.getSigners();
contract = await factory.deploy();
});
describe("Deployment", function () {
it("Should send ether to contract after deployment", async function () {
const params = { from: owner,to: addr1, value: ethers.utils.parseUnits("1", "ether").toHexString()};
const txHash = await owner.provider.sendTransaction(params);
console.log("transactionHash is " + txHash);
});
});
});
When I run npx hardhat test
, I see this:
$ npx hardhat test
HandleEther contract
Deployment
1) Should send ether to contract after deployment
0 passing (447ms)
1 failing
1) HandleEther contract
Deployment
Should send ether to contract after deployment:
Error: invalid hexlify value (argument="value", value={"from":"<SignerWithAddress 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266>","to":"<SignerWithAddress 0x70997970C51812dc3A010C7d01b50e0d17dc79C8>","value":"0x0de0b6b3a7640000"}, code=INVALID_ARGUMENT, version=bytes/5.4.0)
at Logger.makeError (node_modules/@ethersproject/logger/src.ts/index.ts:213:28)
at Logger.throwError (node_modules/@ethersproject/logger/src.ts/index.ts:225:20)
at Logger.throwArgumentError (node_modules/@ethersproject/logger/src.ts/index.ts:229:21)
at Object.hexlify (node_modules/@ethersproject/bytes/src.ts/index.ts:247:19)
at /home/xrd/Projects/ExtraStatic/eth/hardhat-simplified/node_modules/@ethersproject/providers/src.ts/base-provider.ts:1212:74
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at runNextTicks (internal/process/task_queues.js:64:3)
at listOnTimeout (internal/timers.js:524:9)
at processTimers (internal/timers.js:498:7)
My contract contracts/HandleEther.sol
looks like this:
pragma solidity ^0.8.7;
contract HandleEther {
constructor() payable {
}
}
My hardhat.config.js:
require("@nomiclabs/hardhat-waffle");
/**
* @type import('hardhat/config').HardhatUserConfig
*/
module.exports = {
solidity: "0.8.7",
};
My package.json is:
{
"name": "hardhat-simplified",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"hardhat": "^2.6.0"
},
"devDependencies": {
"@nomiclabs/hardhat-ethers": "^2.0.2",
"@nomiclabs/hardhat-waffle": "^2.0.1",
"chai": "^4.3.4",
"ethereum-waffle": "^3.4.0",
"ethers": "^5.4.4"
}
}
Best Answer
Removing the from argument (it's redundant since the owner is calling the
sendTransaction()
, removing.provider
and adding the address (addr1.address
) fixed it:Results in: