New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: new cli commands accounts:notes
and accounts:transactions
#1086
Merged
mat-if
merged 15 commits into
iron-fish:staging
from
wd021:account-transactions-command
Jun 15, 2022
Merged
Changes from 4 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
b8bb56f
new cli command - accounts:transactions
wd021 07f1d7b
fix linting
wd021 90be28f
add assertHasAccount() in getTransactionNotes
wd021 b081ceb
use variable 'account' instead of 'accountName'
wd021 6e3fae9
Merge remote-tracking branch 'upstream/staging' into account-transact…
wd021 001240f
update code to latest version
wd021 3c8b3fc
fix linting
wd021 a1c037f
refactor: convert accounts:transactions -> accounts:notes
wd021 30be84e
refactor: getNotes type change
wd021 7f33281
feat: new accounts:transactions command
wd021 ab81c43
fix: linting on test files
wd021 bbc5d6a
make review changes
wd021 f7ef07a
Merge branch 'staging' into account-transactions-command
wd021 df1e83f
fix test fail
wd021 845b403
fix lint
wd021 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* 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 { expect as expectCli, test } from '@oclif/test' | ||
import { GetTransactionsResponse } from 'ironfish' | ||
|
||
describe('accounts:transactions', () => { | ||
const responseContent: GetTransactionsResponse = { | ||
account: 'default', | ||
notes: [ | ||
{ | ||
isSpender: true, | ||
txHash: '1fa5f38c446e52f8842d8c861507744fc3f354992610e1661e033ef316e2d3d1', | ||
txFee: '1', | ||
isMinerFee: false, | ||
amount: '1', | ||
memo: 'foo', | ||
}, | ||
], | ||
} | ||
|
||
beforeAll(() => { | ||
jest.doMock('ironfish', () => { | ||
const originalModule = jest.requireActual('ironfish') | ||
const client = { | ||
connect: jest.fn(), | ||
getTransactionNotes: jest.fn().mockImplementation(() => ({ | ||
content: responseContent, | ||
})), | ||
} | ||
const module: typeof jest = { | ||
...originalModule, | ||
IronfishSdk: { | ||
init: jest.fn().mockImplementation(() => ({ | ||
connectRpc: jest.fn().mockResolvedValue(client), | ||
client, | ||
})), | ||
}, | ||
} | ||
return module | ||
}) | ||
}) | ||
|
||
afterAll(() => { | ||
jest.dontMock('ironfish') | ||
}) | ||
|
||
describe('fetching the transaction notes for an account', () => { | ||
test | ||
.stdout() | ||
.command(['accounts:transactions', 'default']) | ||
.exit(0) | ||
.it('logs the notes for the given account', (ctx) => { | ||
expectCli(ctx.stdout).include(responseContent.account) | ||
expectCli(ctx.stdout).include(responseContent.notes[0].isSpender ? `✔` : `x`) | ||
expectCli(ctx.stdout).include(responseContent.notes[0].txHash) | ||
expectCli(ctx.stdout).include(responseContent.notes[0].txFee) | ||
expectCli(ctx.stdout).include(responseContent.notes[0].isMinerFee ? `✔` : `x`) | ||
expectCli(ctx.stdout).include(responseContent.notes[0].amount) | ||
expectCli(ctx.stdout).include(responseContent.notes[0].memo) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* 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 { CliUx } from '@oclif/core' | ||
import { IronfishCommand } from '../../command' | ||
import { RemoteFlags } from '../../flags' | ||
|
||
export class TransactionsCommand extends IronfishCommand { | ||
static description = `Display the account transactions` | ||
|
||
static flags = { | ||
...RemoteFlags, | ||
} | ||
|
||
static args = [ | ||
{ | ||
name: 'account', | ||
parse: (input: string): Promise<string> => Promise.resolve(input.trim()), | ||
required: false, | ||
description: 'name of the account to get transactions for', | ||
}, | ||
] | ||
|
||
async start(): Promise<void> { | ||
const { args } = await this.parse(TransactionsCommand) | ||
const account = args.account as string | undefined | ||
|
||
const client = await this.sdk.connectRpc() | ||
|
||
const response = await client.getTransactionNotes({ | ||
account, | ||
}) | ||
|
||
const { account: accountResponse, notes } = response.content | ||
|
||
this.log(`\n ${String(accountResponse)} - Transaction notes\n`) | ||
|
||
CliUx.ux.table(notes, { | ||
isSpender: { | ||
header: 'Spender', | ||
get: (row) => (row.isSpender ? `✔` : `x`), | ||
}, | ||
isMinerFee: { | ||
header: 'Miner Fee', | ||
get: (row) => (row.isMinerFee ? `✔` : `x`), | ||
}, | ||
amount: { | ||
header: 'Amount ($ORE)', | ||
}, | ||
txFee: { | ||
header: 'Tx Fee ($ORE)', | ||
}, | ||
txHash: { | ||
header: 'Tx Hash', | ||
}, | ||
memo: { | ||
header: 'Memo', | ||
}, | ||
}) | ||
|
||
this.log(`\n`) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -596,6 +596,52 @@ export class Accounts { | |
this.scan = null | ||
} | ||
|
||
async getTransactionNotes(account: Account): Promise<{ | ||
notes: { | ||
isSpender: boolean | ||
txHash: string | ||
txFee: string | ||
isMinerFee: boolean | ||
amount: string | ||
memo: string | ||
}[] | ||
}> { | ||
this.assertHasAccount(account) | ||
|
||
const notes = [] | ||
|
||
for (const transactionMapValue of this.transactionMap.values()) { | ||
const transaction = transactionMapValue.transaction | ||
|
||
for (const note of transaction.notes()) { | ||
// Try decrypting the note as the owner | ||
let decryptedNote = note.decryptNoteForOwner(account.incomingViewKey) | ||
let isSpender = false | ||
|
||
if (!decryptedNote) { | ||
// Try decrypting the note as the spender | ||
decryptedNote = note.decryptNoteForSpender(account.outgoingViewKey) | ||
isSpender = true | ||
} | ||
|
||
if (decryptedNote && decryptedNote.value() !== BigInt(0)) { | ||
notes.push({ | ||
isSpender, | ||
txHash: transaction.hash().toString('hex'), | ||
txFee: String(await transaction.fee()), | ||
isMinerFee: await transaction.isMinersFee(), | ||
amount: String(decryptedNote.value()), | ||
memo: decryptedNote.memo().replace(/\x00/g, ''), | ||
}) | ||
|
||
continue | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nope, removed. |
||
} | ||
} | ||
} | ||
|
||
return { notes } | ||
} | ||
|
||
private async getUnspentNotes( | ||
account: Account, | ||
): Promise<ReadonlyArray<{ hash: string; note: Note; index: number | null }>> { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
ironfish/src/rpc/routes/transactions/getTransactionNotes.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* 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 * as yup from 'yup' | ||
import { getAccount } from '../accounts/utils' | ||
import { ApiNamespace, router } from '../router' | ||
|
||
export type GetTransactionsRequest = { account?: string } | ||
|
||
export type GetTransactionsResponse = { | ||
account: string | ||
notes: { | ||
isSpender: boolean | ||
txHash: string | ||
txFee: string | ||
isMinerFee: boolean | ||
amount: string | ||
memo: string | ||
}[] | ||
} | ||
|
||
export const GetTransactionsRequestSchema: yup.ObjectSchema<GetTransactionsRequest> = yup | ||
.object({ | ||
account: yup.string().strip(true), | ||
}) | ||
.defined() | ||
|
||
export const GetTransactionsResponseSchema: yup.ObjectSchema<GetTransactionsResponse> = yup | ||
.object({ | ||
account: yup.string().defined(), | ||
notes: yup | ||
.array( | ||
yup | ||
.object({ | ||
isSpender: yup.boolean().defined(), | ||
txHash: yup.string().defined(), | ||
txFee: yup.string().defined(), | ||
isMinerFee: yup.boolean().defined(), | ||
amount: yup.string().defined(), | ||
memo: yup.string().trim().defined(), | ||
}) | ||
.defined(), | ||
) | ||
.defined(), | ||
}) | ||
.defined() | ||
|
||
router.register<typeof GetTransactionsRequestSchema, GetTransactionsResponse>( | ||
`${ApiNamespace.transaction}/getTransactionNotes`, | ||
GetTransactionsRequestSchema, | ||
async (request, node): Promise<void> => { | ||
const account = getAccount(node, request.data.account) | ||
const { notes } = await node.accounts.getTransactionNotes(account) | ||
request.end({ account: account.displayName, notes }) | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This import didn't work for me, I had to change the path to
../../../../ironfish/src/rpc/routes/transactions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup, this needs updating for the latest version. I just pushed an update.