diff --git a/.changeset/shaky-books-cut.md b/.changeset/shaky-books-cut.md new file mode 100644 index 0000000..a9ade66 --- /dev/null +++ b/.changeset/shaky-books-cut.md @@ -0,0 +1,5 @@ +--- +'@fedimint/core': patch +--- + +Added select_available_gateay rpc and use it for payments instead of list_gateways diff --git a/flake.nix b/flake.nix index 7e10285..0b1d9f1 100644 --- a/flake.nix +++ b/flake.nix @@ -6,7 +6,7 @@ url = "github:fedimint/fedimint/v0.8.1"; }; fedimint-wasm = { - url = "github:fedimint/fedimint?rev=ba238118bf5b204bc73c1113b6cadd62bca4e66c"; + url = "github:fedimint/fedimint?rev=4237c7a8701f599df0c036b4180f5b9200f7efba"; }; }; outputs = diff --git a/packages/core/src/services/LightningService.ts b/packages/core/src/services/LightningService.ts index 953f90e..e835f78 100644 --- a/packages/core/src/services/LightningService.ts +++ b/packages/core/src/services/LightningService.ts @@ -2,6 +2,7 @@ import { TransportClient } from '../transport' import type { CreateBolt11Response, GatewayInfo, + GetAvailableGatewayParams, JSONObject, LightningGateway, LnInternalPayState, @@ -77,10 +78,24 @@ export class LightningService { ) } - private async _getDefaultGatewayInfo() { + async getAvailableGateway({ + gateway, + invoice, + }: GetAvailableGatewayParams | undefined = {}) { + return await this.client.rpcSingle( + 'ln', + 'select_available_gateway', + { + maybe_gateway: gateway ?? null, + maybe_invoice: invoice ?? null, + }, + ) + } + + private async _getDefaultGatewayInfo(invoice?: string) { await this.updateGatewayCache() - const gateways = await this.listGateways() - return gateways[0]?.info + const gateway = await this.getAvailableGateway({ invoice }) + return gateway?.info ?? null } /** https://web.fedimint.org/core/FedimintWallet/LightningService/payInvoice#lightning-payinvoice-invoice-string */ @@ -89,7 +104,7 @@ export class LightningService { gatewayInfo?: GatewayInfo, extraMeta?: JSONObject, ) { - const gateway = gatewayInfo ?? (await this._getDefaultGatewayInfo()) + const gateway = gatewayInfo ?? (await this._getDefaultGatewayInfo(invoice)) return await this.client.rpcSingle( 'ln', 'pay_bolt11_invoice', diff --git a/packages/core/src/types/wallet.ts b/packages/core/src/types/wallet.ts index 6ec707c..6c08814 100644 --- a/packages/core/src/types/wallet.ts +++ b/packages/core/src/types/wallet.ts @@ -14,6 +14,7 @@ type GatewayInfo = { route_hints: RouteHint[] fees: FeeToAmount } + type LightningGateway = { info: GatewayInfo vetted: boolean @@ -36,6 +37,11 @@ type OutgoingLightningPayment = { type PayType = { lightning: string } | { internal: string } +type GetAvailableGatewayParams = { + gateway?: LightningGateway + invoice?: string +} + type LnPayState = | 'created' | 'canceled' @@ -295,4 +301,5 @@ export { WalletTransaction, Transactions, WalletDepositState, + GetAvailableGatewayParams, } diff --git a/packages/integration-tests/src/services/LightningService.test.ts b/packages/integration-tests/src/services/LightningService.test.ts index ee57358..9f3d7d8 100644 --- a/packages/integration-tests/src/services/LightningService.test.ts +++ b/packages/integration-tests/src/services/LightningService.test.ts @@ -1,6 +1,7 @@ import { expect } from 'vitest' import { keyPair } from '../test/crypto' import { walletTest } from '../test/fixtures' +import { LightningGateway } from '@fedimint/core' walletTest( 'createInvoice should create a bolt11 invoice', @@ -263,3 +264,19 @@ walletTest( } }, ) + +walletTest( + 'getAvailableGateway should return a gateway', + async ({ wallet }) => { + expect(wallet).toBeDefined() + expect(wallet.isOpen()).toBe(true) + + const gateway = await wallet.lightning.getAvailableGateway() + expect(gateway).toBeDefined() + expect(gateway).toMatchObject({ + info: expect.any(Object), + vetted: expect.any(Boolean), + ttl: expect.any(Object), + } satisfies LightningGateway) + }, +)