I am able to use the web3.py library and i am able to deploy the contract directly using my node in ethereum.
I created my solidity code in remix and copied the abi in the etherum console to deploy the code. I got the address of the contract as well.
My solidity code is as follows.
pragma solidity ^0.4.0;
//For testing use the below data
//123,"wheat","grade1",50
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;
}
}
I deployed in my etherum network and got the address as In my python i am using the web3 library.
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()
This is the error i am getting.
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'}
I am doing a web3.personal.unlockAccount as well. still it is giving same error. There is only one account associated with my account as well. What am i doing wrong?
how to do the transaction so i can add a new bid from web3 in python and get the transaction hash.