0
//SPDX-License-Identifier:MIT
pragma solidity ^0.8.6;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

contract staking{
    IERC20 public s_stakingToken;
    IERC20 public s_rewardsToken;

     constructor(address stakingToken, address rewardsToken){
        s_stakingToken=IERC20(stakingToken);
        s_rewardsToken=IERC20(rewardsToken);
    }
}

can anyone explain the working of the above code snippet?

  1. why interface name is given as a variable type? IERC20 public s_stakingToken;
  2. how inside constructor, IERC20(stakingToken) will work?
  3. Is IERC20() a constructor? as per my knowledge there is no constructor allowed in interfaces.

1 Answer 1

1

in simple terms 👇

No IERC20 is not a constructor.

In these code snippets, you are associating s_stakingToken with IERC20 functions.

When you assign the stakingToken to the IERC20 of s_stakingToken , you can access the IERC20 functions directly from s_stakingToken such as name, symbol, balanceOf, transfer.

So example, when you do s_stakingToken.name() it will return you the name of the ERC20 token located at the stakingToken address.

Your Answer

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

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