Skip to content

Commit

Permalink
fix: gateway selection (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlwn123 authored Sep 12, 2024
1 parent be1582e commit ad4cf7b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/itchy-suits-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@fedimint/core-web': patch
---

Fix Lightning Gateway selection
2 changes: 1 addition & 1 deletion packages/core-web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const unsubscribe = wallet.subscribeBalance((balance: number) => {
await wallet.reissueNotes('A11qgqpw9thwvaz7t...')

// Pay Lightning Invoice
await wallet.payInvoice('lnbc...')
await wallet.payBolt11Invoice('lnbc...')
```

## Check out the example
Expand Down
43 changes: 40 additions & 3 deletions packages/core-web/src/FedimintWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
StreamError,
CreateBolt11Response,
ModuleKind,
GatewayInfo,
} from './types/wallet.js'

const DEFAULT_CLIENT_NAME = 'fm-default' as const
Expand Down Expand Up @@ -291,13 +292,30 @@ export class FedimintWallet {

// Lightning Network module methods

async createBolt11InvoiceWithGateway(
amount: number,
description: string,
expiryTime: number | null = null,
extraMeta: JSONObject = {},
gatewayInfo: GatewayInfo,
) {
return await this._rpcSingle('ln', 'create_bolt11_invoice', {
amount,
description,
expiry_time: expiryTime,
extra_meta: extraMeta,
gateway: gatewayInfo,
})
}

async createBolt11Invoice(
amount: number,
description: string,
expiryTime: number | null = null,
extraMeta: JSONObject = {},
gateway: LightningGateway | null = null,
): Promise<CreateBolt11Response> {
await this.updateGatewayCache()
const gateway = await this._getDefaultGatewayInfo()
return await this._rpcSingle('ln', 'create_bolt11_invoice', {
amount,
description,
Expand All @@ -307,13 +325,31 @@ export class FedimintWallet {
})
}

async payBolt11InvoiceWithGateway(
invoice: string,
gatewayInfo: GatewayInfo,
extraMeta: JSONObject = {},
) {
return await this._rpcSingle('ln', 'pay_bolt11_invoice', {
maybe_gateway: gatewayInfo,
invoice,
extra_meta: extraMeta,
})
}

async _getDefaultGatewayInfo(): Promise<LightningGateway> {
const gateways = await this.listGateways()
return gateways[0]
}

async payBolt11Invoice(
invoice: string,
maybeGateway: LightningGateway | null = null,
extraMeta: JSONObject = {},
): Promise<OutgoingLightningPayment> {
await this.updateGatewayCache()
const gateway = await this._getDefaultGatewayInfo()
return await this._rpcSingle('ln', 'pay_bolt11_invoice', {
maybe_gateway: maybeGateway,
maybe_gateway: gateway.info,
invoice,
extra_meta: extraMeta,
})
Expand Down Expand Up @@ -376,6 +412,7 @@ export class FedimintWallet {
}

async updateGatewayCache(): Promise<void> {
console.trace('Updating gateway cache')
await this._rpcSingle('ln', 'update_gateway_cache', {})
}
}
11 changes: 10 additions & 1 deletion packages/core-web/src/types/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,22 @@ type JSONValue =

type JSONObject = Record<string, JSONValue>

type LightningGateway = {
type GatewayInfo = {
gateway_id: string
api: string
node_pub_key: string
federation_index: number
route_hints: RouteHint[]
fees: FeeToAmount
}
type LightningGateway = {
info: GatewayInfo
vetted: boolean
ttl: {
nanos: number
secs: number
}
}

type RouteHint = {
// TODO: Define the structure of RouteHint
Expand Down Expand Up @@ -84,6 +92,7 @@ export {
LnPayState,
LnReceiveState,
CreateBolt11Response,
GatewayInfo,
StreamError,
StreamSuccess,
StreamResult,
Expand Down

0 comments on commit ad4cf7b

Please sign in to comment.