Solidity – How to Troubleshoot Difficulties Writing Tests with .connect(notDeployer) in Hardhat

chaihardhatsoliditytesting

I am currently working on my tests on hardhat, and I am currently stuck while trying to perform a transaction with another account than the deployer. Here is the test:

...
 describe("gift", async function () {
...
    it("doesn't allow others to gift", async function () {
                    

                  const connectacc2 = await LPR.connect(
                      "0x70997970C51812dc3A010C7d01b50e0d17dc79C8"
                  )
                  await expect(
                      connectacc2.gift("0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC")
                  ).to.be.revertedWith("Ownable: caller is not the owner")
              })

Somehow I feel that the issue lies with this .connect(), because I've been successfully passing other tests.

doesn't allow others to gift:
     Error: VoidSigner cannot sign transactions (operation="signTransaction", code=UNSUPPORTED_OPERATION, version=abstract-signer/5.7.0)
      at Logger.makeError (node_modules/@ethersproject/logger/src.ts/index.ts:269:28)
      at Logger.throwError (node_modules/@ethersproject/logger/src.ts/index.ts:281:20)
      at /home/giorgio/LPR/LPR-Back/node_modules/@ethersproject/abstract-signer/src.ts/index.ts:355:20

I've tried also to do it with using

 ;[addr1, addr2, addr3] = await ethers.getSigners()

instead of directly writing the addresses but I get similar errors

       gift
         doesn't allow others to gift:
     Error: invalid address or ENS name (argument="name", value="<SignerWithAddress 0x70997970C51812dc3A010C7d01b50e0d17dc79C8>", code=INVALID_ARGUMENT, version=contracts/5.7.0)
      at Logger.makeError (node_modules/@ethersproject/logger/src.ts/index.ts:269:28)
      at Logger.throwError (node_modules/@ethersproject/logger/src.ts/index.ts:281:20)
      at Logger.throwArgumentError (node_modules/@ethersproject/logger/src.ts/index.ts:285:21)
      at /home/giorgio/LPR/LPR-Back/node_modules/@ethersproject/contracts/src.ts/index.ts:123:16
      at step (node_modules/@ethersproject/contracts/lib/index.js:48:23)
      at Object.next (node_modules/@ethersproject/contracts/lib/index.js:29:53)
      at fulfilled (node_modules/@ethersproject/contracts/lib/index.js:20:58)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)

So I am a bit confused with what is happening here, has anyone encountered similar errors in this situation ?


edit

Here is the full concerned part of the code giving me issues

const { assert, expect } = require("chai")
const { ethers, deployments, getNamedAccounts } = require("hardhat")
const {developmentChains,

} = require("../../helper-hardhat-config")

!developmentChains.includes(network.name)
    ? describe.skip
    : describe("LPR", function () {
          let LPR, deployer, addr1, addr2, addr3
          beforeEach(async function () {
              ;[addr1, addr2, addr3] = await ethers.getSigners()
              deployer = (await getNamedAccounts()).deployer
              await deployments.fixture(["all", "LPR"])
              LPR = await ethers.getContract("LPR", deployer)
          })
             describe("gift", async function () {
                  it("doesn't allow others to gift", async function () {
                  
                   expect(await LPR.connect(addr1.address).gift(addr2.address)).to.be.revertedWith("Ownable: caller is not the owner")
              })
        })
})

and this is the gift function

    function gift(address _account) external onlyOwner {
        if (totalSupply() + 1 > MAX_SUPPLY) {
            revert LPR__MintGiftExceeded();
        }

        _safeMint(_account, (s_nftIdCounter).current());
        (s_nftIdCounter).increment();
    }

I also tried the other option of doing

it.only("doesn't allow others to gift", async function () {
                  ;[addr1, addr2] = await ethers.getSigners()
                  expect(await LPR.connect(addr1).gift(addr2)).to.be.revertedWith("Ownable: caller is not the owner")

but I get this error

Error: invalid address or ENS name (argument="name", value="<SignerWithAddress 0x70997970C51812dc3A010C7d01b50e0d17dc79C8>", code=INVALID_ARGUMENT, version=contracts/5.7.0)


After having tried different addresses for both the .connect() an gift() I've noticed that the invalid address or ENS name always reports the address that I put in the gift() function…


PROBLEM SOLVED

Okay so after playing around with the gift() arguments, I finally passed my test

    it("doesn't allow others to gift", async function () {
        await expect(
            LPR.connect(addr2).gift("0x90F79bf6EB2c4f870365E785982E1f101E93b906")
        ).to.be.revertedWith("Ownable: caller is not the owner")
    })
// or

 it("doesn't allow others to gift", async function () {
        await expect(
            LPR.connect(addr2).gift(addr1.address)
        ).to.be.revertedWith("Ownable: caller is not the owner")
    })

I don't know why but if I put addr1, or addr2 in the gift arguments, it won't work, I have to put the address this way, even though it works, I am actually not sure why or why it wasn't working… Anyways thanks for the all the precious help !

Best Answer

You should connect to a signer, not an address.

accounts = await ethers.getSigners()
await LPR.connect(accounts[1]).gift(someAddress)