What is the purpose of Interface used in other contracts as parameters

sobi983

sohaib

Posted on January 15, 2022

What is the purpose of Interface used in other contracts as parameters

I have the code for example this one..
pragma solidity ^0.8.7;
// SPDX-License-Identifier: MIT

import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";

contract FeeCollector {
address public owner;
uint256 public balance;

event TransferReceived(address _from, uint _amount);
event TransferSent(address _from, address _destAddr, uint _amount);

constructor() {
    owner = msg.sender;
}

receive() payable external {
    balance += msg.value;
    emit TransferReceived(msg.sender, msg.value);
}    

function withdraw(uint amount, address payable destAddr) public {
    require(msg.sender == owner, "Only owner can withdraw funds"); 
    require(amount <= balance, "Insufficient funds");

    destAddr.transfer(amount);
    balance -= amount;
    emit TransferSent(msg.sender, destAddr, amount);
}

function transferERC20(IERC20 token, address to, uint256 amount) public {
    require(msg.sender == owner, "Only owner can withdraw funds"); 
    uint256 erc20balance = token.balanceOf(address(this));
    require(amount <= erc20balance, "balance is low");
    token.transfer(to, amount);
    emit TransferSent(msg.sender, to, amount);
}    
Enter fullscreen mode Exit fullscreen mode

}

In the function transferERC20() it is passing IERC20 as a parameter which which came from the imported eRC20.sol file. So my exact Question is that what is the purpose of the interface in the contract parameters because We cannot define any constructor in a interface or we can actually use any function for the output as we cannot define the functions in a interface. I saw several code out there using interfaces in a parameters of the other contract function.

💖 💪 🙅 🚩
sobi983
sohaib

Posted on January 15, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related