From: Chris Duncan Date: Wed, 20 Aug 2025 18:53:44 +0000 (-0700) Subject: Fix block issues with sweeper. X-Git-Tag: v0.10.5~41^2~37 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=78d83a41e90dbdc14362d86290cee0c8386bd218;p=libnemo.git Fix block issues with sweeper. --- diff --git a/src/lib/tools.ts b/src/lib/tools.ts index bf8495c..3bb2710 100644 --- a/src/lib/tools.ts +++ b/src/lib/tools.ts @@ -119,8 +119,8 @@ export async function sign (key: Key, ...input: string[]): Promise { * @param {Rpc|string|URL} rpc - RPC node information required to refresh accounts, calculate PoW, and process blocks * @param {(Wallet)} wallet - Wallet from which to sweep funds * @param {string} recipient - Destination address for all swept funds -* @param {number} from - Starting account index to sweep -* @param {number} to - Ending account index to sweep +* @param {number} [from=0] - Starting account index to sweep +* @param {number} [to=from] - Ending account index to sweep * @returns An array of results including both successes and failures */ export async function sweep ( @@ -139,29 +139,32 @@ export async function sweep ( if (!(rpc instanceof Rpc)) { throw new TypeError('RPC must be a valid node') } + const blockQueue: Promise[] = [] const results: SweepResult[] = [] - const recipientAccount = Account.load(recipient) const accounts = await wallet.refresh(rpc, from, to) + for (const account of accounts) { - if (account.representative?.address && account.frontier_block && account.index != null) { - const block = await new Block(account) - .send(recipientAccount, account.balance ?? 0n) - .sign(wallet, account.index) - const blockRequest: Promise = new Promise(async (resolve) => { - try { - await block.pow() - const hash = await block.process(rpc) - results.push({ status: 'success', address: block.account.address, message: hash }) - } catch (err: any) { - results.push({ status: 'error', address: block.account.address, message: err.message }) - } finally { - resolve() + const blockRequest: Promise = new Promise(async (resolve) => { + let block + try { + if (account.index == null) { + throw new TypeError('Account index is required', { cause: account }) } - }) - blockQueue.push(blockRequest) - } + block = await new Block(account) + .send(recipientAccount, account.balance ?? 0n) + .sign(wallet, account.index) + await block.pow() + const hash = await block.process(rpc) + results.push({ status: 'success', address: block.account.address, message: hash }) + } catch (err: any) { + results.push({ status: 'error', address: account.address, message: err.message }) + } finally { + resolve() + } + }) + blockQueue.push(blockRequest) } await Promise.allSettled(blockQueue) return results