# compile_standart is going to be the main function that we will use to compile this code.
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("Installing...")
install_solc("0.6.0")
# print(simple_storage_file)
# compile our solidity
compiled_sol = compile_standard(
{
"language": "Solidity",
"sources": {"SimpleStorage.sol": {"content": simple_storage_file}},
"settings": {
"outputSelection": {
"*": {
"*": [
"abi",
"metadata",
"evm.bytecode",
"evm.bytecode.sourceMap",
] # (ABI=Application Binary Interface)EVM (Ethereum Virtual Machine) is the core component of the Ethereum network
}
}
},
},
solc_version="0.6.0",
)
# print(compiled_sol)
with open("compiled_code.json", "w") as file: # w means it wil wright from it
json.dump(
compiled_sol, file
) # is it's going to take our compiled soul jason variable and just dump it into this (file) here
# but still it is going to keep it in json syntax
# get bytecode
bytecode = compiled_sol["contracts"]["SimpleStorage.sol"]["SimpleStorage"]["evm"][
"bytecode"
]["object"]
# get abi
abi = json.loads(
compiled_sol["contracts"]["SimpleStorage.sol"]["SimpleStorage"]["metadata"]
)["output"]["abi"]
# for connecting to ganache
w3 = Web3(Web3.HTTPProvider("https://127.0.0.1:7545"))
chain_id = 5777
my_address = "0x630Ee320BcE235224184A31FC687a5D183142BB9"
private_key = "0xd3cf1f678e8a78ace754cf57bd6ebcb28852e9657bb371951d72bbb5a0a3f413"
# private_key = os.getenv(" PRIVATE_KEY ")
# print(private_key)
# Create the contract in Python
SimpleStorage = w3.eth.contract(abi=abi, bytecode=bytecode)
# print(SimpleStorage)
# Get the latest transaction
**nonce = w3.eth.getTransactionCount(my_address)**(I'm having this error in this line)[![**enter image description here**][1]][1]
# print (nounce)
# we could see that the number of transaction=0 because we haven't made any
# 1. Build a transaction
# 2. Sign a transaction
# 3 . Send a transaction
transaction = SimpleStorage.constructor().buildTransaction(
{" 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) # this is how we sign a transaction
tx_hash = w3.eth.send_raw_transaction(signed_txn.rawTransaction)
# 1 good practice to do when we are sending
# a transaction is to wait for some block confirmation to happen
# this will have our code stop and wait for this transaction hash to go through
tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
# working with contracts
# Contract Address
# Contract ABI
# Working with deployed Contracts
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 a favorite number
print(simple_storage.functions.retrieve().call())
# store some value into this 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.rawTransaaction)
tx_receipt = w3.eth.wait_for_transaction_receipt(send_store_tx)
#[1]: https://i.stack.imgur.com/sPikF.png
During handling of the above exception, another exception occurred:
During handling of the above exception, another exception occurred:
During handling of the above exception, another exception occurred: During handling of the above exception, another exception occurred:
During handling of the above exception, another exception occurred: