From 98bab330d9982e5801c16b4f3b512ef545eb6bd5 Mon Sep 17 00:00:00 2001 From: Omar Desogus Date: Wed, 2 Feb 2022 22:25:11 +0100 Subject: [PATCH] docs: update usage section Former-commit-id: 9d3f221b05cf0a8fb82c42d0ef13e5cf947e2c99 [formerly 97c3539fa87452e1a99f127166f12c2310365130] [formerly 2e2c3f49ead1d3d6f2b918abb08cdf03874d1233 [formerly 7375db1d9ec769676ccd17d585d25a400f1bf301]] [formerly c06e1773761101c6beb312692af8c42b61584692 [formerly afab0c65ca473d9d8cebbfdc566059168dbe556a] [formerly cc216fa42fa6bb1bc27597436e8e484dd91260ec [formerly 0585708dd3c8ea89d71f63db319768cdebff1c4d]]] Former-commit-id: 552405449ebc6aa7dc3f538cc2ba79afdd567446 [formerly 0d3e39c29dd4d76a000fde72e0042ce673adab46] [formerly ddea6a906303b7b10036ccf23a6458b7291fcbaa [formerly d2903ebdcdb2d8394ddf022fbed4005709208f0f]] Former-commit-id: 9920ff4e4d6a5daa9c967474e315dab267bb72b3 [formerly 0df57811d0dad13eeea090e6aceb807ad5c497ad] Former-commit-id: ad4b1a2ed4a27816fde141b3d493bc48ec47014a --- .../incremental-merkle-tree.sol/README.md | 68 +++++++++++++++++-- 1 file changed, 63 insertions(+), 5 deletions(-) diff --git a/packages/incremental-merkle-tree.sol/README.md b/packages/incremental-merkle-tree.sol/README.md index fa69328..98903f9 100644 --- a/packages/incremental-merkle-tree.sol/README.md +++ b/packages/incremental-merkle-tree.sol/README.md @@ -2,7 +2,7 @@

Incremental Merkle Trees (Solidity)

-

Incremental Merkle tree smart contracts.

+

Incremental Merkle tree Solidity libraries.

@@ -34,6 +34,13 @@ +## Libraries: + +✔️ [IncrementalBinaryTree](https://github.com/appliedzkp/zk-kit/blob/main/packages/incremental-merkle-tree.sol/contracts/IncrementalBinaryTree.sol)\ +✔️ [IncrementalQuinTree](https://github.com/appliedzkp/zk-kit/blob/main/packages/incremental-merkle-tree.sol/contracts/IncrementalQuinTree.sol) + +> The methods of each library are always the same (i.e `insert`, `remove`, `verify`). + --- ## 🛠 Install @@ -54,13 +61,15 @@ yarn add @zk-kit/incremental-merkle-tree.sol ## 📜 Usage +### Importing and using the library + ```solidity // SPDX-License-Identifier: MIT pragma solidity ^0.8.4; import "@zk-kit/incremental-merkle-tree.sol/contracts/IncrementalBinaryTree.sol"; -contract Test { +contract Example { using IncrementalBinaryTree for IncrementalTreeData; event TreeCreated(bytes32 id, uint8 depth); @@ -70,7 +79,7 @@ contract Test { mapping(bytes32 => IncrementalTreeData) public trees; function createTree(bytes32 _id, uint8 _depth) external { - require(trees[_id].depth == 0, "Test: tree already exists"); + require(trees[_id].depth == 0, "Example: tree already exists"); trees[_id].init(_depth, 0); @@ -78,7 +87,7 @@ contract Test { } function insertLeaf(bytes32 _treeId, uint256 _leaf) external { - require(trees[_treeId].depth != 0, "Test: tree does not exist"); + require(trees[_treeId].depth != 0, "Example: tree does not exist"); trees[_treeId].insert(_leaf); @@ -91,7 +100,7 @@ contract Test { uint256[] memory _proofSiblings, uint8[] memory _proofPathIndices ) external { - require(trees[_treeId].depth != 0, "Test: tree does not exist"); + require(trees[_treeId].depth != 0, "Example: tree does not exist"); trees[_treeId].remove(_leaf, _proofSiblings, _proofPathIndices); @@ -101,6 +110,55 @@ contract Test { ``` +### Creating an Hardhat task to deploy the contract + +```ts +import { poseidon_gencontract as poseidonContract } from "circomlibjs" +import { Contract } from "ethers" +import { task, types } from "hardhat/config" + +task("deploy:example", "Deploy an Example contract") + .addOptionalParam("logs", "Print the logs", true, types.boolean) + .setAction(async ({ logs }, { ethers }): Promise => { + const poseidonT3ABI = poseidonContract.generateABI(2) + const poseidonT3Bytecode = poseidonContract.createCode(2) + + const [signer] = await ethers.getSigners() + + const PoseidonLibT3Factory = new ethers.ContractFactory(poseidonT3ABI, poseidonT3Bytecode, signer) + const poseidonT3Lib = await PoseidonLibT3Factory.deploy() + + await poseidonT3Lib.deployed() + + logs && console.log(`PoseidonT3 library has been deployed to: ${poseidonT3Lib.address}`) + + const IncrementalBinaryTreeLibFactory = await ethers.getContractFactory("IncrementalBinaryTree", { + libraries: { + PoseidonT3: poseidonT3Lib.address + } + }) + const incrementalBinaryTreeLib = await IncrementalBinaryTreeLibFactory.deploy() + + await incrementalBinaryTreeLib.deployed() + + logs && console.log(`IncrementalBinaryTree library has been deployed to: ${incrementalBinaryTreeLib.address}`) + + const ContractFactory = await ethers.getContractFactory("Example", { + libraries: { + IncrementalBinaryTree: incrementalBinaryTreeLib.address + } + }) + + const contract = await ContractFactory.deploy() + + await contract.deployed() + + logs && console.log(`Example contract has been deployed to: ${contract.address}`) + + return contract + }) +``` + ## Contacts ### Developers