Skip to content

Commit

Permalink
Move fetchNotes to utils (#4858)
Browse files Browse the repository at this point in the history
This function will be reused by wallet send and wallet notes combine commands.
  • Loading branch information
patnir authored Mar 21, 2024
1 parent ed5a157 commit fee5eeb
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 51 deletions.
55 changes: 4 additions & 51 deletions ironfish-cli/src/commands/wallet/notes/combine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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')

Expand Down Expand Up @@ -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<number> {
const spendsPerMinute = Math.max(Math.floor(60000 / spendPostTimeMs), 2) // minimum of 2 notes per minute in case the spentPostTime is very high

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.`)
Expand Down
52 changes: 52 additions & 0 deletions ironfish-cli/src/utils/note.ts
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit fee5eeb

Please sign in to comment.