NomicFoundation / hardhat

Hardhat is a development environment to compile, deploy, test, and debug your Ethereum software.
https://hardhat.org
Other
7.23k stars 1.39k forks source link

Issue when working on bsc fork #2195

Closed bios42eth closed 2 years ago

bios42eth commented 2 years ago

Hi, I have an issue with forking BSC mainnet. I don't really know if this is an issue or misuse. I have this piece of code running fine on bsc mainnet, but not when I'm forking bsc mainnet. I guess I'm doing something wrong...

Console :

npx hardhat run dist/scripts/checkRouter.js --network bscmainnet
No need to generate any newer typings.
Router address: 0x10ED43C718714eb63d5aA57B78B54704E256024E
WETH :  0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c
Factory :  0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73

Console :

npx hardhat run dist/scripts/checkRouter.js --network localhost
No need to generate any newer typings.
Router address: 0x10ED43C718714eb63d5aA57B78B54704E256024E
Error: call revert exception (method="WETH()", errorArgs=null, errorName=null, errorSignature=null, reason=null, code=CALL_EXCEPTION, version=abi/5.5.0)

On the fork console I have this message : WARNING: Calling an account which is not a contract It sounds like it cannot find the contract.

Localhost is a fork started with : npx hardhat node --fork https://bsc-dataseed.binance.org/

Here is my network config :

  localhost: {
    url: "http://127.0.0.1:8545",
  },
  bscmainnet: {
    url: "https://bsc-dataseed.binance.org/",
    chainId: 56,
  },

Here is the code :

import { ethers } from "hardhat";
import UniswapV2Router from '@uniswap/v2-periphery/build/IUniswapV2Router02.json'

async function main() {
  let pcs_routerAddress = "0x10ED43C718714eb63d5aA57B78B54704E256024E"

  let router = new ethers.Contract(pcs_routerAddress, JSON.stringify(UniswapV2Router.abi), ethers.provider)

  console.log("Router address:", router.address)
  console.log("WETH : ", await router.WETH())
  console.log("Factory : ", await router.factory())
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
      console.error(error);
      process.exit(1);
  });
bios42eth commented 2 years ago

Btw : tested on bsc tesnet and it works. It sounds like the bsc mainnet cannot be forked.

bios42eth commented 2 years ago

I did add some more info : I'm getting a wallet balance. On the direct access to bsc mainnet, it works fine. When I'm going through the fork, the balance is 0, whereas there is some BNB on the wallet. I've tried an all public bsc nodes, it doesn't work on mainnet. Once again : it works on testnets...

abhishekr700 commented 2 years ago

Came across this while trying to figure it out, just urious what rpc url are you using @bgantaume

bios42eth commented 2 years ago

npx hardhat node --fork https://bsc-dataseed.binance.org/ @abhishekr700

abhishekr700 commented 2 years ago

I see, I'm running into the same issue. @bgantaume

bios42eth commented 2 years ago

I see, I'm running into the same issue. @bgantaume

I don't know if it's a good news or not... :D Please not that it works just fine on the bsc testnet.

I'd be glad to dig deeper, but I just don't know where to look at...

bios42eth commented 2 years ago

@abhishekr700 @fvictorio Any idea of where I should start looking at ?

abhishekr700 commented 2 years ago

Nope, I'm switching to testnet for the meantime

fvictorio commented 2 years ago

Confirmed. Reproduction: start a fork using https://bsc-dataseed.binance.org/, then start a console with npx hardhat console --network localhost and execute:

await network.provider.send("eth_getCode", ["0x10ED43C718714eb63d5aA57B78B54704E256024E", "latest"])

You'll get a value of 0x. This doesn't happen if you connect to that RPC URL directly.

Jovonni commented 2 years ago

+1, forking mainnet or testnet is not working with

networks: {
    hardhat: {
      forking: {
        url: 'https://data-seed-prebsc-1-s1.binance.org:8545/',
        enabled: true,
      },

      accounts: {
        accountsBalance: '1000000000000000000000000', // 1 mil ether,
      },
    },
...
}

Also running with:

hardhat node --network hardhat --show-stack-traces --fork https://data-seed-prebsc-1-s1.binance.org:8545

Are you guys getting testnet to fork with hh? I also tried RPCs:

https://bsc-dataseed1.defibit.io
https://bsc-dataseed.binance.org
Jovonni commented 2 years ago

Could it be that those RPCs are not connecting to archival nodes?

Jovonni commented 2 years ago

Nope, I'm switching to testnet for the meantime

How are you getting forking of the testnet to work?

Jovonni commented 2 years ago

Update, I can fork both main and testnets with ganache.

ganache-cli -f <RPC_URL> -m MNEMONIC-BIP39-STYLE

But still no luck with hardhat directly.

atillabirer commented 2 years ago

Just use moralis bro

cryptowen commented 2 years ago

ran in to the same problem

valle-xyz commented 2 years ago

Just use moralis bro

This is true.

Disclaimer: I am not affiliated to moralis. This is just the best solution I have seen.

Why? Because the BSC RPC Endpoints are not achieve-nodes. Moralis on the other hand offers achieve nodes.

github-actions[bot] commented 2 years ago

This issue was marked as stale because it didn't have any activity in the last 30 days. If you think it's still relevant, please leave a comment indicating so. Otherwise, it will be closed in 7 days.

github-actions[bot] commented 2 years ago

This issue was closed because it has been stalled for 7 days with no activity.

rana-ahmed-ali-invozone commented 1 year ago

Is Binance smart chain ( bsc mainnet) forkable or not? I need to get some smart contracts, already deployed on mainnet. Please reply @fvictorio @huwenchao @bios42eth @Jovonni @abhishekr700 . Thank you!