reactjsnext.jsbinance-smart-chainbscpancakeswap

How to run PancakeSwap V2 with testnet?


I'm testing the Pancakeswap v2 frontend repo on local host, but I can't switch to the testnet. (https://github.com/pancakeswap/pancake-frontend) I get an error when I update NEXT_PUBLIC_CHAIN_ID = "56" to NEXT_PUBLIC_CHAIN_ID = "97" which is valid for testnet.

Unhandled Runtime Error Error: call revert exception (method="canClaim(address)", errorArgs=null, errorName=null, errorSignature=null, reason=null, code=CALL_EXCEPTION, version=abi/5.5.0)

Unhandled Runtime Error Error: call revert exception (method="aggregate((address,bytes)[])", errorArgs=null, errorName=null, errorSignature=null, reason=null, code=CALL_EXCEPTION, version=abi/5.5.0)

How can I use Pancakeswap V2 with bsc testnet or kovan testnet?


Solution

  • Update March 2022
    Pancakeswap now includes the testnet :)

    Old Answer

    This is not the solution, this is workaround.
    These are the errors because some of the pancakeswap contracts aren't deployed on the testnet. You can see the src\config\constants\contracts.ts.
    I'm proposing the workaround to work with your testnet, this isn't the solution as explained by chef-jojo.
    chef-jojo comment The workaround is simple. You need to update a few files. The most important of them is .env.development. These are the env variables we need to change.

    NEXT_PUBLIC_CHAIN_ID = "56"
    NEXT_PUBLIC_NODE_1 = "https://bsc-dataseed1.ninicoin.io"
    NEXT_PUBLIC_NODE_2 = "https://bsc-dataseed1.defibit.io"
    NEXT_PUBLIC_NODE_3 = "https://bsc-dataseed.binance.org"
    NEXT_PUBLIC_NODE_PRODUCTION = "https://nodes.pancakeswap.com"
    

    For BscTestnet the chain id is 97. And you can check the RPC from https://docs.binance.org/smart-chain/developer/rpc.html. I'm selecting the first three and replacing them. We don't need the NEXT_PUBLIC_NODE_PRODUCTION so we will comment this out for our development on testnet.
    After changes it will look something like this.

    NEXT_PUBLIC_CHAIN_ID = "97"
    NEXT_PUBLIC_NODE_1 = "https://data-seed-prebsc-1-s1.binance.org:8545"
    NEXT_PUBLIC_NODE_2 = "https://data-seed-prebsc-2-s1.binance.org:8545"
    NEXT_PUBLIC_NODE_3 = "https://data-seed-prebsc-1-s2.binance.org:8545"
    
    # ; NEXT_PUBLIC_NODE_PRODUCTION = "https://nodes.pancakeswap.com"
    

    Your wallet will now connect to the bscTestnet and you can also make transactions with it. The browser will still show call revert exception but you just need to ignore them by clicking the cross button on the upright corner.
    You can add your chain id 97 contract and try to run them. Here are a few tips to add your contracts and make an instance of it.

    1. Add your contract ABI in src/config/abi/<your name>.json
    2. Add your contract address in src\config\constants\contracts.ts. Follow the styles of other contract addresses and leave the 57 property empty. e.g
      export default {
        contractName: {
          56: '',
          97: '0x0000000000000000000000000000000000',
        },
      }
      
    3. Add your address getter function to src\utils\addressHelpers.ts. e.g
      export const getContractNameAddress = () => {
        return getAddress(addresses.mockToken);
      };
      
    4. Add your contract getter function to src\utils\contractHelpers.ts. Import your Abi from step 1. e.g
      export const getMockTokenContract = (
        signer?: ethers.Signer | ethers.providers.Provider
      ) => {
        return getContract(contractNameAbi, getContractNameAddress(), signer);
      };
      
    5. Add your contract hook in src\hooks\useContract.ts. e.g
      export function useContractNameContract(): Contract | null {
        const { library } = useActiveWeb3React();
        return useMemo(() => getContractNameContract(library.getSigner()), [library]);
      }
      
    6. Now use the hook in step 6 to get your contract. e.g
      const contract = useContractNameContract();
      
    7. Call the read function like this <contract name>.<contact function>(<parameters separated with comma>) e.g
      contract.balanceOf("0x000000000000").then(console.info)
      
    8. Call the write function using the hook name useCallWithGasPrice. Make sure your wallet is connected before the calls. e.g
      const { callWithGasPrice } = useCallWithGasPrice();
      const contract = useContractNameContract();
      const callingFunction = async () => {
        const tx = await callWithGasPrice(contract, "balanceOf", ["0x000000000000000"]);
        const receipt = tx.wait();
        console.info(`Called for balanceOf: `, receipt)
        return receipt;
      };
      
      // call the function
      callingFunction();