I cannot find the contract address for the BSC Testnet network

305 Views Asked by At

There's a code:

import json

from eth_account.signers.local import LocalAccount
from web3 import Web3
from web3.middleware import geth_poa_middleware

rpc = "https://data-seed-prebsc-1-s1.binance.org:8545/"

w3 = Web3(Web3.HTTPProvider(rpc))
w3.middleware_onion.inject(geth_poa_middleware, layer=0)
w3.eth.account.enable_unaudited_hdwallet_features()
if not w3.is_connected():
    print("Disconnected")

contract_address = Web3.to_checksum_address("0xfd5af95C12446b60d23e16A4EA95690cE942e5dC")
seed = ""
with open("abi_testnet.json") as file:
    abi_testnet = json.load(file)

account: LocalAccount = w3.eth.account.from_mnemonic(seed)
contract = w3.eth.contract(address=contract_address, abi=abi_testnet)

balance = contract.functions.balanceOf(account.address).call()
print(balance)

I want to get the balance of BNB Smart Chain Testnet wallet, I have 0.1 BNB on it but when I try to get the balance through balanceOf but I end up getting 0 or error.

..............................................

1

There are 1 best solutions below

2
On

The address mentioned in your code holds an ERC20 token contract on the BSC mainnet. But it doesn't hold any contract on the testnet.

Mainnets and testnets are in absolute most cases (there are very few exceptions) separate networks that do not affect each other. If you perform an action (e.g. deploy a contract) on mainnet, it is not automatically reflected on testnet - and vice versa.


Since there is no contract on the specified address on the testnet, there is nothing that could generate the non-empty response to the call to the address. Some caller tools throw an error because there was an unexpected state (empty response, expected 32 bytes), and some decode the empty response as 0.