Skip to content
Verified contractExactMIT

MerkleDistributor

Address0x152ed7e48e420f748cd41d49c460ddf29ead656a
Balance

0 ETH

 

Transactions

100

 

Compiler

solc 0.8.16

 

Same code

1 more contract

 

Details

Compiler
solc 0.8.16+commit.07a7930e
Language
solidity
Optimization
Yes, 200 runs
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
24,937,232
Creation transaction
0xd130a49db0…5a6d21eeee

contracts/MerkleDistributor.sol

// SPDX-License-Identifier: MIT
pragma solidity 0.8.16;

interface IERC20 {
    function transfer(address to, uint256 amount) external returns (bool);
    function balanceOf(address account) external view returns (uint256);
}

contract MerkleDistributor {
    address public immutable token;
    bytes32 public immutable merkleRoot;
    address public owner;

    mapping(address => bool) public hasClaimed;
    uint256 public totalClaimed;

    event Claimed(address indexed account, uint256 amount);
    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    modifier onlyOwner() {
        require(msg.sender == owner, "Not owner");
        _;
    }

    constructor(address _token, bytes32 _merkleRoot) {
        token = _token;
        merkleRoot = _merkleRoot;
        owner = msg.sender;
    }

    function claim(uint256 amount, bytes32[] calldata proof) external {
        require(!hasClaimed[msg.sender], "Already claimed");

        // Verify merkle proof
        bytes32 leaf = keccak256(bytes.concat(keccak256(abi.encode(msg.sender, amount))));
        require(verify(proof, merkleRoot, leaf), "Invalid proof");

        hasClaimed[msg.sender] = true;
        totalClaimed += amount;

        require(IERC20(token).transfer(msg.sender, amount), "Transfer failed");
        emit Claimed(msg.sender, amount);
    }

    function verify(
        bytes32[] calldata proof,
        bytes32 root,
        bytes32 leaf
    ) internal pure returns (bool) {
        bytes32 computedHash = leaf;
        for (uint256 i = 0; i < proof.length; i++) {
            computedHash = _hashPair(computedHash, proof[i]);
        }
        return computedHash == root;
    }

    function _hashPair(bytes32 a, bytes32 b) private pure returns (bytes32) {
        return a < b
            ? keccak256(abi.encodePacked(a, b))
            : keccak256(abi.encodePacked(b, a));
    }

    // View: check if an address can claim (returns amount if valid proof, 0 if already claimed)
    function canClaim(address account, uint256 amount, bytes32[] calldata proof) external view returns (bool) {
        if (hasClaimed[account]) return false;
        bytes32 leaf = keccak256(bytes.concat(keccak256(abi.encode(account, amount))));
        return verify(proof, merkleRoot, leaf);
    }

    // View: remaining tokens in the distributor
    function remainingTokens() external view returns (uint256) {
        return IERC20(token).balanceOf(address(this));
    }

    function transferOwnership(address newOwner) external onlyOwner {
        require(newOwner != address(0), "Zero address");
        emit OwnershipTransferred(owner, newOwner);
        owner = newOwner;
    }

    // Emergency: owner can rescue OTHER tokens accidentally sent here (not the claim token)
    function rescueTokens(address _token, uint256 amount) external onlyOwner {
        require(_token != token, "Cannot rescue claim token");
        IERC20(_token).transfer(owner, amount);
    }

    function rescueETH() external onlyOwner {
        payable(owner).transfer(address(this).balance);
    }

    receive() external payable {}
}
Compiler settingsShow
{
  "optimizer": {
    "runs": 200,
    "enabled": true
  }
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.