from solcx import compile_standard, install_solc
import json
from web3 import Web3
import os
from dotenv import load_dotenv
load_dotenv()
with open("./SimpleStorage.sol", "r") as file:
simple_storage_file = file.read()
# print(simple_storage_file)
# compile our solidity
install_solc("0.6.0")
compiled_sol = compile_standard(
{
"language": "Solidity",
"sources": {"SimpleStorage.sol": {"content": simple_storage_file}},
"settings": {
"outputSelection": {
"*": {"*": ["abi", "metadata", "evm.bytecode", "evm.sourceMap"]}
}
},
},
solc_version="0.6.0",
)
# print(compiled_sol)
with open("compiled_code.json", "w") as file:
json.dump(compiled_sol, file)
bytecode = compiled_sol["contracts"]["SimpleStorage.sol"]["SimpleStorage"]["evm"][
"bytecode"
]["object"]
# get abi
abi = compiled_sol["contracts"]["SimpleStorage.sol"]["SimpleStorage"]["abi"]
# for connecting to ganache
w3 = Web3(Web3.HTTPProvider("HTTP://127.0.0.1:7545"))
chain_id = 1337
my_address = "0x0fcFb4dBbacfD7AcE1829b228766cd39b4791347"
private_key = os.getenv(
"PRIVATE_KEY2"
) # this is after you define private key in terminal s
# print(private_key) # private key gets
# create contract in python
SimpleStorage = w3.eth.contract(abi=abi, bytecode=bytecode)
# print(SimpleStorage)
nonce = w3.eth.getTransactionCount(my_address)
# print(nonce)
# 1 build a transaction
# 2 sig a transactiob
# 3 send a transactjkns
transaction = SimpleStorage.constructor().buildTransaction(
{
"gasPrice": w3.eth.gas_price,
"chainid": chain_id,
"from": my_address,
"nonce": nonce,
}
)
# print(transaction)
signed_txn = w3.eth.account.sign_transaction(transaction, private_key=private_key)
print(signed_txn)
I get a traceback problem starting with the signed transaction: signed_txn = w3.eth.account.sign_transaction(transaction, private_key=private_key)
strong text
on my .env file , I have defined the PRIVATE KEY from ganache:
export const PRIVATE_KEY2 = "some private key from ganache"
export const SOME_OTHER_VAR = 7**
I also have a gitnore file: .env
I guess I just re installed dotenv....some how that worked, even though I probably installed it previously....somehow...
I entered pip install python-dotenv on the terminal, and things worked from there. I am following some of Patric Alpha's soldity/python tutorial...