0

I have noticed, in Uniswap v2 for exemple, that the contract imports its own library.

For exemple, UniswapV2Pair.sol

pragma solidity =0.5.16;

import './interfaces/IUniswapV2Pair.sol';
...
import './interfaces/IUniswapV2Factory.sol';
import './interfaces/IUniswapV2Callee.sol';

contract UniswapV2Pair is IUniswapV2Pair, UniswapV2ERC20 {

So that UniswapV2Pair is IUniswapV2Pair... Notably, IUniswapV2Pair contains all functions found in IUniswapV2ERC20, and then all the functions from UniswapV2Pair, but without the logic (i.e. just function's name, arguments, returned values).

Where is the need for import its own interface in a smart contract? Can someone shed some lights about what is happening here? Thanks

2 Answers 2

1

A contract needs to import an interface if the contract wants to implement the functions (external) in the interface.

The UniswapV2Pair contract's main purpose is to accept tokens from users and use accumulated reserves of tokens to perform swaps. Every UniswapV2Pair contract can pool only one pair of tokens and allows to perform swaps only between these two tokens - this is why it’s called “pair”. The UniswapV2Pair contract imports the interfaces that the contract needs to know to pool and swap tokens, either because the contract implements them or because it calls contracts that implement them.

3
  • But that doesn't explain why v2Pair imports Iv2Pair. Why would it import the interface of, for exemple this: function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast); when there is already: function getReserves() public view returns (uint112 _reserve0, uint112 _reserve1, uint32 _blockTimestampLast) { _reserve0 = reserve0; _reserve1 = reserve1; _blockTimestampLast = blockTimestampLast; } in the contract itself? Commented Oct 29, 2022 at 1:29
  • If we don't import the function getReserves() from the interface of lv2Pair, v2Pair will have to write its own function getReserves() and its logic in the contract itself. By importing the interface, it's essentially providing a customizable and re-usable approach to implement the logic of the getReserves() function of the lv2Pair. Commented Oct 29, 2022 at 2:07
  • According to the flow of the contract, the code executes from top to bottom. Hence, we import the interface first, then we implement the logic. Not we have already implemented the logic far down, and we question the need for importing the interface in the beginning. Commented Oct 29, 2022 at 2:27
0

It isn't required in general, but it is a good practice. For example if the interface changes then compiling the contract will fail pointing the missing function.

For libraries with hierarchies of contracts, like OpenZeppelin, it saves a type casting when the interface is needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.