Skip to content

Commit 3c34d1e

Browse files
feat(evm): simple merkle tree script
1 parent 1367c24 commit 3c34d1e

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

evm/scripts/Deploy.s.sol

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import "forge-std/Script.sol";
77
import "solady/utils/CREATE3.sol";
88
import "solady/utils/LibString.sol";
99
import "solady/utils/LibBytes.sol";
10+
import "solady/utils/LibSort.sol";
11+
import "solady/utils/MerkleTreeLib.sol";
12+
import "solady/utils/EfficientHashLib.sol";
13+
1014
import "@openzeppelin-foundry-upgradeable/Upgrades.sol";
1115
import "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
1216
import "@openzeppelin/contracts/proxy/ERC1967/ERC1967Utils.sol";
@@ -2327,3 +2331,55 @@ contract MintedAddress is VersionedScript {
23272331
console.log(mintedAddress);
23282332
}
23292333
}
2334+
2335+
struct AirdropEntry {
2336+
address beneficiary;
2337+
uint256 amount;
2338+
}
2339+
2340+
contract SimpleMerkleTree is Script {
2341+
mapping(bytes32 => AirdropEntry) preimages;
2342+
2343+
function pushEntry(
2344+
address beneficiary,
2345+
uint256 amount
2346+
) internal returns (bytes32) {
2347+
bytes32 image =
2348+
EfficientHashLib.hash(abi.encodePacked(beneficiary, amount));
2349+
preimages[image] =
2350+
AirdropEntry({beneficiary: beneficiary, amount: amount});
2351+
return image;
2352+
}
2353+
2354+
function run() public {
2355+
bytes32[] memory leaves = new bytes32[](8);
2356+
leaves[0] = pushEntry(address(1), 1);
2357+
leaves[1] = pushEntry(address(2), 2);
2358+
leaves[2] =
2359+
pushEntry(address(0x50A22f95bcB21E7bFb63c7A8544AC0683dCeA302), 3);
2360+
leaves[3] = pushEntry(address(4), 4);
2361+
leaves[4] = pushEntry(address(5), 5);
2362+
leaves[5] =
2363+
pushEntry(address(0x2FB055fC77D751e2E6B7c88A1B404505154521c3), 6);
2364+
leaves[6] = pushEntry(address(7), 7);
2365+
leaves[7] = pushEntry(address(8), 8);
2366+
LibSort.sort(leaves);
2367+
2368+
bytes32[] memory tree = MerkleTreeLib.build(leaves);
2369+
bytes32 root = MerkleTreeLib.root(tree);
2370+
console.log("Root: ");
2371+
console.logBytes32(root);
2372+
for (uint256 i = 0; i < leaves.length; i++) {
2373+
bytes32[] memory proof = MerkleTreeLib.leafProof(tree, i);
2374+
console.log("==================================");
2375+
console.log("index: ");
2376+
console.log(i);
2377+
console.log("beneficiary: ");
2378+
console.log(preimages[leaves[i]].beneficiary);
2379+
console.log("amount: ");
2380+
console.log(preimages[leaves[i]].amount);
2381+
console.log("proof: ");
2382+
console.logBytes(abi.encode(proof));
2383+
}
2384+
}
2385+
}

0 commit comments

Comments
 (0)