从脚本调用智能合约中的函数并检查变量值

Function calls in smart contract from script and checking value of variables

我目前正在使用Brownie学习智能合约和区块链开发。我无法理解如何使用 python 脚本从智能合约调用函数和检查变量值。我怎样才能做到这一点?

下面我有一个合同 DutchAuction,其中我定义了一个函数 bid(),其中 returns 'Hello world' 只是为了测试目的,我试图调用它。

pragma solidity ^0.8.10;

import "@openzeppelin/contracts/token/ERC1155/IERC1155.sol";
import "@openzeppelin/contracts/token/ERC1155/IERC1155Receiver.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";


contract DutchAuction {

    uint public startTime;
    uint public endTime;

    uint public price;
    uint public startPrice;
    
    address public assetOwner;


    constructor(uint _startPrice, uint _endTime) public {
        startTime = block.timestamp;
        price = _startPrice;
        startPrice = _startPrice;
        endTime = _endTime;
        assetOwner = msg.sender;
        
    }

    function bid() public returns (string calldata) {
        return 'hello world';

    }
    
}

bid() 函数 returns 语句中将 string calldata 更改为 string memory

字符串文字被加载到内存(不是调用数据)然后 returned.

如果您想 return calldata,您需要先将值作为 calldata 传递:

function foo(string calldata _str) public pure returns (string calldata) {
    return _str;
}

文档:https://docs.soliditylang.org/en/v0.8.10/types.html#data-location