diff options
Diffstat (limited to 'routes')
| -rw-r--r-- | routes/balances.js | 13 | ||||
| -rw-r--r-- | routes/bitcoin/receive.js | 3 | ||||
| -rw-r--r-- | routes/lightning/receive.js | 32 | ||||
| -rw-r--r-- | routes/lightning/send.js | 36 | ||||
| -rw-r--r-- | routes/liquid/receive.js | 80 | ||||
| -rw-r--r-- | routes/liquid/send.js | 1 | ||||
| -rw-r--r-- | routes/users.js | 22 |
7 files changed, 102 insertions, 85 deletions
diff --git a/routes/balances.js b/routes/balances.js index eff361ea..86658ed3 100644 --- a/routes/balances.js +++ b/routes/balances.js @@ -9,11 +9,6 @@ app.get("/balances", async (req, res) => { [sequelize.fn("sum", sequelize.col("balance")), "total"] ], group: ["asset"], - where: { - asset: { - [Op.ne]: config.liquid.btcasset - } - } }); const balances = { @@ -23,13 +18,6 @@ app.get("/balances", async (req, res) => { liquid: parseInt(assets.bitcoin * SATS), lnchannel: parseInt((await lna.channelBalance({})).balance), lnwallet: parseInt((await lna.walletBalance({})).total_balance), - user: parseInt( - (await db["User"].findAll({ - attributes: [[sequelize.fn("sum", sequelize.col("balance")), "total"]], - raw: true, - order: sequelize.literal("total DESC") - }))[0].total - ) }; const { bitcoin, liquid, lnchannel, user, lnwallet } = balances; @@ -42,6 +30,5 @@ app.get("/balances", async (req, res) => { balances.ratio = (balances.total / parseInt(user)).toFixed(2); - res.send(balances); }); diff --git a/routes/bitcoin/receive.js b/routes/bitcoin/receive.js index 3083c49c..ff93df16 100644 --- a/routes/bitcoin/receive.js +++ b/routes/bitcoin/receive.js @@ -109,13 +109,12 @@ setInterval(async () => { let hash = arr[i]; let p = await db.Payment.findOne({ - include: [{ model: db.User, as: "user" }], where: { hash, confirmed: 0 } }); p.confirmed = 1; - let user = await p.getUser(); + const user = await getUserById(p.user_id); user.balance += p.amount + p.tip; user.pending -= Math.min(user.pending, p.amount + p.tip); emit(user.username, "user", user); diff --git a/routes/lightning/receive.js b/routes/lightning/receive.js index 6ac4c947..c300d8ea 100644 --- a/routes/lightning/receive.js +++ b/routes/lightning/receive.js @@ -4,19 +4,29 @@ const handlePayment = async msg => { if (!msg.settled) return; const invoice = await db.Invoice.findOne({ - include: { model: db.User, as: "user" }, where: { text: msg.payment_request } }); + if (!invoice) return; - const { text: hash, currency, rate, tip, user } = invoice; + const { text: hash, currency, rate, tip, user_id } = invoice; const amount = parseInt(msg.value) - tip; + let user = await getUserById(user_id); + + const account = await db.Account.findOne({ + where: { + user_id, + asset: config.liquid.btcasset + } + }); + const payment = await db.Payment.create({ - user_id: user.id, + account_id: account.id, + user_id, hash, amount, currency, @@ -24,17 +34,27 @@ const handlePayment = async msg => { received: true, confirmed: true, asset, - tip, + tip }); invoice.received += amount + tip; - user.balance += amount + tip; + account.balance += amount + tip; + await account.save(); await invoice.save(); await payment.save(); await user.save(); + payments.push(msg.payment_request); - l.info("lightning payment received", user.username, payment.amount, payment.tip); + l.info( + "lightning payment received", + user.username, + payment.amount, + payment.tip + ); + + user = await getUser(user.username) + l.info(user.account.balance); emit(user.username, "payment", payment); emit(user.username, "user", user); diff --git a/routes/lightning/send.js b/routes/lightning/send.js index 822fe0e4..c2551896 100644 --- a/routes/lightning/send.js +++ b/routes/lightning/send.js @@ -32,50 +32,63 @@ module.exports = async (req, res) => { await user.save({ transaction }); }); } catch (e) { - l.warn("insufficient funds for lightning payment", user.username, user.balance); + l.warn( + "insufficient funds for lightning payment", + user.username, + user.balance + ); return res.status(500).send("Not enough satoshis"); } let m; try { - let paymentHash = payreq.tags.find(t => t.tagName === 'payment_hash').data; + let paymentHash = payreq.tags.find(t => t.tagName === "payment_hash").data; m = await lna.sendPaymentSync({ - "payment_request": hash, + payment_request: hash }); - + if (m.payment_error) return res.status(500).send(m.payment_error); if (seen.includes(m.payment_preimage)) { l.warn("duplicate payment detected", m.payment_preimage); return res.status(500).send("duplicate payment detected"); - } + } let total = parseInt(m.payment_route.total_amt); let fee = m.payment_route.total_fees; user.balance -= total - payreq.satoshis; - await db.transaction(async transaction => { - await user.save({ transaction }); + await user.save(); + + const account = await db.Account.findOne({ + where: { + user_id: user.id, + asset: config.liquid.btcasset + } + }); const payment = await db.Payment.create( { amount: -total, - fee, + account_id: account.id, user_id: user.id, hash, rate: app.get("rates")[user.currency], currency: user.currency, confirmed: true, - asset: 'LNBTC', - }, - { transaction } + asset: "LNBTC" + } ); + user = await getUser(user.username); + seen.push(m.payment_preimage); l.info("sent lightning", user.username, -payment.amount); emit(user.username, "payment", payment); + + l.info("user payments", user.payments); emit(user.username, "user", user); if (payreq.payeeNodeKey === config.lnb.id) { @@ -91,7 +104,6 @@ module.exports = async (req, res) => { seen.push(hash); res.send(payment); - }); } catch (e) { l.info("error sending lightning", e); return res.status(500).send(e); diff --git a/routes/liquid/receive.js b/routes/liquid/receive.js index 24d17a35..8d2d6ca8 100644 --- a/routes/liquid/receive.js +++ b/routes/liquid/receive.js @@ -1,3 +1,4 @@ +const axios = require("axios"); const reverse = require("buffer-reverse"); const zmq = require("zeromq"); const { Op } = require("sequelize"); @@ -47,27 +48,26 @@ zmqRawTx.on("message", async (topic, message, sequence) => { let confirmed = 0; - if (asset === config.liquid.btcasset) { - user.pending += value; - } else { - const params = { - user_id: user.id, - asset, - }; + const params = { + user_id: user.id, + asset, + }; - const account = await db.Account.findOne({ - where: params - }); + const account = await db.Account.findOne({ + where: params + }); - if (account) { - account.pending += value; - await account.save(); - } else { - params.balance = 0; - params.pending = value; - l.info("creating", params); - await db.Account.create(params); - } + if (account) { + account.pending += value; + await account.save(); + } else { + let assets = await axios.get("https://assets.blockstream.info/"); + let { ticker, precision, name } = assets[asset]; + params = { ...params, ...{ticker, precision, name }}; + + params.balance = 0; + params.pending = value; + account = await db.Account.create(params); } user.confidential = await lq.getNewAddress(); @@ -81,6 +81,7 @@ zmqRawTx.on("message", async (topic, message, sequence) => { addresses[user.liquid] = user.username; const payment = await db.Payment.create({ + account_id: account.id, user_id: user.id, hash: blinded.txid, amount: value - tip, @@ -127,47 +128,26 @@ setInterval(async () => { let p = await db.Payment.findOne({ where: { hash, confirmed: 0, received: 1 } + include: { + model: db.Account, + as: "account", + }, }); - const user = await getUser(p.user_id); - p.confirmed = 1; - const asset = await db.Account.findOne({ - where: { - user_id: user.id, - asset: p.asset - } - }); - - l.info("asset", p.asset); - if (p.asset === config.liquid.btcasset) { - user.balance += p.amount + p.tip; - user.pending -= Math.min(user.pending, p.amount + p.tip); - } else { - asset.balance += p.amount + p.tip; - asset.pending -= Math.min(user.pending, p.amount + p.tip); - await asset.save(); - } + l.info("account", p.account); + p.account.balance += p.amount + p.tip; + p.account.pending -= Math.min(p.account.pending, p.amount + p.tip); + await p.account.save(); l.info("liquid confirmed", user.username, p.asset, p.amount, p.tip); - emit(user.username, "user", user); - await user.save(); await p.save(); - let payments = await db.Payment.findAll({ - where: { - user_id: user.id, - received: { - [Op.ne]: null - } - }, - order: [["id", "DESC"]], - limit: 12 - }); + user = await getUser(user.username); + emit(user.username, "user", user); - emit(user.username, "payments", payments); delete queue[hash]; } }, 1000); diff --git a/routes/liquid/send.js b/routes/liquid/send.js index e1a7e473..b0660929 100644 --- a/routes/liquid/send.js +++ b/routes/liquid/send.js @@ -15,6 +15,7 @@ module.exports = async (req, res) => { const unblinded = await lq.unblindRawTransaction(hex); tx = await lq.decodeRawTransaction(unblinded.hex); + l.info("decoded", tx); let total = 0; let change = 0; diff --git a/routes/users.js b/routes/users.js index 1d9a4f3d..b1e83a4c 100644 --- a/routes/users.js +++ b/routes/users.js @@ -4,7 +4,6 @@ const jwt = require("jsonwebtoken"); const uuidv4 = require("uuid/v4"); const fb = "https://graph.facebook.com/"; const authenticator = require("otplib").authenticator; -const { getUser } = require("../lib/utils"); const pick = (O, ...K) => K.reduce((o, k) => ((o[k] = O[k]), o), {}); let faucet = 1000; @@ -55,10 +54,16 @@ app.get("/liquidate", async (req, res) => { }); const gift = async user => { + const account = user.accounts[0]; + if (faucet > 0) { - user.balance = 100; faucet -= 100; + + account.balance = 100; + await account.save(); + const payment = await db.Payment.create({ + account_id: account.id, user_id: user.id, hash: "Welcome Gift", amount: 100, @@ -68,6 +73,7 @@ const gift = async user => { confirmed: 1, asset: "GIFT" }); + await user.save(); } }; @@ -109,8 +115,20 @@ app.post("/register", async (req, res) => { addresses[user.address] = user.username; addresses[user.liquid] = user.username; user = await db.User.create(user); + + let account = await db.Account.create({ + user_id: user.id, + asset: config.liquid.btcasset, + balance: 0, + pending: 0, + }); + + user.accounts = [account]; await gift(user); + user.account_id = account.id; + await user.save(); + l.info("new user", user.username); res.send(pick(user, ...whitelist)); }); |
