首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >由web3py调用的契约函数没有错误,但没有效果(加纳奇本地网络)

由web3py调用的契约函数没有错误,但没有效果(加纳奇本地网络)
EN

Stack Overflow用户
提问于 2022-04-08 08:45:02
回答 1查看 118关注 0票数 1

通过py-solc-x编译代码,然后使用web3py api将其部署到ganache本地网络。首先,调用一个get_balance函数并按预期返回。第二,调用传递函数,它将无错误地返回,但当我稍后调用get_balance时,余额没有变化。尝试通过发送原始事务来调用传输,但是它仍然没有效果.

metacoin.sol (由块菌文档提供)

代码语言:javascript
复制
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

代码语言:javascript
复制
    # 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.

转移交易看上去不错。天然气/汽油价格和动态收费交易都已尝试过,但余额仍然相同。是因为本地网络设置的问题吗?或者我错过了一些必要的配置步骤。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-08 08:47:55

检查交易是否通过。

代码语言:javascript
复制
assert tx_receipt.status == 1

另外,如果Ganache不起作用,那么试试以太测试为基础。这里的示例令牌测试

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71794128

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档