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 search and try And find this code can work.
But, buildTransaction()
function can't add args to
and nonce
(will get errorValueError: Cannot set nonce in transaction
) And if I just not add to
nonce
to transaction 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/ ?
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['<stdin>: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)