Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create HATAirdrop #542

Merged
merged 18 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions contracts/tge/HATAirdrop.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.16;

import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC20/utils/SafeERC20Upgradeable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC20/IERC20Upgradeable.sol";
import "@openzeppelin/contracts-upgradeable/utils/cryptography/MerkleProofUpgradeable.sol";
import "../tokenlock/ITokenLockFactory.sol";
import "./interfaces/IHATAirdrop.sol";

/*
An airdrop contract that transfers tokens based on a merkle tree.
*/
contract HATAirdrop is IHATAirdrop, Initializable {
error CannotRedeemBeforeStartTime();
error CannotRedeemAfterDeadline();
error LeafAlreadyRedeemed();
error InvalidMerkleProof();
error CannotRecoverBeforeDeadline();
error RedeemerMustBeBeneficiary();

using SafeERC20Upgradeable for IERC20Upgradeable;

bytes32 public root;
uint256 public startTime;
uint256 public deadline;
uint256 public lockEndTime;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should consider lock length

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So here we can either just start from a certain date to a certain date, or we can start on claim and end after X time. I implemented the former but we the latter is also a valid option, we just need to decide what behavior we want here.

uint256 public periods;
jellegerbrandy marked this conversation as resolved.
Show resolved Hide resolved
IERC20Upgradeable public token;
ITokenLockFactory public tokenLockFactory;
address public factory;

mapping (bytes32 => bool) public leafRedeemed;

event MerkleTreeSet(string _merkleTreeIPFSRef, bytes32 _root, uint256 _startTime, uint256 _deadline);
event TokensRedeemed(address indexed _account, address indexed _tokenLock, uint256 _amount);

constructor () {
_disableInitializers();
}

/**
* @notice Initialize a HATAirdrop instance
* @param _merkleTreeIPFSRef new merkle tree ipfs reference.
* @param _root new merkle tree root to use for verifying airdrop data.
* @param _startTime start of the redeem period and of the token lock (if exists)
* @param _deadline end time to redeem from the contract
* @param _lockEndTime end time for the token lock contract. If this date is in the past, the tokens will be transferred directly to the user and no token lock will be created
* @param _periods number of periods of the token lock contract (if exists)
* @param _token the token to be airdropped
* @param _tokenLockFactory the token lock factory to use to deploy the token locks
*/
function initialize(
jellegerbrandy marked this conversation as resolved.
Show resolved Hide resolved
string memory _merkleTreeIPFSRef,
jellegerbrandy marked this conversation as resolved.
Show resolved Hide resolved
bytes32 _root,
uint256 _startTime,
uint256 _deadline,
uint256 _lockEndTime,
uint256 _periods,
IERC20Upgradeable _token,
ITokenLockFactory _tokenLockFactory
) external initializer {
root = _root;
startTime = _startTime;
deadline = _deadline;
lockEndTime = _lockEndTime;
periods = _periods;
token = _token;
tokenLockFactory = _tokenLockFactory;
factory = msg.sender;
emit MerkleTreeSet(_merkleTreeIPFSRef, _root, _startTime, _deadline);
}

function redeem(address _account, uint256 _amount, bytes32[] calldata _proof) external {
if (msg.sender != _account && msg.sender != factory) {
revert RedeemerMustBeBeneficiary();
}
// solhint-disable-next-line not-rely-on-time
if (block.timestamp < startTime) revert CannotRedeemBeforeStartTime();
// solhint-disable-next-line not-rely-on-time
if (block.timestamp > deadline) revert CannotRedeemAfterDeadline();
bytes32 leaf = _leaf(_account, _amount);
if (leafRedeemed[leaf]) revert LeafAlreadyRedeemed();
if(!_verify(_proof, leaf)) revert InvalidMerkleProof();
leafRedeemed[leaf] = true;

address _tokenLock = address(0);
// solhint-disable-next-line not-rely-on-time
if (lockEndTime > block.timestamp) {
_tokenLock = tokenLockFactory.createTokenLock(
address(token),
0x0000000000000000000000000000000000000000,
_account,
_amount,
startTime,
lockEndTime,
periods,
0,
0,
false,
true
);
token.safeTransferFrom(factory, _tokenLock, _amount);
} else {
token.safeTransferFrom(factory, _account, _amount);
}

emit TokensRedeemed(_account, _tokenLock, _amount);
}

function _verify(bytes32[] calldata proof, bytes32 leaf) internal view returns (bool) {
return MerkleProofUpgradeable.verifyCalldata(proof, root, leaf);
}

function _leaf(address _account, uint256 _amount) internal pure returns (bytes32) {
return keccak256(abi.encodePacked(_account, _amount));
}
}
77 changes: 77 additions & 0 deletions contracts/tge/HATAirdropFactory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// SPDX-License-Identifier: MIT
// Disclaimer https://github.com/hats-finance/hats-contracts/blob/main/DISCLAIMER.md

pragma solidity 0.8.16;

import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/proxy/Clones.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "./interfaces/IHATAirdrop.sol";

contract HATAirdropFactory is Ownable {
error RedeemDataArraysLengthMismatch();
error ContractIsNotHATAirdrop();
error HATAirdropInitializationFailed();

using SafeERC20 for IERC20;

mapping(address => bool) public isAirdrop;

event TokensWithdrawn(address indexed _owner, uint256 _amount);
event HATAirdropCreated(address indexed _hatAirdrop, bytes _initData, IERC20 _token, uint256 _totalAmount);

function withdrawTokens(IERC20 _token, uint256 _amount) external onlyOwner {
address owner = msg.sender;
_token.safeTransfer(owner, _amount);
emit TokensWithdrawn(owner, _amount);
}

function redeemMultipleAirdrops(IHATAirdrop[] calldata _airdrops, uint256[] calldata _amounts, bytes32[][] calldata _proofs) external {
if (_airdrops.length != _amounts.length || _airdrops.length != _proofs.length) {
revert RedeemDataArraysLengthMismatch();
}

address caller = msg.sender;
for (uint256 i = 0; i < _airdrops.length;) {
if (!isAirdrop[address(_airdrops[i])]) {
revert ContractIsNotHATAirdrop();
}

_airdrops[i].redeem(caller, _amounts[i], _proofs[i]);

unchecked {
++i;
}
}
}

function createHATAirdrop(
address _implementation,
bytes calldata _initData,
IERC20 _token,
uint256 _totalAmount
) external onlyOwner returns (address result) {
result = Clones.cloneDeterministic(_implementation, keccak256(_initData));

// solhint-disable-next-line avoid-low-level-calls
(bool success,) = result.call(_initData);

if (!success) {
revert HATAirdropInitializationFailed();
}

isAirdrop[result] = true;

_token.safeApprove(result, _totalAmount);

emit HATAirdropCreated(result, _initData, _token, _totalAmount);
}

function predictHATAirdropAddress(
address _implementation,
bytes calldata _initData
) external view returns (address) {
return Clones.predictDeterministicAddress(_implementation, keccak256(_initData));
}
}
9 changes: 9 additions & 0 deletions contracts/tge/interfaces/IHATAirdrop.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: MIT
// Disclaimer https://github.com/hats-finance/hats-contracts/blob/main/DISCLAIMER.md

pragma solidity 0.8.16;


interface IHATAirdrop {
function redeem(address _account, uint256 _amount, bytes32[] calldata _proof) external;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# MerkleProofUpgradeable







*These functions deal with verification of Merkle Tree proofs. The tree and the proofs can be generated using our https://github.com/OpenZeppelin/merkle-tree[JavaScript library]. You will find a quickstart guide in the readme. WARNING: You should avoid using leaf values that are 64 bytes long prior to hashing, or use a hash function other than keccak256 for hashing leaves. This is because the concatenation of a sorted pair of internal nodes in the merkle tree could be reinterpreted as a leaf value. OpenZeppelin&#39;s JavaScript library generates merkle trees that are safe against this attack out of the box.*



12 changes: 0 additions & 12 deletions docs/dodoc/elin/contracts/utils/StorageSlot.md

This file was deleted.

12 changes: 0 additions & 12 deletions docs/dodoc/elin/contracts/utils/math/SignedMath.md

This file was deleted.

Loading
Loading