diff options
| -rw-r--r-- | index.js | 4 | ||||
| -rw-r--r-- | lib/mail.js | 2 | ||||
| -rw-r--r-- | routes/bitcoin/receive.js | 11 | ||||
| -rw-r--r-- | routes/invoices.js | 4 | ||||
| -rw-r--r-- | routes/lightning/receive.js | 10 | ||||
| -rw-r--r-- | routes/liquid/fee.js | 22 | ||||
| -rw-r--r-- | routes/liquid/receive.js | 11 | ||||
| -rw-r--r-- | routes/liquid/send.js | 23 | ||||
| -rw-r--r-- | routes/payments.js | 1 | ||||
| -rw-r--r-- | routes/send.js | 4 |
10 files changed, 55 insertions, 37 deletions
@@ -7,6 +7,7 @@ const cors = require("cors"); const express = require("express"); const { Op } = require("sequelize"); const fs = require("fs"); +const persist = require("./lib/persist"); ah = require("express-async-handler"); @@ -19,11 +20,12 @@ fail = msg => { }; challenge = {}; -convert = {}; logins = {}; sessions = {}; sockets = {}; +convert = persist("data/conversions.json"); + if (config.bitcoin) networks.push("bitcoin"); if (config.liquid) networks.push("liquid"); if (config.lna) networks.push("lightning"); diff --git a/lib/mail.js b/lib/mail.js index e690d305..705880d2 100644 --- a/lib/mail.js +++ b/lib/mail.js @@ -58,7 +58,7 @@ imap.once("ready", function() { let { user } = account; - let rate = app.get("rates")["CAD"]; + let rate = app.get("rates")["CAD"] * 1.02; let amount = Math.round((cad / rate) * 100000000); await account.increment({ balance: amount }, { transaction }); diff --git a/routes/bitcoin/receive.js b/routes/bitcoin/receive.js index e381ccda..1784fd5f 100644 --- a/routes/bitcoin/receive.js +++ b/routes/bitcoin/receive.js @@ -154,9 +154,11 @@ setInterval(async () => { transaction }); - ({ account, address, user } = p); + + ({ address, user } = p); if (p && p.account) { + ({ account } = p); total = p.amount + p.tip; p.confirmed = 1; @@ -179,21 +181,18 @@ setInterval(async () => { } else { l.warn("couldn't find payment", hash); } - - delete queue[hash]; }); let c = convert[address]; if (c) { - let { tx } = c; user.account = account; delete queue[hash]; await sendLiquid({ - address, + address: c.address, + amount: total - 100, user, - tx, limit: total }); } diff --git a/routes/invoices.js b/routes/invoices.js index a1928287..cb4b0cc1 100644 --- a/routes/invoices.js +++ b/routes/invoices.js @@ -27,7 +27,9 @@ app.post( let { liquidAddress, invoice, user, tx } = req.body; let { blindkey } = invoice; - if (liquidAddress) convert[invoice.text] = { address: liquidAddress, tx }; + if (liquidAddress) { + convert[invoice.text] = { address: liquidAddress, tx }; + } if (!user) ({ user } = req); else { diff --git a/routes/lightning/receive.js b/routes/lightning/receive.js index 325f7092..6b8a9077 100644 --- a/routes/lightning/receive.js +++ b/routes/lightning/receive.js @@ -18,7 +18,8 @@ const handlePayment = async msg => { const { text: hash, currency, memo, rate, tip, user_id } = invoice; const amount = parseInt(msg.amt_paid_sat) - tip; - if (amount > 10000000 || amount < 0) throw new Error("amount out of range"); + if (amount > 10000000 || amount < 0) + throw new Error("amount out of range"); account = await db.Account.findOne({ where: { @@ -82,18 +83,17 @@ const handlePayment = async msg => { let c = convert[msg.payment_request]; if (c) { - let { address, tx } = c; user.account = account; try { await sendLiquid({ - address, + address: c.address, + amount: total - 100, user, - tx, limit: total }); } catch (e) { - l.error("problem sending liquid payment"); + l.error("problem sending liquid payment", e.message, e.stack); } } } catch (e) { diff --git a/routes/liquid/fee.js b/routes/liquid/fee.js index debbdf55..2070873a 100644 --- a/routes/liquid/fee.js +++ b/routes/liquid/fee.js @@ -2,6 +2,12 @@ const buildTx = require("../../lib/buildliquidtx"); liquidTx = async ({ address, asset, amount, feeRate, replaceable, user }) => { let tx, fee; + let node = lq; + /* + if (asset === config.liquid.btcasset) { + node = rare; + } + */ if (user.account.pubkey) { let psbt = await buildTx({ @@ -25,7 +31,7 @@ liquidTx = async ({ address, asset, amount, feeRate, replaceable, user }) => { }); if (invoice) { - let { ismine } = await lq.getAddressInfo(address); + let { ismine } = await node.getAddressInfo(address); if (ismine) { emit(user.username, "to", invoice.user); return; @@ -33,7 +39,7 @@ liquidTx = async ({ address, asset, amount, feeRate, replaceable, user }) => { } if (config.liquid.walletpass) - await lq.walletPassphrase(config.liquid.walletpass, 300); + await node.walletPassphrase(config.liquid.walletpass, 300); amount = parseInt(amount); @@ -44,9 +50,9 @@ liquidTx = async ({ address, asset, amount, feeRate, replaceable, user }) => { let value = (amount / SATS).toFixed(8); if (feeRate) params.feeRate = (feeRate / SATS).toFixed(8); - let info = await lq.getAddressInfo(address); + let info = await node.getAddressInfo(address); - tx = await lq.createRawTransaction( + tx = await node.createRawTransaction( [], { [address]: (amount / SATS).toFixed(8) @@ -58,12 +64,12 @@ liquidTx = async ({ address, asset, amount, feeRate, replaceable, user }) => { } ); - tx = await lq.fundRawTransaction(tx, params); + tx = await node.fundRawTransaction(tx, params); - let blinded = await lq.blindRawTransaction(tx.hex); - let signed = await lq.signRawTransactionWithWallet(blinded); + let blinded = await node.blindRawTransaction(tx.hex); + let signed = await node.signRawTransactionWithWallet(blinded); - decoded = await lq.decodeRawTransaction(signed.hex); + decoded = await node.decodeRawTransaction(signed.hex); feeRate = Math.round((tx.fee * SATS * 1000) / decoded.vsize); l.info("estimated", asset, feeRate); diff --git a/routes/liquid/receive.js b/routes/liquid/receive.js index f6a78cfa..14fc8b3d 100644 --- a/routes/liquid/receive.js +++ b/routes/liquid/receive.js @@ -308,7 +308,7 @@ setInterval(async () => { ({ account, address, user } = p); if (p && p.account) { - let total = p.amount + p.tip; + total = p.amount + p.tip; p.confirmed = 1; await p.account.save({ transaction }); @@ -344,20 +344,17 @@ setInterval(async () => { let c = convert[address]; if (c) { - let { tx } = c; user.account = account; - delete queue[hash]; - await sendLiquid({ - address, + address: c.address, + amount: total - 100, user, - tx, limit: total }); } } } catch (e) { - l.error("problem processing queued liquid transaction", e.message); + l.error("problem processing queued liquid transaction", e.message, e.stack); } }, 1000); diff --git a/routes/liquid/send.js b/routes/liquid/send.js index b80471b0..55041845 100644 --- a/routes/liquid/send.js +++ b/routes/liquid/send.js @@ -1,7 +1,19 @@ const btc = config.liquid.btcasset; const lcad = config.liquid.cadasset; -sendLiquid = async ({ user, address, memo, tx, limit }) => { +sendLiquid = async ({ asset, amount, user, address, memo, tx, limit }) => { + l.info("sending liquid", amount, user, address); + if (!tx) { + ({ tx } = await liquidTx({ + address, + asset, + amount, + feeRate: 100, + replaceable: false, + user + })); + } + const isChange = async address => (await lq.getAddressInfo(address)).ismine && !Object.keys(addresses).includes(address); @@ -126,6 +138,8 @@ sendLiquid = async ({ user, address, memo, tx, limit }) => { let signed = await lq.signRawTransactionWithWallet(blinded); let txid = await lq.sendRawTransaction(signed.hex); + l.info("sent liquid tx", txid, address); + let main; for (let i = 0; i < assets.length; i++) { p = payments[i]; @@ -150,12 +164,7 @@ module.exports = ah(async (req, res) => { let { user } = req; try { - let { address, feeRate, memo, tx } = req.body; - - if (!tx) { - ({ tx } = await liquidTx({ ...req.body, user })); - } - res.send(await sendLiquid({ user, address, memo, tx })); + res.send(await sendLiquid({...req.body, user })); } catch (e) { l.error("problem sending liquid", user.username, e.message); return res.status(500).send(e); diff --git a/routes/payments.js b/routes/payments.js index 6d758785..e8e94d8a 100644 --- a/routes/payments.js +++ b/routes/payments.js @@ -130,6 +130,7 @@ ah(async () => { if (config.liquid) { lq = new BitcoinCore(config.liquid); + rare = new BitcoinCore(config.rare); app.post("/liquid/broadcast", optionalAuth, require("./liquid/broadcast")); app.get("/liquid/generate", auth, require("./liquid/generate")); app.post("/liquid/fee", auth, require("./liquid/fee")); diff --git a/routes/send.js b/routes/send.js index 36a0de58..579e70b4 100644 --- a/routes/send.js +++ b/routes/send.js @@ -127,12 +127,14 @@ module.exports = ah(async (req, res, next) => { a2 = await db.Account.create(acc, { transaction }); } + let spread = user.username === "coinos" ? 1.02 : 1; + amount = Math.round(amount/spread); let p2 = await db.Payment.create( { amount, account_id: a2.id, user_id: recipient.id, - rate: app.get("rates")[recipient.currency], + rate: app.get("rates")[recipient.currency] * spread, currency: recipient.currency, confirmed: true, hash: "Payment from " + user.username, |
