Contract Source Code:
File 1 of 1 : TokenClaim
// SPDX-License-Identifier: UNLICENSED
// File: @openzeppelin/contracts/security/ReentrancyGuard.sol
// OpenZeppelin Contracts (last updated v4.9.0) (security/ReentrancyGuard.sol)
pragma solidity ^0.8.0;
/**
* @dev Contract module that helps prevent reentrant calls to a function.
*
* Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
* available, which can be applied to functions to make sure there are no nested
* (reentrant) calls to them.
*
* Note that because there is a single `nonReentrant` guard, functions marked as
* `nonReentrant` may not call one another. This can be worked around by making
* those functions `private`, and then adding `external` `nonReentrant` entry
* points to them.
*
* TIP: If you would like to learn more about reentrancy and alternative ways
* to protect against it, check out our blog post
* https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
*/
abstract contract ReentrancyGuard {
// Booleans are more expensive than uint256 or any type that takes up a full
// word because each write operation emits an extra SLOAD to first read the
// slot's contents, replace the bits taken up by the boolean, and then write
// back. This is the compiler's defense against contract upgrades and
// pointer aliasing, and it cannot be disabled.
// The values being non-zero value makes deployment a bit more expensive,
// but in exchange the refund on every call to nonReentrant will be lower in
// amount. Since refunds are capped to a percentage of the total
// transaction's gas, it is best to keep them low in cases like this one, to
// increase the likelihood of the full refund coming into effect.
uint256 private constant _NOT_ENTERED = 1;
uint256 private constant _ENTERED = 2;
uint256 private _status;
constructor() {
_status = _NOT_ENTERED;
}
/**
* @dev Prevents a contract from calling itself, directly or indirectly.
* Calling a `nonReentrant` function from another `nonReentrant`
* function is not supported. It is possible to prevent this from happening
* by making the `nonReentrant` function external, and making it call a
* `private` function that does the actual work.
*/
modifier nonReentrant() {
_nonReentrantBefore();
_;
_nonReentrantAfter();
}
function _nonReentrantBefore() private {
// On the first call to nonReentrant, _status will be _NOT_ENTERED
require(_status != _ENTERED, "ReentrancyGuard: reentrant call");
// Any calls to nonReentrant after this point will fail
_status = _ENTERED;
}
function _nonReentrantAfter() private {
// By storing the original value once again, a refund is triggered (see
// https://eips.ethereum.org/EIPS/eip-2200)
_status = _NOT_ENTERED;
}
/**
* @dev Returns true if the reentrancy guard is currently set to "entered", which indicates there is a
* `nonReentrant` function in the call stack.
*/
function _reentrancyGuardEntered() internal view returns (bool) {
return _status == _ENTERED;
}
}
// File: gravity/claims.sol
pragma solidity ^0.8.0;
interface IMintableToken {
function mint(address to, uint256 amount) external;
}
contract TokenClaim is ReentrancyGuard {
IMintableToken public token;
struct Claim {
uint256 expiry;
mapping(address => uint256) allocations;
mapping(address => bool) claimed;
uint256 totalAllocated;
address[] allocatedAddresses;
}
mapping(bytes32 => Claim) public claims; // Mapping still uses bytes32 for efficiency
mapping(address => mapping(bytes32 => uint256)) public userClaims;
string[] public claimIds;
address public admin;
event ClaimCreated(string indexed claimId, uint256 expiry);
event TokensAllocated(string indexed claimId, address indexed account, uint256 amount);
event TokensClaimed(string indexed claimId, address indexed account, uint256 amount);
event ClaimExpiryExtended(string indexed claimId, uint256 newExpiry);
modifier onlyAdmin() {
require(msg.sender == admin, "Caller is not admin");
_;
}
modifier claimExists(string memory claimId) {
require(claims[_getClaimHash(claimId)].expiry != 0, "Claim does not exist");
_;
}
modifier onlyBeforeStart(string memory claimId) {
require(block.timestamp < claims[_getClaimHash(claimId)].expiry, "Claim already started");
_;
}
constructor(address tokenAddress, address owner) {
token = IMintableToken(tokenAddress);
admin = owner;
}
function createClaim(string memory claimId, uint256 expiry) external onlyAdmin {
require(expiry > block.timestamp, "Expiry must be in the future");
bytes32 claimHash = _getClaimHash(claimId);
if (claims[claimHash].expiry == 0) {
claims[claimHash].expiry = expiry;
claimIds.push(claimId);
emit ClaimCreated(claimId, expiry);
} else {
require(block.timestamp < claims[claimHash].expiry, "Claim has already started");
claims[claimHash].expiry = expiry;
}
}
function allocateTokens(
string memory claimId,
address[] calldata addresses,
uint256[] calldata amounts
) external onlyAdmin claimExists(claimId) onlyBeforeStart(claimId) {
require(addresses.length == amounts.length, "Addresses and amounts length mismatch");
bytes32 claimHash = _getClaimHash(claimId);
Claim storage claim = claims[claimHash];
for (uint256 i = 0; i < addresses.length; i++) {
require(!claim.claimed[addresses[i]], "Tokens already claimed by address");
if (claim.allocations[addresses[i]] == 0) {
claim.allocatedAddresses.push(addresses[i]);
}
claim.allocations[addresses[i]] += amounts[i];
claim.totalAllocated += amounts[i];
emit TokensAllocated(claimId, addresses[i], amounts[i]);
}
}
function claimTokens(string memory claimId) external claimExists(claimId) nonReentrant {
bytes32 claimHash = _getClaimHash(claimId);
Claim storage claim = claims[claimHash];
require(block.timestamp < claim.expiry, "Claim period has expired");
require(!claim.claimed[msg.sender], "Tokens already claimed");
uint256 amount = claim.allocations[msg.sender];
require(amount > 0, "No tokens allocated");
claim.claimed[msg.sender] = true;
claim.allocations[msg.sender] = 0;
claim.totalAllocated -= amount;
token.mint(msg.sender, amount);
emit TokensClaimed(claimId, msg.sender, amount);
}
function getAllocation(string memory claimId, address account) external view claimExists(claimId) returns (uint256) {
return claims[_getClaimHash(claimId)].allocations[account];
}
function hasClaimed(string memory claimId, address account) external view claimExists(claimId) returns (bool) {
return claims[_getClaimHash(claimId)].claimed[account];
}
function extendClaimExpiry(string memory claimId, uint256 additionalTime) external onlyAdmin claimExists(claimId) {
bytes32 claimHash = _getClaimHash(claimId);
Claim storage claim = claims[claimHash];
require(claim.expiry > block.timestamp, "Claim has already expired");
claim.expiry += additionalTime;
emit ClaimExpiryExtended(claimId, claim.expiry);
}
// Internal function to hash the string claimId to use as a mapping key
function _getClaimHash(string memory claimId) internal pure returns (bytes32) {
return keccak256(abi.encodePacked(claimId));
}
}