web3py 调用的合约函数没有错误但没有效果(ganache 本地网络)
contract function invoked by web3py without error but have no effect (ganache local network)
通过 py-solc-x 编译代码,然后使用 web3py api 将其部署到 ganache 本地网络。
首先,调用 get_balance 函数并按预期调用 return 。
第二,调用传递函数,它 return 没有错误,但是当我稍后调用 get_balance 时余额没有改变。
尝试通过发送原始交易来调用转账,但仍然没有效果...
metacoin.sol(由 truffle 文档提供)
pragma solidity ^0.8.0;
contract MetaCoin {
mapping (address => uint) balances;
event Transfer(address indexed _from, address indexed _to, uint _value);
constructor() public {
balances[msg.sender] = 10000;
}
function transfer(address receiver, uint amount) public returns(bool sufficient) {
if (balances[msg.sender] >= amount)
return false;
balances[msg.sender] -= amount;
balances[receiver] += amount;
emit Transfer(msg.sender, receiver, amount);
return true;
}
function get_balance(address account) public view returns(uint) {
return balances[account];
}
}
interacting.py
# deploy contract by w3.eth.accounts[0]
# the balance of the accounts[0] is 10000 (call get_balance() return 10000)
# then transfer 1000 from accounts[0] to accounts[1]
deployed_address = '0x538574C591F6e01E22eFa951153a29e6Fc505735'
contract = w3.eth.contract(address=HexBytes(deployed_address), abi=abi)
tx_hash = contract.functions.transfer(w3.eth.accounts[1], 1000).transact()
tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
print(tx_receipt.contractAddress)
balance = contract.functions.get_balance(w3.eth.accounts[0]).call()
print(balance)
# still 10000, expect 9000.
转账交易看起来不错。
gas/gasPrice和动态手续费交易都试过了,余额还是一样。
这是 ganache 本地网络设置的问题吗?或者我错过了一些必需的配置步骤。
检查交易是否完成
assert tx_receipt.status == 1
此外,如果 Ganache 不起作用,请尝试 Ethereum Tester based tests. Example token tests here。
通过 py-solc-x 编译代码,然后使用 web3py api 将其部署到 ganache 本地网络。 首先,调用 get_balance 函数并按预期调用 return 。 第二,调用传递函数,它 return 没有错误,但是当我稍后调用 get_balance 时余额没有改变。 尝试通过发送原始交易来调用转账,但仍然没有效果...
metacoin.sol(由 truffle 文档提供)
pragma solidity ^0.8.0;
contract MetaCoin {
mapping (address => uint) balances;
event Transfer(address indexed _from, address indexed _to, uint _value);
constructor() public {
balances[msg.sender] = 10000;
}
function transfer(address receiver, uint amount) public returns(bool sufficient) {
if (balances[msg.sender] >= amount)
return false;
balances[msg.sender] -= amount;
balances[receiver] += amount;
emit Transfer(msg.sender, receiver, amount);
return true;
}
function get_balance(address account) public view returns(uint) {
return balances[account];
}
}
interacting.py
# deploy contract by w3.eth.accounts[0]
# the balance of the accounts[0] is 10000 (call get_balance() return 10000)
# then transfer 1000 from accounts[0] to accounts[1]
deployed_address = '0x538574C591F6e01E22eFa951153a29e6Fc505735'
contract = w3.eth.contract(address=HexBytes(deployed_address), abi=abi)
tx_hash = contract.functions.transfer(w3.eth.accounts[1], 1000).transact()
tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
print(tx_receipt.contractAddress)
balance = contract.functions.get_balance(w3.eth.accounts[0]).call()
print(balance)
# still 10000, expect 9000.
转账交易看起来不错。 gas/gasPrice和动态手续费交易都试过了,余额还是一样。 这是 ganache 本地网络设置的问题吗?或者我错过了一些必需的配置步骤。
检查交易是否完成
assert tx_receipt.status == 1
此外,如果 Ganache 不起作用,请尝试 Ethereum Tester based tests. Example token tests here。