Skip to content

Commit

Permalink
add merkle distributor
Browse files Browse the repository at this point in the history
  • Loading branch information
liberhe committed Dec 22, 2023
1 parent 79759cc commit 6ef1018
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/main/solidity/MerkleDistributor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ contract MerkleDistributor is IMerkleDistributor {
address public immutable override token;
bytes32 public immutable override merkleRoot;
address public owner;
uint256 public expire_time;

// This is a packed array of booleans.
mapping(uint256 => uint256) private claimedBitMap;

constructor(address token_, bytes32 merkleRoot_) public {
constructor(address token_, bytes32 merkleRoot_, uint _duration) public {
token = token_;
merkleRoot = merkleRoot_;
expire_time = block.timestamp + _duration;
}

function isClaimed(uint256 index) public view override returns (bool) {
Expand All @@ -32,8 +34,10 @@ contract MerkleDistributor is IMerkleDistributor {
}

function claim(uint256 index, address account, uint256 amount, bytes32[] calldata merkleProof) external override {
require (expire_time > block.timestamp, "Expired");
require(!isClaimed(index), 'MerkleDistributor: Drop already claimed.');


// Verify the merkle proof.
bytes32 node = keccak256(abi.encodePacked(index, account, amount));
require(MerkleProof.verify(merkleProof, merkleRoot, node), 'MerkleDistributor: Invalid proof.');
Expand All @@ -45,9 +49,10 @@ contract MerkleDistributor is IMerkleDistributor {
emit Claimed(index, account, amount);
}

/// @notice owner withdraw the rest token
/// @notice owner withdraw the rest token
function claimRestTokens(address to ) public returns (bool) {
// only owner
require (expire_time < block.timestamp, "Not expired yet");
require(msg.sender == owner);
require(IERC20(token).balanceOf(address(this)) >= 0);
require(IERC20(token).transfer(to, IERC20(token).balanceOf(address(this))));
Expand Down
4 changes: 2 additions & 2 deletions src/main/solidity/MerkleDistributorFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ contract MerkleDistributorFactory {

event DistributorCreated(address indexed distributorAddress);

function createDistributor(address token, bytes32 merkleRoot) public {
MerkleDistributor distributor = new MerkleDistributor(token, merkleRoot);
function createDistributor(address token, bytes32 merkleRoot, uint _duration ) public {
MerkleDistributor distributor = new MerkleDistributor(token, merkleRoot, _duration);
distributors.push(distributor);
emit DistributorCreated(address(distributor));
}
Expand Down

0 comments on commit 6ef1018

Please sign in to comment.