aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2020-04-05 21:50:05 -0700
committerAdam Soltys <[email protected]>2020-04-11 21:29:23 -0700
commite9b8303212f1e463ad6927e1b3df404a4358db96 (patch)
treebd83e632fcc3a1c6ba279fbda6fc48140e252411
parent1738b0feeef4f2d44aeac58bdd27236684792f64 (diff)
downloadcoinos-server-e9b8303212f1e463ad6927e1b3df404a4358db96.tar.gz
coinos-server-e9b8303212f1e463ad6927e1b3df404a4358db96.zip
accounts table
-rw-r--r--db/models/accounts.js66
-rw-r--r--db/schema.sql144
-rw-r--r--routes/liquid/receive.js44
3 files changed, 100 insertions, 154 deletions
diff --git a/db/models/accounts.js b/db/models/accounts.js
new file mode 100644
index 00000000..57c9e82a
--- /dev/null
+++ b/db/models/accounts.js
@@ -0,0 +1,66 @@
+const { DataTypes } = require('sequelize');
+
+const attributes = {
+ id: {
+ type: DataTypes.INTEGER(11),
+ allowNull: false,
+ defaultValue: null,
+ primaryKey: true,
+ autoIncrement: true,
+ comment: null,
+ field: "id"
+ },
+ user_id: {
+ type: DataTypes.INTEGER(11),
+ allowNull: true,
+ defaultValue: null,
+ primaryKey: false,
+ autoIncrement: false,
+ comment: null,
+ field: "user_id"
+ },
+ createdAt: {
+ type: DataTypes.DATE,
+ allowNull: false,
+ defaultValue: null,
+ primaryKey: false,
+ autoIncrement: false,
+ comment: null,
+ field: "createdAt"
+ },
+ updatedAt: {
+ type: DataTypes.DATE,
+ allowNull: false,
+ defaultValue: null,
+ primaryKey: false,
+ autoIncrement: false,
+ comment: null,
+ field: "updatedAt"
+ },
+ asset: {
+ type: DataTypes.STRING(255),
+ allowNull: true,
+ defaultValue: null,
+ primaryKey: false,
+ autoIncrement: false,
+ comment: null,
+ field: "asset"
+ },
+ balance: {
+ type: DataTypes.DOUBLE,
+ allowNull: true,
+ defaultValue: null,
+ primaryKey: false,
+ autoIncrement: false,
+ comment: null,
+ field: "balance"
+ },
+};
+
+const options = {
+ tableName: "accounts",
+ comment: "",
+ indexes: []
+};
+
+db["Account"] = db.define("accounts_model", attributes, options);
diff --git a/db/schema.sql b/db/schema.sql
index 83d143ce..3d173f08 100644
--- a/db/schema.sql
+++ b/db/schema.sql
@@ -1,148 +1,14 @@
--- MySQL dump 10.17 Distrib 10.3.22-MariaDB, for debian-linux-gnu (x86_64)
---
--- Host: localhost Database: coinos
--- ------------------------------------------------------
--- Server version 10.3.22-MariaDB-1:10.3.22+maria~bionic
-
-/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
-/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
-/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
-/*!40101 SET NAMES utf8mb4 */;
-/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
-/*!40103 SET TIME_ZONE='+00:00' */;
-/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
-/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
-/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
-/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
-
---
--- Current Database: `coinos`
---
-
-CREATE DATABASE /*!32312 IF NOT EXISTS*/ `coinos` /*!40100 DEFAULT CHARACTER SET utf8mb4 */;
-
-USE `coinos`;
-
---
--- Table structure for table `SequelizeMeta`
---
-
-DROP TABLE IF EXISTS `SequelizeMeta`;
+DROP TABLE IF EXISTS `accounts`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
-CREATE TABLE `SequelizeMeta` (
- `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
- PRIMARY KEY (`name`),
- UNIQUE KEY `name` (`name`),
- UNIQUE KEY `SequelizeMeta_name_unique` (`name`)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-/*!40101 SET character_set_client = @saved_cs_client */;
-
---
--- Table structure for table `invoices`
---
-
-DROP TABLE IF EXISTS `invoices`;
-/*!40101 SET @saved_cs_client = @@character_set_client */;
-/*!40101 SET character_set_client = utf8 */;
-CREATE TABLE `invoices` (
+CREATE TABLE `accounts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
- `text` text DEFAULT NULL,
- `createdAt` datetime NOT NULL,
- `updatedAt` datetime NOT NULL,
- `rate` double DEFAULT NULL,
- `currency` varchar(255) NOT NULL,
- `address` varchar(255) DEFAULT NULL,
- `received` double DEFAULT NULL,
- `amount` double DEFAULT NULL,
- `tip` double DEFAULT NULL,
`asset` varchar(255) DEFAULT NULL,
- PRIMARY KEY (`id`)
-) ENGINE=InnoDB AUTO_INCREMENT=3646 DEFAULT CHARSET=latin1;
-/*!40101 SET character_set_client = @saved_cs_client */;
-
---
--- Table structure for table `payments`
---
-
-DROP TABLE IF EXISTS `payments`;
-/*!40101 SET @saved_cs_client = @@character_set_client */;
-/*!40101 SET character_set_client = utf8 */;
-CREATE TABLE `payments` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `user_id` int(11) DEFAULT NULL,
- `hash` text DEFAULT NULL,
- `createdAt` datetime NOT NULL,
- `updatedAt` datetime NOT NULL,
- `rate` double DEFAULT NULL,
- `currency` varchar(255) NOT NULL,
- `address` varchar(255) DEFAULT NULL,
- `received` tinyint(1) DEFAULT NULL,
- `amount` double DEFAULT NULL,
- `tip` double DEFAULT NULL,
- `confirmed` tinyint(1) NOT NULL,
- `fee` double DEFAULT NULL,
- `asset` varchar(255) DEFAULT NULL,
- PRIMARY KEY (`id`)
-) ENGINE=InnoDB AUTO_INCREMENT=3142 DEFAULT CHARSET=latin1;
-/*!40101 SET character_set_client = @saved_cs_client */;
-
---
--- Table structure for table `users`
---
-
-DROP TABLE IF EXISTS `users`;
-/*!40101 SET @saved_cs_client = @@character_set_client */;
-/*!40101 SET character_set_client = utf8 */;
-CREATE TABLE `users` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `username` varchar(255) DEFAULT NULL,
- `password` varchar(255) DEFAULT NULL,
- `email` varchar(255) DEFAULT NULL,
- `address` varchar(255) DEFAULT NULL,
- `unit` varchar(255) DEFAULT NULL,
- `symbol` varchar(255) DEFAULT NULL,
- `currency` varchar(255) DEFAULT NULL,
- `commission` varchar(255) DEFAULT NULL,
- `title` varchar(255) DEFAULT NULL,
- `pubkey` varchar(255) DEFAULT NULL,
- `privkey` varchar(255) DEFAULT NULL,
`createdAt` datetime NOT NULL,
`updatedAt` datetime NOT NULL,
- `index` int(11) DEFAULT NULL,
- `balance` int(11) NOT NULL DEFAULT 0,
- `channel` varchar(255) DEFAULT NULL,
- `readonlypw` varchar(255) DEFAULT NULL,
- `limit` int(11) DEFAULT 0,
- `fbtoken` varchar(255) DEFAULT NULL,
- `friend` tinyint(1) DEFAULT NULL,
- `pending` int(11) DEFAULT 0,
- `name` varchar(255) DEFAULT NULL,
- `pic` varchar(255) DEFAULT NULL,
- `emailToken` varchar(255) DEFAULT NULL,
- `phone` varchar(255) DEFAULT NULL,
- `phoneVerified` tinyint(1) DEFAULT NULL,
- `twofa` tinyint(1) DEFAULT NULL,
- `authyId` varchar(255) DEFAULT NULL,
- `pin` varchar(255) DEFAULT NULL,
- `emailVerified` tinyint(1) DEFAULT NULL,
- `phoneToken` varchar(255) DEFAULT NULL,
- `confidential` varchar(255) DEFAULT NULL,
- `liquid` varchar(255) DEFAULT NULL,
- `currencies` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
- `otpsecret` varchar(255) DEFAULT NULL,
+ `balance` double DEFAULT NULL,
+ `pending` double DEFAULT NULL,
PRIMARY KEY (`id`)
-) ENGINE=InnoDB AUTO_INCREMENT=474 DEFAULT CHARSET=latin1;
+) ENGINE=InnoDB AUTO_INCREMENT=3329 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
-/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
-
-/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
-/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
-/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
-/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
-/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
-/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
-/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-
--- Dump completed on 2020-03-27 16:59:40
diff --git a/routes/liquid/receive.js b/routes/liquid/receive.js
index 2b459af3..68189e38 100644
--- a/routes/liquid/receive.js
+++ b/routes/liquid/receive.js
@@ -11,8 +11,6 @@ const zmqRawTx = zmq.socket("sub");
zmqRawTx.connect("tcp://127.0.0.1:18603");
zmqRawTx.subscribe("rawtx");
-const asset = 'LBTC';
-
zmqRawTx.on("message", async (topic, message, sequence) => {
const hex = message.toString("hex");
const unblinded = await lq.unblindRawTransaction(hex);
@@ -22,9 +20,9 @@ zmqRawTx.on("message", async (topic, message, sequence) => {
Promise.all(
tx.vout.map(async o => {
- if (!o.asset || o.asset !== config.liquid.btcasset) return;
if (!(o.scriptPubKey && o.scriptPubKey.addresses)) return;
+ const { asset } = o.asset;
const value = toSats(o.value);
const address = o.scriptPubKey.addresses[0];
@@ -38,9 +36,9 @@ zmqRawTx.on("message", async (topic, message, sequence) => {
const invoice = await db.Invoice.findOne({
where: {
user_id: user.id,
- asset,
+ asset
},
- order: [ [ 'id', 'DESC' ]]
+ order: [["id", "DESC"]]
});
const currency = invoice ? invoice.currency : user.currency;
@@ -51,7 +49,9 @@ zmqRawTx.on("message", async (topic, message, sequence) => {
user.pending += value;
user.confidential = await lq.getNewAddress();
- user.liquid = (await lq.getAddressInfo(user.confidential)).unconfidential;
+ user.liquid = (await lq.getAddressInfo(
+ user.confidential
+ )).unconfidential;
await user.save();
emit(user.username, "user", user);
@@ -68,7 +68,7 @@ zmqRawTx.on("message", async (topic, message, sequence) => {
tip,
confirmed,
address,
- asset,
+ asset
});
payments.push(blinded.txid);
@@ -106,27 +106,41 @@ setInterval(async () => {
let p = await db.Payment.findOne({
include: [{ model: db.User, as: "user" }],
where: { hash, confirmed: 0, received: 1 }
- })
+ });
p.confirmed = 1;
let user = await p.getUser();
- user.balance += p.amount + p.tip;
- user.pending -= Math.min(user.pending, p.amount + p.tip);
- l.info("liquid confirmed", user.username, p.amount, p.tip);
+
+ const asset = await db.Asset.findOne({
+ where: {
+ user_id: user.id,
+ 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);
+ }
+
+ l.info("liquid confirmed", user.username, p.asset, p.amount, p.tip);
emit(user.username, "user", user);
await user.save();
await p.save();
let payments = await db.Payment.findAll({
- where: {
+ where: {
user_id: user.id,
- received: {
+ received: {
[Op.ne]: null
- },
+ }
},
- order: [['id', 'DESC']],
+ order: [["id", "DESC"]],
limit: 12
});