From fee5eeb235f0bc5943a58a548d5c42c4f27f702a Mon Sep 17 00:00:00 2001 From: Rahul Patni Date: Thu, 21 Mar 2024 10:36:42 -0700 Subject: [PATCH] Move fetchNotes to utils (#4858) This function will be reused by wallet send and wallet notes combine commands. --- .../src/commands/wallet/notes/combine.ts | 55 ++----------------- ironfish-cli/src/utils/note.ts | 52 ++++++++++++++++++ 2 files changed, 56 insertions(+), 51 deletions(-) create mode 100644 ironfish-cli/src/utils/note.ts diff --git a/ironfish-cli/src/commands/wallet/notes/combine.ts b/ironfish-cli/src/commands/wallet/notes/combine.ts index cd20a034be..32cb6738c2 100644 --- a/ironfish-cli/src/commands/wallet/notes/combine.ts +++ b/ironfish-cli/src/commands/wallet/notes/combine.ts @@ -3,7 +3,6 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ import { Asset } from '@ironfish/rust-nodejs' import { - Assert, BenchUtils, CreateTransactionRequest, CurrencyUtils, @@ -21,6 +20,7 @@ import { IronfishCommand } from '../../../command' import { IronFlag, RemoteFlags } from '../../../flags' import { getExplorer } from '../../../utils/explorer' import { selectFee } from '../../../utils/fees' +import { fetchNotes } from '../../../utils/note' import { displayTransactionSummary, TransactionTimer, @@ -106,7 +106,7 @@ export class CombineNotesCommand extends IronfishCommand { }) ).content.publicKey - const notes = await this.fetchNotes(client, account, 10) + const notes = await fetchNotes(client, account, 10) CliUx.ux.action.start('Measuring time to combine 1 note') @@ -189,36 +189,6 @@ export class CombineNotesCommand extends IronfishCommand { return BenchUtils.end(start) } - private async fetchNotes(client: RpcClient, account: string, notesToCombine: number) { - const noteSize = await this.getNoteTreeSize(client) - - const getNotesResponse = await client.wallet.getNotes({ - account, - pageSize: notesToCombine, - filter: { - assetId: Asset.nativeId().toString('hex'), - spent: false, - }, - }) - - // filtering notes by noteSize and sorting them by value in ascending order - const notes = getNotesResponse.content.notes - .filter((note) => { - if (!note.index) { - return false - } - return note.index < noteSize - }) - .sort((a, b) => { - if (a.value < b.value) { - return -1 - } - return 1 - }) - - return notes - } - private async selectNotesToCombine(spendPostTimeMs: number): Promise { const spendsPerMinute = Math.max(Math.floor(60000 / spendPostTimeMs), 2) // minimum of 2 notes per minute in case the spentPostTime is very high @@ -342,23 +312,6 @@ export class CombineNotesCommand extends IronfishCommand { return expiration } - private async getNoteTreeSize(client: RpcClient) { - const getCurrentBlock = await client.chain.getChainInfo() - - const currentBlockSequence = parseInt(getCurrentBlock.content.currentBlockIdentifier.index) - - const getBlockResponse = await client.chain.getBlock({ - sequence: currentBlockSequence, - }) - - Assert.isNotNull(getBlockResponse.content.block.noteSize) - - const config = await client.config.getConfig() - - // Adding a buffer to avoid a mismatch between confirmations used to load notes and confirmations used when creating witnesses to spend them - return getBlockResponse.content.block.noteSize - (config.content.confirmations || 2) - } - private async getCurrentBlockSequence(client: RpcClient) { const getCurrentBlock = await client.chain.getChainInfo() const currentBlockSequence = parseInt(getCurrentBlock.content.currentBlockIdentifier.index) @@ -404,7 +357,7 @@ export class CombineNotesCommand extends IronfishCommand { numberOfNotes = await this.selectNotesToCombine(spendPostTime) } - let notes = await this.fetchNotes(client, from, numberOfNotes) + let notes = await fetchNotes(client, from, numberOfNotes) // If the user doesn't have enough notes for their selection, we reduce the number of notes so that // the largest note can be used for fees. @@ -537,7 +490,7 @@ export class CombineNotesCommand extends IronfishCommand { } private async ensureUserHasEnoughNotesToCombine(client: RpcClient, from: string) { - const notes = await this.fetchNotes(client, from, 10) + const notes = await fetchNotes(client, from, 10) if (notes.length < 3) { this.log(`Your notes are already combined. You currently have ${notes.length} notes.`) diff --git a/ironfish-cli/src/utils/note.ts b/ironfish-cli/src/utils/note.ts new file mode 100644 index 0000000000..969e8f2139 --- /dev/null +++ b/ironfish-cli/src/utils/note.ts @@ -0,0 +1,52 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ +import { Asset } from '@ironfish/rust-nodejs' +import { Assert, RpcClient } from '@ironfish/sdk' + +export async function fetchNotes(client: RpcClient, account: string, notesToCombine: number) { + const noteSize = await getNoteTreeSize(client) + + const getNotesResponse = await client.wallet.getNotes({ + account, + pageSize: notesToCombine, + filter: { + assetId: Asset.nativeId().toString('hex'), + spent: false, + }, + }) + + // filtering notes by noteSize and sorting them by value in ascending order + const notes = getNotesResponse.content.notes + .filter((note) => { + if (!note.index) { + return false + } + return note.index < noteSize + }) + .sort((a, b) => { + if (a.value < b.value) { + return -1 + } + return 1 + }) + + return notes +} + +async function getNoteTreeSize(client: RpcClient) { + const getCurrentBlock = await client.chain.getChainInfo() + + const currentBlockSequence = parseInt(getCurrentBlock.content.currentBlockIdentifier.index) + + const getBlockResponse = await client.chain.getBlock({ + sequence: currentBlockSequence, + }) + + Assert.isNotNull(getBlockResponse.content.block.noteSize) + + const config = await client.config.getConfig() + + // Adding a buffer to avoid a mismatch between confirmations used to load notes and confirmations used when creating witnesses to spend them + return getBlockResponse.content.block.noteSize - (config.content.confirmations || 2) +}