feat: improve error reporting for wallet payments

This commit is contained in:
2023-05-17 16:30:23 +01:00
parent 202b9933e0
commit 167f1c5e65
4 changed files with 46 additions and 6 deletions

View File

@ -123,7 +123,7 @@ export default class LNDHubWallet implements LNWallet {
},
});
const json = await rsp.json();
if ("error" in json) {
if ("code" in json && !rsp.ok) {
const err = json as ErrorResponse;
throw new WalletError(err.code, err.message);
}

View File

@ -1,7 +1,7 @@
import { Connection, RawEvent } from "@snort/nostr";
import { EventBuilder } from "System";
import { EventExt } from "System/EventExt";
import { LNWallet, WalletError, WalletErrorCode, WalletInfo, WalletInvoice } from "Wallet";
import { LNWallet, WalletError, WalletErrorCode, WalletInfo, WalletInvoice, WalletInvoiceState } from "Wallet";
interface WalletConnectConfig {
relayUrl: string;
@ -14,6 +14,23 @@ interface QueueObj {
reject: (e: Error) => void;
}
interface WalletConnectResponse<T> {
result_type?: string;
result?: T;
error?: {
code:
| "RATE_LIMITED"
| "NOT_IMPLEMENTED"
| "INSUFFICIENT_BALANCE"
| "QUOTA_EXCEEDED"
| "RESTRICTED"
| "UNAUTHORIZED"
| "INTERNAL"
| "OTHER";
message: string;
};
}
export class NostrConnectWallet implements LNWallet {
#config: WalletConnectConfig;
#conn?: Connection;
@ -83,9 +100,18 @@ export class NostrConnectWallet implements LNWallet {
async payInvoice(pr: string) {
await this.login();
return await this.#rpc<WalletInvoice>("pay_invoice", {
const rsp = await this.#rpc<WalletConnectResponse<WalletInvoice>>("pay_invoice", {
invoice: pr,
});
if (!rsp.error) {
return {
...rsp.result,
pr,
state: WalletInvoiceState.Paid,
} as WalletInvoice;
} else {
throw new WalletError(WalletErrorCode.GeneralError, rsp.error.message);
}
}
getInvoices() {