smartcontractkit / full-blockchain-solidity-course-py

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

Lesson 4 > Interacting with Our Contract in Python & Web3.py > ganache-cli issue #1532

Open boten500 opened 2 years ago

boten500 commented 2 years ago

time in the video course - 4:21:43

what is the issue? after I ran ganache-cli on vscode I changed the private key, the public key and the HTTP provider and then I closed and reopen vscode to refresh my private key in it after that I ran vscode again and got this error...

requests.exceptions.ConnectionError: HTTPConnectionPool(host='127.0.0.1', port=8545): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0000024465084FD0>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))

more info: the reason I close and reopen vscode is because otherwise vscode won't find my private key in the environment variable. I got just the "ganache" and not the "ganache-cli" also I downloaded the package from npm and not from yarn.

the solution I was looking for is if I find a way to keep the node working after I close vscode so it might solve it but I didn't find a way to do it.

my code:

from itertools import chain
from solcx import compile_standard, install_solc
import json
from web3 import Web3
import os

install_solc("0.8.7")

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

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

# 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:8545"))
chain_id = 1337
my_address = "0x9310E8F692aC53C23aD6051ec22eb365D9c0b114"
private_key = os.getenv("PRIVATE_KEY")

# Create a 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 trnsaction
tx_hash = w3.eth.send_raw_transaction(signed_txn.rawTransaction)
print("Deploying contract...")
tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
print("Contract was deployed!")

# working with a 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 return values
# Transact > Actually making a state change
print(simple_storage.functions.retrieve().call())
store_transaction = simple_storage.functions.store(15).buildTransaction(
    {
        "chainId": chain_id,
        "gasPrice": w3.eth.gas_price,
        "from": my_address,
        "nonce": nonce + 1,
    }
)
sign_store_txn = w3.eth.account.sign_transaction(
    store_transaction, private_key=private_key
)

send_store_tx = w3.eth.send_raw_transaction(sign_store_txn.rawTransaction)
print("Updating contract...")
tx_receipt = w3.eth.wait_for_transaction_receipt(send_store_tx)
print(simple_storage.functions.retrieve().call())
print("Updated!")

huge thnaks in advance for the helpers!

baris-celebi commented 2 years ago

i have same problem. i couldnt find any solution.