From 8bcbef45dd6d8edc1173b93ff2abac88ec07db58 Mon Sep 17 00:00:00 2001 From: Anyhowclick Date: Mon, 23 Aug 2021 16:05:45 +0800 Subject: [PATCH 1/2] Modify merkle generation example, with README --- scripts/README.md | 10 ++++++++++ scripts/example.json | 10 +++++----- 2 files changed, 15 insertions(+), 5 deletions(-) create mode 100644 scripts/README.md diff --git a/scripts/README.md b/scripts/README.md new file mode 100644 index 00000000..c3811cff --- /dev/null +++ b/scripts/README.md @@ -0,0 +1,10 @@ +# Generate merkle proof +From the root directory, run +```shell +$ node scripts/generateMerkleRoot.js -f example.json +``` + +# Example.json +- The tokens can be of different lengths for each user. +- Consistency need not be maintained for each cycle. For instance, the token list can be of a different order. +- The amounts in `cumulativeAmounts` can be either strings, or hexadecimal string equivalents of their numerical values. Refer to the example. diff --git a/scripts/example.json b/scripts/example.json index de4e033f..ae7e01da 100644 --- a/scripts/example.json +++ b/scripts/example.json @@ -2,16 +2,16 @@ "cycle": 1, "userRewards": { "0xF3c6F5F265F503f53EAD8aae90FC257A5aa49AC1": { - "tokens": ["0xdd974d5c2e2928dea5f71b9825b8b646686bd200","0x5228a22e72ccc52d415ecfd199f99d0665e7733b"], - "cumulativeAmounts": [10000, 500] + "tokens": ["0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"], + "cumulativeAmounts": ["1234567800"] }, "0xecabe455bd6cd440bba804c63da360d85cb16b75": { "tokens": ["0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48","0x57ab1ec28d129707052df4df418d58a2d46d5f51"], - "cumulativeAmounts": [5000, 1300] + "cumulativeAmounts": ["0x2710", "0x3800"] }, "0x529676cc3ffe94fd594d3bc0f4d2b8d89ed69a91": { - "tokens": ["0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48","0x57ab1ec28d129707052df4df418d58a2d46d5f51"], - "cumulativeAmounts": [500, 400] + "tokens": ["0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE","0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"], + "cumulativeAmounts": ["1234567800", "0x123"] } } } From eb68e1c520195cc60b00aa30c155a7703965f7b3 Mon Sep 17 00:00:00 2001 From: Anyhowclick Date: Mon, 23 Aug 2021 20:35:00 +0800 Subject: [PATCH 2/2] Update script, example.json and add readme --- scripts/README.md | 2 ++ scripts/example.json | 2 +- scripts/generateMerkleRoot.js | 6 ++++-- scripts/merkleDist/parseRewards.js | 4 ++-- 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/scripts/README.md b/scripts/README.md index c3811cff..3035b116 100644 --- a/scripts/README.md +++ b/scripts/README.md @@ -4,6 +4,8 @@ From the root directory, run $ node scripts/generateMerkleRoot.js -f example.json ``` +The script will export the result into `merkle_data_$CYCLE.json`, where `$CYCLE` is the cycle number. + # Example.json - The tokens can be of different lengths for each user. - Consistency need not be maintained for each cycle. For instance, the token list can be of a different order. diff --git a/scripts/example.json b/scripts/example.json index ae7e01da..3b650472 100644 --- a/scripts/example.json +++ b/scripts/example.json @@ -1,5 +1,5 @@ { - "cycle": 1, + "cycle": 3, "userRewards": { "0xF3c6F5F265F503f53EAD8aae90FC257A5aa49AC1": { "tokens": ["0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"], diff --git a/scripts/generateMerkleRoot.js b/scripts/generateMerkleRoot.js index 449cdcc7..28d89f22 100644 --- a/scripts/generateMerkleRoot.js +++ b/scripts/generateMerkleRoot.js @@ -4,6 +4,8 @@ const argv = require('yargs/yargs')(process.argv.slice(2)).argv; const parseRewards = require('./merkleDist/parseRewards').parseRewards; const configPath = argv.f; -const json = JSON.parse(fs.readFileSync(path.join(__dirname, configPath), { encoding: 'utf8' })) +let json = JSON.parse(fs.readFileSync(path.join(__dirname, configPath), { encoding: 'utf8' })); +let cycle = json['cycle']; if (typeof json !== 'object') throw new Error('Invalid JSON'); -console.log(JSON.stringify(parseRewards(json))); +json = JSON.stringify(parseRewards(json), null, 2); +fs.writeFileSync(path.join(__dirname, `merkle_data_${cycle}.json`), json); diff --git a/scripts/merkleDist/parseRewards.js b/scripts/merkleDist/parseRewards.js index 3a480b7b..4c5134e8 100644 --- a/scripts/merkleDist/parseRewards.js +++ b/scripts/merkleDist/parseRewards.js @@ -17,8 +17,8 @@ module.exports.parseRewards = function (rewardInfo) { const leaves = hashElements(treeElements, cycle); const tree = new MerkleTree(leaves, keccak256, {sort: true}); const userRewardsWithProof = treeElements.reduce((memo, {account}, index) => { - tokens = mappedTokensAmounts[account].tokens; - cumulativeAmounts = mappedTokensAmounts[account].cumulativeAmounts.map((amt) => amt.toHexString()); + let tokens = mappedTokensAmounts[account].tokens; + let cumulativeAmounts = mappedTokensAmounts[account].cumulativeAmounts.map((amt) => amt.toHexString()); memo[account] = { index,