Solidity – ERC20 Token Initialization with Uniswap V2 Router Address and Method to Alter Address

remixsolidityuniswapuniswapv2

I'm working on ERC20 token with a uniswapV2 router address initialized in the constructor. I also have method to change the router address. but I'm confused because when i create the new Pair with a different router address. The router Address variable updates.
My question is even after the updated router address for example from uniswap to sushiswap. can a user will still be able to buy/sell on uniswap?

IUniswapV2Router02 public UniswapV2Router ;
    address[] public UniswapV2Pairs;

constructor() ERC20("Hello", "HEY") {
UniswapV2Router = IUniswapV2Router02(0xC532a74256D3Db42D0Bf7a0400fEFDbad7694008);
address _UniswapV2Pair = IUniswapV2Factory(UniswapV2Router.factory()).createPair(address(this), UniswapV2Router.WETH());
UniswapV2Pairs.push(_UniswapV2Pair)
}

function addPairAddress(address newPair) external onlyOwner {
    require(newPair != address(0), "Invalid pair address");
    UniswapV2Pairs.push(newPair);
   
}

 function changeRouterAddress(address newRouter) external onlyOwner {
    require(newRouter != address(0), "Invalid router address");
        UniswapV2Router = IUniswapV2Router02(newRouter);
    address _UniswapV2Pair = IUniswapV2Factory(UniswapV2Router.factory())
        .createPair(address(this), UniswapV2Router.WETH());
    UniswapV2Pairs.push(_UniswapV2Pair);
}


function swapTokensForEth(uint256 tokenAmount) private {
        address[] memory path = new address[](2);
        path[0] = address(this);
        path[1] = UniswapV2Router.WETH();
        _approve(address(this), address(UniswapV2Router), tokenAmount);
        UniswapV2Router.swapExactTokensForETHSupportingFeeOnTransferTokens(
            tokenAmount,
            0,
            path,
            address(this),
            block.timestamp + 200
        );
    }

this is the code. my original router is uniswapv2 after adding a new router( sushiswap) the variable UniswapV2Router is updated with sushiswap address.
Can i still be able to buy/sell on uniswap?

Best Answer

addPairAddress() just pushes the address into UniswapV2Pairs[], it doesn't update the router address.

changeRouterAddress() updates the router address and creates a new pair by calling the factory listed at the router address. Pair is for the wrapped native coin listed at the router, and your calling token contract.

Swap just calls swapExactTokensForETHSupportingFeeOnTransferTokens() on the stored router, if you update the router address the function will swap on that new pool from that new factory. The old pool will still exist and people will be able to swap on it but not by calling your function, that will only swap on the one pool from the factory listed at the router.

On a side note you might want to test if swaps get sandwiched, as you are using 0 for the minimum output amount. Unless you are going to have it called as part of an atomic operation that does make a safety check.

Related Topic