繁体   English   中英

在python中与ganache-cli同步

[英]Sync with ganache-cli in python

我想测试一个小写的以太坊智能合约ganache打印账户,而web3给我一个错误:

web3.exceptions.InvalidAddress :(“ Web3.py仅接受校验和地址。向您提供此非校验和地址的软件应视为不安全,请在其平台上将其归档为bug。请尝试使用ENS名称。或者,如果必须接受较低的安全性,请使用Web3.toChecksumAddress(lower_case_address)。','0xfcad0b19bb29d4674531d6f115237e16afce377c')

然后,我使用以下方法将地址转换为混合地址:

Web3.toChecksumAddress(the_lower_case_ganache_address)

它会引发一个错误:

在call_contract_function中的文件“ /usr/local/lib/python3.7/site-packages/web3/contract.py”中的第1385行从web3.exceptions.BadFunctionCallOutput引发BadFunctionCallOutput(msg):无法与/调用合同功能进行交易,合同是否已正确部署并进行了链同步? 127.0.0.1--[2019年1月25日21:35:21]“ POST / blockchain / user HTTP / 1.1” 500-

这是我的python代码:

def check_gender(data):
    valid_list = ["male", "female"]
    if data not in valid_list:
        raise ValidationError(
            'Invalid gender. Valid choices are'+ valid_list
        )

class UserSchema(Schema):
    name = fields.String(required=True)
    gender = fields.String(required=True, validate=check_gender)


app = Flask(__name__)


# api to set new user every api call
@app.route("/blockchain/user", methods=['POST'])
def transaction():

    w3.eth.defaultAccount = w3.eth.accounts[0]
    with open("data.json", 'r') as f:
        datastore = json.load(f)
    abi = datastore["abi"]
    contract_address = datastore["contract_address"]

    # Create the contract instance with the newly-deployed address
    user = w3.eth.contract(
        address=contract_address, abi=abi,
    )
    body = request.get_json()
    result, error = UserSchema().load(body)
    if error:        
        return jsonify(error), 422
    tx_hash = user.functions.setUser(
        result['name'], result['gender']
    )
    tx_hash = tx_hash.transact()
    # Wait for transaction to be mined...
    w3.eth.waitForTransactionReceipt(tx_hash)
    user_data = user.functions.getUser().call()
    return jsonify({"data": user_data}), 200



if __name__ == '__main__':
    app.run()

`和json文件:

{
    "abi": [
        {
            "constant": false,
            "inputs": [
                {
                    "name": "name",
                    "type": "string"
                },
                {
                    "name": "gender",
                    "type": "string"
                }
            ],
            "name": "setUser",
            "outputs": [],
            "payable": false,
            "stateMutability": "nonpayable",
            "type": "function"
        },
        {
            "constant": false,
            "inputs": [],
            "name": "getUser",
            "outputs": [
                {
                    "name": "",
                    "type": "string"
                },
                {
                    "name": "",
                    "type": "string"
                }
            ],
            "payable": false,
            "stateMutability": "nonpayable",
            "type": "function"
        }
    ],
    "contract_address": "0xFCAd0B19bB29D4674531d6f115237E16AfCE377c"
}

该错误表明Ganache找不到与之交互的已部署合同。

您的代码似乎有效,但是此行上可能发生错误:

tx_hash = user.functions.setUser(
    result['name'], result['gender']
)

该代码尝试设置用户,但找不到与之交互的合同(即使ABI和合同实例有效)。

如果您使用的是Ganache,则很可能每次运行代码时都会重新部署合同,因此,如果从静态文件中提取代码,则以下行可能无法正常工作:

contract_address = datastore["contract_address"]

如果您每次都部署它,则需要从Ganache动态获取合同地址。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM