|
| 1 | +//SPDX-License-Identifier: MIT |
| 2 | + |
| 3 | +pragma solidity ^0.8.24; |
| 4 | + |
| 5 | +import {MerkleProof} from "@openzeppelin/contracts/utils/cryptography/MerkleProof.sol"; |
| 6 | +import {IERC20, SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; |
| 7 | +import {ERC712} from "@openzeppelin/contracts/utils/cryptography/EIP712.sol"; |
| 8 | +import {SignatureChecker} from "@openzeppelin/contracts/utils/cryptography/SignatureChecker.sol"; |
| 9 | +import {ECDSA} from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol"; |
| 10 | +import {MessageHashUtils} from "@openzeppelin/contracts/utils/cryptography/MessageHashUtils.sol"; |
| 11 | + |
| 12 | +contract MerkleAirdrop is EIP712 { |
| 13 | + using ECDSA for bytes32; |
| 14 | + using SafeERC20 for IERC20; |
| 15 | + |
| 16 | + error MerkleAirdrop__InvalidProof(); |
| 17 | + error MerkleAirdrop__InvalidSignature(); |
| 18 | + error MerkleAirdrop__AlreadyClaimed(); |
| 19 | + |
| 20 | + IERC20 public immutable i_airdropToken; |
| 21 | + bytes32 private immutable i_merkleRoot; |
| 22 | + mapping(address => bool) private s_hasClaimed; |
| 23 | + bytes32 private immutable MESSAGE_TYPEHASH = keccak256("Airdrop(address account,uint256 amount)"); |
| 24 | + |
| 25 | + struct AirdropClaim { |
| 26 | + address account; |
| 27 | + uint256 amount; |
| 28 | + } |
| 29 | + |
| 30 | + event Claimed(address account, uint256 amount); |
| 31 | + event MerkleRootUpdated(bytes32 newMerkleRoot); |
| 32 | + |
| 33 | + constructor(bytes32 merkleRoot, IERC20 airdropToken) EIP712("MerkleAirdrop", "1.0.0") { |
| 34 | + i_merkleRoot = merkleRoot; |
| 35 | + i_airdropToken = airdropToken; |
| 36 | + } |
| 37 | + |
| 38 | + function Claim(address account, uint256 amount, bytes32[] calldata merkleProof, uint8 v, bytes32 r, bytes32 s) |
| 39 | + external |
| 40 | + { |
| 41 | + if (s_hasClaimed[account]) { |
| 42 | + revert MerkleAirdrop__AlreadyClaimed(); |
| 43 | + } |
| 44 | + if (!_isValidSignature(account, getMessageHash(account, amount), v, r, s)) { |
| 45 | + revert MerkleAirdrop__InvalidSignature(); |
| 46 | + } |
| 47 | + bytes32 leaf = keccak256(bytes.concat(keccak256(abi.encode(MESSAGE_TYPEHASH, account, amount)))); |
| 48 | + if (!MerkleProof.verify(merkleProof, i_merkleRoot, leaf)) { |
| 49 | + revert MerkleAirdrop__InvalidProof(); |
| 50 | + } |
| 51 | + s_hasClaimed[account] = true; |
| 52 | + emit Claimed(account, amount); |
| 53 | + } |
| 54 | + |
| 55 | + function getMessageHash(address account, uint256 amount) internal view returns (bytes32) { |
| 56 | + return |
| 57 | + _hashTypedDataV4(keccak256(abi.encode(MESSAGE_TYPEHASH, AirdropClaim({account: account, amount: amount})))); |
| 58 | + } |
| 59 | + |
| 60 | + function getMerkleRoot() external view returns (bytes32) { |
| 61 | + return i_merkleRoot; |
| 62 | + } |
| 63 | + |
| 64 | + function getAirdropToken() external view returns (IERC20) { |
| 65 | + return i_airdropToken; |
| 66 | + } |
| 67 | + |
| 68 | + function hasClaimed(address account) external view returns (bool) { |
| 69 | + return s_hasClaimed[account]; |
| 70 | + } |
| 71 | + |
| 72 | + function _isValidSignature(address signer, bytes32 digest, uint8 _v, bytes32 _r, bytes32 _s) |
| 73 | + internal |
| 74 | + pure |
| 75 | + returns (bool) |
| 76 | + { |
| 77 | + ( |
| 78 | + address actualSigner, |
| 79 | + /*ECDSA.RecoverError recoverError*/ |
| 80 | + , |
| 81 | + /*bytes32 SignatureLength*/ |
| 82 | + ) = ECDSA.tryRecover(digest, _v, _r, _s); |
| 83 | + return actualSigner == signer; |
| 84 | + } |
| 85 | +} |
0 commit comments