Skip to content

Instantly share code, notes, and snippets.

@CavaTrendy
Created July 23, 2019 07:10
Show Gist options
  • Save CavaTrendy/48308aff4bf385df44a62f0e5593d6d1 to your computer and use it in GitHub Desktop.
Save CavaTrendy/48308aff4bf385df44a62f0e5593d6d1 to your computer and use it in GitHub Desktop.
How to write transaction on Infuria with data
####
# How to write transaction on Infuria with data by Giorgio Alessandro Motta
####
####
# Import
####
from web3 import Web3
import json
import calendar
import time
######
# Contract Abi loading
#####
w3 = Web3(Web3.HTTPProvider("https://ropsten.infura.io/YOUR INFURIA KEY"))
with open("C:/Users/XXXX/YOUR CONTRACTe.json") as f: #loaad you contract
info_json = json.load(f)
abi = info_json["abi"]
address = 'YOUR CONTRACT' #address of the contract
contract = w3.eth.contract(address=address, abi=abi)
account = 'PUBLIC KEY ACCOUNT'#account from which you want to do the transaction
######
# information of the transaction
#####
nonce = w3.eth.getTransactionCount('ACCOUNT PUBLIC ADDRESS', 'latest')
private_key = 'INSERT ACCOUNT PRIVATE KEY'
######
# Build Transaction
#####
ts = w3.toInt(calendar.timegm(time.gmtime())) #get the timestamp
tx = contract.functions.registerHarvest(100, ts).buildTransaction(
{'nonce': nonce,
'gasPrice': w3.eth.gasPrice,
'gas': 2100000})
signed_txn = w3.eth.account.signTransaction(tx, private_key) #sign the transction
# get the hash
hash_transaction = w3.eth.sendRawTransaction(signed_txn.rawTransaction)
print(w3.toHex(hash_transaction)) #get the transaction hash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment