From 70bbc4f32d193d7e68f7d453a09cda2643155da4 Mon Sep 17 00:00:00 2001 From: Gabriel Montes Date: Fri, 12 Apr 2024 11:26:54 -0300 Subject: [PATCH] Add docs on testing Merkle claims and improve the libs --- README.md | 33 +++++++++ packages/erc-20-lib/src/index.js | 2 + .../scripts/create-claim-group.js | 71 +++++++++++-------- .../merkle-box-lib/scripts/create-dataset.js | 4 +- packages/merkle-box-lib/src/index.js | 3 + 5 files changed, 81 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index de0680e..5bdc02e 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,39 @@ Then open the browser at http://localhost:3000. 1. Add it to the `Utilities` component so it appears in the home page. 1. Go from there! +## Testing + +### Merkle claims + +Go to `packages/merkle-box-lib` and create a list of recipients as a JSON file: + +```json +[ + { + "account": "0x0000000000000000000000000000000000000010", + "amount": "1000000000000000000" + }, + { + "account": "0x0000000000000000000000000000000000000020", + "amount": "2000000000000000000" + } +] +``` + +Create the dataset: + +```sh +node scripts/create-dataset.js recipients.json > dataset.json +``` + +And copy the data set file to the development web server at `site/public`. + +Set `NODE_URL`, `MNEMONIC` and create the claim group: + +```sh +node scripts/create-claim-group.js WETH http://localhost:3000/test20241231.json 2024-12-31 +``` + ## End-to-end tests Set the following environment variables: `BASE_NODE_URL`, `MNEMONIC`. diff --git a/packages/erc-20-lib/src/index.js b/packages/erc-20-lib/src/index.js index 88c47ed..8bb1d7b 100644 --- a/packages/erc-20-lib/src/index.js +++ b/packages/erc-20-lib/src/index.js @@ -37,6 +37,8 @@ const createErc20 = function (web3, address, options = {}) { const totalSupply = () => contract.methods.totalSupply().call() return { + getAddress: () => contract.options.address, + getInfo: () => Promise.all([ contract.methods.symbol().call(), diff --git a/packages/merkle-box-lib/scripts/create-claim-group.js b/packages/merkle-box-lib/scripts/create-claim-group.js index a211a26..fe64d79 100755 --- a/packages/merkle-box-lib/scripts/create-claim-group.js +++ b/packages/merkle-box-lib/scripts/create-claim-group.js @@ -19,23 +19,33 @@ const Web3 = require('web3') const createMerkleBox = require('..') -const [token, datasetUrl, unlock] = process.argv.slice(2) - -const provider = new HDWalletProvider({ - addressIndex: Number.parseInt(process.env.ACCOUNT || '0'), - mnemonic: process.env.MNEMONIC || '', - numberOfAddresses: 1, - providerOrUrl: process.env.NODE_URL -}) -const from = provider.getAddress(0) -const web3 = new Web3(provider) +const { ACCOUNT = '0', MNEMONIC = '', NODE_URL, PRIVATE_KEYS } = process.env -const memo = `datasetUri=${datasetUrl}` +const [token, datasetUrl, unlock] = process.argv.slice(2) -const toTimestamp = str => - /^[0-9]+$/.test(str) - ? Number.parseInt(str) - : Math.round(new Date(str).getTime() / 1000) +const getContractObjects = () => + // @ts-ignore ts(2351) + new Web3(NODE_URL).eth.getChainId().then(function (chainId) { + const provider = new HDWalletProvider({ + addressIndex: Number.parseInt(ACCOUNT), + chainId, + numberOfAddresses: 1, + providerOrUrl: NODE_URL, + ...(PRIVATE_KEYS + ? { privateKeys: PRIVATE_KEYS.split(',') } + : { mnemonic: MNEMONIC }) + }) + const from = provider.getAddress(0) + // @ts-ignore ts(2351) + const web3 = new Web3(provider) + const tokenAddress = token.startsWith('0x') + ? token + : createErc20.util.tokenAddress(token, chainId) + const erc20 = createErc20(web3, tokenAddress, { from }) + const merkleBoxAddress = createMerkleBox.addresses[chainId] + const merkleBox = createMerkleBox(web3, merkleBoxAddress, { from }) + return { erc20, merkleBox, provider } + }) const parseDataSet = () => fetch(datasetUrl) @@ -53,30 +63,29 @@ const parseDataSet = () => } }) -Promise.all([web3.eth.getChainId(), parseDataSet()]) - .then(function ([chainId, { root, total }]) { - const tokenAddress = token.startsWith('0x') - ? token - : createErc20.util.tokenAddress(token, chainId) - const erc20 = createErc20(web3, tokenAddress, { from }) - const merkleBoxAddress = createMerkleBox.addresses[chainId] - const merkleBox = createMerkleBox(web3, merkleBoxAddress, { from }) - return erc20 - .approve(merkleBoxAddress, total) +const toTimestamp = str => + /^[0-9]+$/.test(str) + ? Number.parseInt(str) + : Math.round(new Date(str).getTime() / 1000) + +Promise.all([getContractObjects(), parseDataSet()]) + .then(([{ erc20, merkleBox, provider }, { root, total }]) => + erc20 + .approve(merkleBox.getAddress(), total) .then(() => merkleBox.newClaimsGroup( - tokenAddress, + erc20.getAddress(), total, root, toTimestamp(unlock), - memo + `datasetUri=${datasetUrl}` ) ) - }) + .finally(function () { + provider.engine.stop() + }) + ) .then(function (receipt) { console.log(receipt.events.NewMerkle.returnValues) }) .catch(console.error) - .finally(function () { - provider.engine.stop() - }) diff --git a/packages/merkle-box-lib/scripts/create-dataset.js b/packages/merkle-box-lib/scripts/create-dataset.js index 32468b0..a0f96ff 100755 --- a/packages/merkle-box-lib/scripts/create-dataset.js +++ b/packages/merkle-box-lib/scripts/create-dataset.js @@ -1,9 +1,11 @@ /** * Usage: * - * node scripts/create-dataset.js ./recipients.json > dataset.json + * node scripts/create-dataset.js recipients.json > dataset.json */ +/* eslint-disable no-console */ + 'use strict' const createMerkleBox = require('..') diff --git a/packages/merkle-box-lib/src/index.js b/packages/merkle-box-lib/src/index.js index 1e31b33..56b2e39 100644 --- a/packages/merkle-box-lib/src/index.js +++ b/packages/merkle-box-lib/src/index.js @@ -39,8 +39,11 @@ const createMerkleBox = function (web3, address, options = {}) { { from, ...txOps } ) + const getAddress = () => merkleBox.options.address + return { claim, + getAddress, getHolding, isClaimable, newClaimsGroup