How to fix Pancakeswap's Router address in Safemoon

May I please ask where in the safemoon code one needs to enter the factory address. I didn’t see anywhere on the code where to input that. I did see where to put in the router though.

Hey FreezyEx,
I tried to test your code in the contract but the router address is immutable, so after removing the immutability and testing the code, i get a gas estimation error when trying to overwrite the address of the router.

Are you trying to migrate to which router?

I was trying to migrate from the test net router, to the test net router… (I just wanted to see if I could overwrite it). At first I used the pancake v2 router (and wanted to test if I could set up the testnet router) but if you try to deploy a contract using a mainnet router for the bsc test net you also get a gas estimation error when you deploy the contract on the wrong network

to test you should deploy using this: 0xD99D1c33F9fC3444f8101754aBC46c52416550D1
then try to change router to this: 0x9Ac64Cc6e4415144C455BD8E4837Fea55603e5c3

alrighty, will try it right away

The swap and liquify function does not seem to work anymore after changing the routers. Even after turning it off and on again the tokens remain to be accumulated in the contract instead of being sent to charity wallet

when I try to swap the routers again I get a gas estimation error which reverts the transaction. So that is also strange

did you added liquidity in the new router?

because if you read the function code, you will see that it create the pair, (it is supposed to be used one time only for router). SO, if the pair already exist it can’t be used.
To fix this we can use this:

 function setRouterAddress(address newRouter) external onlyOwner {
      //Thanks to FreezyEx
        IUniswapV2Router02 _newRouter = IUniswapV2Router02(newRouter);
        address get_pair = IUniswapV2Factory(_newRouter.factory()).getPair(address(this), _newRouter.WETH());
        //checks if pair already exists
        if (get_pair == address(0)) {
            uniswapV2Pair = IUniswapV2Factory(_newRouter.factory()).createPair(address(this), _newRouter.WETH());
        }
        else {
            uniswapV2Pair = get_pair;
        }
        uniswapV2Router = _newRouter;
    }

If the contracts already renounced, we cant execute this command since it call only owner right?

yes exactly, you can’t

image

I have the numTokensSellToAddToLiquidity set at 30k tokens with 9 decimals, I used your updated setRouterAddress function and it doesnt liquify the tokens after reaching the 30k threshold. I have also set swapandliquify to true

I’ve been thinking, could the reason that the swap and liquify function doesnt work have to do with that I have not used added any liquidity to this router address (the one you told me to use upon deployment): 0xD99D1c33F9fC3444f8101754aBC46c52416550D1.

if so, where does this intial router belong to?

The other router you shared:
0x9Ac64Cc6e4415144C455BD8E4837Fea55603e5c3, belongs to the pancakeswap testrouter.

Could this be the reason for my swapandliquify function not working? That i first have to make a LP for the initial router, then migrate router address, then transfer the liquidity to the final router?

How can i send you DM, mate. need help with this immutable thing.

Hello FreezyEx, I find your solutions are somewhat closer in resolving my current problem in deploying the contract. I am currently encountering this "gas estimation failed" error and I am not sure where the problem is exactly, I am using remix to deploy the contract in BSC Testnet, with router of pancakeswap for the testnet also, but still doesn't work after finding that the problem relies on the pancakeswap router being defined. Can you help me identify in which part of these codes that I need to correct?

// liquidity
    bool public  _swapAndLiquifyEnabled = true;
    bool private _inSwapAndLiquify;
    IUniswapV2Router02 public _uniswapV2Router;
    address            public _uniswapV2Pair;
    event MinTokensBeforeSwapUpdated(uint256 minTokensBeforeSwap);
    event SwapAndLiquifyEnabledUpdated(bool enabled);
    event SwapAndLiquify(
        uint256 tokensSwapped,
        uint256 bnbReceived,
        uint256 tokensIntoLiquidity
    );
    
    modifier lockTheSwap {
        _inSwapAndLiquify = true;
        _;
        _inSwapAndLiquify = false;
    }
    
    constructor (address cOwner, address reserveWallet) Ownable(cOwner) {
        _reserveWallet = reserveWallet;

        _rOwned[cOwner] = _rTotal;
        
        // Create a uniswap pair for this new token
        // IUniswapV2Router02 uniswapV2Router = IUniswapV2Router02(0x10ED43C718714eb63d5aA57B78B54704E256024E);
        IUniswapV2Router02 uniswapV2Router = IUniswapV2Router02(0xD99D1c33F9fC3444f8101754aBC46c52416550D1);
        _uniswapV2Pair = IUniswapV2Factory(uniswapV2Router.factory()).createPair(address(this), uniswapV2Router.WETH());
        _uniswapV2Router = uniswapV2Router;

        // exclude system addresses from fee
        _isExcludedFromFee[owner()]        = true;
        _isExcludedFromFee[address(this)]  = true;
        _isExcludedFromFee[_reserveWallet] = true;

        _isExcludedFromAutoLiquidity[_uniswapV2Pair]            = true;
        _isExcludedFromAutoLiquidity[address(_uniswapV2Router)] = true;
        
        emit Transfer(address(0), cOwner, _tTotal);
    }

    receive() external payable {}

Appreciate your assistance regarding this one. :slight_smile:

The way you put Ownable(cOwner) right in your constructor is something unusual. Besides, "gas estimation failed" error can be solved by manually providing a large amount of gas.

If numTokensSellToAddToLiquidity reached 30K limit in the current transaction, it would not trigger swapAndLiquify. If all other conditions are met, the next transaction will do.

A post was merged into an existing topic: A contract that can't estimate gas while selling, but can buy Without issue

Metamask is throwing: Transaction Error. Exception thrown in contract code.