-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleTokenAirdrop.sol
More file actions
84 lines (65 loc) · 2.54 KB
/
SimpleTokenAirdrop.sol
File metadata and controls
84 lines (65 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;
interface IERC20 {
function transfer(address to, uint256 amount) external returns (bool);
function transferFrom(address from, address to, uint256 amount) external returns (bool);
}
contract SimpleTokenAirdrop {
address public owner;
IERC20 public token;
mapping(address => bool) public claimed;
uint256 public claimAmount;
uint256 public claimDeadline;
error NotOwner();
error ZeroAddress();
error ZeroAmount();
error ClaimEnded();
error AlreadyClaimed();
error TransferFailed();
event Claimed(address indexed user, uint256 amount);
event Configured(uint256 claimAmount, uint256 claimDeadline);
event OwnerChanged(address indexed oldOwner, address indexed newOwner);
constructor(address tokenAddress, uint256 _claimAmount, uint256 _durationSeconds) {
if (tokenAddress == address(0)) revert ZeroAddress();
if (_claimAmount == 0) revert ZeroAmount();
owner = msg.sender;
token = IERC20(tokenAddress);
claimAmount = _claimAmount;
claimDeadline = block.timestamp + _durationSeconds;
emit Configured(claimAmount, claimDeadline);
}
modifier onlyOwner() {
if (msg.sender != owner) revert NotOwner();
_;
}
function claim() external {
if (block.timestamp > claimDeadline) revert ClaimEnded();
if (claimed[msg.sender]) revert AlreadyClaimed();
claimed[msg.sender] = true;
bool ok = token.transfer(msg.sender, claimAmount);
if (!ok) revert TransferFailed();
emit Claimed(msg.sender, claimAmount);
}
function setClaimAmount(uint256 newAmount) external onlyOwner {
if (newAmount == 0) revert ZeroAmount();
claimAmount = newAmount;
emit Configured(claimAmount, claimDeadline);
}
function extendDeadline(uint256 extraSeconds) external onlyOwner {
if (extraSeconds == 0) revert ZeroAmount();
claimDeadline += extraSeconds;
emit Configured(claimAmount, claimDeadline);
}
function transferOwnership(address newOwner) external onlyOwner {
if (newOwner == address(0)) revert ZeroAddress();
address old = owner;
owner = newOwner;
emit OwnerChanged(old, newOwner);
}
function withdrawTokens(address to, uint256 amount) external onlyOwner {
if (to == address(0)) revert ZeroAddress();
if (amount == 0) revert ZeroAmount();
bool ok = token.transfer(to, amount);
if (!ok) revert TransferFailed();
}
}