lesson 3: printing retrieve function #1144
-
I was trying to print a function from a simple contract, but I am getting an attribution error Traceback (most recent call last): File "C:\Users\Rishi Garg personal\demos\web3_py_simple_storage\deploy.py", line 141, in address=tx_receipt.contractAdress, abi=abi AttributeError: 'AttributeDict' object has no attribute 'contractAdress' below is my code File "C:\Users\Rishi Garg personal\demos\web3_py_simple_storage\deploy.py", line 141, in # this file is to figure out how we deploy the simple storage solidity code in python
#'first we read the solidity file'
# we use py-solc-x as our compiler
# always use formatter to be blsck (python formatting provider for better code writing)
from numpy import signedinteger
from solcx import compile_standard, install_solc
import json
from web3 import Web3, HTTPProvider
import os # for getting private key
from dotenv import load_dotenv
load_dotenv() # this load_dotenv() looks for this .env file and automatically imports it into our script
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( # this is equal to us calling this compile standard function where we are using the compiler
{
"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) # gives the low level code
with open("compiled_code.json", "w") as file:
json.dump(
compiled_sol, file
) # this line will take our compiled_sol json variable and just dump it into this file but it will keep it in the json syntax
# to deploy, we first need the bytecode of the file so that we can actually deploy it
bytecode = compiled_sol["contracts"]["SimpleStorage.sol"]["SimpleStorage"]["evm"][
"bytecode"
]["object"]
# what we are doing above is when we're typing in all these words like contract, simplestorage, simple storage, is we're walking down the json file here
# its really low level stuff
# getting abi
abi = compiled_sol["contracts"]["SimpleStorage.sol"]["SimpleStorage"]["abi"]
# print(abi)
# to test this, we use ganache, which is a simulated or a fake blockchain that we can use to deploy our smart contracts to, interact like its a real blockchain
# ganache is going to allow us to spin up our own blockchain. Ganache is our javascript Vm
# in remix, metamask is our http provider
# we will use the rpc server url in ganache(top middle situated) to connect to this blockchain
# for connecting to ganache
w3 = Web3(
Web3.HTTPProvider("http://127.0.0.1:7545")
) # remember to use the documentation for web3 web3py.readthedocs.io
# next we are gonna need is the chain id or the network id
chain_id = 1337
my_address = "0x1CB34727217ad03aF4314cf2B1110Bfa4fE94189"
# private_key = "0x112690d5ad13f5c21795a3fa1056a3193061556e42ddad0aa78f141462cbf941" # private Keys must start with 0x (just add it at the front). python is always gonna look for the hexadecimal version of the private key
# never hardcode your private keys, as if we push to github, they can see our private keys.
# environment variables: they are variables that we can set in our terminal and our command lines
# exported environment variables only last the duration of the shell/terminal
private_key = os.getenv(
"PRIVATE_KEY"
) # using this command, our python script can pull our private key from our environment variable
# .env file is where people store envioronment variables. if we do the .env file way, create another file called .gitignore which will make it harder for us to accidently push my .envfolder or .envfile to github
# loading .env in python, gotta install pip install python-dotenv and then go at the top where we write the import statement
print(
os.getenv("PRIVATE_KEY")
) # look at the .env file for the PRIVATE_KEY variable. we can also print private_key
# deploying
SimpleStorage = w3.eth.contract(
abi=abi, bytecode=bytecode
) # this doesnt mean we have deployed, it just means we have a contract down
# print(SimpleStorage)
# definition for nonce(in cryptography): an arbitrary number that can be used just once in a cryptographic communication, this is when we studied about nonce initially
# over here the account nonces just rack the number of transactions made by our account
# we can actually get our nonce by just grabbing our latest transaction count. getting latest transaction
nonce = w3.eth.getTransactionCount(my_address)
# print(nonce)
# to deploy this contract, we need to make a transaction
# we need to build our transactions. 1. Build the contract deploy transaction
# 2. Sign the transaction
# 3. Send the transaction
transaction = SimpleStorage.constructor().buildTransaction(
{
"gasPrice": w3.eth.gas_price, # Otherwise you get a value error if you don't set the gasPrice
"chainId": chain_id,
"from": my_address,
"nonce": nonce,
}
) # every contract has atleast an implied constructor
# we need to have transaction parameters (atleast a couple of parameters we always have to give)
# so we have this transaction buut we need to sign it from somebody. Since we are sending it from our address, our private key is gonna be the only key thats going to work to sign this
# print(transaction)
signed_txn = w3.eth.account.sign_transaction(transaction, private_key=private_key)
# print(signed_txn) # this is a signed transaction
# now we want to send this signed transaction to the blockhain
# send this signed transaction
tx_hash = w3.eth.send_raw_transaction(
signed_txn.rawTransaction
) # we dont print anything but the transaction did go through, if we look at ganache
# one other thing thats really good practice whenever sending a transaction is we wait for some block transactions to happen
tx_receipt = w3.eth.wait_for_transaction_receipt(
tx_hash
) # this will have our code stop and wait for this transaction hash to go through
# now we have deployed the contract, but how do we actually interact and work with the contract
# working with a contract
# we need contract address and the contract abi
simple_storage = w3.eth.contract(
address=tx_receipt.contractAdress, abi=abi
) # creating this simple storage contract so we can interact with it
# when making transactions in the blockchain, there are actually 2 ways that we can interaact with them
# call -> Simulate making the call and getting a return value; calls dont make a state change; likein remix, the blue buttons were the calls, orange ones were the transacts
# transact -> actually make a state change
print(simple_storage.functions.retrieve().call())
# calling a view function with web3.py |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hello @rishigarg256 you have a typo, most likely here: simple_storage = w3.eth.contract(
address=tx_receipt.contractAdress, abi=abi
) Is not contractAddress? with double D |
Beta Was this translation helpful? Give feedback.
Hello @rishigarg256 you have a typo, most likely here:
Is not contractAddress? with double D