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;
}
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
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
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
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.