Send ERC20s to Contracts

ERC-20 tokens can be integrated into other smart contracts in the ecosystem by calling the functions defined in the ERC-20 interface. Here is an example of a smart contract that integrates an ERC-20 token:

pragma solidity ^0.8.0;

interface IERC20 {
    function transfer(address to, uint256 value) external returns (bool);
    function balanceOf(address who) external view returns (uint256);
    function allowance(address owner, address spender) external view returns (uint256);
    function approve(address spender, uint256 value) external returns (bool);
    function transferFrom(address from, address to, uint256 value) external returns (bool);
}

contract MyContract {
    IERC20 public token;

    constructor(address tokenAddress) {
        token = IERC20(tokenAddress);
    }

    function transferToken(address recipient, uint256 amount) external {
        require(token.transfer(recipient, amount), "Transfer failed.");
    }
}

In this example, the MyContract smart contract uses the ERC-20 interface to transfer tokens. The token variable is an instance of the IERC20 interface and is initialized with the ERC-20 token contract address. The transferToken function is called by the smart contract user and uses the transfer function from the ERC-20 interface to transfer tokens to the recipient.

To transfer tokens to a smart contract and allow the smart contract to account for that transfer, you can use the approve() and transferFrom() functions of the ERC20 contract.

Here's an example of how it works:

  1. First, the user needs to approve the smart contract to spend a certain amount of tokens. This is done by calling the approve() function on the ERC20 contract:

ERC20.approve(contractAddress, amount);

This approves the smart contract to spend amount of tokens from the user's account.

  1. Next, the smart contract can call the transferFrom() function on the ERC20 contract to transfer the tokens from the user's account to the smart contract's account:

ERC20.transferFrom(userAddress, contractAddress, amount);

This transfers amount of tokens from the user's account to the smart contract's account. The smart contract can then account for this transfer in its logic.

It's important to note that the smart contract will need to implement the ERC20 interface and have a balance mapping to keep track of the token balances. Here's an example of how this could be done:

contract Spender {
    mapping(address => uint) pooled;
    address erc20;
    
    function poolTokens(uint256 amount) public returns (bool success) {
        // pull the tokens from the msg.sender using transferFrom
        bool success = ERC20(erc20).transferFrom(msg.sender, address(this), amount);
        require(success);

        pooled[msg.sender] += amount;
    }
}

contract ERC20 {
    mapping (address => uint256) balances;
    mapping (address => mapping (address => uint256)) allowed;

    function transferFrom(address from, address to, uint256 value) public returns (bool success) {
        balances[to] += value;
        balances[from] -= value;
        allowed[from][msg.sender] -= value;
        emit Transfer(from, to, value);
        return true;
    }
}

In this case, we would call poolTokens on Spender and this contract would pull those tokens from the ERC20 contract. Then the Spender contract can account for this balance change, by keeping its own record in the pooled mapping.

Last updated