[Ethereum] Is it possible to send ether to a contract after deployment inside a hardhat test

ethers.jshardhatmocha

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:

const params = { to: addr1.address, value: ethers.utils.parseUnits("1", "ether").toHexString()};
const txHash = await owner.sendTransaction(params);
console.log("transactionHash is " + txHash);  

Results in:

$ npx hardhat test



  HandleEther contract
    Deployment
transactionHash is  {
  hash: '0xbc9a920f404109c6c97a3acbe3f2470864a2a650a0034faf353e235290b40be4',
  type: 2,
  accessList: [],
  blockHash: '0xd95b35db38076c84bc26be176d9305a862e2851436509eeafc913b8853300ba5',
  blockNumber: 2,
  transactionIndex: 0,
  confirmations: 1,
  from: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
  gasPrice: BigNumber { _hex: '0x6944bd18', _isBigNumber: true },
  maxPriorityFeePerGas: BigNumber { _hex: '0x3b9aca00', _isBigNumber: true },
  maxFeePerGas: BigNumber { _hex: '0x96eeb030', _isBigNumber: true },
  gasLimit: BigNumber { _hex: '0x5209', _isBigNumber: true },
  to: '0x70997970C51812dc3A010C7d01b50e0d17dc79C8',
  value: BigNumber { _hex: '0x0de0b6b3a7640000', _isBigNumber: true },
  nonce: 1,
  data: '0x',
  r: '0x7b7bf73f8f68b55148d0333b50deaccbf066b5a4268dba03cd9a7dfe3a8ba811',
  s: '0x7f0a57ac7646890fc748c22599f28a2da318e580890ca24f1e2bf704ce19bfbe',
  v: 0,
  creates: null,
  chainId: 31337,
  wait: [Function (anonymous)]
}
      ✓ Should send ether to contract after deployment


  1 passing (441ms)