smartcontractkit / full-blockchain-solidity-course-py

Ultimate Solidity, Blockchain, and Smart Contract - Beginner to Expert Full Course | Python Edition
MIT License
10.67k stars 2.89k forks source link

Type Error due to environment variable and ganache #1863

Open Amandeep404 opened 1 year ago

Amandeep404 commented 1 year ago

Hey can someone help me...I am facing the same issues on windows as many are facing but unable to correct it. Shows the same error as everyone else here :

in sign_transaction
    raise TypeError(
TypeError: from field must match key's 0x090dC592fDF4d7adB16CCCf15262335DE66eD316, but it was 0x7052b17De0ccc1De68437b03b197bF3057E45807

This happened when i closed the ganache and terminal and when i came back to work on this project its showing this error

Here is the whole deploy.py file

from solcx import compile_standard
import json
import os
from web3 import Web3
from dotenv import load_dotenv

load_dotenv()

with open("./SimpleStorage.sol", 'r') as f:
    simple_storage_file = f.read()

compiled_sol = compile_standard(
    {
        "language": "Solidity",
        "sources": {"SimpleStorage.sol": {"content": simple_storage_file}},
        "settings": {
            "outputSelection": {
                "*": {
                    "*": ["abi", "metadata", "evm.bytecode", "evm.bytecode.sourceMap"]
                }
            }
        },
    }
)

with open("compiled_code.json", "w") as f:
    json.dump(compiled_sol, f)

# get bytecode
# this is iterating through the compiled_sol.json and getting the 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"))
# print(w3.eth.chain_id) # This is the chain id of the ganache network which returns 1337
chain_id = 1337
my_address = os.getenv("My_ADDRESS")
private_key = os.getenv("MY_PRIVATE_KEY")

# Create the contract in python
SimpleStorage = w3.eth.contract(abi=abi, bytecode=bytecode)
print(SimpleStorage)

# Get the latest transaction count i.e nonce value
nonce = w3.eth.get_transaction_count(my_address)
print(nonce)

# Create the transaction
transaction = SimpleStorage.constructor().build_transaction({"chainId": chain_id, "from": my_address, "nonce": nonce})  
signed_txn = w3.eth.account.sign_transaction(transaction, private_key=private_key)

# Send the raw transaction
# tx_hash = w3.eth.send_raw_transaction(signed_txn.rawTransaction)
# tx_reciept = w3.eth.wait_for_transaction_receipt(tx_hash)

# working with the contract
        #for that we need contact address and ABI
# simple_storage = w3.eth.contract(address=tx_reciept.contractAddress, abi=abi)
# Call -> Simulate making the call and getting a return value
# Transact -> Actually make a state change

print(w3.eth.get_balance(my_address))
print(my_address)
print(private_key)

And this is my .env file

export MY_ADDRESS ="0xb117deF084176e5Fdac7d979aa6524e3c8282049"
export MY_PRIVATE_KEY ="0x31792fce4b6f6408398f3d73f8b2f438b44c515fffd576eee1766463ecdf7c93"

Help will be Appreciated !!

BrightTNut commented 1 year ago

pls make sure your using correct private key ,, I guess this is issue I think some thing is wrong in "from" field in using private key , please notify is it usefull or not