From df063d6d63ecbc0a739d8f46775f07a735d86dfc Mon Sep 17 00:00:00 2001 From: gandlaf21 Date: Sat, 18 Feb 2023 14:31:49 +0100 Subject: [PATCH] add backup --- .gitignore | 3 ++- server.js | 57 ++++++++++++++++++++++++++++++++++++------------------ 2 files changed, 40 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index b512c09..18b790a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -node_modules \ No newline at end of file +node_modules +tokens.json \ No newline at end of file diff --git a/server.js b/server.js index b49b4a4..2edb985 100644 --- a/server.js +++ b/server.js @@ -1,9 +1,10 @@ require('dotenv').config() const { getEncodedProofs } = require("@gandlaf21/cashu-ts") -const { AnarchoFaucet , utils } = require ("@gandlaf21/cashu-tools") +const { AnarchoFaucet, utils } = require("@gandlaf21/cashu-tools") const rateLimit = require('express-rate-limit') const cors = require('cors'); +const fs = require('fs') const express = require('express') const app = express() @@ -13,37 +14,55 @@ let faucet const limiter = rateLimit({ windowMs: process.env.REQUEST_TIMEOUT_INTERVAL, // 15 seconds - max: process.env.REQUEST_TIMEOUT_MAX, // Limit each IP to 100 requests per `window` (here, per 15 minutes) - standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers - legacyHeaders: false, // Disable the `X-RateLimit-*` headers + max: process.env.REQUEST_TIMEOUT_MAX, // Limit each IP to 100 requests per `window` (here, per 15 minutes) + standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers + legacyHeaders: false, // Disable the `X-RateLimit-*` headers }) app.use(limiter) app.use(cors()); app.get('/', (req, res) => { - let responseString ="Token has already been claimed! waiting for next token..." - if(faucet.currentToken){ - responseString = - getEncodedProofs(faucet.currentToken.proofs ,faucet.currentToken.mints) - } - res.send({token: responseString}) + let responseString = "Token has already been claimed! waiting for next token..." + if (faucet.currentToken) { + responseString = + getEncodedProofs(faucet.currentToken.proofs, faucet.currentToken.mints) + } + res.send({ token: responseString }) }) app.get('/balance', async (req, res) => { - res.send({remaining: utils.getAmountForTokenSet(faucet.balance), - mintCount: faucet.wallets.length}) + res.send({ + remaining: utils.getAmountForTokenSet(faucet.balance), + mintCount: faucet.wallets.length + }) }) app.get('/charge', async (req, res) => { - const token = req.query.token - const message = await faucet.charge(token) - res.send({message}) - }) + const token = req.query.token + const message = await faucet.charge(token) + res.send({ message }) +}) app.listen(port, async () => { - faucet = new AnarchoFaucet(process.env.FAUCET_INTERVAL, process.env.SATS_PER_INTERVAL) - await faucet.start() - console.log(`Example app listening on port ${port}`) + faucet = new AnarchoFaucet(process.env.FAUCET_INTERVAL, process.env.SATS_PER_INTERVAL) + await faucet.start() + setInterval(writeTokensToFile, 10000) + console.log(`Example app listening on port ${port}`) }) + + +function writeTokensToFile() { + try { + const data = JSON.stringify({tokens: faucet.balance, mints: faucet.wallets.map(w=> { return {mint: w.mint.mintUrl, keysets: w.keysets}})}) + fs.writeFile('tokens.json', data, err => { + if (err) { + throw err + } + console.log('saved tokens to file.') + }) + } catch (error) { + console.log(error) + } +} \ No newline at end of file