Skip to content

Commit

Permalink
add backup
Browse files Browse the repository at this point in the history
  • Loading branch information
gandlafbtc committed Feb 18, 2023
1 parent ee54eaa commit df063d6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
tokens.json
57 changes: 38 additions & 19 deletions server.js
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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)
}
}

0 comments on commit df063d6

Please sign in to comment.