Web3.py: `buildTransaction` and `signTransaction` don't match.

Created on 4 Apr 2018  Â·  2Comments  Â·  Source: ethereum/web3.py

  • Version: 4.0.0
  • Python: 3.4
  • OS: win

What was wrong?

the orgin question is below:

How to connect to Ropston and deploy contract use web3.py?
I have deploy a contract to Ropston use remix and MetaMask.
I want know how to deploy a contract to Ropston via infura using web3.py without geth or parity.
I read the Documention and find there is no how-to chapter.
A small demo code will be very helpful.

And I just shearch and try. And I find this code can work.
But, buildTransaction()function can't add args to and nonce (will get error ValueError: Cannot set nonce in transaction) And if I just not add to nonce to transaction but in signTransaction() function I will get errorTypeError: Not all fields initialized

There must be something wrong.
I guess I should use another function rather than buildTransaction() But I can't find it in document.

So, what function I should use?
And what‘s pythonic way to deploy contract to https://ropsten.infura.io/ ?

````python
from eth_account import Account
from web3 import Web3, HTTPProvider
from solc import compile_source

privateKey = "xxxxxx"
contract_source_code = '''
pragma solidity ^0.4.0;

contract Greeter {
string public greeting;

function Greeter() {
    greeting = 'Hello';
}

function setGreeting(string _greeting) public {
    greeting = _greeting;
}

function greet() constant returns (string) {
    return greeting;
}

}
'''

compiled_sol = compile_source(contract_source_code)
contract_interface = compiled_sol[':Greeter']
w3 = Web3(HTTPProvider("https://ropsten.infura.io/"))
w3.eth.enable_unaudited_features()
account = Account()
acct = account.privateKeyToAccount(privateKey)
contract_ = w3.eth.contract(abi=contract_interface['abi'], bytecode=contract_interface['bin'])
contract_data = contract_.constructor().buildTransaction({'from': acct.address,
'gas': 1728712,
'gasPrice': w3.toWei('21', 'gwei')})
contract_data["to"] = "" # <----
contract_data["nonce"] = w3.eth.getTransactionCount(acct.address) #<------
signed = w3.eth.account.signTransaction(contract_data, privateKey)
w3.eth.sendRawTransaction(signed.rawTransaction)
````

How can it be fixed?

I don't know

Most helpful comment

I ran into this recently too. contract.constructor().buildTransaction(...) should set the "to" = b''. This is a bug imho.

All 2 comments

I ran into this recently too. contract.constructor().buildTransaction(...) should set the "to" = b''. This is a bug imho.

Sorry, my bad. I think I overlooked the comments when I did the pull request.

Was this page helpful?
0 / 5 - 0 ratings