Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

MerkleDistributor

Git Source

Inherits: Owned, ReentrancyGuard

Title: MerkleDistributor

Distributes ERC20 tokens to eligible addresses using Merkle proofs

Implements a custom claiming window with admin pause functionality

State Variables

_paused

Internal variable to track pause status

bool private _paused

token

The ERC20 token being distributed

ERC20 public immutable token

merkleRoot

The Merkle root of the distribution tree

bytes32 public immutable merkleRoot

claimDeadline

Timestamp when the claiming period ends (90 days from deployment)

uint256 public immutable claimDeadline

claimed

Tracks claimed amounts for each address

mapping(address => uint256) public claimed

totalClaimed

Total amount of tokens claimed so far

uint256 public totalClaimed

totalAllocated

Total amount of tokens allocated for distribution

uint256 public totalAllocated

Functions

constructor

Initializes the distributor with token and merkle root

constructor(
    address _token,
    bytes32 _merkleRoot,
    address _owner,
    uint256 window,
    uint256 _totalAllocated
) Owned(_owner);

Parameters

NameTypeDescription
_tokenaddressThe ERC20 token to distribute
_merkleRootbytes32The merkle root of the distribution tree
_owneraddressThe owner address for admin functions
windowuint256Time window to allow claims for in seconds
_totalAllocateduint256Total token allocation for merkle drop

whenNotPaused

Modifier to make a function callable only when the contract is not paused.

modifier whenNotPaused() ;

claim

Claims tokens for the caller

function claim(uint256 amount, bytes32[] calldata merkleProof)
    external
    nonReentrant
    whenNotPaused;

Parameters

NameTypeDescription
amountuint256The amount of tokens to claim
merkleProofbytes32[]The merkle proof for the claim

claimFor

Claims tokens on behalf of an account

function claimFor(
    address account,
    uint256 amount,
    bytes32[] calldata merkleProof
) external nonReentrant whenNotPaused;

Parameters

NameTypeDescription
accountaddressThe account to claim for
amountuint256The amount of tokens to claim
merkleProofbytes32[]The merkle proof for the claim

canClaim

Checks if an address can claim tokens

function canClaim(
    address account,
    uint256 amount,
    bytes32[] calldata merkleProof
) external view returns (bool _canClaim, uint256 claimableAmount);

Parameters

NameTypeDescription
accountaddressThe address to check
amountuint256The amount to check
merkleProofbytes32[]The merkle proof

Returns

NameTypeDescription
_canClaimboolWhether the address can claim
claimableAmountuint256The amount that can be claimed

pause

Pauses claiming functionality

Only callable by owner

function pause() external whenNotPaused onlyOwner;

unpause

Unpauses claiming functionality

Only callable by owner

function unpause() external onlyOwner;

recoverERC20

Recovers ERC20 tokens sent to this contract

Can only recover non-distribution tokens before deadline, or any token after deadline

function recoverERC20(address tokenAddress, address to) external onlyOwner;

Parameters

NameTypeDescription
tokenAddressaddressThe token to recover
toaddressThe address to send recovered tokens to

withdrawUnclaimed

Withdraws unclaimed tokens after the deadline

Only callable by owner after claim deadline

function withdrawUnclaimed(address to) external onlyOwner;

Parameters

NameTypeDescription
toaddressThe address to send unclaimed tokens to

setTotalAllocated

Sets the total allocated amount for tracking

Should be called after funding the contract

function setTotalAllocated(uint256 _totalAllocated) external onlyOwner;

Parameters

NameTypeDescription
_totalAllocateduint256The total amount allocated for distribution

_claim

Internal claim logic

function _claim(
    address account,
    uint256 amount,
    bytes32[] calldata merkleProof
) internal;

Parameters

NameTypeDescription
accountaddressThe account claiming tokens
amountuint256The total amount allocated to the account
merkleProofbytes32[]The merkle proof for verification

getClaimedAmount

Returns the amount already claimed by an account

function getClaimedAmount(address account) external view returns (uint256);

Parameters

NameTypeDescription
accountaddressThe account to check

Returns

NameTypeDescription
<none>uint256The amount already claimed

getRemainingTime

Returns the remaining time until claim deadline

function getRemainingTime() external view returns (uint256);

Returns

NameTypeDescription
<none>uint256The remaining time in seconds (0 if deadline passed)

isClaimingActive

Checks if the claiming period is still active

function isClaimingActive() external view returns (bool);

Returns

NameTypeDescription
<none>boolWhether claiming is still possible

paused

Returns true if the contract is paused, and false otherwise.

function paused() public view virtual returns (bool);

Events

Claimed

event Claimed(address indexed account, uint256 amount);

EmergencyWithdraw

event EmergencyWithdraw(
    address indexed token, address indexed to, uint256 amount
);

Paused

event Paused(address account);

Unpaused

event Unpaused(address account);

Errors

InvalidProof

error InvalidProof();

ClaimingPeriodEnded

error ClaimingPeriodEnded();

AlreadyClaimed

error AlreadyClaimed();

ZeroAddress

error ZeroAddress();

ZeroAmount

error ZeroAmount();

InvalidMerkleRoot

error InvalidMerkleRoot();

DeadlineNotReached

error DeadlineNotReached();

EnforcedPause

error EnforcedPause();