diff options
| author | Derrick <[email protected]> | 2021-07-09 14:25:51 -0600 |
|---|---|---|
| committer | Derrick <[email protected]> | 2021-07-09 14:25:51 -0600 |
| commit | bf37962b00b323c412c2ea89bf60d24fbe4aa87b (patch) | |
| tree | 70ca171462fa40108495542c1b02c9844200859e /routes | |
| parent | d7ef5e9ab7c6e04267bd1797466add8f57673598 (diff) | |
| parent | 4bde8faf1b9d91898447bdf2c63b0afd227f978c (diff) | |
| download | coinos-server-masterZ.tar.gz coinos-server-masterZ.zip | |
Merge branch 'master' into masterZmasterZ
Diffstat (limited to 'routes')
| -rw-r--r-- | routes/bitcoin/receive.js | 21 | ||||
| -rw-r--r-- | routes/lightning/receive.js | 2 | ||||
| -rw-r--r-- | routes/liquid/receive.js | 136 | ||||
| -rw-r--r-- | routes/payments.js | 4 |
4 files changed, 82 insertions, 81 deletions
diff --git a/routes/bitcoin/receive.js b/routes/bitcoin/receive.js index 45199976..d782627d 100644 --- a/routes/bitcoin/receive.js +++ b/routes/bitcoin/receive.js @@ -2,9 +2,6 @@ const reverse = require("buffer-reverse"); const zmq = require("zeromq/v5-compat"); const { Op } = require("sequelize"); const { fromBase58 } = require("bip32"); -const wretch = require("wretch"); -const fetch = require("node-fetch"); -wretch().polyfills({ fetch }); const bitcoin = require("bitcoinjs-lib"); @@ -117,6 +114,7 @@ zmqRawTx.on("message", async (topic, message, sequence) => { emit(user.username, "payment", payment); l.info("bitcoin detected", user.username, value); notify(user, `${value} SAT payment detected`); + await callWebhook(invoice, payment); } } catch (e) { console.log(e); @@ -155,26 +153,18 @@ setInterval(async () => { as: "account" }, { - model: db.User, - as: "user" - }, - { model: db.Invoice, as: "invoice" + }, + { + model: db.User, + as: "user" } ], lock: transaction.LOCK.UPDATE, transaction }); - if (p.invoice && p.invoice.webhook) { - l.info("calling webhook", p.invoice.webhook); - console.log(await wretch() - .url(p.invoice.webhook) - .post({ oh: "yeah" }) - .json()); - } - if (p && p.address) address = p.address; if (p && p.user) user = p.user; @@ -199,6 +189,7 @@ setInterval(async () => { emit(user.username, "payment", p); l.info("bitcoin confirmed", user.username, p.amount, p.tip); notify(user, `${total} SAT payment confirmed`); + await callWebhook(p.invoice, p); } else { l.warn("couldn't find bitcoin payment", hash); } diff --git a/routes/lightning/receive.js b/routes/lightning/receive.js index 55193d2d..32c54de6 100644 --- a/routes/lightning/receive.js +++ b/routes/lightning/receive.js @@ -69,6 +69,8 @@ const handlePayment = async msg => { payment = payment.get({ plain: true }); payment.account = account.get({ plain: true }); + await callWebhook(invoice, payment); + emit(user.username, "payment", payment); emit(user.username, "account", payment.account); notify(user, `Received ${total} SAT`); diff --git a/routes/liquid/receive.js b/routes/liquid/receive.js index 9e6238c7..c377a6b5 100644 --- a/routes/liquid/receive.js +++ b/routes/liquid/receive.js @@ -101,7 +101,7 @@ zmqRawTx.on("message", async (topic, message, sequence) => { user_id: user.id, network: "liquid" }, - order: [["id", "DESC"]] + order: [["id", "DESC"]], }); if (!invoice) return; @@ -175,6 +175,7 @@ zmqRawTx.on("message", async (topic, message, sequence) => { emit(user.username, "account", payment.account); l.info("liquid detected", address, user.username, asset, value); notify(user, `${value} SAT payment detected`); + await callWebhook(invoice, payment); }); } } catch (e) { @@ -187,75 +188,44 @@ zmqRawTx.on("message", async (topic, message, sequence) => { let queue = {}; zmqRawBlock.on("message", async (topic, message, sequence) => { - const payments = await db.Payment.findAll({ - where: { confirmed: 0 } - }); + try { + const payments = await db.Payment.findAll({ + where: { confirmed: 0 } + }); - const block = Block.fromHex(message.toString("hex"), true); + const block = Block.fromHex(message.toString("hex"), true); - let hash, json; + let hash, json; - try { hash = await lq.getBlockHash(block.height); json = await lq.getBlock(hash, 2); - } catch (e) { - return console.log(e); - } - - json.tx.map(async tx => { - if (issuances[tx.txid]) { - await db.transaction(async transaction => { - const { - user_id, - asset, - asset_amount, - asset_payment_id, - token, - token_amount, - token_payment_id - } = issuances[tx.txid]; - - const user = await getUserById(user_id); - - let account = await db.Account.findOne({ - where: { user_id, asset }, - lock: transaction.LOCK.UPDATE, - transaction - }); - account.balance = asset_amount * SATS; - account.pending = 0; - await account.save({ transaction }); - let payment = await db.Payment.findOne({ - where: { id: asset_payment_id }, - include: { - model: db.Account, - as: "account" - }, - lock: transaction.LOCK.UPDATE, - transaction - }); + json.tx.map(async tx => { + if (issuances[tx.txid]) { + await db.transaction(async transaction => { + const { + user_id, + asset, + asset_amount, + asset_payment_id, + token, + token_amount, + token_payment_id + } = issuances[tx.txid]; - payment.confirmed = true; - await payment.save({ transaction }); - payment = payment.get({ plain: true }); - payment.account = account.get({ plain: true }); + const user = await getUserById(user_id); - emit(user.username, "account", account); - emit(user.username, "payment", payment); - - if (token) { - account = await db.Account.findOne({ - where: { user_id, asset: token }, + let account = await db.Account.findOne({ + where: { user_id, asset }, lock: transaction.LOCK.UPDATE, transaction }); - account.balance = token_amount * SATS; + account.balance = asset_amount * SATS; account.pending = 0; await account.save({ transaction }); - payment = await db.Payment.findOne({ - where: { id: token_payment_id }, + let payment = await db.Payment.findOne({ + where: { id: asset_payment_id }, include: { model: db.Account, as: "account" @@ -266,20 +236,50 @@ zmqRawBlock.on("message", async (topic, message, sequence) => { payment.confirmed = true; await payment.save({ transaction }); - payment = payment.get({ plain: true }); payment.account = account.get({ plain: true }); emit(user.username, "account", account); emit(user.username, "payment", payment); - } - }); - } else if (payments.find(p => p.hash === tx.txid)) queue[tx.txid] = 1; - }); + + if (token) { + account = await db.Account.findOne({ + where: { user_id, asset: token }, + lock: transaction.LOCK.UPDATE, + transaction + }); + account.balance = token_amount * SATS; + account.pending = 0; + await account.save({ transaction }); + + payment = await db.Payment.findOne({ + where: { id: token_payment_id }, + include: { + model: db.Account, + as: "account" + }, + lock: transaction.LOCK.UPDATE, + transaction + }); + + payment.confirmed = true; + await payment.save({ transaction }); + + payment = payment.get({ plain: true }); + payment.account = account.get({ plain: true }); + + emit(user.username, "account", account); + emit(user.username, "payment", payment); + } + }); + } else if (payments.find(p => p.hash === tx.txid)) queue[tx.txid] = 1; + }); + } catch (e) { + return console.log(e); + } }); setInterval(async () => { - // throw new Error("boom"); try { const arr = Object.keys(queue); @@ -296,6 +296,10 @@ setInterval(async () => { as: "account" }, { + model: db.Invoice, + as: "invoice" + }, + { model: db.User, as: "user" } @@ -334,6 +338,7 @@ setInterval(async () => { ); notify(user, `${total} SAT payment confirmed`); + await callWebhook(p.invoice, p); } else { l.warn("couldn't find liquid payment", hash); } @@ -343,7 +348,12 @@ setInterval(async () => { let c = convert[address]; if (address && c) { - l.info("liquid detected for conversion request", address, c.address, user.username); + l.info( + "liquid detected for conversion request", + address, + c.address, + user.username + ); user.account = account; diff --git a/routes/payments.js b/routes/payments.js index 41dfce26..a4bae562 100644 --- a/routes/payments.js +++ b/routes/payments.js @@ -134,7 +134,6 @@ ah(async () => { } }, 50); } - /* if (config.liquid) { lq = new BitcoinCore(config.liquid); @@ -152,7 +151,7 @@ ah(async () => { const parts = hdkeypath.split("/"); app.set("lqAddressIndex", parts[parts.length - 1].slice(0, -1)); } catch (e) { - console.error(e); + console.error("Problem getting liquid address", e); } }, 50); } @@ -199,5 +198,4 @@ ah(async () => { } }) ); -*/ })(); |
