Skip to content
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: add get_budget to NWCClient #264

Merged
merged 2 commits into from
Oct 22, 2024
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
23 changes: 23 additions & 0 deletions examples/nwc/client/get-budget.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import "../../crypto-polyfill.js";
import "websocket-polyfill"; // required in node.js

import * as readline from "node:readline/promises";
import { stdin as input, stdout as output } from "node:process";

import { nwc } from "../../../dist/index.module.js";

const rl = readline.createInterface({ input, output });

const nwcUrl =
process.env.NWC_URL ||
(await rl.question("Nostr Wallet Connect URL (nostr+walletconnect://...): "));
rl.close();

const client = new nwc.NWCClient({
nostrWalletConnectUrl: nwcUrl,
});
const response = await client.getBudget();

console.info(response);

client.close();
25 changes: 25 additions & 0 deletions src/NWCClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type WithOptionalId = {
type Nip47SingleMethod =
| "get_info"
| "get_balance"
| "get_budget"
| "make_invoice"
| "pay_invoice"
| "pay_keysend"
Expand All @@ -47,6 +48,16 @@ export type Nip47GetInfoResponse = {
notifications?: Nip47NotificationType[];
};

export type Nip47GetBudgetResponse =
| {
used_budget: number; // msats
total_budget: number; // msats
renews_at?: number; // timestamp
im-adithya marked this conversation as resolved.
Show resolved Hide resolved
renewal_period: "daily" | "weekly" | "monthly" | "yearly" | "never";
}
// eslint-disable-next-line @typescript-eslint/ban-types
| {};

export type Nip47GetBalanceResponse = {
balance: number; // msats
};
Expand Down Expand Up @@ -490,6 +501,20 @@ export class NWCClient {
}
}

async getBudget(): Promise<Nip47GetBudgetResponse> {
try {
const result = await this.executeNip47Request<Nip47GetBudgetResponse>(
"get_budget",
{},
(result) => result !== undefined,
);
return result;
} catch (error) {
console.error("Failed to request get_budget", error);
throw error;
}
}

async getBalance(): Promise<Nip47GetBalanceResponse> {
try {
const result = await this.executeNip47Request<Nip47GetBalanceResponse>(
Expand Down
5 changes: 4 additions & 1 deletion src/webln/NostrWeblnProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ type Nip07Provider = {
signEvent(event: UnsignedEvent): Promise<Event>;
};

const nip47ToWeblnRequestMap: Record<Nip47Method, WebLNMethod> = {
const nip47ToWeblnRequestMap: Record<
Exclude<Nip47Method, "get_budget">,
WebLNMethod
> = {
get_info: "getInfo",
get_balance: "getBalance",
make_invoice: "makeInvoice",
Expand Down
Loading