aboutsummaryrefslogtreecommitdiffstats
path: root/routes/liquid
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2020-04-24 22:46:45 -0700
committerAdam Soltys <[email protected]>2020-04-28 22:33:02 -0700
commitfae6e8b55ea12989d688eb777c002222199c169b (patch)
tree3efed4974408fc1bc8911aa9fb9fc657456d69ee /routes/liquid
parent306357c24d3014b55ae1cd25b954a695ea2e5cc6 (diff)
downloadcoinos-server-fae6e8b55ea12989d688eb777c002222199c169b.tar.gz
coinos-server-fae6e8b55ea12989d688eb777c002222199c169b.zip
improved assets support
Diffstat (limited to 'routes/liquid')
-rw-r--r--routes/liquid/receive.js89
-rw-r--r--routes/liquid/send.js148
2 files changed, 113 insertions, 124 deletions
diff --git a/routes/liquid/receive.js b/routes/liquid/receive.js
index 24d17a35..5d7eb237 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,29 @@ 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,
- };
+ let 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/");
+
+ if (assets[asset]) {
+ 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();
@@ -76,11 +79,13 @@ zmqRawTx.on("message", async (topic, message, sequence) => {
)).unconfidential;
await user.save();
+ user = await getUser(user.username);
emit(user.username, "user", user);
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,
@@ -90,7 +95,7 @@ zmqRawTx.on("message", async (topic, message, sequence) => {
tip,
confirmed,
address,
- asset
+ network: "LBTC"
});
payments.push(blinded.txid);
@@ -126,48 +131,24 @@ setInterval(async () => {
let hash = arr[i];
let p = await db.Payment.findOne({
- where: { hash, confirmed: 0, received: 1 }
- });
-
- const user = await getUser(p.user_id);
-
- p.confirmed = 1;
-
- const asset = await db.Account.findOne({
- where: {
- user_id: user.id,
- asset: p.asset
+ where: { hash, confirmed: 0, received: 1 },
+ include: {
+ model: db.Account,
+ as: "account"
}
});
- 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("liquid confirmed", user.username, p.asset, p.amount, p.tip);
- emit(user.username, "user", user);
+ p.confirmed = 1;
+ p.account.balance += p.amount + p.tip;
+ p.account.pending -= Math.min(p.account.pending, p.amount + p.tip);
- await user.save();
+ await p.account.save();
await p.save();
- let payments = await db.Payment.findAll({
- where: {
- user_id: user.id,
- received: {
- [Op.ne]: null
- }
- },
- order: [["id", "DESC"]],
- limit: 12
- });
+ let user = await getUserById(p.user_id);
+ emit(user.username, "user", user);
+ l.info("liquid confirmed", user.username, p.asset, p.amount, p.tip);
- emit(user.username, "payments", payments);
delete queue[hash];
}
}, 1000);
diff --git a/routes/liquid/send.js b/routes/liquid/send.js
index e1a7e473..461b0c0a 100644
--- a/routes/liquid/send.js
+++ b/routes/liquid/send.js
@@ -4,96 +4,104 @@ module.exports = async (req, res) => {
let { user } = req;
let {
address,
- asset,
tx: { hex }
} = req.body;
- const isChange = async address =>
- ((await lq.getAddressInfo(address)).ismine) &&
- (!Object.keys(addresses).includes(address) ||
- address === user.liquid);
+ const isChange = async address =>
+ (await lq.getAddressInfo(address)).ismine &&
+ (!Object.keys(addresses).includes(address) || address === user.liquid);
const unblinded = await lq.unblindRawTransaction(hex);
tx = await lq.decodeRawTransaction(unblinded.hex);
-
- let total = 0;
- let change = 0;
+
+ let totals = {};
+ let change = {};
let fee = 0;
for (let i = 0; i < tx.vout.length; i++) {
- let o = tx.vout[i];
- total += toSats(o.value);
- if (o.scriptPubKey.type === 'fee') fee = toSats(o.value);
-
- if (o.scriptPubKey.addresses) {
- if ((await isChange(o.scriptPubKey.addresses[0]))) {
- change += toSats(o.value);
+ let {
+ asset,
+ value,
+ scriptPubKey: { type, addresses }
+ } = tx.vout[i];
+
+ if (!totals[asset]) totals[asset] = change[asset] = 0;
+ totals[asset] += toSats(value);
+ if (type === "fee") fee = toSats(value);
+
+ if (addresses) {
+ if (await isChange(addresses[0])) {
+ change[asset] += toSats(value);
}
}
- }
+ }
- total = total - change;
- let amount = total - fee;
+ let assets = Object.keys(totals);
+ for (let i = 0; i < assets.length; i++) {
+ let asset = assets[i];
+ let total = totals[asset];
+ if (change[asset]) total -= change[asset];
- l.info("attempting liquid payment", user.username, amount, fee);
+ l.info("attempting liquid payment", user.username, asset, total, fee);
- try {
- await db.transaction(async transaction => {
- let { balance } = await db.User.findOne({
- where: {
- username: user.username
- },
- lock: transaction.LOCK.UPDATE,
- transaction
- });
+ try {
+ await db.transaction(async transaction => {
+ let account = await db.Account.findOne({
+ where: {
+ user_id: user.id,
+ asset
+ },
+ lock: transaction.LOCK.UPDATE,
+ transaction
+ });
- if (amount !== balance && total > balance) {
- l.warn("amount exceeds balance", { amount, fee, balance });
- throw new Error("insufficient funds");
- }
+ if (total > account.balance) {
+ l.warn("amount exceeds balance", {
+ total,
+ fee,
+ balance: account.balance
+ });
+ throw new Error("insufficient funds");
+ }
- req.user.balance -= total;
- await user.save({ transaction });
- });
- } catch (e) {
- l.warn("insufficient funds for liquid payment", user.username);
- return res.status(500).send("Not enough satoshis");
- }
+ account.balance -= total;
+ await account.save({ transaction });
- try {
- if (config.liquid.walletpass)
- await lq.walletPassphrase(config.liquid.walletpass, 300);
+ try {
+ if (config.liquid.walletpass)
+ await lq.walletPassphrase(config.liquid.walletpass, 300);
- hex = await lq.blindRawTransaction(hex);
- rawtx = (await lq.signRawTransactionWithWallet(hex)).hex;
- let txid = await lq.sendRawTransaction(rawtx);
+ hex = await lq.blindRawTransaction(hex);
+ rawtx = (await lq.signRawTransactionWithWallet(hex)).hex;
+ let txid = await lq.sendRawTransaction(rawtx);
- await db.transaction(async transaction => {
- await user.save({ transaction });
- emit(user.username, "user", user);
+ const payment = await db.Payment.create({
+ amount: -total,
+ account_id: account.id,
+ fee,
+ user_id: user.id,
+ hash: txid,
+ rate: app.get("rates")[user.currency],
+ currency: user.currency,
+ address,
+ confirmed: true,
+ received: false,
+ network: "LBTC"
+ }, { transaction });
- const payment = await db.Payment.create(
- {
- amount: -total,
- fee,
- user_id: user.id,
- hash: txid,
- rate: app.get("rates")[user.currency],
- currency: user.currency,
- address,
- confirmed: true,
- received: false,
- asset: 'LBTC'
- },
- { transaction }
- );
+ payment.account = account;
- l.info("sent liquid", user.username, total);
- emit(user.username, "payment", payment);
- res.send(payment);
- });
- } catch (e) {
- l.error("liquid send failed", e);
- return res.status(500).send(e.message);
+ user = await getUser(user.username);
+ emit(user.username, "user", user);
+ res.send(payment);
+ } catch (e) {
+ l.error("liquid send failed", e);
+ return res.status(500).send(e.message);
+ }
+ });
+ } catch (e) {
+ l.warn("insufficient funds for liquid payment", user.username, e.message);
+ return res.status(500).send("Not enough satoshis");
+ }
}
};