/pancake-swap-v1-bsc-testnet

Pancake swap v1 on Binance Smart Chain (BSC) Testnet

Primary LanguageTypeScript

Pancake Swap V1 on BSC Testnet

This repo contains all source code of Pancake Swap V1. It has modified to run on Binance Smart Chain (BSC) Testnet.
My original purpose is to use it to test my DApps that connect to Pancake Swap. Using the BSC Testnet environment has helped me a lot in reducing the cost and time of DApps development. I see thay it very useful for developers so I shared this version.

If you want to use Pancake Swap v1 on BSC Testnet, please to the link: https://pancake.kiemtienonline360.com/
If you want to implement your own Pancake Swap, please see detailed instructions below.

Step by step to deploy Pancake Swap V1

  • Step 1: Deploy Pancake Factory Contract
  • Step 2: Deploy Pancake Router Contract
  • Step 3: Deploy Pancake Swap Interface

Of these three steps, step 3 will take the longest time, step 2 need for a little attention. It takes time for me, but I will guide you how to deploy them in just 10 minutes.

Before you start, you need to install NodeJs (Version 8 or later) and GIT. In addition, you must also have experience to deploy contracts on Remix. You also install Metamask extension on Chrome browser and added "BSC Testnet".

Step 1: Deploy Pancake Factory Contract

You can use Truffle Remix to build and deploy the contract. But for simplicity you should use Remix.

The steps are as follows:

  • 1. Install required libraries
    Go to "pancake-swap-core-v1" folder and run npm install:
         cd pancake-swap-core-v1
         npm install
  • 2. Join contract files to 1 file
    I created a small tool to help join the contract files into 1 file. Run below command to join:
         mkdir build
         node tools/merge-contract.js
    You can see the file PancakeFactory.sol.merge.txt in build directory.
  • 3. Deploy on Remix
    - Go the Remix website: https://remix.ethereum.org
    - Create new contract file PancakeFactory.sol on Remix and then copy all content in PancakeFactory.sol.merge.txt to the PancakeFactory.sol file.
    - Click left menu item "Solidity complier", select "0.5.16" in COMPILER section. And then click "Complile PancakeFactory.sol", compile success and no error.
    - Click left menu item "Deploy & run transactions", select "Injected Web3" in ENVIRONMENT section. This step request to connect an account in Metamask.
    Select "PancakeFactory - ..." in CONTRACT item. Enter your any bsc wallet address in "address_feeToSetter" input. And then click "Deploy" button, and wait.
    - Once deployed, you will see the contract address of PancakeFactory. In the "Deployed Contracts" section, you will see the new deployed contract. Expand it and you will get INIT_CODE_PAIR_HASH. You need to store them, and they will be used in Step 2.

For example with my deployed contract:

Note: Sometimes the https://remix.ethereum.org page has a problem to create new sol file, you can use the link http://remix.ethereum.org (Not HTTPS)

Step 2: Deploy Pancake Router Contract

The steps are almost the same as Step 1, but with some notices.

The steps are as follows:

  • 1. Install required libraries
    Go to "pancake-swap-router-v1" folder and run npm install:
         cd pancake-swap-router-v1
         npm install
  • 2. Slightly edited in the source of the contract
    Open the file "contracts/libraries/PancakeLibrary.sol", go to line 25 (In function pairFor()), change the hexa string to INIT_CODE_PAIR_HASH of PancakeFactory in Step 1. And then save the file.
  • 3. Join contract files to 1 file
    I created a small tool to help join the contract files into 1 file. Run below command to join:
         mkdir build
         node tools/merge-contract.js
    You can see the file PancakeRouter01.sol.merge.txt in build directory.
  • 4. Deploy on Remix
    - Go the Remix website: https://remix.ethereum.org
    - Create new contract file PancakeRouter.sol on Remix and then copy all content in PancakeRouter01.sol.merge.txt to the PancakeRouter.sol file.
    - Click left menu item "Solidity complier", select "0.6.6" in COMPILER section, check "Enable optimization" in COMPILER CONFIGURATION section. And then click "Complile PancakeRouter.sol", compile success and no error.
    - Click left menu item "Deploy & run transactions", select "Injected Web3" in ENVIRONMENT section. This step request to connect an account in Metamask.
    Select "PancakeRouter01 - ..." in CONTRACT item. Enter the PancakeFactory address (In Step 1) and WBNB address 0xae13d989daC2f0dEbFf460aC112a837C89BAa7cd as two parameters when initializing PancakeRouter. And then click "Deploy" button, and wait.
    - Once deployed, you will see the contract address of PancakeRouter. You need to store this address, and it will be used in Step 3.

For example with my deployed contract:

Note: You can use the above WBNB address or you can create a new WBNB contract yourself.

Step 3: Deploy PancakeSwap Interface

This step is the most complicated because there are many modifications.

The steps are as follows:

  • 1. Install required libraries
    Go to "pancake-swap-interface-v1" folder and run npm install:
         cd pancake-swap-interface-v1
         npm install --global yarn
         npm install
    And then copy .env.development to .env
  • 2. Modify source code
    There are quite a few places to edit, I list them as below, not necessarily in order:
    • Go to the file "src/constants/token/pancakeswap.json" to add or edit the default supported tokens on Testnet. Tokens on BSC Testnet you can deploy yourself, token source code can refer to: USDT, ETH, BUSD, DAI. Remember to add images for your new tokens in the "public/images/coins" folder with the token address in lowercase.
    • Go to the file "src/constants/index.ts", update the value for ROUTER_ADDRESS which is the PancakeRouter address deployed in Step 2. In this file you also edit the default display tokens.
    • I fixed supportedChainIds and BscConnector in file "src/connectors/index.ts" to support only BSC Testnet. You don't need to change this file unless you want to deploy on another network.
    • Currently the menu I have commented on most of the menu items, if you want to change you can edit it in the file "src/components/Menu/config.ts".
    • Open the 3 files "node_modules/@pancakeswap-libs/sdk/dist/constants.d.ts", "node_modules/@pancakeswap-libs/sdk/dist/sdk.cjs.development.js" and "node_modules/@pancakeswap-libs/sdk/dist/sdk.cjs.production.min.js", update the values for two variables: FACTORY_ADDRESS and INIT_CODE_HASH.
    • If the WBNB you use is not 0xae13d989daC2f0dEbFf460aC112a837C89BAa7cd, you should update the new WBNB address in the two files "node_modules/@pancakeswap-libs/sdk/dist/sdk.cjs.development.js" and "node_modules/@pancakeswap-libs/sdk/dist/sdk.cjs.production.min.js".
  • 3. Run test at local
    You run the following command to test locally:
         yarn start
    If you see the error "Type 'MergedState' is not assignable to type..." in the file "src/state/index.ts", please see the link laptrinhbockchain#1 to fix the error.
  • 4. Deploy to Github or your host
    You open the file ".env.production" to edit the configuration, especially the PUBLIC_URL configuration. Then you type the following command to build:
         yarn build
    Then you put all the files in the build directory to github or host to run.

You can find out more at https://kiemtienonline360.com/huong-dan-trien-khai-pancake-swap-v1-tren-moi-truong-binance-smart-chain-bsc-testnet/