Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changelog/tempo-max-spend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
wallet-cli: patch
---

Honor `TEMPO_MAX_SPEND` when `tempo request --max-spend` is not set.
8 changes: 8 additions & 0 deletions src/commands/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,15 @@ export function parseRequestArgs(argv: readonly string[]): RequestOptions {
return { ...options, url };
}

export function applyMaxSpendEnv(options: RequestOptions): RequestOptions {
if (options.maxSpend !== undefined) return options;
const fromEnv = process.env.TEMPO_MAX_SPEND?.trim();
if (!fromEnv) return options;
return { ...options, maxSpend: fromEnv };
}

export async function executeRequest(options: RequestOptions, io: RequestRunOptions = {}) {
options = applyMaxSpendEnv(options);
const stdout = io.stdout ?? process.stdout;
const started = Date.now();
const request = await buildFetchRequest(options);
Expand Down
7 changes: 5 additions & 2 deletions src/request-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ const args = z.object({

const options = z.object({
"dry-run": z.boolean().optional().describe("Show payment challenge without paying"),
"max-spend": z.string().optional().describe("Hard cap for cumulative payment spend"),
"max-spend": z
.string()
.optional()
.describe("Hard cap for cumulative payment spend (or TEMPO_MAX_SPEND)"),
"private-key": z.string().optional().describe("Sign payments with an ephemeral private key"),
network: z
.string()
Expand Down Expand Up @@ -216,7 +219,7 @@ function describeRequestCli() {
name: "max_spend",
long: "--max-spend",
value_name: "AMOUNT",
help: "Hard cap for cumulative payment spend",
help: "Hard cap for cumulative payment spend (or TEMPO_MAX_SPEND)",
},
{
name: "private_key",
Expand Down
30 changes: 30 additions & 0 deletions test/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Abis as TempoAbis, Channel as TempoChannel, KeyAuthorizationManager } f
import { afterEach, describe, expect, it } from "vitest";

import {
applyMaxSpendEnv,
buildTopUpTransactionRequest,
isSessionInvalidationResponse,
parseRequestArgs,
Expand Down Expand Up @@ -354,6 +355,35 @@ describe("request command", () => {
});
});

it("applies TEMPO_MAX_SPEND when --max-spend is omitted", () => {
const previous = process.env.TEMPO_MAX_SPEND;
process.env.TEMPO_MAX_SPEND = " 2.50 ";
try {
expect(applyMaxSpendEnv(parseRequestArgs(["https://example.com"]))).toMatchObject({
maxSpend: "2.50",
url: "https://example.com",
});
} finally {
if (previous === undefined) delete process.env.TEMPO_MAX_SPEND;
else process.env.TEMPO_MAX_SPEND = previous;
}
});

it("prefers --max-spend over TEMPO_MAX_SPEND", () => {
const previous = process.env.TEMPO_MAX_SPEND;
process.env.TEMPO_MAX_SPEND = "9.00";
try {
expect(
applyMaxSpendEnv(parseRequestArgs(["--max-spend", "1.00", "https://example.com"])),
).toMatchObject({
maxSpend: "1.00",
});
} finally {
if (previous === undefined) delete process.env.TEMPO_MAX_SPEND;
else process.env.TEMPO_MAX_SPEND = previous;
}
});

it("recovers stale session locks left behind by killed request processes", async () => {
const home = await useTempHome();
const lockDir = join(home, ".tempo", "wallet", "session-locks");
Expand Down