1

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

下面我有一个合同DutchAuction,我在其中定义了一个函数,该函数仅出于测试目的bid()而返回'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';

    }
    
}
4

1 回答 1

1

在函数语句中更改为string calldatastring memorybid()returns

字符串文字被加载到内存(而不是调用数据)然后返回。

如果要返回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

于 2021-12-01T22:25:22.390 回答