-
Deploy.py_____ from solcx import compile_standard, install_solc
import json
from web3 import Web3
import os
from dotenv import load_dotenv
load_dotenv()
install_solc("0.6.0")
from solcx import compile_standard
with open("./SimpleStorage.sol", "r") as file:
simple_storage_file = file.read()
# Compile Our Solidity
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",
)
with open("compiled_code.json", "w") as file:
json.dump(compiled_sol, file)
# get bytecode
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
version = 5777
my_address = "0x4434398D50Bf378c47C054B8d91E5862FfDad20C"
private_key = os.getenv("PRIVATE_KEY2")
# Create the contract in Python
SimpleStorage = w3.eth.contract(abi=abi, bytecode=bytecode)
# Get the latest transaction
nonce = w3.eth.getTransactionCount(my_address)
# 1. Build a transaction
# 2. Sign a transaction
# 3. Send a transaction
transaction = SimpleStorage.constructor().buildTransaction(
{
"chainId": chain_id,
"gasPrice": w3.eth.gas_price,
"from": my_address,
"nonce": nonce,
}
)
signed_txn = w3.eth.account.sign_transaction(transaction, private_key=private_key)
# Send this signed transaction
print("Deploying contact...")
tx_hash = w3.eth.send_raw_transaction(signed_txn.rawTransaction)
tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
print("Deployed!")
# Working with Contract, you need:
# Contract ABI
# Contract Address
simple_storage = w3.eth.contract(address=tx_receipt.contractAddress, abi=abi)
# Call -> Simulate making the call and getting a return value
# Transact -> Actually make a state change
# Initial value of favorite number
print(simple_storage.functions.retrieve().call())
print("Updating Contract...")
store_transaction = simple_storage.functions.store(15).buildTransaction(
{
"chainId": chain_id,
"gasPrice": w3.eth.gas_price,
"from": my_address,
"nonce": nonce + 1,
}
)
signed_store_txn = w3.eth.account.sign_transaction(
store_transaction, private_key=private_key
)
send_store_tx = w3.eth.send_raw_transaction(signed_store_txn.rawTransaction)
tx_receipt = w3.eth.wait_for_transaction_receipt(send_store_tx)
print("Updated!")
print(simple_storage.functions.retrieve().call()) .env_____
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hello @Pasonnn Now, based on the error message it seems you are not getting correctly that private key from your env, so please be sure you are using load_dotenv and actually loading the .env key before using on this line: signed_txn = w3.eth.account.sign_transaction(transaction, private_key=private_key) My guess is that you should have a wrong name on the env file.. like not setting up a dot |
Beta Was this translation helpful? Give feedback.
Hello @Pasonnn
First of all please do not share your private keys here, I edited the post and remove it. Remember is a security risk.
Now, based on the error message it seems you are not getting correctly that private key from your env, so please be sure you are using load_dotenv and actually loading the .env key before using on this line:
signed_txn = w3.eth.account.sign_transaction(transaction, private_key=private_key)
My guess is that you should have a wrong name on the env file.. like not setting up a dot
.env
or creating it no the incorrect folder.Please let us know if this make sense for you.