aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2023-04-04 00:05:23 -0700
committerAdam Soltys <[email protected]>2023-04-04 00:05:23 -0700
commit0aea60330afff02ad8bb525c5fddd7c367abf03a (patch)
tree52e0ce713368f5ab0f4d40c37f5194dcd55ae1f2
parent44c59b03f100792708425bbcd806422fa921f1c5 (diff)
downloadcoinos-server-0aea60330afff02ad8bb525c5fddd7c367abf03a.tar.gz
coinos-server-0aea60330afff02ad8bb525c5fddd7c367abf03a.zip
skip processed payments
-rw-r--r--index.js3
-rw-r--r--lib/lightning.js5
-rw-r--r--lib/payments.js10
-rw-r--r--lib/rates.js6
4 files changed, 18 insertions, 6 deletions
diff --git a/index.js b/index.js
index 3430b67c..0dddc818 100644
--- a/index.js
+++ b/index.js
@@ -19,11 +19,12 @@ import payments from "$routes/payments";
import requests from "$routes/requests";
fillPool();
-listenForLightning();
getLocations();
getRates();
+setTimeout(listenForLightning, 2000);
+
setInterval(sendRates, 1000);
setInterval(sendHeartbeat, 2000);
diff --git a/lib/lightning.js b/lib/lightning.js
index c9b41533..7ef62cc9 100644
--- a/lib/lightning.js
+++ b/lib/lightning.js
@@ -15,7 +15,7 @@ export async function listenForLightning() {
pay_index,
status,
msatoshi_received,
- payment_preimage: preimage,
+ payment_preimage: preimage
} = event;
await s("pay_index", pay_index);
@@ -31,6 +31,9 @@ export async function listenForLightning() {
let invoice = await g(`invoice:${hash}`);
if (!invoice) return warn("received lightning with no invoice", bolt11);
+ let payment = await g(`payment:${hash}`);
+ if (payment) return warn("already processed", hash);
+
await credit(hash, received, invoice.memo, preimage, types.lightning);
} catch (e) {
console.log(e);
diff --git a/lib/payments.js b/lib/payments.js
index 6e2e3a99..5dc24c9b 100644
--- a/lib/payments.js
+++ b/lib/payments.js
@@ -115,7 +115,15 @@ export let credit = async (hash, amount, memo, ref, type = types.internal) => {
let { id: uid, currency, username } = user;
let rate = store.rates[currency];
- if (Math.abs(invoice.rate / rate - 1) < 0.01) rate = invoice.rate;
+
+ let equivalentRate =
+ invoice.rate * (store.rates[currency] / store.rates[invoice.currency]);
+
+ if (Math.abs(invoice.rate / store.rates[invoice.currency] - 1) < 0.01) {
+ rate = equivalentRate;
+ } else {
+ warn("rate slipped", hash, invoice.rate, equivalentRate);
+ }
let p = {
hash,
diff --git a/lib/rates.js b/lib/rates.js
index 0b2daab2..362aaa98 100644
--- a/lib/rates.js
+++ b/lib/rates.js
@@ -15,11 +15,11 @@ let connect = async () => {
ws = new WebSocket("wss://stream.binance.com:9443/ws/btcusdt@ticker");
- ws.onmessage = async function (event) {
+ ws.onmessage = async function(event) {
try {
let msg = JSON.parse(event.data);
- Object.keys(store.fx).map((symbol) => {
+ Object.keys(store.fx).map(symbol => {
store.rates[symbol] = msg.c * store.fx[symbol];
});
@@ -31,7 +31,7 @@ let connect = async () => {
}
};
- ws.onerror = async function (error) {
+ ws.onerror = async function(error) {
err("binance socket error", error.message);
};