如何使用web3.py将参数从Django视图传递给契约函数

问题描述 投票:1回答:1

我有一个具有三个输入的支付功能的合同:

function payout(uint256[] ids, address[] recipients, uint256[] amounts) public authorized {    
require(ids.length == recipients.length && ids.length == amounts.length);
for (uint i = 0; i < recipients.length; i++) {
    Payout(ids[i], recipients[i].send(amounts[i]));
  }
}

并且在views.py中定义了一个函数,它调用:

q=Question.objects.filter()
ids = []
adds = []
for cur in q:
        for a in cur.answer.all():
            ids = a.user.id
            adds = a.user.user_wallet_address
            bounty_for_answering(ids, adds, cur.ether)
return redirect('/')

bounty_for_answering()是:

def bounty_for_answering(ids, usersWalletAddress, bountyAmount):
amount_in_wei = w3.toWei(bountyAmount, 'ether')
nonce=w3.eth.getTransactionCount(usersWalletAddress)
txn_dict = contract_instance.functions.payout(ids, usersWalletAddress, bountyAmount).buildTransaction({
    'gas': 30000,
    'gasPrice': w3.toWei('40', 'gwei'),
    'nonce': nonce,
    'chainId': 3
})

signed_txn = w3.eth.account.signTransaction(txn_dict, wallet_private_key)

txn_hash = w3.eth.sendRawTransaction(signed_txn.rawTransaction)

txn_receipt = w3.eth.getTransactionReceipt(txn_hash)

count = 0
while tx_receipt is None and (count < 30):
    time.sleep(10)

    tx_receipt = w3.eth.getTransactionReceipt(txn_hash)

    print(tx_receipt)

if tx_receipt is None:
    return {'status': 'failed', 'error': 'timeout'}

哪个user_wallet_address是一个MetaMask钱包地址。

问题是我不知道如何将输入参数传递给bounty_for_answering,我得到这个错误:

无法识别名称为payout的目标函数,(<class 'list'>, <class 'list'>, <class 'set'>)类型的位置参数和{}类型的关键字参数。找到1个名为payout的函数:['payout(uint256 [],address [],uint256 [])']由于没有匹配的参数类型,函数调用失败。

django django-views web3
1个回答
0
投票

该错误告诉您正在将不正确的类型传递给合同方法。如果没有bounty_for_answering()的定义,很难真正具体,但这里重复了错误信息,相关部分突出显示:

无法使用名称支付,(<class 'list'>, <class 'list'>类型的位置参数(<class 'set'>)和类型{}的关键字参数来识别预期的功能。找到1个名为payout的函数:['payout(uint256 [],address [],uint256[])']由于没有匹配的参数类型,函数调用失败。

python set是无序的,但是契约函数期望有序列表作为第三个参数。订单在契约函数中很有用,因此在将值传递给契约方法调用之前,您可能需要做一些工作来对值进行排序。

© www.soinside.com 2019 - 2024. All rights reserved.