【发布时间】:2019-12-26 02:06:28
【问题描述】:
我在以太坊测试网络上部署了一个合约,其中包含一些功能,并且在使用 Remix 界面时它们都可以正常工作。当尝试在 Python 中使用 web3.py 调用这些函数时,我只能调用公共函数并且该部分工作正常。 问题是调用具有“限制”的函数,例如具有“所有者要求”,这意味着只有创建合约的地址才能调用该特定函数。我已经用谷歌搜索了它,但没有运气。我猜我应该在调用该函数时同时使用该以太坊帐户的“地址”和“密码”作为参数,但我不知道该怎么做。函数称为“set()”,它只需要 2 个字符串值。
这是 Solidity 代码的一部分,它使函数“set()”只能由该合约的所有者访问。
constructor() public {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
function set(string memory _lastHash,
string memory _fullHash) public onlyOwner {
lastHash = _lastHash;
fullHash = _fullHash;
}
这是我用来从其他 2 个函数中获取返回值的 python 函数,我没有包括:
data = contract.functions.getFullHash().call()
函数称为“getFullHash()”。鉴于 Python 代码不适用于函数“set()”。
【问题讨论】:
-
设置值你必须调用函数为
contract.functions.set(arg1, arg2).transact()。 -
这对我不起作用,起作用的是:signed_txn = web3.eth.account.signTransaction(transaction, private_key=private_key)。交易需要如下所示: transaction = contract.functions.set( 'string1', 'string2' ).buildTransaction({ 'gas': 70000, 'gasPrice': web3.toWei('1', 'gwei') , 'from': 地址, 'nonce': nonce })
标签: python blockchain ethereum web3