web3py调用合约函数没有错误但没有效果(ganache本地网络)

发布于 2025-01-19 21:25:49 字数 1682 浏览 3 评论 0原文

通过 py-solc-x 编译代码,然后使用 web3py api 将其部署到 ganache 本地网络。 首先,调用 get_balance 函数,它会按预期返回。 其次,调用transfer函数,它没有错误地返回,但是当我稍后调用get_balance时,余额没有改变。 尝试通过发送原始交易来调用转账,但仍然没有效果...

metacoin.sol(由truffle doc提供)

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本地网络设置的问题吗?或者我错过了一些必需的配置步骤。

compiled code by py-solc-x, then deployed it to the ganache local network using web3py api.
first, call a get_balance function and it return as expected.
second, call the transfer function and it return without error, but the balance have not changed when I call get_balance later.
try to call transfer by sending a raw transaction but it still no effect...

metacoin.sol (provided by truffle doc)

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.

the transfer transaction looks good.
both gas/gasPrice and dynamic fee transaction have been tried but the balance still the same.
it's the problem from the ganache local network setting ? or some required configured steps I missed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尝蛊 2025-01-26 21:25:49

检查交易

assert tx_receipt.status == 1

是否还通过了Ganache是​​否不起作用,请尝试基于以太坊测试仪的测试。示例在此处进行标记测试

Check that the transaction went through

assert tx_receipt.status == 1

Also if Ganache does not work then try Ethereum Tester based tests. Example token tests here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文