diff options
| author | Adam Soltys <[email protected]> | 2024-08-23 23:39:05 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2024-08-23 23:39:05 -0700 |
| commit | 193457c5bd6c7c892c3be50674579dabd07fd2f2 (patch) | |
| tree | 82792280db6ea9945643a76112a210d904d49239 /lib/payments.js | |
| parent | 7be5db4d1dae7226a48623f42aca864adfa54e02 (diff) | |
| download | coinos-server-193457c5bd6c7c892c3be50674579dabd07fd2f2.tar.gz coinos-server-193457c5bd6c7c892c3be50674579dabd07fd2f2.zip | |
use redis to store rates
Diffstat (limited to 'lib/payments.js')
| -rw-r--r-- | lib/payments.js | 31 |
1 files changed, 12 insertions, 19 deletions
diff --git a/lib/payments.js b/lib/payments.js index 264a2417..92e17839 100644 --- a/lib/payments.js +++ b/lib/payments.js @@ -1,6 +1,5 @@ import locales from "$lib/locales/index"; import config from "$config"; -import store from "$lib/store"; import { emit } from "$lib/sockets"; import { v4 } from "uuid"; import { notify } from "$lib/notifications"; @@ -49,13 +48,16 @@ export let debit = async ({ type = types.internal, rate, }) => { + amount = parseInt(amount); if (type !== types.internal && (await g("freeze"))) fail("Problem sending payment"); // if (!user.unlimited && amount > 1000000) // fail(`⚡️${amount} exceeds max withdrawal of ⚡️1,000,000`); let ref; let { id: uid, currency, username } = user; - if (!rate) rate = store.rates[currency]; + + let rates = await g('rates'); + if (!rate) rate = rates[currency]; let invoice = await getInvoice(hash); let iid; @@ -67,9 +69,9 @@ export let debit = async ({ ref = invoice.uid; let equivalentRate = - invoice.rate * (store.rates[currency] / store.rates[invoice.currency]); + invoice.rate * (rates[currency] / rates[invoice.currency]); - if (Math.abs(invoice.rate / store.rates[invoice.currency] - 1) < 0.01) { + if (Math.abs(invoice.rate / rates[invoice.currency] - 1) < 0.01) { rate = equivalentRate; } else { warn("rate slipped", hash, invoice.rate, equivalentRate); @@ -120,7 +122,7 @@ export let debit = async ({ await db.lPush(`${uid}:payments`, id); l(user.username, "sent", type, amount); - emit(user.id, "payment", p); + //emit(user.id, "payment", p); return p; }; @@ -145,15 +147,16 @@ export let credit = async (hash, amount, memo, ref, type = types.internal) => { let user = await g(`user:${inv.uid}`); let { id: uid, currency, username } = user; - let rate = store.rates[currency]; + let rates = await g('rates'); + let rate = rates[currency]; if (!rate) await sleep(1000); - rate = store.rates[currency]; + rate = rates[currency]; let equivalentRate = - inv.rate * (store.rates[currency] / store.rates[inv.currency]); + inv.rate * (rates[currency] / rates[inv.currency]); - if (Math.abs(inv.rate / store.rates[inv.currency] - 1) < 0.01) { + if (Math.abs(inv.rate / rates[inv.currency] - 1) < 0.01) { rate = equivalentRate; } else { // warn("rate slipped", hash, invoice.rate, equivalentRate); @@ -208,16 +211,6 @@ export let credit = async (hash, amount, memo, ref, type = types.internal) => { callWebhook(inv, p); l(username, "received", type, amount); - // let items = ""; - // for (let i of inv.items) { - // items += `${i.quantity} ${i.name} ${f( - // i.price * i.quantity, - // inv.currency, - // )}\n`; - // } - // - // console.log(items); - let items; if (inv.items && inv.items.length) { formatReceipt(inv.items, inv.currency); |
