Last active
September 25, 2023 19:54
-
-
Save WPSmartContracts/16a3f48eabd460b1bc31a2749e0782bf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.2; | |
// Standard OpenZeppelin Contracts | |
import "../access/Ownable.sol"; | |
import "../security/ReentrancyGuard.sol"; | |
import "../token/ERC20/IERC20.sol"; | |
import "../utils/Address.sol"; | |
import "../token/ERC20/utils/SafeERC20.sol"; | |
import "../utils/Context.sol"; | |
import "../security/Pausable.sol"; | |
/** | |
* @title Guava Airdrop Contract | |
* @dev This smart contract facilitates a one-time fixed amount airdrop of ERC20 tokens to beneficiaries. | |
* The contract owner can set the total amount of tokens available for each beneficiary. | |
* Beneficiaries can claim their tokens at any time. | |
*/ | |
contract Guava is Ownable, Pausable, ReentrancyGuard { | |
using SafeERC20 for IERC20; | |
// The ERC20 token to be airdropped | |
IERC20 public token; | |
// The wallet address that holds the funds for the airdrop | |
address public wallet; | |
// Amount of tokens each beneficiary can claim | |
uint256 public amount; | |
// Mapping to track claimed tokens for each beneficiary | |
mapping (address => bool) public claimed; | |
// Event emitted when a beneficiary claims tokens | |
event Claim(address indexed user, uint256 qty); | |
// Event emitted when the owner sets the airdrop amount | |
event AmountChanged(uint256 amount); | |
// Event emitted when the owner sets a new Token | |
event TokenChanged(address indexed newToken); | |
// Event emitted when the owner sets a new Wallet | |
event WalletChanged(address indexed newWallet); | |
/** | |
* @dev Constructor to initialize the Guava contract | |
* @param _token The ERC20 token contract address | |
* @param _owner The initial owner of the contract | |
* @param _wallet The address holding the airdrop tokens | |
* @param _amount The initial airdrop amount per beneficiary | |
*/ | |
constructor (IERC20 _token, address _owner, address _wallet, uint256 _amount) Ownable() { | |
require(address(_token) != address(0), "Token cannot be the zero address"); | |
require(_wallet != address(0), "Wallet cannot be the zero address"); | |
token = _token; | |
wallet = _wallet; | |
amount = _amount; | |
transferOwnership(_owner); | |
} | |
/** | |
* @dev Allows the contract owner to set the airdrop amount. | |
* @param amount_ The new total amount that any beneficiary can claim once | |
*/ | |
function set(uint256 amount_) external onlyOwner { | |
emit AmountChanged(amount_); | |
amount = amount_; | |
} | |
/** | |
* @dev Allows beneficiaries to claim their airdrop tokens. | |
* The claim is subject to available balance and approval by the contract owner. | |
*/ | |
function claim() external nonReentrant whenNotPaused { | |
require(amount > 0, "Airdrop is not available at this time"); | |
require(!claimed[msg.sender], "You have already claimed your airdrop tokens."); | |
// Check allowance before transferring tokens | |
uint256 allowance = token.allowance(wallet, address(this)); | |
require(allowance >= amount, "Insufficient allowance. Approve the contract to spend tokens."); | |
claimed[msg.sender] = true; | |
emit Claim(msg.sender, amount); | |
token.safeTransferFrom(wallet, msg.sender, amount); | |
} | |
/** | |
* Owner functions to manage contract parameters | |
*/ | |
/** | |
* @dev Changes the address of the ERC20 token contract. Only the contract owner can perform this action. | |
* @param token_ The new ERC20 token contract address | |
*/ | |
function changeToken(IERC20 token_) external onlyOwner { | |
require(address(token_) != address(0), "Token cannot be the zero address"); | |
emit TokenChanged(address(token_)); | |
token = token_; | |
} | |
/** | |
* @dev Changes the wallet address holding the airdrop tokens. Only the contract owner can perform this action. | |
* @param _wallet The new wallet address | |
*/ | |
function changeWallet(address _wallet) external onlyOwner { | |
require(_wallet != address(0), "Wallet cannot be the zero address"); | |
emit WalletChanged(_wallet); | |
wallet = _wallet; | |
} | |
/** | |
* @dev Pause the airdrop activity. Only the contract owner can pause and resume the airdrop. | |
*/ | |
function pause() external onlyOwner() { | |
_pause(); | |
} | |
/** | |
* @dev Resume the airdrop activity. Only the contract owner can pause and resume the airdrop. | |
*/ | |
function unpause() external onlyOwner() { | |
_unpause(); | |
} | |
// Fallback function to handle accidental transfers of Ether and revert them. | |
receive() external payable { | |
revert("Ether transfers are not accepted by this contract."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment