diff options
| author | Adam Soltys <[email protected]> | 2020-04-27 11:14:43 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2020-04-28 22:33:02 -0700 |
| commit | e166ab2c743e4c66eacb51c6484711c5965dd090 (patch) | |
| tree | a3b309cb38ceceb6e581963f0001a6869452b531 | |
| parent | fae6e8b55ea12989d688eb777c002222199c169b (diff) | |
| download | coinos-server-e166ab2c743e4c66eacb51c6484711c5965dd090.tar.gz coinos-server-e166ab2c743e4c66eacb51c6484711c5965dd090.zip | |
select payments from current account
| -rw-r--r-- | db/models/accounts.js | 2 | ||||
| -rw-r--r-- | lib/passport.js | 4 | ||||
| -rw-r--r-- | lib/sockets.js | 1 | ||||
| -rw-r--r-- | lib/utils.js | 81 | ||||
| -rw-r--r-- | routes/invoices.js | 2 | ||||
| -rw-r--r-- | routes/lightning/receive.js | 1 | ||||
| -rw-r--r-- | routes/liquid/receive.js | 39 | ||||
| -rw-r--r-- | routes/users.js | 22 |
8 files changed, 92 insertions, 60 deletions
diff --git a/db/models/accounts.js b/db/models/accounts.js index a4b38992..6bc9c955 100644 --- a/db/models/accounts.js +++ b/db/models/accounts.js @@ -83,7 +83,7 @@ const attributes = { field: "pending" }, precision: { - type: DataTypes.DOUBLE, + type: DataTypes.INTEGER(11), allowNull: true, defaultValue: null, primaryKey: false, diff --git a/lib/passport.js b/lib/passport.js index e0c2dd62..1d80d9b8 100644 --- a/lib/passport.js +++ b/lib/passport.js @@ -16,8 +16,9 @@ passport = Passport.use( secretOrKey: config.jwt }, async (payload, next) => { + const { username } = payload; try { - next(null, await getUser(payload.username)); + next(null, await db.User.findOne({ where: { username } })); } catch (e) { l.error("error finding user for session", e); next(null, { username: "guest" }); @@ -28,4 +29,3 @@ passport = Passport.use( auth = passport.authenticate("jwt", { session: false }); app.use(passport.initialize()); - diff --git a/lib/sockets.js b/lib/sockets.js index 9a5457c3..fe93bb9f 100644 --- a/lib/sockets.js +++ b/lib/sockets.js @@ -36,7 +36,6 @@ socket.sockets.on("connect", async socket => { socket.emit("connected"); if (app.get("rates")) socket.emit("rate", app.get("rates").ask); socket.on("getuser", async (data, callback) => { - l.info("socket open", socket.request.user); callback(await getUser(socket.request.user)); }); diff --git a/lib/utils.js b/lib/utils.js index 2f0214d5..58bf29f9 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,27 +1,10 @@ -const { Op } = require("sequelize"); +const sequelize = require("sequelize"); +const { Op } = sequelize; getUser = async username => { - return db.User.findOne({ + let user = await db.User.findOne({ include: [ { - model: db.Payment, - as: "payments", - order: [["id", "DESC"]], - where: { - [Op.or]: { - received: true, - amount: { - [Op.lt]: 0 - } - } - }, - limit: 12, - include: { - model: db.Account, - as: "account" - } - }, - { model: db.Account, as: "accounts" }, @@ -32,30 +15,31 @@ getUser = async username => { ], where: { username } }); + + user = user.get({ plain: true }); + + user.payments = await db.Payment.findAll({ + order: [["id", "DESC"]], + where: { + user_id: user.id, + account_id: user.account.id, + [Op.or]: { + received: true, + amount: { + [Op.lt]: 0 + } + } + }, + limit: 12 + }); + + return user; }; getUserById = async id => { - return db.User.findOne({ + let user = db.User.findOne({ include: [ { - model: db.Payment, - as: "payments", - order: [["id", "DESC"]], - where: { - [Op.or]: { - received: true, - amount: { - [Op.lt]: 0 - } - } - }, - limit: 12, - include: { - model: db.Account, - as: "account" - } - }, - { model: db.Account, as: "accounts" }, @@ -66,4 +50,23 @@ getUserById = async id => { ], where: { id } }); + + user = user.get({ plain: true }); + + user.payments = await db.Payment.findAll({ + order: [["id", "DESC"]], + where: { + user_id: user.id, + account_id: user.account.id, + [Op.or]: { + received: true, + amount: { + [Op.lt]: 0 + } + } + }, + limit: 12 + }); + + return user; }; diff --git a/routes/invoices.js b/routes/invoices.js index 50f578ac..651863d6 100644 --- a/routes/invoices.js +++ b/routes/invoices.js @@ -3,7 +3,7 @@ const { Op } = require("sequelize"); app.post("/invoice", auth, async (req, res) => { const { invoice } = req.body; invoice.user_id = req.user.id; - l.info("creating invoice", req.user.username, invoice.asset, invoice.amount, invoice.tip, invoice.currency, invoice.rate.toFixed(2)); + l.info("creating invoice", req.user.username, invoice.network, invoice.amount, invoice.tip, invoice.currency, invoice.rate.toFixed(2)); const exists = await db.Invoice.findOne({ where: { diff --git a/routes/lightning/receive.js b/routes/lightning/receive.js index c300d8ea..3083eca7 100644 --- a/routes/lightning/receive.js +++ b/routes/lightning/receive.js @@ -9,7 +9,6 @@ const handlePayment = async msg => { } }); - if (!invoice) return; const { text: hash, currency, rate, tip, user_id } = invoice; diff --git a/routes/liquid/receive.js b/routes/liquid/receive.js index 5d7eb237..858cc0ed 100644 --- a/routes/liquid/receive.js +++ b/routes/liquid/receive.js @@ -34,17 +34,6 @@ zmqRawTx.on("message", async (topic, message, sequence) => { } }); - const invoice = await db.Invoice.findOne({ - where: { - user_id: user.id, - asset - }, - order: [["id", "DESC"]] - }); - - const currency = invoice ? invoice.currency : user.currency; - const rate = invoice ? invoice.rate : app.get("rates")[user.currency]; - const tip = invoice ? invoice.tip : null; let confirmed = 0; @@ -61,13 +50,17 @@ zmqRawTx.on("message", async (topic, message, sequence) => { account.pending += value; await account.save(); } else { - let assets = await axios.get("https://assets.blockstream.info/"); + let name = asset.substr(0, 6); + let ticker = asset.substr(0, 3); + let precision = 8; + + const assets = await axios.get("https://assets.blockstream.info/"); if (assets[asset]) { - let { ticker, precision, name } = assets[asset]; - params = { ...params, ...{ ticker, precision, name } }; + ({ticker, precision, name } = assets[asset]); } + params = { ...params, ...{ ticker, precision, name } }; params.balance = 0; params.pending = value; account = await db.Account.create(params); @@ -84,6 +77,23 @@ zmqRawTx.on("message", async (topic, message, sequence) => { addresses[user.liquid] = user.username; + l.info("account", account.id); + + let invoice; + if (asset === config.liquid.btcasset) { + invoice = await db.Invoice.findOne({ + where: { + user_id: user.id, + network: 'LBTC', + }, + order: [["id", "DESC"]] + }); + } + + const currency = invoice ? invoice.currency : user.currency; + const rate = invoice ? invoice.rate : app.get("rates")[user.currency]; + const tip = invoice ? invoice.tip : null; + const payment = await db.Payment.create({ account_id: account.id, user_id: user.id, @@ -97,6 +107,7 @@ zmqRawTx.on("message", async (topic, message, sequence) => { address, network: "LBTC" }); + payments.push(blinded.txid); l.info("liquid detected", user.username, asset, value); diff --git a/routes/users.js b/routes/users.js index cc7b62cc..d77cd8a0 100644 --- a/routes/users.js +++ b/routes/users.js @@ -46,7 +46,10 @@ app.get("/liquidate", async (req, res) => { user_id: user.id, asset: config.liquid.btcasset, balance: user.balance, - pending: 0 + pending: 0, + ticker: 'BTC', + name: 'Bitcoin', + precision: 8, }); } } @@ -212,6 +215,7 @@ app.post("/user", auth, async (req, res) => { } await user.save(); + user = await getUser(user.username); emit(user.username, "user", user); res.send({ user, token }); } catch (e) { @@ -360,4 +364,20 @@ app.post("/address", auth, async (req, res) => { res.send(user.address); }); +app.post("/shiftAccount", auth, async (req, res) => { + let { user } = req; + let { asset } = req.body; + + const account = await db.Account.findOne({ + where: { user_id: user.id, asset } + }); + + user.account_id = account.id; + await user.save(); + + user = await getUser(user.username); + emit(user.username, "user", user); + res.send(account); +}); + setInterval(() => (faucet = 2000), DAY); |
