I have a contract that's responsible for distributing an ERC20 token to a list of addresses. In the contract there's a function that simplifies to something like:

function sendTokenToAddresses(uint amount, address[] memory addresses) external onlyOwner() nonReentrant(){
    for(uint i=0;i<addresses.length;i++){           
        erc20Contract.approve(addresses[i], amount);
        erc20Contract.transferFrom(address(this), addresses[i], amount);

The approve function for the ERC20 token is fairly standard and looks like this:

    function approve(address spender, uint256 amount) public virtual override returns (bool) {
      allowed[msg.sender][spender] = amount;
      emit Approval(msg.sender, spender, amount);
      return true;

This function is only meant to be run by the contract owner (set in the constructor). But in testing (truffle) my testing on this function fails because it's trying to pass tokens from the caller (owner) rather than the contract address.

How can I call the approve method on the erc20Contract so that the msg.sender value is the contract address rather than the contract owner address?

1 Answer 1


So what you're trying to do is to transfer tokens from the contract address to a list of addresses right? in that case the approve call in your function is useless and you shouldnt use transferFromjust use transfer instead. (also your approve function is wrong syntax wise ( spender and amount becomes _spender and _value??) but i assume thats not your issue rn, and its easy to fix anyways.)

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.