Skip to content

Commit

Permalink
feat: add info and reserves api endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz committed Aug 18, 2024
1 parent c0ebb1a commit 6cddb82
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 4 deletions.
7 changes: 6 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@ AUTH_TOKEN="eyJhb..."
DATABASE_URL="file:./jim.db"
BASE_URL="http://localhost:3000"
DAILY_WALLET_LIMIT=10
#APP_NAME_PREFIX = "Alby Jim "
#APP_NAME_PREFIX = "Alby Jim "

# Info
NAME="Uncle Jim Demo Server"
DESCRIPTION="This demo server shows how easy it is for new users to get a wallet. For demo purposes only - this server has a small amount of liquidity and will not be increased."
IMAGE="https://upload.wikimedia.org/wikipedia/commons/thumb/c/ce/Bust_of_Satoshi_Nakamoto_in_Budapest.jpg/440px-Bust_of_Satoshi_Nakamoto_in_Budapest.jpg"
45 changes: 42 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,56 @@ App Connections have a 10 sat / 1% reserve to account for possible routing fees.

## API

You can also create new wallets via the API. Simply do a POST request to `/api/wallets` which will return a JSON response like:
### Create a new wallet

`POST /api/wallets`

returns:

```json
{
"connectionSecret": "nostr+walletconnect://xxx?relay=yyy&secret=zzz&[email protected]",
"lightningAddress": "[email protected]"
"lightningAddress": "[email protected]",
"valueTag": "<podcast:value type=...</podcast:value>"
}
```

If a password is required, specify it in the `Authorization` header in the basic auth format, where the ID is an empty string. e.g. `"Authorization": "Basic OjEyMw=="` for password `123`.

### Get reserves

`GET /api/reserves`

returns:

```json
{
"numApps": 4,
"totalAppBalance": 114000,
"numChannels": 1,
"hasPublicChannels": false,
"totalOutgoingCapacity": 4821836,
"totalChannelCapacity": 198000000
}
```

### Get instance info

> See `.env.example` on how to set this info.
returns:

```json
{
"name": "Uncle Jim Demo Server",
"description": "This demo server shows how easy it is for new users to get a wallet. For demo purposes only - this server has a small amount of liquidity and will not be increased.",
"image": "https://upload.wikimedia.org/wikipedia/commons/thumb/c/ce/Bust_of_Satoshi_Nakamoto_in_Budapest.jpg/440px-Bust_of_Satoshi_Nakamoto_in_Budapest.jpg",
"dailyWalletLimit": 10
}
```

`GET /api/info`

## Development

Copy .env.example to .env.local and update the ALBYHUB_URL and AUTH_TOKEN properties.
Expand Down Expand Up @@ -68,7 +107,7 @@ You can get the ALBYHUB_URL, AUTH_TOKEN and ALBYHUB_NAME by logging into Alby Hu
- [x] podcasting value tag
- [x] daily wallet creation rate limit
- [x] scan QR
- [ ] get Jim instance info via REST API
- [x] get Jim reserves and instance info via REST API
- [ ] daily record of reserves + charts
- [ ] per-connection limits (so one user cannot use all the liquidity provided by the service)
- [ ] extra open actions (Alby Account, Mobile Wallet, Web Wallet, Nostrudel?, ...) & instructions
Expand Down
12 changes: 12 additions & 0 deletions app/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type Reserves = {
totalChannelCapacity: number;
numApps: number;
totalAppBalance: number;
hasPublicChannels: boolean;
};

export type Wallet = {
Expand Down Expand Up @@ -146,6 +147,7 @@ export async function getReserves(): Promise<Reserves | undefined> {
localSpendableBalance: number;
localBalance: number;
remoteBalance: number;
public: boolean;
}[];

const relevantApps = apps.filter(
Expand All @@ -166,6 +168,7 @@ export async function getReserves(): Promise<Reserves | undefined> {
numApps: relevantApps.length,
totalAppBalance,
numChannels: channels.length,
hasPublicChannels: channels.some((channel) => channel.public),
totalOutgoingCapacity,
totalChannelCapacity,
};
Expand All @@ -175,6 +178,15 @@ export async function getReserves(): Promise<Reserves | undefined> {
}
}

export async function getInfo() {
return {
name: process.env.NAME,
description: process.env.DESCRIPTION,
image: process.env.IMAGE,
dailyWalletLimit: getDailyWalletLimit(),
};
}

function getHeaders() {
return {
Authorization: `Bearer ${process.env.AUTH_TOKEN}`,
Expand Down
9 changes: 9 additions & 0 deletions app/api/info/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { getInfo } from "@/app/actions";

export const dynamic = "force-dynamic";

export async function GET(request: Request) {
const info = await getInfo();

return Response.json(info);
}
9 changes: 9 additions & 0 deletions app/api/reserves/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { getReserves } from "@/app/actions";

export const dynamic = "force-dynamic";

export async function GET(request: Request) {
const reserves = await getReserves();

return Response.json(reserves);
}

0 comments on commit 6cddb82

Please sign in to comment.