2

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 !

2
  • What is this getContract ? Does this exist ? I couldn't find this in ethersjs docs
    – 0xAnon
    Aug 30, 2022 at 12:53
  • Actually I am using hardhat-deploy to deploy my contract, await deployments.fixture(["all", "LPR"]) this line will keep track of all the previous deployments. And hardhat-deploy comes with a function, getContract, which will wrap ethersjs, and get for us the most recent deployment of the contract name that we give it. hence the deployments import in const { ethers, deployments, getNamedAccounts } = require("hardhat")
    – GiorgioDG
    Aug 30, 2022 at 17:01

4 Answers 4

2

You should connect to a signer, not an address.

accounts = await ethers.getSigners()
await LPR.connect(accounts[1]).gift(someAddress)
2
  • doesn't accounts = await ethers.getSigners() pretty much do the same as ;[addr1, addr2,a ddr3] = await ethers.getSigners() ?
    – GiorgioDG
    Aug 30, 2022 at 10:09
  • @GiorgioDG That is true, but the point here is the second line, when you call connect, you should pass one of the objects you get from getSigners(), but not a string (not accounts[1].address). Aug 30, 2022 at 12:21
1

well do this way here we are connecting addr1 and gifting to addr2

const connectacc2 = await LPR.connect(addr1.address).gift(addr2.address);
4
  • I tried this one, sadly also won't get me pass my test .. I am getting the Void signer error
    – GiorgioDG
    Aug 30, 2022 at 10:11
  • send me the full code i get to know it first
    – DereK
    Aug 30, 2022 at 11:22
  • I've edited my initial post, you can now take a peak if you would like to
    – GiorgioDG
    Aug 30, 2022 at 12:04
  • actually This will not work because .connect() expects a signer and not an address Oct 20, 2022 at 19:04
1

According to ethersjs docs:

A Signer in ethers is an abstraction of an Ethereum Account, which can be used to sign messages and transactions and send signed transactions to the Ethereum Network to execute state changing operations.

Why don't you do something like this -

describe("LPR", function () {
  let LPR,lpr;

  beforeEach(async function () {
    [deployer, addr1, addr2, addr3] = await ethers.getSigners();
    LPR = await ethers.getContractFactory("LPR");
    lpr = await LPR.deploy();
    await lpr.deployed();
  });

    ...
 /**  LPR.connect(addr1.address) - this is the problem in code. 
Shouldn't use address since it accepts an account object
**/

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

   ...

  }
6
  • thanks again for your help fren, It looks similar to what I already tried... I still get the Error: invalid address or ENS name (argument="name", value="<SignerWithAddress 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266>", code=INVALID_ARGUMENT, version=contracts/5.7.0)
    – GiorgioDG
    Aug 30, 2022 at 17:20
  • do you have this code hosted somewhere, I might try it in my local
    – 0xAnon
    Aug 30, 2022 at 18:03
  • hey fren, I finally made it work, but as I said in my answer I don't really know why it wasn't working. But at least it works :)
    – GiorgioDG
    Aug 30, 2022 at 18:26
  • Fren, can you put the updated code (in a new section at the end) in the question. So others can also know.
    – 0xAnon
    Aug 30, 2022 at 20:52
  • 1
    Yes I removed the (.address) but If I wanted expect(await LPR.connect(addr1).gift(deployer)).to.be.revertedWith("Ownable: caller is not the owner") this to work I should do expect(awaitLPR.connect(addr1).gift(deployer.address).to.be.revertedWith("Ownable: caller is not the owner") (added .address to deployer)
    – GiorgioDG
    Aug 30, 2022 at 23:25
0

To do a transaction from other than the default account you can do like this:

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

Moreover, we can also destructure the accounts array and use it like this:

[owner, otherAccount] = await ethers.getSigners()
await LPR.connect(otherAccount).gift(someAddress)

If you also want to deploy a contract from other than the default account you can do it like this:

const Factory = await ethers.getContractFactory("Factory", otherAccount);
const contract = await Factory.deploy()

1
  • Thank you, but I don't really want to deploy the contract from another account. I've already tried something which is similar, and somehow I get invalid Address or ENS name..
    – GiorgioDG
    Aug 30, 2022 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.