我可以使用 web3.py 库,并且可以使用我在以太坊中的节点直接部署合约。
我在 remix 中创建了我的 Solidity 代码,并在以太坊控制台中复制了 abi 以部署代码。我也得到了合同的地址。
我的solidity代码如下。
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。它仍然给出同样的错误。我的帐户也只有一个帐户关联。我究竟做错了什么?