aboutsummaryrefslogtreecommitdiffstats
path: root/routes
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2020-09-08 07:26:14 -0700
committerAdam Soltys <[email protected]>2020-09-08 07:26:14 -0700
commitb5607df482440b55bd3e802f5bea4ead95e6491e (patch)
tree6b8948fbb703c1feaca1aca10f3ac86c8062768b /routes
parente40e68f49b10d6ef70546f7128bad891256525c0 (diff)
downloadcoinos-server-b5607df482440b55bd3e802f5bea4ead95e6491e.tar.gz
coinos-server-b5607df482440b55bd3e802f5bea4ead95e6491e.zip
bump account index
Diffstat (limited to 'routes')
-rw-r--r--routes/bitcoin/receive.js54
-rw-r--r--routes/bitcoin/send.js5
-rw-r--r--routes/liquid/fee.js1
-rw-r--r--routes/send.js18
-rw-r--r--routes/users.js12
5 files changed, 47 insertions, 43 deletions
diff --git a/routes/bitcoin/receive.js b/routes/bitcoin/receive.js
index 987f5079..63a34760 100644
--- a/routes/bitcoin/receive.js
+++ b/routes/bitcoin/receive.js
@@ -26,7 +26,7 @@ zmqRawTx.on("message", async (topic, message, sequence) => {
if (payments.includes(hash)) return;
Promise.all(
- tx.outs.map(async (o) => {
+ tx.outs.map(async o => {
const { value } = o;
let address;
@@ -44,18 +44,24 @@ zmqRawTx.on("message", async (topic, message, sequence) => {
let user = await db.User.findOne({
where: {
- username: addresses[address],
- },
+ username: addresses[address]
+ }
});
const invoice = await db.Invoice.findOne({
where: {
user_id: user.id,
- network: "BTC",
+ network: "BTC"
},
order: [["id", "DESC"]],
+ include: {
+ model: db.Account,
+ as: "account",
+ },
});
+ if (!invoice) return;
+
const currency = invoice ? invoice.currency : user.currency;
const rate = invoice ? invoice.rate : app.get("rates")[user.currency];
const tip = invoice ? invoice.tip : 0;
@@ -63,27 +69,7 @@ zmqRawTx.on("message", async (topic, message, sequence) => {
let confirmed = false;
- const accounts = await db.Account.findAll({
- where: {
- user_id: user.id,
- asset: config.liquid.btcasset,
- },
- });
-
- let account = accounts.find(({ index, pubkey }) => {
- const { payments } = bitcoin;
- index &&
- pubkey &&
- address ===
- payments.p2wpkh({
- pubkey: fromBase58(pubkey, network).derivePath(
- `${index}`
- ).publicKey,
- network,
- }).address;
- });
-
- if (!account) account = accounts[0];
+ let { account } = invoice;
account.pending += value;
await account.save();
@@ -120,7 +106,7 @@ zmqRawTx.on("message", async (topic, message, sequence) => {
tip,
confirmed,
address,
- network: "BTC",
+ network: "BTC"
});
payment = payment.get({ plain: true });
payment.account = account.get({ plain: true });
@@ -138,13 +124,13 @@ let queue = {};
zmqRawBlock.on("message", async (topic, message, sequence) => {
const payments = await db.Payment.findAll({
- where: { confirmed: false },
+ where: { confirmed: false }
});
let block = bitcoin.Block.fromHex(message.toString("hex"));
- block.transactions.map((tx) => {
+ block.transactions.map(tx => {
let hash = reverse(tx.getHash()).toString("hex");
- if (payments.find((p) => p.hash === hash)) queue[hash] = 1;
+ if (payments.find(p => p.hash === hash)) queue[hash] = 1;
});
});
@@ -154,21 +140,21 @@ setInterval(async () => {
for (let i = 0; i < arr.length; i++) {
const hash = arr[i];
- await db.transaction(async (transaction) => {
+ await db.transaction(async transaction => {
let p = await db.Payment.findOne({
where: { hash, confirmed: 0, received: 1 },
include: [
{
model: db.Account,
- as: "account",
+ as: "account"
},
{
model: db.User,
- as: "user",
- },
+ as: "user"
+ }
],
lock: transaction.LOCK.UPDATE,
- transaction,
+ transaction
});
const { user } = p;
diff --git a/routes/bitcoin/send.js b/routes/bitcoin/send.js
index 8745c3dd..5d22152e 100644
--- a/routes/bitcoin/send.js
+++ b/routes/bitcoin/send.js
@@ -1,4 +1,5 @@
const bitcoin = require("bitcoinjs-lib");
+const btc = config.liquid.btcasset;
module.exports = ah(async (req, res) => {
let { user } = req;
@@ -34,11 +35,11 @@ module.exports = ah(async (req, res) => {
let params;
try {
await db.transaction(async transaction => {
- if (account.asset !== btcasset) {
+ if (account.asset !== btc) {
account = await db.Account.findOne({
where: {
user_id: user.id,
- asset: config.liquid.btcasset
+ asset: btc,
},
lock: transaction.LOCK.UPDATE,
transaction
diff --git a/routes/liquid/fee.js b/routes/liquid/fee.js
index acf28160..6bf52afc 100644
--- a/routes/liquid/fee.js
+++ b/routes/liquid/fee.js
@@ -10,7 +10,6 @@ module.exports = ah(async (req, res) => {
let psbt = await buildTx({ address, asset, amount, feeRate, user });
return res.send(psbt);
} catch(e) {
- throw e;
return res.status(500).send(e.message);
}
}
diff --git a/routes/send.js b/routes/send.js
index 616ac137..cafbaed8 100644
--- a/routes/send.js
+++ b/routes/send.js
@@ -63,6 +63,23 @@ module.exports = ah(async (req, res, next) => {
where: { username },
});
+
+ const invoice = await db.Invoice.findOne({
+ where: {
+ user_id: user.id,
+ network: "BTC"
+ },
+ order: [["id", "DESC"]],
+ include: {
+ model: db.Account,
+ as: "account",
+ },
+ });
+
+ if (invoice) ({ account } = invoice);
+ else if (user.account.asset === asset) ({ account } = user);
+ else {
+
let params = {
user_id: user.id,
asset,
@@ -73,6 +90,7 @@ module.exports = ah(async (req, res, next) => {
lock: transaction.LOCK.UPDATE,
transaction,
});
+ }
if (account) {
account.balance += amount;
diff --git a/routes/users.js b/routes/users.js
index 6ffed55b..c7b6bfa4 100644
--- a/routes/users.js
+++ b/routes/users.js
@@ -251,7 +251,7 @@ app.post(
auth,
ah(async (req, res) => {
const { id } = req.body;
- const account = await db.Account.findOne({ where: { id }});
+ const account = await db.Account.findOne({ where: { id } });
if (account) await account.destroy();
res.end();
})
@@ -279,11 +279,11 @@ app.post(
emit(user.username, "account", account);
- if (pubkey) {
- user.index++;
- await user.save();
- emit(user.username, "user", user);
- }
+ if (pubkey) user.index++;
+ user.account_id = account.id;
+ await user.save();
+ user.account = account;
+ emit(user.username, "user", user);
res.send(account);
})