For my project, I need a smart contract which will automatically add the specific (my project's tokens) erc20 token senders address to the array. In case of ether it works perfect. But the same code doesn't work with erc20 tokens. Can please anyone make this script to automatically ++ the counter and automatically add the senders address to the array, when deposit is made?

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/Counters.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/IERC20.sol";
contract weedOw {
    address public owner;
    IERC20 public legalToken;
    uint public lotteryId;
    address[] public sender;
    using Counters for Counters.Counter;
    Counters.Counter public _senderIds;
    uint _minAmount = 1*(10**18);
    //mapping(address => uint) public winnings;
    // event TransferReceived(IERC20 legalToken, address _from, uint _amount);
    //event TransferSent(address _from, address _destAddr,uint _amount);
    event Transfer(address indexed from, address indexed to, uint256 value);

    constructor(IERC20 _legalToken) {
        legalToken = _legalToken;
        owner = msg.sender;
        lotteryId = 1;

    receive() payable external {}

    function legalTokenBalance() public  view returns(uint){
        return IERC20(legalToken).balanceOf(address(this));

    function depositLegalToken(uint256 value) internal virtual returns (bool) {
        require(IERC20(legalToken).transfer(address(this), value));
        emit Transfer(msg.sender, address(this), value);

        if(_senderIds.current() == 3) {
            IERC20(legalToken).transfer(sender[2], legalTokenBalance());
            return true;

    function resetLottery() internal {
        sender = new address[](0);
        lotteryId ++;

1 Answer 1


Unlike with ether transfers and fallback() and receive() functions, ERC20 tokens do not notify the receiver when a transfer is made and, therefore, you cannot rely on this interaction to handle deposits in your contract.

Nonetheless, what you can do is have the user approve your smart contract for spending the deposit amount of tokens by interacting with the ERC20 contract's approve function from outside:


And, then, have the user call your deposit function in your contract:

function depositLegalToken(uint256 value) public {
  IERC20.transferFrom(msg.sender, address(this), value);
  // your logic
  • thanks for your answer. I have tried to implement the approve method in different ways, and inside the transfer function of erc20 token and inside deposit function of this contract but it doesn't work automatically as it is going on in case of ether. May be I am doing something wrong but this silly situation makes me crazy. I just need it to be private - when erc20 tokens are on erc20(token).balanceOf(address(this) the counter to be ++ and the sender address to be added to the array. At first sight it is simple, but that doesn't happen...???? Commented Oct 23, 2022 at 18:41
  • It cannot be done in 1 step as with ether transfers. First, the sender has to approve the contract for the transfer (outside of your contract) and, only then, to interact with a function within your contract that triggers the counter and the address to be pushed to the array.
    – tinom9
    Commented Oct 23, 2022 at 21:55
  • ...that's not a deal for my project. Can't I call the function which triggers the counter and adds the address, from erc20 token smart contracts transfer function? I mean when the holder will transfer tokens to this address he will also call that magical function?))) Commented Oct 23, 2022 at 22:30
  • No, because the holder will transfer tokens by interacting with the ERC20 contract. You can transfer their tokens by interacting with the ERC20 contract only if they have approved your contract by interacting with the ERC20 contract previously. Either way, the holder has to interact once with the ERC20 contract and, then, once with your contract, to achieve what you are looking for.
    – tinom9
    Commented Oct 23, 2022 at 22:49
  • very sad :( ethereum developers are sneering at me... I will keep seeking for other ways. Thanks! Commented Oct 24, 2022 at 9:38

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.