Skip to content

Instantly share code, notes, and snippets.

@Linch1
Created December 22, 2021 12:00
Show Gist options
  • Select an option

  • Save Linch1/1353b6c9718c761f1ac4b79e9ccb6776 to your computer and use it in GitHub Desktop.

Select an option

Save Linch1/1353b6c9718c761f1ac4b79e9ccb6776 to your computer and use it in GitHub Desktop.

Revisions

  1. Linch1 created this gist Dec 22, 2021.
    40 changes: 40 additions & 0 deletions calculatePair.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    /*
    In this script you can undestand how to compute offchain an address of any pancakeswap pair
    given the pair tokens
    This script can be used also to compute any pancakeswap similar dex like
    uniswap ( ethereum )
    quickswap ( polygon )
    by changing the value of the variables
    _hexadem
    _factory
    */

    const Web3 = require('web3');

    let tokenA = "0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c"; // change me!
    let tokenB = "0xe9e7cea3dedca5984780bafc599bd69add087d56"; // change me!

    let web3 = new Web3("PROVIDER"); // change me!

    function getPair(tokenA, tokenB) {

    let _hexadem = '0x00fb7f630766e6a796048ea87d01acd3068e8ff67d078148a3fa3f4a84f69bd5';
    let _factory = "0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73";
    let [token0, token1] = tokenA < tokenB ? [tokenA, tokenB] : [tokenB, tokenA];

    let abiEncoded1 = web3.eth.abi.encodeParameters(['address', 'address'], [token0, token1]);
    abiEncoded1 = abiEncoded1.split("0".repeat(24)).join("");
    let salt = web3.utils.soliditySha3(abiEncoded1);
    let abiEncoded2 = web3.eth.abi.encodeParameters(['address', 'bytes32'], [_factory, salt]);
    abiEncoded2 = abiEncoded2.split("0".repeat(24)).join("").substr(2);
    let pair = '0x' + Web3.utils.soliditySha3( '0xff' + abiEncoded2, _hexadem ).substr(26);
    return pair
    }

    console.log( 'PAIR: ', getPair(tokenA, tokenB) )