Delete duplicate code in generateMerkleProof function - add computeDepositEventsTree call instead of a separate mapping in loop

This commit is contained in:
Theo 2023-05-09 19:21:49 +03:00
parent b7929bd16c
commit e9fcc51dae

19
cli.js
View File

@ -373,25 +373,12 @@ async function deposit({ currency, amount, commitmentNote }) {
* @param deposit Deposit object
*/
async function generateMerkleProof(deposit, currency, amount) {
let leafIndex = -1;
// Get all deposit events from smart contract and assemble merkle tree from them
const cachedEvents = await fetchEvents({ type: 'deposit', currency, amount });
const { tree, leaves, root } = computeDepositEventsTree(cachedEvents);
const leaves = cachedEvents
.sort((a, b) => a.leafIndex - b.leafIndex) // Sort events in chronological order
.map((e) => {
const index = toBN(e.leafIndex).toNumber();
if (toBN(e.commitment).eq(toBN(deposit.commitmentHex))) {
leafIndex = index;
}
return toBN(e.commitment).toString(10);
});
const tree = new merkleTree(MERKLE_TREE_HEIGHT, leaves);
// Validate that our data is correct
const root = tree.root();
// Validate that merkle tree is valid, deposit data is correct and note not spent.
const leafIndex = leaves.findIndex((commitment) => toBN(deposit.commitmentHex).toString(10) === commitment);
let isValidRoot, isSpent;
if (!isTestRPC && !multiCall) {
const callContract = await useMultiCall([