How to fix Pancakeswap's Router address in Safemoon

For adding LP?
I guess ye.
I’ve tried to study how to do it from contract itself, but that’s still something I need to dig deeper.

I just saw one contract to use PancakeRouter/Pair instead of the regular uniswap.
It was this token: https://bscscan.com/address/0x6507458bb53aec6be863161641ec28739c41cc97#code

Sorry but whats the “immutable” keyword here? Again sorry kinda new to this. Still learning

seacrh uniswapV2Pair and you will see an immutable word

Its normally little above the router construct.

Search for something like this (your code might have IUniswapvV2Router instead of pancake:

IpancakeRouter02 public immutable pancakeRouter;
Address public immutable pancakePair;

ok so just change what needed

I’ve added this custom function on my fork and it works pretty fine for those who are interested :

function updateDex(address _newDexAddress) external onlyOwner() {
        IUniswapV2Router02 _uniswapV2Router = IUniswapV2Router02(_newDexAddress);
        // Create a uniswap pair for this new token
        uniswapV2Pair = IUniswapV2Factory(_uniswapV2Router.factory())
            .createPair(address(this), _uniswapV2Router.WETH());

        // set the rest of the contract variables
        uniswapV2Router = _uniswapV2Router;
    }
1 Like

Is this initially the same, except the _uniswapV2Router -> pancakerouter
and newRouter -> _newDexAddress

And the modifier changed public -> external ?
Is this due gasfees? Any other reason to do this?

Ahahah man are you serious? Is this what I wrotebut different names?

Ish don’t know, didn’t see your post, i’ve read the thread before your answer so when i found how to do it i directly responded to it!

PS : not exactly the same since i use external to reduce the gas fees

According to the docs, “External functions are sometimes more efficient when they receive large arrays of data.” . It also in this case reduces the gas fees

1 Like

I understand no problem. :grin: :grin:

when I clone saemoon code in remix testnet
no token has created

Hi, this new code is not working for me. I have tried to change from the pancake test router address to multiple others (pancake v1, v2, ropsten, and even just to itself on the pancake test). Will this update only work for going from v1 to v2. If so, why?

Can you give more details so I can chexk any problem and fix. Anyway if you get errors I think is due to can’t change router of testnet, mainnet etc. If you try to deploy a contract with router v1 on mainnet you will get an error

1 Like

Here is the transaction. Yeah I figured it is because I am trying to switch from test to router. I have seen the error when deploying on main net with the v1 router. So in that case is there anyway to test this code? Would I have to deploy on main net v2 and use it to go back to v1 then to v2 again? I want to make sure it is working when pancake v3 comes around.

first off all i am learn alot from you here guys . i am a really² beginner ,
i did deploy safemoon clone on testnet using truffle and my question is , is it true safemoon fork has bot that can buy our coin by automatically ? and also how i can set my liquidity
any way can you take a look my safemoon clone here the sc adress

best regards and thanks

Plus 1 i am getting the same error and want to be sure before deployment

Hi FreezyEx.

How can I approve Pancake Router?
Screenshot 2021-06-11 183018

I just got error like this when convert my token to BNB.
Please help me.

use Approve function and put router address in

This way looks fine, but what if the contract deployer wants to move in between different dex?
So my solution is

    struct RouterInfo {
        address pair_address;
    }

    mapping (address => RouterInfo) private routerInformation;
    address[] private _routerAddress;

    IUniswapV2Router02 public uniswapV2Router;
    address public uniswapV2Pair;

    function initialize_pair_router(address _router) public onlyOwner returns (address, address) {
        require(initialization == false, "The contract has already been initialized!");
        initialization = true;
        _routerAddress.push(_router);
        router_address_change = block.timestamp + 14 days;
        
        IUniswapV2Router02 _uniswapV2Router = IUniswapV2Router02(_router);
        
        // 0x05fF2B0DB69458A0750badebc4f9e13aDd608C7F - PancakeSwap V1 Router
        //  - PancakeSwap V2 Router
        
        // Create a uniswap pair for this new token
        uniswapV2Pair = IUniswapV2Factory(_uniswapV2Router.factory()).createPair(address(this), _uniswapV2Router.WETH());

        // set the rest of the contract variables
        uniswapV2Router = _uniswapV2Router;
        
        // add the information of the pair address into the structure
        routerInformation[_router].pair_address = uniswapV2Pair;
        return (_router, uniswapV2Pair);
    }
    
    function change_pair_router(address _router) public onlyOwner returns (address, address) {
        require(initialization == true, "The contract has not been initialized!");
        require(block.timestamp >= router_address_change, "The contract cannot be changed right now!");
        
        for (uint256 i; i < _routerAddress.length; i++) {
            if (_routerAddress[i] == _router) {
                // if the router exists in the past, then use the old information instead of creating a new one
                uniswapV2Pair = routerInformation[_router].pair_address;
                
                // changing the contract variables
                IUniswapV2Router02 _uniswapV2Router = IUniswapV2Router02(_router);
                
                // for the rest of the contract to use
                uniswapV2Router = _uniswapV2Router;
            } else {
                // setting the interface regarding the router address
                IUniswapV2Router02 _uniswapV2Router = IUniswapV2Router02(_router);
                
                // 0x05fF2B0DB69458A0750badebc4f9e13aDd608C7F - PancakeSwap V1 Router
                // 0x10ED43C718714eb63d5aA57B78B54704E256024E - PancakeSwap V2 Router
                
                // Create a uniswap pair for this new token
                uniswapV2Pair = IUniswapV2Factory(_uniswapV2Router.factory())
                    .createPair(address(this), _uniswapV2Router.WETH());
        
                // set the rest of the contract variables
                uniswapV2Router = _uniswapV2Router;
                
                //add the router address into the list
                _routerAddress.push(_router);
                
                // for an unseen router, new pair will be created 
                // it will add the information of the pair address into the structure
                routerInformation[_router].pair_address = uniswapV2Pair;
                
            }
        }
        
        return (_router, uniswapV2Pair);
    }

This is just to ensure when the deployer switches back to an old router address, the smart contract does not need to create a new pair. It can just simply use the old pair address. Also, the 14 days is just to make sure the router address does not change frequently.

I was planning to add a timelock function in there so when the deployer wants to change the router address, they will have to first initiate the transaction then execute the transition later. More community friendly but not the deployer.

Let me know if the code works!!!

1 Like