Solidity

使用 .connect(notDeployer) 編寫測試時遇到困難

  • August 31, 2022

我目前正在對安全帽進行測試,目前在嘗試與部署者以外的另一個帳戶執行交易時遇到困難。這是測試:

...
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")
             })

不知何故,我覺得問題出在這個 .connect() 上,因為我已經成功通過了其他測試。

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

我也嘗試過使用

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

而不是直接寫地址,但我得到類似的錯誤

      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)

所以我對這裡發生的事情有點困惑,有沒有人在這種情況下遇到過類似的錯誤?


編輯

這是給我問題的程式碼的全部相關部分

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")
             })
       })
})

這是禮物功能

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

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

我還嘗試了另一種選擇

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")

但我得到這個錯誤

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

在為 .connect() 和 gift() 嘗試了不同的地址後,我注意到無效地址或 ENS 名稱總是報告我放入 gift() 函式中的地址……


問題解決了

好吧,在玩弄了 gift() 參數之後,我終於通過了我的測試

   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")
   })

我不知道為什麼,但是如果我將 addr1 或 addr2 放在禮物參數中,它將不起作用,我必須以這種方式輸入地址,即使它有效,我實際上不確定為什麼或為什麼它不是t工作…無論如何感謝所有寶貴的幫助!

您應該連接到簽名者,而不是地址。

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

很好地這樣做,我們正在連接addr1並贈送給addr2

const connectacc2 = await LPR.connect(addr1.address).gift(addr2.address);

引用自:https://ethereum.stackexchange.com/questions/134596