aboutsummaryrefslogtreecommitdiffstats
path: root/routes/liquid/receive.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2021-01-15 15:31:46 -0800
committerAdam Soltys <[email protected]>2021-01-15 15:31:46 -0800
commitd01159ce9eee956dca32fd69525c0edd5aae9477 (patch)
treee6a4d4a928daffb5bcedbc3411a0dfded6ff422f /routes/liquid/receive.js
parentd03dad4d85ccd107068719c4b68cbd6a255864f6 (diff)
downloadcoinos-server-d01159ce9eee956dca32fd69525c0edd5aae9477.tar.gz
coinos-server-d01159ce9eee956dca32fd69525c0edd5aae9477.zip
add more transactions and locks
Diffstat (limited to 'routes/liquid/receive.js')
-rw-r--r--routes/liquid/receive.js158
1 files changed, 90 insertions, 68 deletions
diff --git a/routes/liquid/receive.js b/routes/liquid/receive.js
index 2976bf57..ce1c6d4b 100644
--- a/routes/liquid/receive.js
+++ b/routes/liquid/receive.js
@@ -12,9 +12,11 @@ const network =
config.liquid.network === "mainnet" ? "liquid" : config.liquid.network
];
-const getAccount = async params => {
+const getAccount = async (params, transaction) => {
let account = await db.Account.findOne({
- where: params
+ where: params,
+ lock: transaction.LOCK.UPDATE,
+ transaction
});
if (account) {
@@ -39,7 +41,9 @@ const getAccount = async params => {
pubkey
},
order: [["id", "ASC"]],
- limit: 1
+ limit: 1,
+ lock: transaction.LOCK.UPDATE,
+ transaction
});
if (existing) {
@@ -51,7 +55,7 @@ const getAccount = async params => {
params = { ...params, ...{ domain, ticker, precision, name } };
params.balance = 0;
params.network = "liquid";
- return db.Account.create(params);
+ return db.Account.create(params, { transaction });
};
const zmqRawBlock = zmq.socket("sub");
@@ -82,78 +86,91 @@ zmqRawTx.on("message", async (topic, message, sequence) => {
Object.keys(addresses).includes(address) &&
!change.includes(address)
) {
- let user = await getUser(addresses[address]);
+ await db.transaction(async transaction => {
+ let user = await getUser(addresses[address], transaction);
- let invoice = await db.Invoice.findOne({
- where: {
- unconfidential: address,
- user_id: user.id,
- network: "liquid"
- },
- order: [["id", "DESC"]],
- include: {
- model: db.Account,
- as: "account"
- }
- });
-
- if (!invoice) return;
+ let invoice = await db.Invoice.findOne({
+ where: {
+ unconfidential: address,
+ user_id: user.id,
+ network: "liquid"
+ },
+ order: [["id", "DESC"]]
+ });
- let confirmed = 0;
+ if (!invoice) return;
- let { account } = invoice;
+ let confirmed = 0;
- if (
- account.asset === asset &&
- (!account.pubkey || account.network === "liquid")
- ) {
- account.pending += value;
- await account.save();
- } else {
- account = await getAccount({
- seed: account.seed,
- path: account.path,
- user_id: user.id,
- asset,
- pubkey: account.pubkey,
- pending: value,
- index: 0
+ let account = await db.Account.findOne({
+ where: {
+ id: invoice.account_id
+ },
+ lock: transaction.LOCK.UPDATE,
+ transaction
});
- }
- if (config.liquid.walletpass)
- await lq.walletPassphrase(config.liquid.walletpass, 300);
+ console.log(account);
+ if (
+ account.asset === asset &&
+ (!account.pubkey || account.network === "liquid")
+ ) {
+ await account.increment({ pending: value }, { transaction });
+ await account.reload({ transaction });
+ } else {
+ account = await getAccount(
+ {
+ seed: account.seed,
+ path: account.path,
+ user_id: user.id,
+ asset,
+ pubkey: account.pubkey,
+ pending: value,
+ index: 0
+ },
+ transaction
+ );
+ }
- await user.save();
+ if (config.liquid.walletpass)
+ await lq.walletPassphrase(config.liquid.walletpass, 300);
- const currency = invoice ? invoice.currency : user.currency;
- const rate = invoice ? invoice.rate : app.get("rates")[user.currency];
- const tip = invoice ? invoice.tip : 0;
- const memo = invoice ? invoice.memo : "";
+ await user.save({ transaction });
- let payment = await db.Payment.create({
- account_id: account.id,
- user_id: user.id,
- hash: blinded.txid,
- amount: value - tip,
- currency,
- memo,
- rate,
- received: true,
- tip,
- confirmed,
- address,
- network: "liquid"
- });
+ const currency = invoice ? invoice.currency : user.currency;
+ const rate = invoice
+ ? invoice.rate
+ : app.get("rates")[user.currency];
+ const tip = invoice ? invoice.tip : 0;
+ const memo = invoice ? invoice.memo : "";
- payments.push(blinded.txid);
- payment = payment.get({ plain: true });
- payment.account = account.get({ plain: true });
+ let payment = await db.Payment.create(
+ {
+ account_id: account.id,
+ user_id: user.id,
+ hash: blinded.txid,
+ amount: value - tip,
+ currency,
+ memo,
+ rate,
+ received: true,
+ tip,
+ confirmed,
+ address,
+ network: "liquid"
+ },
+ { transaction }
+ );
- emit(user.username, "payment", payment);
- emit(user.username, "account", payment.account);
- l.info("liquid detected", address, user.username, asset, value);
- notify(user, `${value} SAT payment detected`);
+ payments.push(blinded.txid);
+ payment = payment.get({ plain: true });
+ payment.account = account.get({ plain: true });
+
+ emit(user.username, "payment", payment);
+ emit(user.username, "account", payment.account);
+ l.info("liquid detected", address, user.username, asset, value);
+ notify(user, `${value} SAT payment detected`);
+ });
}
} catch (e) {
l.error("Problem processing transaction", e.message, e.stack);
@@ -279,10 +296,15 @@ setInterval(async () => {
if (p) {
let total = p.amount + p.tip;
p.confirmed = 1;
- p.account.balance += total;
- p.account.pending -= Math.min(p.account.pending, total);
-
await p.account.save({ transaction });
+
+ await p.account.increment({ balance: total }, { transaction });
+ await p.account.decrement(
+ { pending: Math.min(p.account.pending, total) },
+ { transaction }
+ );
+ await p.account.reload({ transaction });
+
await p.save({ transaction });
p = p.get({ plain: true });