简体   繁体   English

如何测试从特定地址调用合约函数?

[英]How can I test calling a contract function from a specific address?

Here's my contract:这是我的合同:

// SPDX-License-Identifier: ISC
pragma solidity ^0.8.17;

contract NoteDrop {
    string public text = "Hello, world!";
    address public lastPoster;

    event NewText(
        string text,
        address lastPoster
    );

    constructor() {
        // Do nothing
    }

    function setText(string memory _text) public {
        text = _text;
        lastPoster = msg.sender;
        emit NewText(_text, msg.sender);
    }
}

Here's my test:这是我的测试:

contract('NoteDrop', () => {
  let noteDropContract;

  beforeEach(async() => {
    noteDropContract = await NoteDropContract.new();
  })

  describe('lastPoster', async() => {
    it('is changed automatically', async() => {
      await noteDropContract.setText("something", {from: "XXXXX"});
      const lastPoster = await noteDropContract.lastPoster();
      assert.equal(lastPoster, "XXXXX");
    })
  })
})

I would like to test that the lastPoster field gets updated on the contract to the address that last called the setText function.我想测试lastPoster字段是否在合同上更新为最后调用setText函数的地址。

What I don't know is how to generate (or fetch) an address that has a balance.我不知道的是如何生成(或获取)具有余额的地址。 I need that to call setText from that specific address, so that I can then assert that noteDrop.lastPoster() equals that sender address.我需要from该特定地址调用setText ,这样我就可以assert noteDrop.lastPoster()等于该发件人地址。

What am I missing?我错过了什么? How can I generate an address to do this?我怎样才能生成一个地址来做到这一点?

I found the solution quickly.我很快找到了解决方案。 The answer is that the contract test function can take an optional parameter, accounts :答案是合约测试函数可以采用可选参数accounts

contract('NoteDrop', (accounts) => {

You can then use this down in your tests like so:然后你可以像这样在你的测试中使用它:

it('is changed automatically', async() => {
  await noteDropContract.setText("something", {from: accounts[0]});
  const lastPoster = await noteDropContract.lastPoster();
  assert.equal(lastPoster, accounts[0]);
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从另一个合约中获取合约的合约地址? - How can I get contract address of a contract from another contract? 如何从特定地址调用合同功能 - How to call a contract function from a specific address 如何在ganache / truffle / web3中解锁合同地址,以便我可以使用from作为调用函数? - How to unlock a contract address in ganache/truffle/web3 so that I can use it as from to call a function? 如何从不同节点访问合约功能? - How can i access the function of contract from different nodes? 智能合约的返回地址函数返回承诺对象而不是松露测试中的地址 - Return address function from smart contract returns promise object instead of address in truffle test 如何将合约余额从内部函数转移到外部地址? - How to transfer a contract balance to an external address from an internal function? 如何在智能合约中将地址连接到午餐 function? | TypeError:无法读取未定义的属性(读取“连接”)ether.js - how can i connect an address to lunch a function in the smart contract ? | TypeError: Cannot read properties of undefined (reading 'connect') ether.js 在特定地址使用合约 - Use a contract at a specific address 松露测试给出“错误:尝试运行调用合同功能的交易,但收件人地址___不是合同地址” - Truffle test gives “Error: Attempting to run transaction which calls a contract function, but recipient address ___ is not a contract address” 调用实体合同的set()函数(使用web3js)将创建一个新的合同地址。 为什么? - Calling the set() function (with web3js) of a solidity contract is creating a new contract address. Why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM