diff options
| -rw-r--r-- | bitcoinPayments.js | 9 | ||||
| -rw-r--r-- | liquidPayments.js | 9 | ||||
| -rw-r--r-- | models/payments.js | 9 | ||||
| -rw-r--r-- | payUser.js | 7 | ||||
| -rw-r--r-- | sendCoins.js | 23 | ||||
| -rw-r--r-- | sendLiquid.js | 36 | ||||
| -rw-r--r-- | sendPayment.js | 50 | ||||
| -rw-r--r-- | sockets.js | 4 |
8 files changed, 87 insertions, 60 deletions
diff --git a/bitcoinPayments.js b/bitcoinPayments.js index d3f2f1c4..803a7192 100644 --- a/bitcoinPayments.js +++ b/bitcoinPayments.js @@ -81,7 +81,7 @@ module.exports = (app, bc, db, addresses, payments, emit) => { hash, amount: o.value, currency: user.currency, - rate: app.get("ask"), + rate: app.get("rates")[user.currency], received: true, tip, confirmed, @@ -131,7 +131,12 @@ module.exports = (app, bc, db, addresses, payments, emit) => { await p.save(); let payments = await db.Payment.findAll({ - where: { user_id: user.id }, + where: { + user_id: user.id, + received: { + [Sequelize.Op.ne]: null + }, + }, order: [['id', 'DESC']], limit: 12 }); diff --git a/liquidPayments.js b/liquidPayments.js index afa33466..dda85232 100644 --- a/liquidPayments.js +++ b/liquidPayments.js @@ -77,7 +77,7 @@ module.exports = (app, db, addresses, payments, emit) => { hash: blinded.txid, amount: value, currency: liquid.currency, - rate: app.get("ask"), + rate: app.get("rates")[user.currency], received: true, tip, confirmed, @@ -131,7 +131,12 @@ module.exports = (app, db, addresses, payments, emit) => { await p.save(); let payments = await db.Payment.findAll({ - where: { user_id: user.id }, + where: { + user_id: user.id, + received: { + [Sequelize.Op.ne]: null + }, + }, order: [['id', 'DESC']], limit: 12 }); diff --git a/models/payments.js b/models/payments.js index 8e4f42c7..28f4912a 100644 --- a/models/payments.js +++ b/models/payments.js @@ -94,6 +94,15 @@ module.exports = sequelize => { comment: null, field: "amount" }, + fee: { + type: DataTypes.DOUBLE, + allowNull: true, + defaultValue: null, + primaryKey: false, + autoIncrement: false, + comment: null, + field: "fee" + }, tip: { type: DataTypes.DOUBLE, allowNull: true, @@ -25,10 +25,11 @@ module.exports = (app, db, lnb) => async (req, res) => { user_id: user.id, hash, amount, - currency: "CAD", - rate: app.get("rates").ask, + rate: app.get("rates")[req.user.currency], + currency: req.user.currency, tip: 0, - confirmed: true + confirmed: true, + received: false, }); req.url = "/sendPayment"; diff --git a/sendCoins.js b/sendCoins.js index 4f0ffd36..8480e72f 100644 --- a/sendCoins.js +++ b/sendCoins.js @@ -7,7 +7,7 @@ const SATS = 100000000; module.exports = (app, bc, db, emit) => async (req, res) => { let { address, amount } = req.body; - let rawtx, hex, fee; + let rawtx, hex, fee, total; try { amount = parseInt(amount); @@ -22,13 +22,14 @@ module.exports = (app, bc, db, emit) => async (req, res) => { ({ hex, fee } = rawtx); fee = parseInt(fee * SATS); + total = amount + fee; } catch (e) { l(e); return res.status(500).send(e.message); } try { - l("sending coins", req.user.username, amount, address); + l("sending coins", req.user.username, amount, fee, address); await db.transaction(async transaction => { let { balance } = await db.User.findOne({ where: { @@ -38,12 +39,12 @@ module.exports = (app, bc, db, emit) => async (req, res) => { transaction }); - if (amount !== balance && amount + fee > balance) { + if (amount !== balance && total > balance) { l("amount exceeds balance", amount, fee, balance); throw new Error("insufficient funds"); } - req.user.balance -= amount + fee; + req.user.balance -= total; await req.user.save({ transaction }); }); } catch (e) { @@ -60,9 +61,6 @@ module.exports = (app, bc, db, emit) => async (req, res) => { let tx = bitcoin.Transaction.fromHex(rawtx); - let total = Math.min(amount + fee, req.user.balance); - if (req.user.balance < 0) req.user.balance = 0; - await db.transaction(async transaction => { await req.user.save({ transaction }); emit(req.user.username, "user", req.user); @@ -70,20 +68,21 @@ module.exports = (app, bc, db, emit) => async (req, res) => { const payment = await db.Payment.create( { amount: -total, + fee, user_id: req.user.id, hash: txid, - rate: app.get("ask"), - currency: "CAD", + rate: app.get("rates")[req.user.currency], + currency: req.user.currency, address, - confirmed: true + confirmed: true, + received: false }, { transaction } ); emit(req.user.username, "payment", payment); + res.send(payment); }); - - res.send({ txid, tx, amount, fees: fee }); } catch (e) { l(e); return res.status(500).send(e.message); diff --git a/sendLiquid.js b/sendLiquid.js index 17fb2a2e..e7d0d066 100644 --- a/sendLiquid.js +++ b/sendLiquid.js @@ -10,7 +10,8 @@ const bc = new BitcoinCore(config.liquid); module.exports = (app, db, emit) => async (req, res) => { let { address, amount } = req.body; - let rawtx, hex, fee; + let rawtx, hex, fee, total; + let { user } = req; try { amount = parseInt(amount); @@ -20,13 +21,14 @@ module.exports = (app, db, emit) => async (req, res) => { }); rawtx = await bc.fundRawTransaction(rawtx, { - subtractFeeFromOutputs: amount === req.user.balance ? [0] : [] + subtractFeeFromOutputs: amount === user.balance ? [0] : [] }); hex = await bc.blindRawTransaction(rawtx.hex); ({ fee } = rawtx); fee = parseInt(fee * SATS); + total = amount + fee; } catch (e) { return res.status(500).send(e.message); } @@ -35,19 +37,19 @@ module.exports = (app, db, emit) => async (req, res) => { await db.transaction(async transaction => { let { balance } = await db.User.findOne({ where: { - username: req.user.username + username: user.username }, lock: transaction.LOCK.UPDATE, transaction }); - if (amount !== balance && amount + fee > balance) { + if (amount !== balance && total > balance) { l("amount exceeds balance", amount, fee, balance); throw new Error("insufficient funds"); } - req.user.balance -= amount + fee; - await req.user.save({ transaction }); + req.user.balance -= total; + await user.save({ transaction }); }); } catch (e) { l(e); @@ -61,30 +63,28 @@ module.exports = (app, db, emit) => async (req, res) => { rawtx = (await bc.signRawTransactionWithWallet(hex)).hex; let txid = await bc.sendRawTransaction(rawtx); - let total = Math.min(amount + fee, req.user.balance); - if (req.user.balance < 0) req.user.balance = 0; - await db.transaction(async transaction => { - await req.user.save({ transaction }); - emit(req.user.username, "user", req.user); + await user.save({ transaction }); + emit(user.username, "user", user); const payment = await db.Payment.create( { amount: -total, - user_id: req.user.id, + fee, + user_id: user.id, hash: txid, - rate: app.get("ask"), - currency: "CAD", + rate: app.get("rates")[user.currency], + currency: user.currency, address, - confirmed: 1 + confirmed: true, + received: false }, { transaction } ); - emit(req.user.username, "payment", payment); + emit(user.username, "payment", payment); + res.send(payment); }); - - res.send({ txid, amount, fees: fee }); } catch (e) { l(e); return res.status(500).send(e.message); diff --git a/sendPayment.js b/sendPayment.js index 2450e6bc..24853605 100644 --- a/sendPayment.js +++ b/sendPayment.js @@ -6,7 +6,9 @@ const l = console.log; module.exports = (app, db, emit, seen, lna, lnb) => async (req, res) => { let hash = req.body.payreq; let payreq = bolt11.decode(hash); - l("sending lightning", req.user.username, payreq.satoshis); + let { user } = req; + + l("sending lightning", user.username, payreq.satoshis); if (seen.includes(hash)) { return res.status(500).send("Invoice has been paid, can't pay again"); @@ -17,7 +19,7 @@ module.exports = (app, db, emit, seen, lna, lnb) => async (req, res) => { let { balance } = await db.User.findOne( { where: { - username: req.user.username + username: user.username } }, { transaction } @@ -27,8 +29,8 @@ module.exports = (app, db, emit, seen, lna, lnb) => async (req, res) => { throw new Error(); } - req.user.balance -= payreq.satoshis; - await req.user.save({ transaction }); + user.balance -= payreq.satoshis; + await user.save({ transaction }); }); } catch (e) { return res.status(500).send("Not enough satoshis"); @@ -45,40 +47,42 @@ module.exports = (app, db, emit, seen, lna, lnb) => async (req, res) => { seen.push(m.payment_preimage); let total = parseInt(m.payment_route.total_amt); - req.user.balance -= total - payreq.satoshis; + let fee = m.payment_route.total_fees; + + user.balance -= total - payreq.satoshis; await db.transaction(async transaction => { - await req.user.save({ transaction }); + await user.save({ transaction }); const payment = await db.Payment.create( { amount: -total, - user_id: req.user.id, + fee, + user_id: user.id, hash, - rate: app.get("ask"), - currency: "CAD", + rate: app.get("rates")[user.currency], + currency: user.currency, confirmed: true }, { transaction } ); - emit(req.user.username, "payment", payment); - }); - - emit(req.user.username, "user", req.user); + emit(user.username, "payment", payment); + emit(user.username, "user", user); - if (payreq.payeeNodeKey === config.lnb.id) { - let invoice = await lna.addInvoice({ value: payreq.satoshis }); - let payback = lnb.sendPayment(lnb.meta, {}); + if (payreq.payeeNodeKey === config.lnb.id) { + let invoice = await lna.addInvoice({ value: payreq.satoshis }); + let payback = lnb.sendPayment(lnb.meta, {}); - /* eslint-disable-next-line */ - let { payment_request } = invoice; - /* eslint-disable-next-line */ - payback.write({ payment_request }); - } + /* eslint-disable-next-line */ + let { payment_request } = invoice; + /* eslint-disable-next-line */ + payback.write({ payment_request }); + } - seen.push(hash); - res.send(m); + seen.push(hash); + res.send(payment); + }); } }); @@ -1,6 +1,7 @@ const jwt = require("jsonwebtoken"); const io = require("socket.io"); const whitelist = require("./whitelist"); +const Sequelize = require("sequelize"); /* eslint-disable-next-line */ const pick = (O, ...K) => K.reduce((o, k) => ((o[k] = O[k]), o), {}); @@ -47,6 +48,9 @@ module.exports = (app, db, server) => { model: db.Payment, as: "payments", order: [["id", "DESC"]], + received: { + [Sequelize.Op.ne]: null + }, limit: 12 } ], |
