Skip to content

Commit

Permalink
[UMA JS]: Add versioning docs (#7007) (#298)
Browse files Browse the repository at this point in the history
* [UMA JS]: Add versioning docs (#7007)

GitOrigin-RevId: b300bf03212024f1fb850cbec761826b27492b1f

* [lightspark-sdk] Move transaction polling into new client method (#7006)

GitOrigin-RevId: 6bd25924934725f930fd5054f72b776940a77c19

* Create empty-icons-pull.md

---------

Co-authored-by: Jeremy Klein <[email protected]>
Co-authored-by: Corey Martin <[email protected]>
  • Loading branch information
3 people authored Oct 19, 2023
1 parent 24782f5 commit 1372f3a
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/empty-icons-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@lightsparkdev/lightspark-sdk": patch
---

Move transaction polling into new client method
1 change: 0 additions & 1 deletion apps/examples/uma-vasp/src/ReceivingVasp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ export default class ReceivingVasp {
}

private async handleUmaLnurlp(req: Request, res: Response, next: any) {
const uuid = req.params.uuid;
let umaQuery: uma.LnurlpRequest;
try {
umaQuery = uma.parseLnurlpRequest(
Expand Down
49 changes: 49 additions & 0 deletions packages/lightspark-sdk/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
LightsparkException,
LightsparkSigningException,
NodeKeyCache,
pollUntil,
Requester,
SigningKeyType,
StubAuthProvider,
Expand Down Expand Up @@ -46,6 +47,7 @@ import { SendPayment } from "./graphql/SendPayment.js";
import { SingleNodeDashboard as SingleNodeDashboardQuery } from "./graphql/SingleNodeDashboard.js";
import { TransactionsForNode } from "./graphql/TransactionsForNode.js";
import { TransactionSubscription } from "./graphql/TransactionSubscription.js";
import { TransactionStatus } from "./index.js";
import NodeKeyLoaderCache from "./NodeKeyLoaderCache.js";
import Account from "./objects/Account.js";
import { ApiTokenFromJson } from "./objects/ApiToken.js";
Expand Down Expand Up @@ -789,6 +791,53 @@ class LightsparkClient {
);
}

/**
* Waits for a transaction to have a completed status, and returns the transaction.
*
* @param transactionId The ID of the transaction to wait for
* @param pollTimeoutSecs The timeout in seconds that we will wait before throwing an exception
*/
public async waitForTransactionComplete(
transactionId: string,
pollTimeoutSecs = 60,
) {
const pollIntervalMs = 250;
const pollMaxTimeouts = (pollTimeoutSecs * 1000) / pollIntervalMs;
const pollIgnoreErrors = false;

const transaction = (await pollUntil(
() => {
return this.getTransaction(transactionId);
},
(current, response) => {
if (
current &&
[
TransactionStatus.SUCCESS,
TransactionStatus.CANCELLED,
TransactionStatus.FAILED,
].includes(current.status)
) {
return {
stopPolling: true,
value: current,
};
}
return response;
},
pollIntervalMs,
pollMaxTimeouts,
pollIgnoreErrors,
() =>
new LightsparkException(
"Timeout",
"Timeout waiting for transaction to complete.",
),
)) as Transaction;

return transaction;
}

/**
* Sends a payment directly to a node on the Lightning Network through the public key of the node without an invoice.
*
Expand Down
30 changes: 8 additions & 22 deletions packages/lightspark-sdk/src/tests/integration/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
BitcoinNetwork,
PaymentRequestStatus,
TransactionStatus,
type Transaction,
} from "../../index.js";
import { logger } from "../../logger.js";

Expand Down Expand Up @@ -155,27 +154,14 @@ describe("lightspark-sdk client", () => {
const payment = await lightsparkClient.payInvoice(nodeId, invoice, 60);
log("payment.id", payment?.id);

const transaction = (await pollUntil(
() => {
if (!payment) {
throw new Error("No payment");
}
return lightsparkClient.getTransaction(payment.id);
},
(current, response) => {
if (current && current.status === TransactionStatus.SUCCESS) {
return {
stopPolling: true,
value: current,
};
}
return response;
},
pollIntervalMs,
pollMaxTimeouts,
pollIgnoreErrors,
() => new Error("Timeout waiting for payment to be received"),
)) as Transaction;
if (!payment) {
throw new Error("No payment");
}

const transaction = await lightsparkClient.waitForTransactionComplete(
payment.id,
pollTimeoutSecs,
);

expect(transaction.status).toEqual(TransactionStatus.SUCCESS);
}, 30_000);
Expand Down

0 comments on commit 1372f3a

Please sign in to comment.