Skip to content

Commit

Permalink
Merge pull request #264 from getAlby/feat/get-budget
Browse files Browse the repository at this point in the history
feat: add get_budget to NWCClient
  • Loading branch information
im-adithya authored Oct 22, 2024
2 parents 6e30a3a + 8c0b50e commit 91705b1
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
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
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

0 comments on commit 91705b1

Please sign in to comment.