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?