Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/shaky-books-cut.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@fedimint/core': patch
---

Added select_available_gateay rpc and use it for payments instead of list_gateways
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
23 changes: 19 additions & 4 deletions packages/core/src/services/LightningService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { TransportClient } from '../transport'
import type {
CreateBolt11Response,
GatewayInfo,
GetAvailableGatewayParams,
JSONObject,
LightningGateway,
LnInternalPayState,
Expand Down Expand Up @@ -77,10 +78,24 @@ export class LightningService {
)
}

private async _getDefaultGatewayInfo() {
async getAvailableGateway({
gateway,
invoice,
}: GetAvailableGatewayParams | undefined = {}) {
return await this.client.rpcSingle<LightningGateway | null>(
'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 */
Expand All @@ -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<OutgoingLightningPayment>(
'ln',
'pay_bolt11_invoice',
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/types/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type GatewayInfo = {
route_hints: RouteHint[]
fees: FeeToAmount
}

type LightningGateway = {
info: GatewayInfo
vetted: boolean
Expand All @@ -36,6 +37,11 @@ type OutgoingLightningPayment = {

type PayType = { lightning: string } | { internal: string }

type GetAvailableGatewayParams = {
gateway?: LightningGateway
invoice?: string
}

type LnPayState =
| 'created'
| 'canceled'
Expand Down Expand Up @@ -295,4 +301,5 @@ export {
WalletTransaction,
Transactions,
WalletDepositState,
GetAvailableGatewayParams,
}
17 changes: 17 additions & 0 deletions packages/integration-tests/src/services/LightningService.test.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand Down Expand Up @@ -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)
},
)
Loading