我想测试一个简单的以太坊智能合约
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行
      从e引发BadFunctionCallOutput(msg)
  web3.exceptions.BadFunctionCallOutput:无法与/调用合同功能进行交易,合同是否已正确部署且已同步链?
  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动态获取合同地址。

关于python - 在python中与ganache-cli同步,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54370707/

10-11 21:40