Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
LinkCreatedResponse,
RateLimited,
TokenCreatedResponse,
TransactionsResponse,
ValidationErrorResponse,
} from "./responses/index.js";

Expand Down Expand Up @@ -51,6 +52,16 @@ class NovaApiClient {
return Ok(validatedResponse);
}

async getTransactions(address: string) {
const endpoint = AccountsEndpoints[this.network];
const response = await this.http
.get(`${endpoint}/transactions`, { searchParams: { address } })
.json();
const validatedResponse = TransactionsResponse(response);
if (validatedResponse instanceof type.errors) return parseError(response);
return Ok(validatedResponse);
}
Comment thread
SynthLuvr marked this conversation as resolved.

async login(email: string, publicKey: string) {
const endpoint = AuthEndpoints[this.network];
const response = await this.http.post(`${endpoint}/login`, {
Expand Down
37 changes: 37 additions & 0 deletions commands/transactions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Ok } from "ts-handling";
import { api } from "../api.js";
import program, { logExit, printOk } from "../cli.js";
import { getAddressFromTokenOrKey } from "./address.js";

const getTransactions = async (address?: string) => {
const resolvedAddress = address
? Ok(address)
: await getAddressFromTokenOrKey();
if (!resolvedAddress.ok) return resolvedAddress;

const response = await api.getTransactions(resolvedAddress.data);
if (!response.ok) return response;
return Ok(response.data.contents.transactions);
};

program
.command("transactions")
.description("Shows transactions for an account")
.option("-j, --json", "Output results as JSON")
.option("-t, --toon", "Output results as TOON")
.argument("[address]", "The account address to get transactions for")
.action(async (address: string | undefined) => {
const transactions = await getTransactions(address);
if (!transactions.ok) return logExit(transactions.error);

const humanReadable =
transactions.data.length === 0
? "No transactions"
: transactions.data
.map((tx) => `${tx.type} ${tx.amount} (${tx.txId})`)
.join("\n");
Comment thread
SynthLuvr marked this conversation as resolved.

printOk({ transactions: transactions.data }, humanReadable);
});

export { getTransactions };
1 change: 1 addition & 0 deletions entrypoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import "./commands/import-key.js";
import "./commands/login.js";
import "./commands/send.js";
import "./commands/token.js";
import "./commands/transactions.js";
import "./commands/withdraw.js";

try {
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
"bin": {
"nova": "bin/nova.mjs"
},
"files": [
"dist"
],
"files": ["dist"],
"scripts": {
"prebuild": "pnpm del -rf dist",
"build": "pnpm tsc",
Expand Down
1 change: 1 addition & 0 deletions responses/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export * from "./generate.js";
export * from "./link-created.js";
export * from "./rate-limited.js";
export * from "./token-created.js";
export * from "./transactions.js";
export * from "./validation-error.js";
17 changes: 17 additions & 0 deletions responses/transactions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { type } from "arktype";

const TransactionsResponse = type({
code: "200",
Comment thread
SynthLuvr marked this conversation as resolved.
contents: {
transactions: type({
amount: "string",
Comment thread
SynthLuvr marked this conversation as resolved.
time: "number",
txId: "string",
type: "'deposit' | 'transfer' | 'withdrawal'",
"from?": "string",
"to?": "string",
}).array(),
},
});

export { TransactionsResponse };
34 changes: 34 additions & 0 deletions tests/transactions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { describe, expect } from "vitest";
import { it } from "./base.js";

describe("nova transactions", () => {
it("prints no transactions for a fresh account", async ({ nova }) => {
const stdout = await nova(["transactions"]);
expect(stdout).toBe("No transactions");
});

it("prints no transactions for a fresh account in JSON format", async ({
nova,
}) => {
const stdout = await nova(["-j", "transactions"]);
const result = JSON.parse(stdout);
expect(result.status).toBe("ok");
expect(result.result.transactions).toEqual([]);
});

it("prints no transactions for a given address", async ({ nova }) => {
const address = await nova(["address"]);
const stdout = await nova(["transactions", address]);
expect(stdout).toBe("No transactions");
});

it("prints no transactions for a given address in JSON format", async ({
nova,
}) => {
const address = await nova(["address"]);
const stdout = await nova(["-j", "transactions", address]);
const result = JSON.parse(stdout);
expect(result.status).toBe("ok");
expect(result.result.transactions).toEqual([]);
});
});