aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2021-12-17 02:15:14 -0500
committerAdam Soltys <[email protected]>2021-12-17 02:15:14 -0500
commit75bd92372f9649fecd6159f2ba9839efc5061cd6 (patch)
tree6b511dd0b6ff405d4dcc8a262d8f527f3696d647
parentcced852e8df945b95deb332cc1162522d9c67137 (diff)
downloadcoinos-server-75bd92372f9649fecd6159f2ba9839efc5061cd6.tar.gz
coinos-server-75bd92372f9649fecd6159f2ba9839efc5061cd6.zip
use current account when possible
-rw-r--r--lib/send.js71
-rw-r--r--routes/send.js54
2 files changed, 81 insertions, 44 deletions
diff --git a/lib/send.js b/lib/send.js
index e08e9033..20b4c319 100644
--- a/lib/send.js
+++ b/lib/send.js
@@ -22,18 +22,28 @@ send = async (amount, memo, hash, user) => {
let error, fee, total, preimage, payment;
await db.transaction(async transaction => {
- let account = await db.Account.findOne({
- where: {
- user_id: user.id,
- asset: config.liquid.btcasset,
- pubkey: null,
- balance: {
- [Op.gte]: amount
- }
- },
- lock: transaction.LOCK.UPDATE,
- transaction
- });
+ let account;
+ if (user.account.asset === config.liquid.btcasset)
+ account = await db.Account.findOne({
+ where: {
+ id: user.account.id
+ },
+ lock: transaction.LOCK.UPDATE,
+ transaction
+ });
+ else
+ account = await db.Account.findOne({
+ where: {
+ user_id: user.id,
+ asset: config.liquid.btcasset,
+ pubkey: null,
+ balance: {
+ [Op.gte]: amount
+ }
+ },
+ lock: transaction.LOCK.UPDATE,
+ transaction
+ });
if (!account || account.balance < amount) {
throw new Error("Insufficient funds");
@@ -42,6 +52,8 @@ send = async (amount, memo, hash, user) => {
await account.decrement({ balance: amount }, { transaction });
await account.reload({ transaction });
+ l.info("debited account", account.id);
+
if (config.lna.clightning) {
let m = await lna.pay(hash);
let sent;
@@ -58,7 +70,7 @@ send = async (amount, memo, hash, user) => {
amt: amount,
payment_request: hash,
max_parts: 10,
- fee_limit: { fixed: Math.min(10000, account.balance) },
+ fee_limit: { fixed: Math.min(10000, account.balance) }
});
if (!m) throw new Error(`lightning payment failed ${amount} ${hash}`);
@@ -79,15 +91,28 @@ send = async (amount, memo, hash, user) => {
});
await db.transaction(async transaction => {
- let account = await db.Account.findOne({
- where: {
- user_id: user.id,
- asset: config.liquid.btcasset,
- pubkey: null
- },
- lock: transaction.LOCK.UPDATE,
- transaction
- });
+ let account;
+ if (user.account.asset === config.liquid.btcasset)
+ account = await db.Account.findOne({
+ where: {
+ id: user.account.id
+ },
+ lock: transaction.LOCK.UPDATE,
+ transaction
+ });
+ else
+ account = await db.Account.findOne({
+ where: {
+ user_id: user.id,
+ asset: config.liquid.btcasset,
+ pubkey: null,
+ balance: {
+ [Op.gte]: fee
+ }
+ },
+ lock: transaction.LOCK.UPDATE,
+ transaction
+ });
total = parseInt(total);
fee = parseInt(fee);
@@ -112,6 +137,8 @@ send = async (amount, memo, hash, user) => {
{ transaction }
);
+ l.info("created payment record", payment.id);
+
payment = payment.get({ plain: true });
payment.account = account.get({ plain: true });
diff --git a/routes/send.js b/routes/send.js
index 59b30d57..83b1e501 100644
--- a/routes/send.js
+++ b/routes/send.js
@@ -9,7 +9,7 @@ module.exports = ah(async (req, res, next) => {
unconfidential,
asset,
memo,
- username,
+ username
} = req.body;
let { user } = req;
amount = parseInt(amount);
@@ -20,16 +20,26 @@ module.exports = ah(async (req, res, next) => {
return res.status(500).send("Amount must be greater than zero");
try {
- await db.transaction(async (transaction) => {
- let account = await db.Account.findOne({
- where: {
- user_id: user.id,
- asset,
- pubkey: null,
- },
- lock: transaction.LOCK.UPDATE,
- transaction,
- });
+ await db.transaction(async transaction => {
+ let account;
+ if (user.account.asset === asset)
+ account = await db.Account.findOne({
+ where: {
+ id: user.account.id,
+ },
+ lock: transaction.LOCK.UPDATE,
+ transaction
+ });
+ else
+ account = await db.Account.findOne({
+ where: {
+ user_id: user.id,
+ asset,
+ pubkey: null
+ },
+ lock: transaction.LOCK.UPDATE,
+ transaction
+ });
if (account.balance < amount) {
throw new Error("Insufficient funds");
@@ -49,7 +59,7 @@ module.exports = ah(async (req, res, next) => {
currency: user.currency,
confirmed: true,
hash: username ? `Payment to ${username}` : "Internal Transfer",
- network: "COINOS",
+ network: "COINOS"
};
if (!username) {
@@ -75,8 +85,8 @@ module.exports = ah(async (req, res, next) => {
where: { username },
include: {
model: db.Account,
- as: "account",
- },
+ as: "account"
+ }
},
{ transaction }
);
@@ -84,7 +94,7 @@ module.exports = ah(async (req, res, next) => {
let text = address || payreq;
params = {
where: { user_id: recipient.id },
- order: [["id", "DESC"]],
+ order: [["id", "DESC"]]
};
if (text) params.where.text = text;
let invoice = await db.Invoice.findOne(params);
@@ -93,7 +103,7 @@ module.exports = ah(async (req, res, next) => {
let acc = {
user_id: recipient.id,
asset,
- pubkey: null,
+ pubkey: null
};
if (recipient.account.asset === asset && !recipient.account.pubkey)
@@ -102,7 +112,7 @@ module.exports = ah(async (req, res, next) => {
a2 = await db.Account.findOne({
where: acc,
lock: transaction.LOCK.UPDATE,
- transaction,
+ transaction
});
}
@@ -122,12 +132,12 @@ module.exports = ah(async (req, res, next) => {
} else {
const existing = await db.Account.findOne({
where: {
- asset,
+ asset
},
order: [["id", "ASC"]],
limit: 1,
lock: transaction.LOCK.UPDATE,
- transaction,
+ transaction
});
if (existing) {
@@ -155,7 +165,7 @@ module.exports = ah(async (req, res, next) => {
hash: "Payment from " + user.username,
memo,
network: "COINOS",
- received: true,
+ received: true
};
if (invoice) {
@@ -176,7 +186,7 @@ module.exports = ah(async (req, res, next) => {
address: c.address,
amount: amount - 100,
user: recipient,
- limit: amount,
+ limit: amount
}).catch(console.log);
}
}
@@ -198,7 +208,7 @@ module.exports = ah(async (req, res, next) => {
l.error(
"problem sending internal payment",
user.username,
- user.balance,
+ account.balance,
e.message
);
return res.status(500).send(e.message);