smartcontractkit / full-blockchain-solidity-course-py

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

Lesson 10: No contract deployed #619

Closed vlasebian closed 2 years ago

vlasebian commented 2 years ago

Hello!

First of all, thank you for providing such a great course on smart contracts!

I am stuck at lesson 10 while trying to get some WETH. I believe it may be something related to how I forked the mainnet, but I couldn't solve the issue.

This is the error that I get when i run brownie run scripts/aave_borrow.py:

ContractNotFound: No contract deployed at 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2

This is how I added the mainnet-fork to brownie networks:

brownie networks add development mainnet-fork cmd=ganache-cli host=http://127.0.0.1 fork=https://eth-mainnet.alchemyapi.io/v2/<KEY> accounts=10 mnemonic=brownie port=8545

This is my code:

# get_weth.py
from brownie import interface, network, config
from scripts.util import get_account

def get_weth():
    """
    Mints WETH by depositing ETH.
    """

    account = get_account()
    weth = interface.IWeth(config["networks"][network.show_active()]["weth_token"])
    tx = weth.deposit({"from": account, "value": 0.1 * 10 ** 18})
    tx.wait(1)
    print("Received 0.1 WETH.")

def main():
    get_weth()
# aave_borrow.py
from brownie import network, config
from scripts.util import get_account
from scripts.get_weth import get_weth

def main():
    account = get_account()
    erc20_address = config["networks"][network.show_active()]["weth_token"]
    if network.show_active() in ["mainnet-fork"]:
        get_weth()
# util.py (this is helper_scripts.py, I renamed it)
from brownie import accounts, config, network

LOCAL_BLOCKCHAIN_ENVIRONMENTS = [
    "development",
    "ganache",
    "ganache-local",
    "mainnet-fork",
]

def get_account(index=None, id=None):
    if index:
        return accounts[index]

    if network.show_active() in LOCAL_BLOCKCHAIN_ENVIRONMENTS:
        return accounts[0]

    if id:
        return accounts.load(id)

    if network.show_active() in config["networks"]:
        return accounts.add(config["wallets"]["from_key"])

    return None
# brownie-config.yml
networks:
  default: mainnet-fork
  development:
    verify: False
  kovan:
    weth_token: '0xd0a1e359811322d97991e03f863a0c30c2cf029c' 
    verify: True
  mainnet-fork:
    weth_token: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2'
    verify: True

wallets:
  from_key: ${PRIVATE_KEY}

dotenv: .env

What am I doing wrong here?

Thanks, Vlad

cromewar commented 2 years ago

Hello there @vlasebian This line: if network.show_active() in ["mainnet-fork"]: should be: `if network.show_active() in LOCAL_BLOCKCHAIN_ENVIRONMENTS:

Also you must import LOCAL_BLOCKCHAIN_ENVIRONMENTS from helpful scripts.

vlasebian commented 2 years ago

I made the suggested changes, this is my updated aave_borrow.py:

from brownie import network, config
from scripts.util import LOCAL_BLOCKCHAIN_ENVIRONMENTS, get_account
from scripts.get_weth import get_weth

def main():
    account = get_account()
    erc20_address = config["networks"][network.show_active()]["weth_token"]
    if network.show_active() in LOCAL_BLOCKCHAIN_ENVIRONMENTS:
        get_weth()

However, I still get the same error as before:

ContractNotFound: No contract deployed at 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2
cromewar commented 2 years ago

It seems a problem with you alchemy then, make sure your app looks like this: image

Also delete the mainnet-fork network and try adding again.

vlasebian commented 2 years ago

I had the environment set to Staging, I changed it to Development. Then, I removed the network and added it again like this:

brownie networks delete mainnet-fork
brownie networks add development mainnet-fork cmd=ganache-cli host=http://127.0.0.1 fork=https://eth-mainnet.alchemyapi.io/v2/<key> accounts=10 mnemonic=brownie port=8545

It still gives me the same error :(

cromewar commented 2 years ago

This is weird, the error is saying that there is not a contract on that address, but that address actually exists: https://etherscan.io/address/0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2 just for curiosity, are you adding the --network identifier when you run it?

vlasebian commented 2 years ago

This is how I run it:

brownie run scripts/get_weth.py --network mainnet-fork
cromewar commented 2 years ago

Jum this is weird, @PatrickAlphaC Have you any clue on this?

PatrickAlphaC commented 2 years ago

Odd... What happens when you do

brownie console --network mainnet-fork

And then in the shell

from scripts.helpful_scripts import 
weth = interface.IWeth(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2)
print(weth)

Or also try:

print(Contract(f"{pair}.data.eth").latestAnswer() / 1e8)

If both of those don't work, it means you're not actually connecting to a mainnet fork.

brownie networks add development mainnet-fork cmd=ganache-cli host=http://127.0.0.1 fork=<FORK> accounts=10 mnemonic=brownie port=8545
cromewar commented 2 years ago

Yep I was thinking the same, is not connecting to mainnet fork, @vlasebian did something Patrick said helped you.

EzeCerino commented 2 years ago

Hello, I'm having the same problem on my code. Try to add again the mainnet-fork with @cromewar specs. Also tried what @PatrickAlphaC ask for and it seems that is not connecting to a mainnet-fork. @vlasebian you were able to solve this? Anything else that I can check on this?

EzeCerino commented 2 years ago

Okay, I think I solved it, but please let me know if this is something that it's okay to do. So I went to settings in my ganache app and under server tab I turned this on and add the alchemy fork address. image the I ran:

PS C:\Users\ezequ\demos\aave_brownie> brownie run scripts/aave_borrow.py --network mainnet-fork
INFORMACIÓN: no se pudo encontrar ningún archivo para los patrones dados.
Brownie v1.17.2 - Python development framework for Ethereum

AaveBrownieProject is the active project.
C:\Users\ezequ\AppData\Local\Programs\Python\Python39\lib\site-packages\brownie\network\main.py:44: BrownieEnvironmentWarning: Development network has a block 
height of 14495918
  warnings.warn(
Attached to local RPC client listening at '127.0.0.1:7545'...

Running 'scripts\aave_borrow.py::main'...
Transaction sent: 0xb23bd37f9b1d3f361e93dffa6e06392d29c27012367f7a73c7e3d9e05116a842
  Gas price: 0.0 gwei   Gas limit: 6721975   Nonce: 0
  Transaction confirmed   Block: 14495919   Gas used: 28738 (0.43%)

  Transaction confirmed   Block: 14495919   Gas used: 28738 (0.43%)

Recieved 0.1 weth

something that kept my attention was this: BrownieEnvironmentWarning: Development network has a block height of 14495918

Is there something else to set up in Brownie?

Also, when I click under blocks or transactions tabs on my ganache app this bug appears image

It's working now but let me know if there is something else to set up on my ganache up.

MayankB02 commented 2 years ago

Hi , thank you for such a great course . I am facing the same issue. If not solve how can I possibly circumvent this problem?

cromewar commented 2 years ago

hello @EzeCerino and @MayankB02 sorry for the delay, please go to %APPDATA% and delete the ganache folder, and then try opening it again.

MayankB02 commented 2 years ago

Thanks @cromewar . Its working now .