I'm writing a simple airdrop contract in which user will register himself using register(), and his address will be added to an array registered[].
After the registration period is over, admin (who has all the tokens) will run fn setTokenAmount() by way of which he will set the token (contract name FUDX, from file FUDXToken.sol) amount to be given for free and wil also approve each address to withdraw from his account.
Next, user(s) will run claimAirdrop() for getting the tokens. But everytime, a user runs the fn, it gives an error saying allowance is not available.
Can somebody pl suggest the remedy ?
// SPDX-License-Identifier: MIT
pragma solidity 0.8.4;
import "./FUDXToken.sol";
contract AirdropFudx is FUDX {
IERC20 private token;
constructor (FUDX _token) {
(admin) = payable(msg.sender);
token = _token;
}
address[] registered;
uint256 public count;
function register() external {
count++;
registered.push(msg.sender);
}
modifier onlyOwner{
require (msg.sender == admin, "Only Admin");
_;
}
uint256 tokenAmount;
function setTokenAmount(uint256 _tokenamount) external onlyOwner {
tokenAmount = _tokenamount;
uint256 num = registered.length;
// approve(address(this), num*tokenAmount);
for( uint i=0; i<registered.length; i++) {
approve(registered[i], tokenAmount);
}
}
function claimAirdrop() external returns(bool success) {
token.transferFrom(admin, msg.sender, tokenAmount);
return true;
}
}