From 33d67e431416ea8b69b424e7d043ef63953c355a Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Wed, 4 Sep 2024 20:40:35 -0700 Subject: fx endpoint --- config.js.sample | 32 -------------------------------- config.ts.sample | 35 +++++++++++++++++++++++++++++++++++ index.ts | 1 + lib/invoices.ts | 12 ++++++------ lib/rates.ts | 15 +++++++++------ lib/rpc.ts | 6 +++--- routes/rates.ts | 13 +++++++++---- routes/users.ts | 1 - setup.sh | 4 ++-- 9 files changed, 65 insertions(+), 54 deletions(-) delete mode 100644 config.js.sample create mode 100644 config.ts.sample diff --git a/config.js.sample b/config.js.sample deleted file mode 100644 index 2a27f694..00000000 --- a/config.js.sample +++ /dev/null @@ -1,32 +0,0 @@ -export default { - archive: "redis://archive", - db: "redis://db", - nostr: "ws://nostr:8080", - relays: [ - "ws://nostr:8080", - "wss://nostr-pub.wellorder.net", - "wss://brb.io", - "wss://nostr.v0l.io", - "wss://relay.nostr.bg", - "wss://nostr.orangepill.dev" - ], - jwt: "00d0ab4f7738a83feb37f661526512063c41e49278b7c32cba87314269a5788b", - bitcoin: { - host: "bc", - wallet: "coinos", - username: "admin1", - password: "123", - network: "regtest", - port: 18443, - }, - liquid: { - host: "lq", - wallet: "coinos", - username: "admin1", - password: "123", - network: "regtest", - port: 7040, - btc: "5ac9f65c0efcc4775e0baec4ec03abdde22473cd3cf33c0419ca290e0751b225" - }, - lightning: "/app/data/lightning/regtest/lightning-rpc" -}; diff --git a/config.ts.sample b/config.ts.sample new file mode 100644 index 00000000..d5726fc4 --- /dev/null +++ b/config.ts.sample @@ -0,0 +1,35 @@ +export default { + archive: "redis://archive", + db: "redis://db", + nostr: "ws://nostr:8080", + relays: [ + "ws://nostr:8080", + "wss://nostr-pub.wellorder.net", + "wss://brb.io", + "wss://nostr.v0l.io", + "wss://relay.nostr.bg", + "wss://nostr.orangepill.dev" + ], + jwt: "00d0ab4f7738a83feb37f661526512063c41e49278b7c32cba87314269a5788b", + bitcoin: { + host: "bc", + wallet: "coinos", + username: "admin1", + password: "123", + network: "regtest", + port: 18443, + }, + liquid: { + host: "lq", + wallet: "coinos", + username: "admin1", + password: "123", + network: "regtest", + port: 7040, + btc: "5ac9f65c0efcc4775e0baec4ec03abdde22473cd3cf33c0419ca290e0751b225" + }, + lightning: "/app/data/lightning/regtest/lightning-rpc" + fee: 0.001, + adminpass: "pwa", + support: "support@coinos.io" +}; diff --git a/index.ts b/index.ts index ce0723b0..b2072952 100644 --- a/index.ts +++ b/index.ts @@ -36,6 +36,7 @@ setInterval(sendHeartbeat, 2000); app.get("/balances", info.balances); app.post("/email", email.send); +app.get("/fx", rates.fx); app.get("/rate", rates.last); app.get("/rates", rates.index); diff --git a/lib/invoices.ts b/lib/invoices.ts index 04e1200b..ee0eb73d 100644 --- a/lib/invoices.ts +++ b/lib/invoices.ts @@ -1,16 +1,16 @@ -import crypto from "crypto"; -import store from "$lib/store"; -import { l } from "$lib/logging"; import { emit } from "$lib/sockets"; import { db, g, s } from "$lib/db"; import { getUser, bip21, fail, SATS } from "$lib/utils"; import { types } from "$lib/payments"; import { v4 } from "uuid"; -import lq from "$lib/liquid"; -import bc from "$lib/bitcoin"; +import bc_ from "$lib/bitcoin"; +import lq_ from "$lib/liquid"; import ln from "$lib/ln"; +let bc = bc_ as any; +let lq = lq_ as any; + export let generate = async ({ invoice, user, sender = undefined }) => { let { bolt11, @@ -37,7 +37,7 @@ export let generate = async ({ invoice, user, sender = undefined }) => { else if (sender) user = await getUser(sender.username); if (!user) fail("user not provided"); - let rates = await g('rates'); + let rates = await g("rates"); if (!currency) currency = user.currency; if (!rate) rate = rates[currency]; if (fiat) amount = Math.round((SATS * fiat) / rate); diff --git a/lib/rates.ts b/lib/rates.ts index fe942453..f840592e 100644 --- a/lib/rates.ts +++ b/lib/rates.ts @@ -1,7 +1,6 @@ import got from "got"; import config from "$config"; -import store from "$lib/store"; -import { err, warn } from "$lib/logging"; +import { err } from "$lib/logging"; import { g, s } from "$lib/db"; import { sleep } from "$lib/utils"; import WebSocket from "ws"; @@ -51,10 +50,14 @@ export let getFx = async () => { if (Date.now() - date > 24 * 60 * 60 * 1000) { date = Date.now(); try { - let r = await got( - `http://data.fixer.io/api/latest?access_key=${config.fixer}`, - ).json(); - let { rates: fx } = r; + if (config.fixer) { + ({ rates: fx } = (await got( + `http://data.fixer.io/api/latest?access_key=${config.fixer}`, + ).json()) as any); + } else { + ({ fx } = (await got("https://coinos.io/api/fx").json()) as any); + } + let USD = fx["USD"]; Object.keys(fx).map((k) => { diff --git a/lib/rpc.ts b/lib/rpc.ts index bb57d075..bcf695a4 100644 --- a/lib/rpc.ts +++ b/lib/rpc.ts @@ -1,11 +1,11 @@ import got from "got"; -export default ({ host, port, wallet, username, password }) => +export default ({ host, port, wallet, username, password })=> new Proxy( {}, { get: - (target, prop) => + (_, prop) => (...params) => ((method, ...params) => { let url = `http://${host}:${port}/wallet/${wallet}`; @@ -23,6 +23,6 @@ export default ({ host, port, wallet, username, password }) => }) .json() .then(({ result }) => result); - })(prop.toLowerCase(), ...params), + })((prop as string).toLowerCase(), ...params), }, ); diff --git a/routes/rates.ts b/routes/rates.ts index 828e5b2c..f75afc57 100644 --- a/routes/rates.ts +++ b/routes/rates.ts @@ -1,11 +1,16 @@ import { g } from "$lib/db"; export default { - async last(req, res) { - res.send(await g('rate')); + async fx(_, res) { + let { fx } = await g("fx"); + res.send({fx}); }, - async index(req, res) { - res.send(await g('rates')); + async last(_, res) { + res.send(await g("rate")); + }, + + async index(_, res) { + res.send(await g("rates")); }, }; diff --git a/routes/users.ts b/routes/users.ts index e6044a47..3fcaad66 100644 --- a/routes/users.ts +++ b/routes/users.ts @@ -16,7 +16,6 @@ import upload from "$lib/upload"; const { encode, decode, fromWords, toWords } = bech32; -console.log("HI"); export default { upload, diff --git a/setup.sh b/setup.sh index 1c5115a8..07bd2c36 100755 --- a/setup.sh +++ b/setup.sh @@ -1,9 +1,9 @@ -cp config.js.sample config.js +cp config.ts.sample config.ts cp compose.yml.sample compose.yml cp -r sampledata data sudo chown 100:100 data/nostr/data docker compose up -d -docker run -it -v $(pwd):/app --entrypoint pnpm asoltys/coinos-server i +docker run -it -v $(pwd):/app --entrypoint bun asoltys/coinos-server i docker exec -it bc bitcoin-cli createwallet coinos docker exec -it bc bitcoin-cli rescanblockchain docker exec -it bc bitcoin-cli generatetoaddress 500 $(docker exec -it bc bitcoin-cli getnewaddress "" "p2sh-segwit") -- cgit v1.2.3