Skip to content

Commit

Permalink
Address sdk feedback (#93)
Browse files Browse the repository at this point in the history
* chore: fix react exmaple

* fix: convert amount fields to amountMsats

* feat: add several docs pages
  • Loading branch information
alexlwn123 authored Oct 30, 2024
1 parent 649b296 commit 9468ef5
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 29 deletions.
5 changes: 5 additions & 0 deletions .changeset/mighty-eels-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@fedimint/core-web': patch
---

Rename amount fields to include units: (e.g., amountMsats)
23 changes: 20 additions & 3 deletions docs/core/FedimintWallet/cleanup.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
This guide hasn't been written yet.
# cleanup

If you'd like to contribute, please open a PR!
### `cleanup()`

You can use the `Suggest changes to this page` link below.
Cleans up browser resources associated with the wallet. This should be called when the wallet is no longer needed.

```ts twoslash
// @esModuleInterop
import { FedimintWallet } from '@fedimint/core-web'

const wallet = new FedimintWallet()

await wallet.open()
// ... use the wallet

// Once we're no longer using the wallet, // [!code focus]
// we can call cleanup to free up resources // [!code focus]
await wallet.cleanup() // [!code focus]

// If we want to use the wallet again, we can call open() again
await wallet.open()
```
8 changes: 4 additions & 4 deletions docs/core/FedimintWallet/constructor.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ until needed. Default is false.
```ts twoslash
import { FedimintWallet } from '@fedimint/core-web'

// Create a wallet with immediate initialization
const wallet = new FedimintWallet() // wasm gets initialized here
// Create a wallet with immediate initialization // [!code focus]
const wallet = new FedimintWallet() // wasm gets initialized here // [!code focus]
wallet.open()

// Create a wallet with lazy initialization
const lazyWallet = new FedimintWallet(true) // lazy = true
// Create a wallet with lazy initialization // [!code focus]
const lazyWallet = new FedimintWallet(true) // lazy = true // [!code focus]
// Some time later...
lazyWallet.open() // wasm gets initialized here
```
Expand Down
21 changes: 18 additions & 3 deletions docs/core/FedimintWallet/isOpen.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
This guide hasn't been written yet.
# open

If you'd like to contribute, please open a PR!
### `isOpen()`

You can use the `Suggest changes to this page` link below.
Check if the wallet is open.

```ts twoslash
// @esModuleInterop
import { FedimintWallet } from '@fedimint/core-web'

const wallet = new FedimintWallet()

const isOpen = wallet.isOpen() // [!code focus]

if (!isOpen) {
await wallet.joinFederation('fed123...')
} else {
const balance = await wallet.balance.getBalance()
}
```
30 changes: 27 additions & 3 deletions docs/core/FedimintWallet/joinFederation.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
This guide hasn't been written yet.
# joinFederation

If you'd like to contribute, please open a PR!
### `joinFederation(federationId: string)`

You can use the `Suggest changes to this page` link below.
Attempts to join a federation.

```ts twoslash
// @esModuleInterop
import { FedimintWallet } from '@fedimint/core-web'

const wallet = new FedimintWallet()

const didJoin = await wallet.joinFederation('fed123...') // [!code focus]

if (didJoin) {
const balance = await wallet.balance.getBalance()
}
```

To support multiple wallets within a single application, you can pass in a custom client name.

```ts twoslash
// @esModuleInterop
import { FedimintWallet } from '@fedimint/core-web'

const wallet = new FedimintWallet()

const didJoin = await wallet.joinFederation('fed456...', 'my-client-name') // [!code focus]
```
34 changes: 31 additions & 3 deletions docs/core/FedimintWallet/open.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
This guide hasn't been written yet.
# open

If you'd like to contribute, please open a PR!
### `open(clientName?: string)`

You can use the `Suggest changes to this page` link below.
Attempts to open an existing wallet.

Default client name is `fm-wallet`.

```ts twoslash
// @esModuleInterop
import { FedimintWallet } from '@fedimint/core-web'

const wallet = new FedimintWallet()

const isOpen = await wallet.open() // [!code focus]

if (isOpen) {
const balance = await wallet.balance.getBalance()
} else {
await wallet.joinFederation('fed123...')
}
```

To support multiple wallets within a single application, you can pass in a custom client name.

```ts twoslash
// @esModuleInterop
import { FedimintWallet } from '@fedimint/core-web'

const wallet = new FedimintWallet()

const isOpen = await wallet.open('my-client-name') // [!code focus]
```
15 changes: 12 additions & 3 deletions docs/core/FedimintWallet/setLogLevel.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
This guide hasn't been written yet.
# setLogLevel

If you'd like to contribute, please open a PR!
### `setLogLevel()`

You can use the `Suggest changes to this page` link below.
Set the log level for the wallet.

```ts twoslash
// @esModuleInterop
import { FedimintWallet } from '@fedimint/core-web'

const wallet = new FedimintWallet()

wallet.setLogLevel('debug') // [!code focus]
```
5 changes: 2 additions & 3 deletions packages/core-web/src/services/BalanceService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { MSats } from '../types'
import { WorkerClient } from '../worker'

/**
Expand All @@ -17,7 +16,7 @@ export class BalanceService {
* const balance = await wallet.balance.getBalance()
* ```
*/
async getBalance(): Promise<MSats> {
async getBalance(): Promise<number> {
return await this.client.rpcSingle('', 'get_balance', {})
}

Expand All @@ -35,7 +34,7 @@ export class BalanceService {
* ```
*/
subscribeBalance(
onSuccess: (balance: MSats) => void = () => {},
onSuccess: (balanceMsats: number) => void = () => {},
onError: (error: string) => void = () => {},
) {
const unsubscribe = this.client.rpcStream<string>(
Expand Down
9 changes: 4 additions & 5 deletions packages/core-web/src/services/LightningService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,22 @@ import type {
LightningGateway,
LnPayState,
LnReceiveState,
MSats,
OutgoingLightningPayment,
} from '../types'

export class LightningService {
constructor(private client: WorkerClient) {}

async createInvoice(
amount: MSats,
amountMsats: number,
description: string,
expiryTime?: number, // in seconds
gatewayInfo?: GatewayInfo,
extraMeta?: JSONObject,
): Promise<CreateBolt11Response> {
const gateway = gatewayInfo ?? (await this._getDefaultGatewayInfo())
return await this.client.rpcSingle('ln', 'create_bolt11_invoice', {
amount,
amount: amountMsats,
description,
expiry_time: expiryTime ?? null,
extra_meta: extraMeta ?? {},
Expand All @@ -32,7 +31,7 @@ export class LightningService {
}

async createInvoiceTweaked(
amount: MSats,
amountMsats: number,
description: string,
tweakKey: string,
index: number,
Expand All @@ -45,7 +44,7 @@ export class LightningService {
'ln',
'create_bolt11_invoice_for_user_tweaked',
{
amount,
amount: amountMsats,
description,
expiry_time: expiryTime ?? null,
user_key: tweakKey,
Expand Down
4 changes: 2 additions & 2 deletions packages/core-web/src/services/MintService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class MintService {
}

async spendNotes(
minAmount: MSats,
amountMsats: number,
// Tells the wallet to automatically try to cancel the spend if it hasn't completed
// after the specified number of seconds. If the receiver has already redeemed
// the notes at this time, the notes will not be cancelled.
Expand All @@ -62,7 +62,7 @@ export class MintService {
'mint',
'spend_notes',
{
min_amount: minAmount,
min_amount: amountMsats,
try_cancel_after: duration,
include_invite: includeInvite,
extra_meta: extraMeta,
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/components/HooksDemo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ function HooksDemo() {
</div>

<div className="section">
<b>useBalance()</b>
<div className="row">
<b>useBalance</b>
<p>{balance ? `${balance / 1000} sats` : 'no balance'}</p>
Expand Down

0 comments on commit 9468ef5

Please sign in to comment.