From 0aea60330afff02ad8bb525c5fddd7c367abf03a Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Tue, 4 Apr 2023 00:05:23 -0700 Subject: skip processed payments --- lib/lightning.js | 5 ++++- lib/payments.js | 10 +++++++++- lib/rates.js | 6 +++--- 3 files changed, 16 insertions(+), 5 deletions(-) (limited to 'lib') 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); }; -- cgit v1.2.3