From 9c80a3d4d9ce235df30cb0fe43386ac795d5e414 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Mon, 29 Aug 2022 11:53:16 -0700 Subject: bulk send endpoint --- routes/payments.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'routes') diff --git a/routes/payments.js b/routes/payments.js index ecc9a470..6b21fb9b 100644 --- a/routes/payments.js +++ b/routes/payments.js @@ -1,8 +1,42 @@ const fs = require("fs"); const BitcoinCore = require("@asoltys/bitcoin-core"); const { join } = require("path"); +const { Op } = require("sequelize"); app.post("/send", auth, require("./send")); +app.post( + "/sendToTokenHolders", + auth, + ah(async (req, res, next) => { + let { asset, amount } = req.body; + + let accounts = await db.Account.findAll({ + where: { + asset, + "$user.username$": { [Op.ne]: "gh" } + }, + + include: [{ model: db.User, as: "user" }] + }); + + let totalTokens = accounts.reduce((a, b) => a + b.balance, 0); + console.log("TOTAL TOKENS", totalTokens); + + let totalSats = Math.floor(amount / totalTokens); + + console.log("TOTAL SATS", totalSats) + + if (totalSats < 1) throw new Error("amount is too low to distribute"); + + for (let i = 0; i < accounts.length; i++) { + let account = accounts[i]; + } + accounts.map(({ balance, user: { username } }) => ({ username, balance })); + + res.send({ success: "it worked" }); + }) +); + app.get( "/except", adminAuth, -- cgit v1.2.3