Etherum Blockchain尝试使用web3.py发送事务

时间:2018-05-25 11:04:25

标签: python-3.x blockchain ethereum web3 geth

我可以使用web3.py库,我可以使用以太坊中的节点直接部署合同。

我在混音中创建了我的可靠性代码,并在以太网控制台中复制了abi以部署代码。我也得到了合同的地址。

我的可靠性代码如下。

pragma solidity ^0.4.0;


contract bidding {

struct bid {
    string bidderName;
    uint grainId;
    uint bidAmount;
    uint bidTime;
}

mapping (uint => bid) bidInfos;
uint[] public bidIds;

function addBid(
    uint _bidId,
    string _bidderName, 
    uint _grainId, 
    uint _bidAmount
) public {
    var bid_info = bidInfos[_bidId];
    bid_info.bidderName = _bidderName;
    bid_info.grainId = _grainId;
    bid_info.bidAmount=_bidAmount;
    bid_info.bidTime= now;

    bidIds.push(_bidId) -1;
}

function getallBids() view public returns(uint[]) {
    return bidIds;
}

function getBid(uint _bidId) view public returns (string, uint, uint, uint) {
    return (
        bidInfos[_bidId].bidderName, 
        bidInfos[_bidId].grainId,
        bidInfos[_bidId].bidAmount, 
        bidInfos[_bidId].bidTime
    );
}

function getbidcount() view public returns (uint) {
    return bidIds.length;
}
}

我部署在我的以太网络中并获得了地址 在我的python中,我正在使用web3库。

from web3 import Web3, HTTPProvider, IPCProvider, WebsocketProvider
import json
web3 = Web3(IPCProvider())
bid_abi=json.loads('''[{"constant":false,"inputs":[{"name":"_bidId","type":"uint256"},{"name":"_bidderName","type":"string"},{"name":"_grainId","type":"uint256"},{"name":"_bidAmount","type":"uint256"}],"name":"addBid","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"getallBids","outputs":[{"name":"","type":"uint256[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_bidId","type":"uint256"}],"name":"getBid","outputs":[{"name":"","type":"string"},{"name":"","type":"uint256"},{"name":"","type":"uint256"},{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getbidcount","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"bidIds","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}]''')

contract_bid=web3.eth.contract(abi=bid_abi,address='0xbcd8ffbb0f8a5410194ab5a3badf46caaa693098')

contract_bid.functions.addBid(10,"xyzz",1234,5600).transact()

这是我得到的错误。

  Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\WinPython\python-3.5.4.amd64\lib\site-packages\web3\contract.py", line 1064, in transact
**self.kwargs)
  File "D:\WinPython\python-3.5.4.amd64\lib\site-packages\web3\contract.py", line 1341, in transact_with_contract_function
txn_hash = web3.eth.sendTransaction(transact_transaction)
  File "D:\WinPython\python-3.5.4.amd64\lib\site-packages\web3\eth.py", line 244, in sendTransaction
[transaction],
  File "D:\WinPython\python-3.5.4.amd64\lib\site-packages\web3\manager.py", line 106, in request_blocking
raise ValueError(response["error"])
 ValueError: {'code': -32000, 'message': 'unknown account'}

我也在做web3.personal.unlockAccount。仍然是给出同样的错误。 我的帐户只有一个帐户。我做错了什么?

1 个答案:

答案 0 :(得分:0)

当您用于出价的地址未托管在服务器中时,会发生此错误。 尝试将Keystore导入到自己的服务器或尝试使用您托管的帐户。