From 96a18d3c5ca7efe9c583e7435c4badcf2de98c9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Mon, 24 Oct 2022 12:41:40 +0200 Subject: [PATCH] Bundle import txs into blocks, wait for confirmations --- scripts/import/contributions.js | 45 +++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/scripts/import/contributions.js b/scripts/import/contributions.js index e0a8466..4cd26c1 100644 --- a/scripts/import/contributions.js +++ b/scripts/import/contributions.js @@ -1,6 +1,8 @@ const fs = require('fs'); const Kredits = require('../../lib/kredits'); +const PARALLEL_TXS = 5; + async function main() { kredits = new Kredits(hre.ethers.provider, hre.ethers.provider.getSigner()) await kredits.init(); @@ -18,19 +20,40 @@ async function main() { const confirmationPeriod = 40320 // blocks const unconfirmedHeight = currentBlockHeight + confirmationPeriod; - for (const contributionId of ids) { - const c = contributions[contributionId.toString()]; + const txBundlesAmount = Math.ceil(ids.length / PARALLEL_TXS); + let txBundles = []; - const confirmedAtBlock = c.confirmed ? currentBlockHeight : unconfirmedHeight; + for (let i = 0; i < txBundlesAmount; i++) { + txBundles.push(ids.slice((i * PARALLEL_TXS), (i * PARALLEL_TXS) + PARALLEL_TXS)); + } - const result = await kredits.Contribution.contract.add( - c.amount, c.contributorId, - c.hashDigest, c.hashFunction, c.hashSize, - confirmedAtBlock, c.vetoed - ); - // await result.wait(); - console.log(`Added contribution #${contributionId}: ${result.hash}`); - }; + for (const txBundle of txBundles) { + console.log(`Adding contributions #${txBundle[0]} to #${txBundle[txBundle.length - 1]}`) + + let resultPromises = []; + + for (const contributionId of txBundle) { + const c = contributions[contributionId.toString()]; + + const confirmedAtBlock = c.confirmed ? currentBlockHeight : unconfirmedHeight; + + const result = await kredits.Contribution.contract.add( + c.amount, c.contributorId, + c.hashDigest, c.hashFunction, c.hashSize, + confirmedAtBlock, c.vetoed + ); + + resultPromises.push(result.wait()); + + console.log(`Added contribution #${contributionId}: ${result.hash}`); + } + + console.log(`Waiting for confirmations...`); + + await Promise.all(resultPromises); + + console.log('Transactions confirmed'); + } } catch(e) { console.log(e); }