aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2022-11-30 12:02:06 -0800
committerAdam Soltys <[email protected]>2022-11-30 12:02:06 -0800
commit6d3fd0464356d22ace37ee6777f6401df8f6b559 (patch)
treee373563eadbeea163a521754c9700a43d4c5df00
parent27dfb892f38b6895ac19e85337fa05f348f7e113 (diff)
downloadcoinos-server-6d3fd0464356d22ace37ee6777f6401df8f6b559.tar.gz
coinos-server-6d3fd0464356d22ace37ee6777f6401df8f6b559.zip
conversion requests and lightning payment on liquid confirmation
-rw-r--r--db/index.js12
-rw-r--r--db/models/conversions.js41
-rw-r--r--routes/invoices.js9
-rw-r--r--routes/liquid/receive.js17
4 files changed, 75 insertions, 4 deletions
diff --git a/db/index.js b/db/index.js
index 0437ece6..7b289aa0 100644
--- a/db/index.js
+++ b/db/index.js
@@ -8,6 +8,7 @@ db.authenticate().catch(err => {
import "./models/accounts";
import "./models/codes";
+import "./models/conversions";
import "./models/deposits";
import "./models/invoices";
import "./models/keys";
@@ -20,7 +21,12 @@ import "./models/withdrawals";
import "./models/referrals";
import "./models/waiting_list";
-const { User, Account, Payment, Invoice, Key, Order, Referral, Request } = db;
+const { Conversion, User, Account, Payment, Invoice, Key, Order, Referral, Request } = db;
+
+Invoice.hasMany(Conversion, {
+ as: "conversions",
+ foreignKey: "invoice_id"
+});
User.hasMany(Account, {
as: "accounts",
@@ -152,6 +158,10 @@ Invoice.hasOne(Request, {
foreignKey: "invoice_id"
});
+Conversion.belongsTo(Invoice, {
+ as: "invoice",
+ foreignKey: "invoice_id"
+});
db.Order = Order;
diff --git a/db/models/conversions.js b/db/models/conversions.js
new file mode 100644
index 00000000..4443128e
--- /dev/null
+++ b/db/models/conversions.js
@@ -0,0 +1,41 @@
+import db from "$db/db";
+import { DataTypes } from '@sequelize/core';
+
+const attributes = {
+ id: {
+ type: DataTypes.INTEGER(11),
+ allowNull: false,
+ defaultValue: null,
+ primaryKey: true,
+ autoIncrement: true,
+ comment: null,
+ field: "id"
+ },
+ invoice_id: {
+ type: DataTypes.INTEGER(11),
+ allowNull: false,
+ defaultValue: null,
+ primaryKey: false,
+ autoIncrement: false,
+ comment: null,
+ field: "invoice_id"
+ },
+ text: {
+ type: DataTypes.TEXT,
+ allowNull: true,
+ defaultValue: null,
+ primaryKey: false,
+ autoIncrement: false,
+ comment: null,
+ field: "text"
+ },
+};
+
+const options = {
+ tableName: "conversions",
+ comment: "",
+ indexes: [],
+ timestamps: false
+};
+
+db["Conversion"] = db.define("conversions_model", attributes, options);
diff --git a/routes/invoices.js b/routes/invoices.js
index 7fa82afb..86836295 100644
--- a/routes/invoices.js
+++ b/routes/invoices.js
@@ -207,3 +207,12 @@ app.post(
let { network, username } = req.params;
}
);
+
+app.post("/conversion",
+ async (req, res, next) => {
+ let { invoice_id, text } = req.body;
+
+ let conversion = await db.Conversion.create({ invoice_id, text });
+ res.send(conversion);
+ }
+);
diff --git a/routes/liquid/receive.js b/routes/liquid/receive.js
index 718feba5..2554904d 100644
--- a/routes/liquid/receive.js
+++ b/routes/liquid/receive.js
@@ -14,6 +14,7 @@ import bitcoin from "bitcoinjs-lib";
import liquidJs from "liquidjs-lib";
import { l, err, warn } from "$lib/logging";
import lq from "$lib/liquid";
+import sendLightning from "$lib/send";
const { Block, networks, Transaction } = liquidJs;
@@ -317,7 +318,7 @@ setInterval(async () => {
for (let i = 0; i < arr.length; i++) {
const hash = arr[i];
- let account, invoice, address, user, total, p;
+ let account, invoice, address, user, total, p, conversions;
await db.transaction(async transaction => {
p = await db.Payment.findOne({
where: { hash, confirmed: 0, received: 1 },
@@ -344,6 +345,7 @@ setInterval(async () => {
if (p && p.account) {
({ account, invoice } = p);
+ conversions = await invoice.getConversions();
total = p.amount + p.tip;
p.confirmed = 1;
await account.save({ transaction });
@@ -381,6 +383,17 @@ setInterval(async () => {
delete queue[hash];
});
+ user.account = p.account;
+
+ console.log("CONVERSIONS", conversions.length)
+
+ for (let i = 0; i < conversions.length; i++) {
+ console.log("sending lightning");
+ let conversion = conversions[i];
+ let result = await sendLightning(p.amount, "", conversion.text, user);
+ console.log("RESULT", result);
+ }
+
let c = store.convert[address];
if (address && c && p.account.asset === network.assetHash) {
l(
@@ -390,8 +403,6 @@ setInterval(async () => {
user.username
);
- user.account = p.account;
-
sendLiquid({
address: c.address,
amount: total - 100,