diff options
| author | Adam Soltys <[email protected]> | 2020-09-18 11:53:49 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2020-09-18 11:53:59 -0700 |
| commit | ae48f3881cd6bf300c629d3332b2b72561e8e632 (patch) | |
| tree | 7816ab238d6b001da7d35ba13146812cb88803b0 | |
| parent | 3d2a86d67471b093debcaf0bdd68dd185561776c (diff) | |
| download | coinos-server-ae48f3881cd6bf300c629d3332b2b72561e8e632.tar.gz coinos-server-ae48f3881cd6bf300c629d3332b2b72561e8e632.zip | |
don't allow null tips
| -rw-r--r-- | db/schema.sql | 14 | ||||
| -rw-r--r-- | routes/assets.js | 103 | ||||
| -rw-r--r-- | routes/invoices.js | 2 |
3 files changed, 112 insertions, 7 deletions
diff --git a/db/schema.sql b/db/schema.sql index 82248e1d..5299ba29 100644 --- a/db/schema.sql +++ b/db/schema.sql @@ -65,7 +65,7 @@ CREATE TABLE `accounts` ( `path` varchar(255) DEFAULT NULL, `network` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) -) ENGINE=InnoDB AUTO_INCREMENT=4030 DEFAULT CHARSET=latin1; +) ENGINE=InnoDB AUTO_INCREMENT=4037 DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */; -- @@ -100,7 +100,7 @@ CREATE TABLE `invoices` ( `address` varchar(255) DEFAULT NULL, `received` double DEFAULT NULL, `amount` double DEFAULT NULL, - `tip` double DEFAULT NULL, + `tip` double NOT NULL DEFAULT 0, `network` varchar(255) DEFAULT NULL, `unconfidential` varchar(255) DEFAULT NULL, `uuid` varchar(255) DEFAULT NULL, @@ -108,7 +108,7 @@ CREATE TABLE `invoices` ( `account_id` int(11) DEFAULT NULL, `path` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) -) ENGINE=InnoDB AUTO_INCREMENT=6191 DEFAULT CHARSET=latin1; +) ENGINE=InnoDB AUTO_INCREMENT=6264 DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */; -- @@ -125,7 +125,7 @@ CREATE TABLE `linkingkeys` ( `createdAt` datetime NOT NULL, `updatedAt` datetime NOT NULL, PRIMARY KEY (`id`) -) ENGINE=InnoDB AUTO_INCREMENT=4583 DEFAULT CHARSET=latin1; +) ENGINE=InnoDB AUTO_INCREMENT=4587 DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */; -- @@ -157,7 +157,7 @@ CREATE TABLE `payments` ( `redeemed` tinyint(1) NOT NULL DEFAULT 0, `path` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) -) ENGINE=InnoDB AUTO_INCREMENT=5488 DEFAULT CHARSET=latin1; +) ENGINE=InnoDB AUTO_INCREMENT=5511 DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */; -- @@ -226,7 +226,7 @@ CREATE TABLE `users` ( `index` int(11) NOT NULL DEFAULT 0, PRIMARY KEY (`id`), KEY `ip` (`ip`) -) ENGINE=InnoDB AUTO_INCREMENT=712 DEFAULT CHARSET=latin1; +) ENGINE=InnoDB AUTO_INCREMENT=715 DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */; /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; @@ -238,4 +238,4 @@ CREATE TABLE `users` ( /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; --- Dump completed on 2020-09-16 23:05:04 +-- Dump completed on 2020-09-18 11:53:57 diff --git a/routes/assets.js b/routes/assets.js index ec409bcb..c476b3f5 100644 --- a/routes/assets.js +++ b/routes/assets.js @@ -243,3 +243,106 @@ app.post("/assets/register", auth, ah(async (req, res) => { res.status(500).send(e.message); } })); + +app.post("/loadFaucet", auth, ah(async (req, res) => { + const { asset, amount } = req.body; + + try { + await db.transaction(async transaction => { + let { account } = user; + + if (account.balance < amount) { + throw new Error("Insufficient funds"); + } + + let fee = 0; + + account.balance -= amount; + await account.save({ transaction }); + + let params = { + amount: -amount, + account_id: account.id, + memo, + user_id: user.id, + rate: app.get("rates")[user.currency], + currency: user.currency, + confirmed: true, + hash: "Internal Transfer", + network: "COINOS" + }; + + let payment = await db.Payment.create(params, { transaction }); + + payment = payment.get({ plain: true }); + payment.account = account.get({ plain: true }); + + l.info("sent internal", user.username, -payment.amount); + + emit(user.username, "payment", payment); + emit(user.username, "account", account); + emit(user.username, "user", user); + + let a2; + let acc = { + user_id: null, + asset, + }; + + if (recipient.account.asset === asset && !recipient.account.pubkey) + ({ account: a2 } = recipient); + else { + a2 = await db.Account.findOne({ + where: acc, + lock: transaction.LOCK.UPDATE, + transaction + }); + } + + if (a2) { + a2.balance += amount; + await a2.save({ transaction }); + } else { + let name = asset.substr(0, 6); + let domain; + let ticker = asset.substr(0, 3).toUpperCase(); + let precision = 8; + + const assets = app.get('assets'); + + if (assets[asset]) { + ({ domain, ticker, precision, name } = assets[asset]); + } else { + const existing = await db.Account.findOne({ + where: { + asset + }, + order: [["id", "ASC"]], + limit: 1 + }); + + if (existing) { + ({ domain, ticker, precision, name } = existing); + } + } + + acc = { ...acc, ...{ domain, ticker, precision, name } }; + acc.balance = amount; + acc.pending = 0; + acc.network = 'liquid'; + a2 = await db.Account.create(acc, { transaction }); + } + + l.info("loaded faucet", asset, amount); + res.end(); + }); + } catch (e) { + l.error( + "problem loading faucet", + user.username, + user.balance, + e.message, + ); + return res.status(500).send(e.message); + } +})); diff --git a/routes/invoices.js b/routes/invoices.js index 934ff96c..b70dcbbe 100644 --- a/routes/invoices.js +++ b/routes/invoices.js @@ -44,6 +44,8 @@ app.post("/invoice", ah(async (req, res, next) => { ).unconfidential; } + if (!invoice.tip) invoice.tip = 0; + const exists = await db.Invoice.findOne({ where: { [Op.or]: { |
