2

I just started on building Tokens using ETH & BSC, this is one statement which I see in many Contracts. Inside the Constructor method, the Uniswap router is iniliazed probably with the V2 version. What is the use of this?

 constructor () public {
 _rOwned[_msgSender()] = _rTotal;
        
        IUniswapV2Router02 _uniswapV2Router = IUniswapV2Router02(0x10ED43C718714eb63d5aA57B78B54704E256024E);
         // 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;
        

Why is this initialization required? What is the functionality of this?

Appreciate if someone could help.

Thanks

BlockChain Learner
  • 125
  • 1
  • 10
  • 18

1 Answers1

3
IUniswapV2Router02 _uniswapV2Router = IUniswapV2Router02(0x10ED43C718714eb63d5aA57B78B54704E256024E);

This line initializes a pointer to the 0x10ED... address and expects the contract (deployed at the 0x10ED... address) to implement the IUniswapV2Router02 interface.

The interface is defined somewhere with the caller contract source code.

It allows you to execute and call functions defined by the interface instead of building low-level calls. It also allows you to use the returned datatypes instead of parsing the returned binary.

Example:

pragma solidity ^0.8.5;

interface IRemote {
    function foo() external view returns (bool);
}

contract MyContract {
    IRemote remote;

    constructor() {
        remote = IRemote(address(0x123));
    }

    function getFoo() external view returns (bool) {
        bool returnedValue = remote.foo();
        return returnedValue;
    }
}
Petr Hejda
  • 40,554
  • 8
  • 72
  • 100
  • Thanks @Petr...Got the point..However that address 0x1... is the Uniswap V2 Router. So how does the main token contract implements the Uniswap interface? Thanks – BlockChain Learner Jun 25 '21 at 06:42
  • @user3161840 The main token contract doesn't **implement** the interface. It only **defines** it (without implementing it). The implementation is done by contract on the 0x1 address... By defining (not implementing) the interface in the token contract, the token contract can execute functions that are implemented in the Router contract. – Petr Hejda Jun 25 '21 at 07:18