Verified contractExactMIT
ElpidaV2Distributor
Address0x30c8c1f345f68c3b48efee450fa916f6ce6340a7
- Balance
0 ETH
- Transactions
1
- Compiler
solc 0.8.26
- Same code
Only this contract
Details
- Compiler
- solc 0.8.26+commit.8a97fa7a
- Language
- solidity
- Optimization
- Yes, 200 runs
- EVM version
- cancun
- Via IR
- yes
- License
- MIT
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 26,484,625
- Creation transaction
- 0x12f3468b9a…866369fb96
contracts/evm/v2/ElpidaV2Distributor.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.26;
import {MerkleProof} from "@openzeppelin/contracts/utils/cryptography/MerkleProof.sol";
interface IConvertRouter {
struct ExactInputSingleParams { address tokenIn; address tokenOut; uint24 fee; address recipient; uint256 amountIn; uint256 amountOutMinimum; uint160 sqrtPriceLimitX96; }
function exactInputSingle(ExactInputSingleParams calldata) external payable returns (uint256);
}
interface IWrappedNative { function withdraw(uint256) external; }
/// @notice ELPIDA V2.0 reward distributor (NOT AUDITED; local chains and test networks only). One per chain and purpose:
/// the trader airdrop (calendar-month rounds) and holder rewards (fixed rounds, e.g. weekly). The V2 hook pays the
/// fee share straight in; nothing else funds it, and its money can only go to the wallets in a published round.
///
/// A round's schedule is fixed here, so anyone can see when it ends. After it ends the operator (the keeper) posts the
/// round's Merkle root for each payout asset, with the round's total, which must be covered by funds not yet promised
/// to an earlier round. Then anyone may pay any listed wallet with its proof (the keeper pushes them; a wallet can
/// also claim itself). A payment that fails (a paused stock, a wallet that refuses) stays claimable. What a round
/// leaves unclaimed for CLAIM_WINDOW_ROUNDS rounds goes back to the pool for later rounds, never to anyone else.
/// The operator cannot move funds: it can only post a root once per round and asset, and (holder rewards only) convert
/// unpromised USDC into the native coin through the chain's Uniswap v3 router, the proceeds staying here (Malik,
/// 9 November: holders of USDC-paired tokens are paid in the native coin).
contract ElpidaV2Distributor {
uint256 public constant VERSION = 1;
uint256 public constant CLAIM_WINDOW_ROUNDS = 12;
address public immutable operator;
/// 0: calendar months (UTC). Otherwise the round length in seconds.
uint256 public immutable period;
/// Start of round 0 (for calendar months, the first of a month at 00:00 UTC).
uint256 public immutable genesis;
uint256 private immutable genesisMonth;
/// What this distributor pays, for people reading it on an explorer: "trader-airdrop" or "holder-rewards".
string public purpose;
/// Holder rewards only: the one asset that may be converted (USDC), the Uniswap v3 router and the wrapped native
/// coin. Zero when conversion is off.
address public immutable convertible;
address public immutable swapRouter;
address public immutable wrappedNative;
struct Round {
bytes32 root;
uint128 total;
uint128 claimed;
uint32 recipients;
uint64 postedAt;
bool expired;
}
mapping(uint256 round => mapping(address asset => Round)) public rounds;
/// Posted but not yet paid or expired, per asset.
mapping(address asset => uint256) public reserved;
mapping(bytes32 leaf => bool) public paid;
bool private entered;
event RootPosted(uint256 indexed round, address indexed asset, bytes32 root, uint256 total, uint256 recipients, string manifestURI);
event Claimed(uint256 indexed round, address indexed asset, address indexed account, uint256 amount);
event ClaimFailed(uint256 indexed round, address indexed asset, address indexed account, uint256 amount);
event Expired(uint256 indexed round, address indexed asset, uint256 returnedToPool);
event Received(address indexed from, uint256 amount);
event Converted(address indexed asset, uint256 amountIn, uint256 nativeOut);
error OnlyOperator();
error RoundNotOver();
error AlreadyPosted();
error NotFunded();
error BadProof();
error AlreadyPaid();
error RoundClosed();
error NotStarted();
error BadTerms();
error Reentrancy();
modifier nonReentrant() { if (entered) revert Reentrancy(); entered = true; _; entered = false; }
constructor(address operator_, uint256 period_, uint256 genesis_, string memory purpose_, address convertible_, address swapRouter_, address wrappedNative_) {
if (operator_ == address(0) || genesis_ % 1 days != 0 || bytes(purpose_).length == 0) revert BadTerms();
if ((convertible_ == address(0)) != (swapRouter_ == address(0)) || (swapRouter_ == address(0)) != (wrappedNative_ == address(0))) revert BadTerms();
convertible = convertible_; swapRouter = swapRouter_; wrappedNative = wrappedNative_;
if (period_ == 0) {
(uint256 y, uint256 m, uint256 d) = _civil(genesis_ / 1 days);
if (d != 1) revert BadTerms();
genesisMonth = y * 12 + m - 1;
} else if (period_ < 1 days) revert BadTerms();
operator = operator_; period = period_; genesis = genesis_; purpose = purpose_;
}
receive() external payable { emit Received(msg.sender, msg.value); }
// ---- schedule ----
function roundStart(uint256 round) public view returns (uint256) {
if (period != 0) return genesis + round * period;
uint256 index = genesisMonth + round;
return _days(index / 12, index % 12 + 1, 1) * 1 days;
}
function roundEnd(uint256 round) public view returns (uint256) { return roundStart(round + 1); }
function currentRound() public view returns (uint256) {
if (block.timestamp < genesis) revert NotStarted();
if (period != 0) return (block.timestamp - genesis) / period;
(uint256 y, uint256 m,) = _civil(block.timestamp / 1 days);
return y * 12 + m - 1 - genesisMonth;
}
/// Funds of `asset` not promised to any posted round: what the next root may distribute.
function available(address asset) public view returns (uint256) {
return _balance(asset) - reserved[asset];
}
/// The leaf for one payout. Binds the chain and this distributor, so a proof works nowhere else.
function leaf(uint256 round, address asset, address account, uint256 amount) public view returns (bytes32) {
return keccak256(bytes.concat(keccak256(abi.encode(block.chainid, address(this), round, asset, account, amount))));
}
// ---- rounds ----
function postRoot(uint256 round, address asset, bytes32 root, uint256 total, uint32 recipients, string calldata manifestURI) external {
if (msg.sender != operator) revert OnlyOperator();
if (block.timestamp < roundEnd(round)) revert RoundNotOver();
Round storage r = rounds[round][asset];
if (r.root != bytes32(0)) revert AlreadyPosted();
if (root == bytes32(0) || total == 0 || recipients == 0 || total > type(uint128).max) revert BadTerms();
if (total > available(asset)) revert NotFunded();
r.root = root; r.total = uint128(total); r.recipients = recipients; r.postedAt = uint64(block.timestamp);
reserved[asset] += total;
emit RootPosted(round, asset, root, total, recipients, manifestURI);
}
/// Pays `account` its amount for `round` in `asset`. Anyone may call. Returns false (and keeps it claimable) if the
/// transfer fails.
function claim(uint256 round, address asset, address account, uint256 amount, bytes32[] calldata proof) external nonReentrant returns (bool) {
return _claim(round, asset, account, amount, proof);
}
struct Payout { address account; uint256 amount; bytes32[] proof; }
/// Pays many wallets of one round and asset; a failed transfer is skipped and stays claimable.
/// A leaf that was already paid (for instance its wallet claimed it itself) is skipped, so one self-claim never
/// blocks the rest of a batch; a closed round still reverts as a whole.
function claimMany(uint256 round, address asset, Payout[] calldata payouts) external nonReentrant returns (uint256 ok) {
for (uint256 i = 0; i < payouts.length; i++) {
if (paid[leaf(round, asset, payouts[i].account, payouts[i].amount)]) continue;
if (_claim(round, asset, payouts[i].account, payouts[i].amount, payouts[i].proof)) ok++;
}
}
/// Converts `amountIn` of unpromised USDC into the native coin (Uniswap v3, pool fee `poolFee`), at least `minOut`.
/// Operator only; the proceeds stay in this distributor for later rounds.
function convertToNative(uint256 amountIn, uint256 minOut, uint24 poolFee) external nonReentrant returns (uint256 out) {
if (msg.sender != operator) revert OnlyOperator();
if (swapRouter == address(0) || minOut == 0) revert BadTerms();
if (amountIn == 0 || amountIn > available(convertible)) revert NotFunded();
if (!_approve(convertible, swapRouter, amountIn)) revert BadTerms();
uint256 before = address(this).balance;
out = IConvertRouter(swapRouter).exactInputSingle(IConvertRouter.ExactInputSingleParams(convertible, wrappedNative, poolFee, address(this), amountIn, minOut, 0));
_approve(convertible, swapRouter, 0);
IWrappedNative(wrappedNative).withdraw(out);
if (address(this).balance - before != out) revert BadTerms();
emit Converted(convertible, amountIn, out);
}
/// After CLAIM_WINDOW_ROUNDS more rounds, what a round left unclaimed returns to the pool for later rounds.
function expire(uint256 round, address asset) external {
Round storage r = rounds[round][asset];
if (r.root == bytes32(0) || r.expired) revert BadTerms();
if (currentRound() <= round + CLAIM_WINDOW_ROUNDS) revert RoundNotOver();
r.expired = true;
uint256 left = r.total - r.claimed;
reserved[asset] -= left;
emit Expired(round, asset, left);
}
function _claim(uint256 round, address asset, address account, uint256 amount, bytes32[] calldata proof) private returns (bool) {
Round storage r = rounds[round][asset];
if (r.root == bytes32(0) || r.expired) revert RoundClosed();
bytes32 l = leaf(round, asset, account, amount);
if (paid[l]) revert AlreadyPaid();
if (!MerkleProof.verifyCalldata(proof, r.root, l)) revert BadProof();
paid[l] = true; r.claimed += uint128(amount); reserved[asset] -= amount;
if (_send(asset, account, amount)) { emit Claimed(round, asset, account, amount); return true; }
paid[l] = false; r.claimed -= uint128(amount); reserved[asset] += amount;
emit ClaimFailed(round, asset, account, amount);
return false;
}
// ---- helpers ----
function _balance(address asset) private view returns (uint256) {
if (asset == address(0)) return address(this).balance;
(bool ok, bytes memory ret) = asset.staticcall(abi.encodeWithSelector(0x70a08231, address(this)));
return ok && ret.length >= 32 ? abi.decode(ret, (uint256)) : 0;
}
function _send(address asset, address to, uint256 amount) private returns (bool ok) {
if (asset == address(0)) { (ok,) = to.call{value: amount}(""); return ok; }
if (asset.code.length == 0) return false;
bytes memory ret;
(ok, ret) = asset.call(abi.encodeWithSelector(0xa9059cbb, to, amount));
return ok && (ret.length == 0 || (ret.length >= 32 && abi.decode(ret, (bool))));
}
function _approve(address asset, address spender, uint256 amount) private returns (bool ok) {
bytes memory ret;
(ok, ret) = asset.call(abi.encodeWithSelector(0x095ea7b3, spender, amount));
return ok && (ret.length == 0 || (ret.length >= 32 && abi.decode(ret, (bool))));
}
/// Days since 1970-01-01 for a civil date (Howard Hinnant's algorithm; years from 1970).
function _days(uint256 y, uint256 m, uint256 d) private pure returns (uint256) {
if (m <= 2) y -= 1;
uint256 era = y / 400;
uint256 yoe = y - era * 400;
uint256 doy = (153 * (m > 2 ? m - 3 : m + 9) + 2) / 5 + d - 1;
return era * 146097 + yoe * 365 + yoe / 4 - yoe / 100 + doy - 719468;
}
function _civil(uint256 z) private pure returns (uint256 y, uint256 m, uint256 d) {
z += 719468;
uint256 era = z / 146097;
uint256 doe = z - era * 146097;
uint256 yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
uint256 doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
uint256 mp = (5 * doy + 2) / 153;
d = doy - (153 * mp + 2) / 5 + 1;
m = mp < 10 ? mp + 3 : mp - 9;
y = yoe + era * 400 + (m <= 2 ? 1 : 0);
}
}
@openzeppelin/contracts/utils/cryptography/Hashes.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.3.0) (utils/cryptography/Hashes.sol)
pragma solidity ^0.8.20;
/**
* @dev Library of standard hash functions.
*
* _Available since v5.1._
*/
library Hashes {
/**
* @dev Commutative Keccak256 hash of a sorted pair of bytes32. Frequently used when working with merkle proofs.
*
* NOTE: Equivalent to the `standardNodeHash` in our https://github.com/OpenZeppelin/merkle-tree[JavaScript library].
*/
function commutativeKeccak256(bytes32 a, bytes32 b) internal pure returns (bytes32) {
return a < b ? efficientKeccak256(a, b) : efficientKeccak256(b, a);
}
/**
* @dev Implementation of keccak256(abi.encode(a, b)) that doesn't allocate or expand memory.
*/
function efficientKeccak256(bytes32 a, bytes32 b) internal pure returns (bytes32 value) {
assembly ("memory-safe") {
mstore(0x00, a)
mstore(0x20, b)
value := keccak256(0x00, 0x40)
}
}
}
@openzeppelin/contracts/utils/cryptography/MerkleProof.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (utils/cryptography/MerkleProof.sol)
// This file was procedurally generated from scripts/generate/templates/MerkleProof.js.
pragma solidity ^0.8.20;
import {Hashes} from "./Hashes.sol";
/**
* @dev 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's JavaScript library generates Merkle trees that are safe
* against this attack out of the box.
*
* IMPORTANT: Consider memory side-effects when using custom hashing functions
* that access memory in an unsafe way.
*
* NOTE: This library supports proof verification for merkle trees built using
* custom _commutative_ hashing functions (i.e. `H(a, b) == H(b, a)`). Proving
* leaf inclusion in trees built using non-commutative hashing functions requires
* additional logic that is not supported by this library.
*/
library MerkleProof {
/**
*@dev The multiproof provided is not valid.
*/
error MerkleProofInvalidMultiproof();
/**
* @dev Returns true if a `leaf` can be proved to be a part of a Merkle tree
* defined by `root`. For this, a `proof` must be provided, containing
* sibling hashes on the branch from the leaf to the root of the tree. Each
* pair of leaves and each pair of pre-images are assumed to be sorted.
*
* This version handles proofs in memory with the default hashing function.
*/
function verify(bytes32[] memory proof, bytes32 root, bytes32 leaf) internal pure returns (bool) {
return processProof(proof, leaf) == root;
}
/**
* @dev Returns the rebuilt hash obtained by traversing a Merkle tree up
* from `leaf` using `proof`. A `proof` is valid if and only if the rebuilt
* hash matches the root of the tree. When processing the proof, the pairs
* of leaves & pre-images are assumed to be sorted.
*
* This version handles proofs in memory with the default hashing function.
*/
function processProof(bytes32[] memory proof, bytes32 leaf) internal pure returns (bytes32) {
bytes32 computedHash = leaf;
for (uint256 i = 0; i < proof.length; i++) {
computedHash = Hashes.commutativeKeccak256(computedHash, proof[i]);
}
return computedHash;
}
/**
* @dev Returns true if a `leaf` can be proved to be a part of a Merkle tree
* defined by `root`. For this, a `proof` must be provided, containing
* sibling hashes on the branch from the leaf to the root of the tree. Each
* pair of leaves and each pair of pre-images are assumed to be sorted.
*
* This version handles proofs in memory with a custom hashing function.
*/
function verify(
bytes32[] memory proof,
bytes32 root,
bytes32 leaf,
function(bytes32, bytes32) view returns (bytes32) hasher
) internal view returns (bool) {
return processProof(proof, leaf, hasher) == root;
}
/**
* @dev Returns the rebuilt hash obtained by traversing a Merkle tree up
* from `leaf` using `proof`. A `proof` is valid if and only if the rebuilt
* hash matches the root of the tree. When processing the proof, the pairs
* of leaves & pre-images are assumed to be sorted.
*
* This version handles proofs in memory with a custom hashing function.
*/
function processProof(
bytes32[] memory proof,
bytes32 leaf,
function(bytes32, bytes32) view returns (bytes32) hasher
) internal view returns (bytes32) {
bytes32 computedHash = leaf;
for (uint256 i = 0; i < proof.length; i++) {
computedHash = hasher(computedHash, proof[i]);
}
return computedHash;
}
/**
* @dev Returns true if a `leaf` can be proved to be a part of a Merkle tree
* defined by `root`. For this, a `proof` must be provided, containing
* sibling hashes on the branch from the leaf to the root of the tree. Each
* pair of leaves and each pair of pre-images are assumed to be sorted.
*
* This version handles proofs in calldata with the default hashing function.
*/
function verifyCalldata(bytes32[] calldata proof, bytes32 root, bytes32 leaf) internal pure returns (bool) {
return processProofCalldata(proof, leaf) == root;
}
/**
* @dev Returns the rebuilt hash obtained by traversing a Merkle tree up
* from `leaf` using `proof`. A `proof` is valid if and only if the rebuilt
* hash matches the root of the tree. When processing the proof, the pairs
* of leaves & pre-images are assumed to be sorted.
*
* This version handles proofs in calldata with the default hashing function.
*/
function processProofCalldata(bytes32[] calldata proof, bytes32 leaf) internal pure returns (bytes32) {
bytes32 computedHash = leaf;
for (uint256 i = 0; i < proof.length; i++) {
computedHash = Hashes.commutativeKeccak256(computedHash, proof[i]);
}
return computedHash;
}
/**
* @dev Returns true if a `leaf` can be proved to be a part of a Merkle tree
* defined by `root`. For this, a `proof` must be provided, containing
* sibling hashes on the branch from the leaf to the root of the tree. Each
* pair of leaves and each pair of pre-images are assumed to be sorted.
*
* This version handles proofs in calldata with a custom hashing function.
*/
function verifyCalldata(
bytes32[] calldata proof,
bytes32 root,
bytes32 leaf,
function(bytes32, bytes32) view returns (bytes32) hasher
) internal view returns (bool) {
return processProofCalldata(proof, leaf, hasher) == root;
}
/**
* @dev Returns the rebuilt hash obtained by traversing a Merkle tree up
* from `leaf` using `proof`. A `proof` is valid if and only if the rebuilt
* hash matches the root of the tree. When processing the proof, the pairs
* of leaves & pre-images are assumed to be sorted.
*
* This version handles proofs in calldata with a custom hashing function.
*/
function processProofCalldata(
bytes32[] calldata proof,
bytes32 leaf,
function(bytes32, bytes32) view returns (bytes32) hasher
) internal view returns (bytes32) {
bytes32 computedHash = leaf;
for (uint256 i = 0; i < proof.length; i++) {
computedHash = hasher(computedHash, proof[i]);
}
return computedHash;
}
/**
* @dev Returns true if the `leaves` can be simultaneously proven to be a part of a Merkle tree defined by
* `root`, according to `proof` and `proofFlags` as described in {processMultiProof}.
*
* This version handles multiproofs in memory with the default hashing function.
*
* CAUTION: Not all Merkle trees admit multiproofs. See {processMultiProof} for details.
*
* NOTE: Consider the case where `root == proof[0] && leaves.length == 0` as it will return `true`.
* The `leaves` must be validated independently. See {processMultiProof}.
*/
function multiProofVerify(
bytes32[] memory proof,
bool[] memory proofFlags,
bytes32 root,
bytes32[] memory leaves
) internal pure returns (bool) {
return processMultiProof(proof, proofFlags, leaves) == root;
}
/**
* @dev Returns the root of a tree reconstructed from `leaves` and sibling nodes in `proof`. The reconstruction
* proceeds by incrementally reconstructing all inner nodes by combining a leaf/inner node with either another
* leaf/inner node or a proof sibling node, depending on whether each `proofFlags` item is true or false
* respectively.
*
* This version handles multiproofs in memory with the default hashing function.
*
* CAUTION: Not all Merkle trees admit multiproofs. To use multiproofs, it is sufficient to ensure that: 1) the tree
* is complete (but not necessarily perfect), 2) the leaves to be proven are in the opposite order they are in the
* tree (i.e., as seen from right to left starting at the deepest layer and continuing at the next layer).
*
* NOTE: The _empty set_ (i.e. the case where `proof.length == 1 && leaves.length == 0`) is considered a no-op,
* and therefore a valid multiproof (i.e. it returns `proof[0]`). Consider disallowing this case if you're not
* validating the leaves elsewhere.
*/
function processMultiProof(
bytes32[] memory proof,
bool[] memory proofFlags,
bytes32[] memory leaves
) internal pure returns (bytes32 merkleRoot) {
// This function rebuilds the root hash by traversing the tree up from the leaves. The root is rebuilt by
// consuming and producing values on a queue. The queue starts with the `leaves` array, then goes onto the
// `hashes` array. At the end of the process, the last hash in the `hashes` array should contain the root of
// the Merkle tree.
uint256 leavesLen = leaves.length;
uint256 proofFlagsLen = proofFlags.length;
// Check proof validity.
if (leavesLen + proof.length != proofFlagsLen + 1) {
revert MerkleProofInvalidMultiproof();
}
// The xxxPos values are "pointers" to the next value to consume in each array. All accesses are done using
// `xxx[xxxPos++]`, which return the current value and increment the pointer, thus mimicking a queue's "pop".
bytes32[] memory hashes = new bytes32[](proofFlagsLen);
uint256 leafPos = 0;
uint256 hashPos = 0;
uint256 proofPos = 0;
// At each step, we compute the next hash using two values:
// - a value from the "main queue". If not all leaves have been consumed, we get the next leaf, otherwise we
// get the next hash.
// - depending on the flag, either another value from the "main queue" (merging branches) or an element from the
// `proof` array.
for (uint256 i = 0; i < proofFlagsLen; i++) {
bytes32 a = leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++];
bytes32 b = proofFlags[i]
? (leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++])
: proof[proofPos++];
hashes[i] = Hashes.commutativeKeccak256(a, b);
}
if (proofFlagsLen > 0) {
if (proofPos != proof.length) {
revert MerkleProofInvalidMultiproof();
}
unchecked {
return hashes[proofFlagsLen - 1];
}
} else if (leavesLen > 0) {
return leaves[0];
} else {
return proof[0];
}
}
/**
* @dev Returns true if the `leaves` can be simultaneously proven to be a part of a Merkle tree defined by
* `root`, according to `proof` and `proofFlags` as described in {processMultiProof}.
*
* This version handles multiproofs in memory with a custom hashing function.
*
* CAUTION: Not all Merkle trees admit multiproofs. See {processMultiProof} for details.
*
* NOTE: Consider the case where `root == proof[0] && leaves.length == 0` as it will return `true`.
* The `leaves` must be validated independently. See {processMultiProof}.
*/
function multiProofVerify(
bytes32[] memory proof,
bool[] memory proofFlags,
bytes32 root,
bytes32[] memory leaves,
function(bytes32, bytes32) view returns (bytes32) hasher
) internal view returns (bool) {
return processMultiProof(proof, proofFlags, leaves, hasher) == root;
}
/**
* @dev Returns the root of a tree reconstructed from `leaves` and sibling nodes in `proof`. The reconstruction
* proceeds by incrementally reconstructing all inner nodes by combining a leaf/inner node with either another
* leaf/inner node or a proof sibling node, depending on whether each `proofFlags` item is true or false
* respectively.
*
* This version handles multiproofs in memory with a custom hashing function.
*
* CAUTION: Not all Merkle trees admit multiproofs. To use multiproofs, it is sufficient to ensure that: 1) the tree
* is complete (but not necessarily perfect), 2) the leaves to be proven are in the opposite order they are in the
* tree (i.e., as seen from right to left starting at the deepest layer and continuing at the next layer).
*
* NOTE: The _empty set_ (i.e. the case where `proof.length == 1 && leaves.length == 0`) is considered a no-op,
* and therefore a valid multiproof (i.e. it returns `proof[0]`). Consider disallowing this case if you're not
* validating the leaves elsewhere.
*/
function processMultiProof(
bytes32[] memory proof,
bool[] memory proofFlags,
bytes32[] memory leaves,
function(bytes32, bytes32) view returns (bytes32) hasher
) internal view returns (bytes32 merkleRoot) {
// This function rebuilds the root hash by traversing the tree up from the leaves. The root is rebuilt by
// consuming and producing values on a queue. The queue starts with the `leaves` array, then goes onto the
// `hashes` array. At the end of the process, the last hash in the `hashes` array should contain the root of
// the Merkle tree.
uint256 leavesLen = leaves.length;
uint256 proofFlagsLen = proofFlags.length;
// Check proof validity.
if (leavesLen + proof.length != proofFlagsLen + 1) {
revert MerkleProofInvalidMultiproof();
}
// The xxxPos values are "pointers" to the next value to consume in each array. All accesses are done using
// `xxx[xxxPos++]`, which return the current value and increment the pointer, thus mimicking a queue's "pop".
bytes32[] memory hashes = new bytes32[](proofFlagsLen);
uint256 leafPos = 0;
uint256 hashPos = 0;
uint256 proofPos = 0;
// At each step, we compute the next hash using two values:
// - a value from the "main queue". If not all leaves have been consumed, we get the next leaf, otherwise we
// get the next hash.
// - depending on the flag, either another value from the "main queue" (merging branches) or an element from the
// `proof` array.
for (uint256 i = 0; i < proofFlagsLen; i++) {
bytes32 a = leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++];
bytes32 b = proofFlags[i]
? (leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++])
: proof[proofPos++];
hashes[i] = hasher(a, b);
}
if (proofFlagsLen > 0) {
if (proofPos != proof.length) {
revert MerkleProofInvalidMultiproof();
}
unchecked {
return hashes[proofFlagsLen - 1];
}
} else if (leavesLen > 0) {
return leaves[0];
} else {
return proof[0];
}
}
/**
* @dev Returns true if the `leaves` can be simultaneously proven to be a part of a Merkle tree defined by
* `root`, according to `proof` and `proofFlags` as described in {processMultiProof}.
*
* This version handles multiproofs in calldata with the default hashing function.
*
* CAUTION: Not all Merkle trees admit multiproofs. See {processMultiProof} for details.
*
* NOTE: Consider the case where `root == proof[0] && leaves.length == 0` as it will return `true`.
* The `leaves` must be validated independently. See {processMultiProofCalldata}.
*/
function multiProofVerifyCalldata(
bytes32[] calldata proof,
bool[] calldata proofFlags,
bytes32 root,
bytes32[] memory leaves
) internal pure returns (bool) {
return processMultiProofCalldata(proof, proofFlags, leaves) == root;
}
/**
* @dev Returns the root of a tree reconstructed from `leaves` and sibling nodes in `proof`. The reconstruction
* proceeds by incrementally reconstructing all inner nodes by combining a leaf/inner node with either another
* leaf/inner node or a proof sibling node, depending on whether each `proofFlags` item is true or false
* respectively.
*
* This version handles multiproofs in calldata with the default hashing function.
*
* CAUTION: Not all Merkle trees admit multiproofs. To use multiproofs, it is sufficient to ensure that: 1) the tree
* is complete (but not necessarily perfect), 2) the leaves to be proven are in the opposite order they are in the
* tree (i.e., as seen from right to left starting at the deepest layer and continuing at the next layer).
*
* NOTE: The _empty set_ (i.e. the case where `proof.length == 1 && leaves.length == 0`) is considered a no-op,
* and therefore a valid multiproof (i.e. it returns `proof[0]`). Consider disallowing this case if you're not
* validating the leaves elsewhere.
*/
function processMultiProofCalldata(
bytes32[] calldata proof,
bool[] calldata proofFlags,
bytes32[] memory leaves
) internal pure returns (bytes32 merkleRoot) {
// This function rebuilds the root hash by traversing the tree up from the leaves. The root is rebuilt by
// consuming and producing values on a queue. The queue starts with the `leaves` array, then goes onto the
// `hashes` array. At the end of the process, the last hash in the `hashes` array should contain the root of
// the Merkle tree.
uint256 leavesLen = leaves.length;
uint256 proofFlagsLen = proofFlags.length;
// Check proof validity.
if (leavesLen + proof.length != proofFlagsLen + 1) {
revert MerkleProofInvalidMultiproof();
}
// The xxxPos values are "pointers" to the next value to consume in each array. All accesses are done using
// `xxx[xxxPos++]`, which return the current value and increment the pointer, thus mimicking a queue's "pop".
bytes32[] memory hashes = new bytes32[](proofFlagsLen);
uint256 leafPos = 0;
uint256 hashPos = 0;
uint256 proofPos = 0;
// At each step, we compute the next hash using two values:
// - a value from the "main queue". If not all leaves have been consumed, we get the next leaf, otherwise we
// get the next hash.
// - depending on the flag, either another value from the "main queue" (merging branches) or an element from the
// `proof` array.
for (uint256 i = 0; i < proofFlagsLen; i++) {
bytes32 a = leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++];
bytes32 b = proofFlags[i]
? (leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++])
: proof[proofPos++];
hashes[i] = Hashes.commutativeKeccak256(a, b);
}
if (proofFlagsLen > 0) {
if (proofPos != proof.length) {
revert MerkleProofInvalidMultiproof();
}
unchecked {
return hashes[proofFlagsLen - 1];
}
} else if (leavesLen > 0) {
return leaves[0];
} else {
return proof[0];
}
}
/**
* @dev Returns true if the `leaves` can be simultaneously proven to be a part of a Merkle tree defined by
* `root`, according to `proof` and `proofFlags` as described in {processMultiProof}.
*
* This version handles multiproofs in calldata with a custom hashing function.
*
* CAUTION: Not all Merkle trees admit multiproofs. See {processMultiProof} for details.
*
* NOTE: Consider the case where `root == proof[0] && leaves.length == 0` as it will return `true`.
* The `leaves` must be validated independently. See {processMultiProofCalldata}.
*/
function multiProofVerifyCalldata(
bytes32[] calldata proof,
bool[] calldata proofFlags,
bytes32 root,
bytes32[] memory leaves,
function(bytes32, bytes32) view returns (bytes32) hasher
) internal view returns (bool) {
return processMultiProofCalldata(proof, proofFlags, leaves, hasher) == root;
}
/**
* @dev Returns the root of a tree reconstructed from `leaves` and sibling nodes in `proof`. The reconstruction
* proceeds by incrementally reconstructing all inner nodes by combining a leaf/inner node with either another
* leaf/inner node or a proof sibling node, depending on whether each `proofFlags` item is true or false
* respectively.
*
* This version handles multiproofs in calldata with a custom hashing function.
*
* CAUTION: Not all Merkle trees admit multiproofs. To use multiproofs, it is sufficient to ensure that: 1) the tree
* is complete (but not necessarily perfect), 2) the leaves to be proven are in the opposite order they are in the
* tree (i.e., as seen from right to left starting at the deepest layer and continuing at the next layer).
*
* NOTE: The _empty set_ (i.e. the case where `proof.length == 1 && leaves.length == 0`) is considered a no-op,
* and therefore a valid multiproof (i.e. it returns `proof[0]`). Consider disallowing this case if you're not
* validating the leaves elsewhere.
*/
function processMultiProofCalldata(
bytes32[] calldata proof,
bool[] calldata proofFlags,
bytes32[] memory leaves,
function(bytes32, bytes32) view returns (bytes32) hasher
) internal view returns (bytes32 merkleRoot) {
// This function rebuilds the root hash by traversing the tree up from the leaves. The root is rebuilt by
// consuming and producing values on a queue. The queue starts with the `leaves` array, then goes onto the
// `hashes` array. At the end of the process, the last hash in the `hashes` array should contain the root of
// the Merkle tree.
uint256 leavesLen = leaves.length;
uint256 proofFlagsLen = proofFlags.length;
// Check proof validity.
if (leavesLen + proof.length != proofFlagsLen + 1) {
revert MerkleProofInvalidMultiproof();
}
// The xxxPos values are "pointers" to the next value to consume in each array. All accesses are done using
// `xxx[xxxPos++]`, which return the current value and increment the pointer, thus mimicking a queue's "pop".
bytes32[] memory hashes = new bytes32[](proofFlagsLen);
uint256 leafPos = 0;
uint256 hashPos = 0;
uint256 proofPos = 0;
// At each step, we compute the next hash using two values:
// - a value from the "main queue". If not all leaves have been consumed, we get the next leaf, otherwise we
// get the next hash.
// - depending on the flag, either another value from the "main queue" (merging branches) or an element from the
// `proof` array.
for (uint256 i = 0; i < proofFlagsLen; i++) {
bytes32 a = leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++];
bytes32 b = proofFlags[i]
? (leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++])
: proof[proofPos++];
hashes[i] = hasher(a, b);
}
if (proofFlagsLen > 0) {
if (proofPos != proof.length) {
revert MerkleProofInvalidMultiproof();
}
unchecked {
return hashes[proofFlagsLen - 1];
}
} else if (leavesLen > 0) {
return leaves[0];
} else {
return proof[0];
}
}
}
Compiler settingsShow
{
"viaIR": true,
"libraries": {},
"optimizer": {
"runs": 200,
"enabled": true
},
"evmVersion": "cancun"
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.