Skip to content
Draft
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ export * as l402 from "./l402";
export * as boostagrams from "./podcasting2/boostagrams";
export * as fiat from "./utils/fiat";
export * as nostr from "./utils/nostr";
export * from "./utils/Amount";
export { fetchWithL402, sendBoostagram, LightningAddress, Invoice };
export * from "./types";
20 changes: 20 additions & 0 deletions src/utils/Amount.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { resolveAmount, SATS } from "./Amount";

describe("Amount", () => {
test("SATS", async () => {
const amount = SATS(10);
expect(amount.satoshi).toBe(10);
});
test("resolveAmount", async () => {
const resolved = await resolveAmount({ satoshi: 10 });
expect(resolved.satoshi).toBe(10);
expect(resolved.millisat).toBe(10_000);
});
test("resolveAmount async", async () => {
const resolved = await resolveAmount({
satoshi: new Promise((resolve) => setTimeout(() => resolve(10), 300)),
});
expect(resolved.satoshi).toBe(10);
expect(resolved.millisat).toBe(10_000);
});
});
28 changes: 28 additions & 0 deletions src/utils/Amount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* An amount in satoshis
*/
export type Amount = { satoshi: number } | { satoshi: Promise<number> };

export const SATS: (amount: number) => Amount = (amount) => ({
satoshi: amount,
});

/**
* Resolve a satoshi amount, possibly from a promise (e.g. from fiat conversion)
*/
export async function resolveAmount(
amount: Amount,
): Promise<{ satoshi: number; millisat: number }> {
if (typeof amount === "number") {
return {
satoshi: amount,
millisat: amount * 1000,
};
}
Comment thread
rolznz marked this conversation as resolved.
Outdated
const satoshi = await Promise.resolve(amount.satoshi);

return {
satoshi: satoshi,
millisat: satoshi * 1000,
};
}
29 changes: 29 additions & 0 deletions src/utils/fiat/FiatAmount.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { resolveAmount } from "../Amount";
import { USD } from "./FiatAmount";
import fetchMock from "jest-fetch-mock";

describe("FiatAmount", () => {
test("interoperable with Amount", async () => {
fetchMock.mockIf(/.*/, (_) => {
return Promise.resolve(
JSON.stringify({
code: "USD",
symbol: "$",
rate: "77135.00",
rate_float: 77135.0,
rate_cents: 7713500,
USD: {
code: "USD",
symbol: "$",
rate: "77135.00",
rate_float: 77135.0,
rate_cents: 7713500,
},
}),
);
});
const fiatAmount = USD(1);
const resolved = await resolveAmount(fiatAmount);
expect(resolved.satoshi).toBeGreaterThan(0);
});
});
21 changes: 21 additions & 0 deletions src/utils/fiat/FiatAmount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { fiat } from "../..";

/**
* An amount in a fiat currency represented in satoshis
*/
export class FiatAmount {
satoshi: Promise<number>;
constructor(amount: number, currency: string) {
this.satoshi = fiat.getSatoshiValue({
amount,
currency,
});
}
}

// Most popular fiat currencies
export const USD = (amount: number) => new FiatAmount(amount, "USD");
export const EUR = (amount: number) => new FiatAmount(amount, "EUR");
export const JPY = (amount: number) => new FiatAmount(amount, "JPY");
export const GBP = (amount: number) => new FiatAmount(amount, "GBP");
export const CHF = (amount: number) => new FiatAmount(amount, "CHF");
File renamed without changes.
2 changes: 2 additions & 0 deletions src/utils/fiat/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./fiat";
export * from "./FiatAmount";