How to fix Pancakeswap's Router address in Safemoon

Hey guys, with Safemoon we discovered a huge problem. They hardcoded Pancake v1 Router so all the people that are trying to fork Safemoon will run into the same problem. If you want to leave it hardcoded, then just change it with Pancake v2 Router address 0x10ED43C718714eb63d5aA57B78B54704E256024E.
Otherwise, if you want to avoid another time this situation… maybe with Pancake v3… you can add these lines of code.

//New Pancakeswap router version?
    //No problem, just change it!
    function setRouterAddress(address newRouter) public onlyOwner() {
       //Thank you FreezyEx
        IUniswapV2Router02 _newPancakeRouter = IUniswapV2Router02(newRouter);
        uniswapV2Pair = IUniswapV2Factory(_newPancakeRouter.factory()).createPair(address(this), _newPancakeRouter.WETH());
        uniswapV2Router = _newPancakeRouter;
    }

I didn’t test it, so let me know if there are any problems. I hope it works :grin:

9 Likes

Great solution. Hope this gets more attention from people making clones.

4 Likes

Thanks for sharing @FreezyEx! Just edited your post’s title so it says more to someone scrolling through the guides, please be mindful of it next time! :slight_smile:

2 Likes

Ups You are right, Sorry😁

2 Likes

Hey I randomly ran into this looking for a soluttion to using pancake swap on testnet. I can’t seem to add liquidity and have rand into errors like

Error: Internal JSON-RPC error. { "code": -32000, "message": "execution reverted" }

I am able to create the pair successfully but i can’t add. I have even tried the safemoon code and still the same issue

1 Like

Please share more info. Did you approved the router address to use tokens?

1 Like

Hey,

I’m now attempting with safemoon contract but i updated it to testnet with:

        IUniswapV2Router02 _uniswapV2Router = IUniswapV2Router02(0xD99D1c33F9fC3444f8101754aBC46c52416550D1);

        uniswapV2Pair = IUniswapV2Factory(_uniswapV2Router.factory())
            .createPair(address(this), _uniswapV2Router.WETH());

        uniswapV2Router = _uniswapV2Router;

I’ve tried two ways, through here: https://testnet.bscscan.com/address/0xD99D1c33F9fC3444f8101754aBC46c52416550D1#writeContract

and with the code itself, both don’t work

function addLiquidity(uint256 tokenAmount, uint256 ethAmount) public {
    // approve token transfer to cover all possible scenarios
    _approve(address(this), address(uniswapV2Router), tokenAmount);

    // add the liquidity
    uniswapV2Router.addLiquidityETH{value: ethAmount}(
        address(this),
        tokenAmount,
        0, // slippage is unavoidable
        0, // slippage is unavoidable
        owner(),
        block.timestamp
    );
}

The error comes from the code above.

The pair is working as i can check for it but can’t add.

Even with the safemoon adding liquidity if the contract goes above 500000 * 10**6 * 10**9, it won’t work because then it tells me that there isn’t enough liquidity, although, this makes sense because i never added any. Or? Should it just work regardless and add tthe BNB/SAFEMOON>?

1 Like

to add liqudiity you have to go on router address

1 Like

This is testnet pancake swap router address

Factory: 0x6725F303b657a9451d8BA641348b6761A6CC7a17

Router: 0xD99D1c33F9fC3444f8101754aBC46c52416550D1

Have you tried this in testnet?

The token i am building isn’t a copy of safemoon but it does include a pricing floor similar to safemoon so i need to be able to provide liquidity on demand

I figured since theres works, i could just copy it but i ran into the same issues

2 Likes

yes <i have testest on testnet

1 Like

And you were able to add liquidity?

Can you share?

1 Like

A quick update. I was able to create a pair on 2 test tokens i made with createPair

I then used the same thing but with addLiquidityETH but i get the same error. I’ve tried from remix, truffle, genache, and web3 through module.exports but i still get the same error…

StatusError: Transaction: 0x000.... exited with an error (status 0)

receipt: {
    blockHash: '0x000',
    blockNumber: 00...,
    contractAddress: null,
    cumulativeGasUsed: 2611469,
    from: '0x000',
    gasUsed: 2055052,
    logs: [],
    logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
    status: false,
    to: '0xd99d1c33f9fc3444f8101754abc46c52416550d1',
    transactionHash: '0x000',
    transactionIndex: 1,
    type: '0x0',
    rawLogs: []
  },

I assume maybe pancake hasn’t set this up yet?

Did you add liquidity with WBNB/BNB or what? I’m super confused

1 Like

why do they use the multiple ‘uniswap’ references in the code when its clearly for pancakeswap though?

1 Like

Well yeah i’m trying to test on pancake testnet but i couldnt add liquidity so i grabbed safemoon since i new they’re doing it but same issues. so now im just using their file until i figure it out.

You able to addd liquidity to their testnet?

I was able to ddo it with 2 tokens i made together but not WBNB or BNB

1 Like

Did you approbe Pancake Router?

2 Likes

Because Pancake is Uniswap Fork

2 Likes

Tried both uniswap and pancake.

Although i am having an issue in rinkbey adding liquidity manually into uniswap. I get

Something went wrong
RangeError: Division by zero
    at Function.value (https://app.uniswap.org/static/js/4.5121d2e8.chunk.js:2:46528)
    at t.get (https://app.uniswap.org/static/js/4.5121d2e8.chunk.js:2:31545)
    at new t (https://app.uniswap.org/static/js/4.5121d2e8.chunk.js:2:31890)
    at Function.t.fromFractionalAmount (https://app.uniswap.org/static/js/4.5121d2e8.chunk.js:2:32113)
    at t.r.divide (https://app.uniswap.org/static/js/4.5121d2e8.chunk.js:2:32772)
    at e.get (https://app.uniswap.org/static/js/4.5121d2e8.chunk.js:2:238684)
    at e.t.priceOf (https://app.uniswap.org/static/js/4.5121d2e8.chunk.js:2:235699)
    at https://app.uniswap.org/static/js/main.8d689671.chunk.js:1:525377
    at Object.Eo [as useMemo] (https://app.uniswap.org/static/js/4.5121d2e8.chunk.js:2:1485830)
    at t.useMemo (https://app.uniswap.org/static/js/4.5121d2e8.chunk.js:2:1405284)

I cant even launch safemoon on ropsten due to a gas issue. Unsure if it is remix or not.

This same error happens on kovan as well. If i use a normal erc20 like openzepp default, it works. I assume this may be some deflationary issue. although i can add liq on v3

1 Like

Great solution thanks for sharing this @FreezyEx :grinning:

2 Likes

The default address will be kept like this?

IUniswapV2Router02 _uniswapV2Router =
IUniswapV2Router02(0x10ed43c718714eb63d5aa57b78b54704e256024e);

1 Like

Which pancake testnet you are using? Link?

1 Like