pago/src/main.ts

69 lines
1.6 KiB
TypeScript

import dotenv from 'dotenv'
import express, { Express, Request, Response } from 'express'
import Wallet from 'monero-wallet-rpc-js'
(async () => {
dotenv.config()
// Load monero wallet
let wallet; try {
wallet = await new Wallet({
url: process.env.WALLET_RPC_URI,
filename: process.env.WALLET_FILENAME,
password: process.env.WALLET_PASSWORD,
address: process.env.WALLET_ADDRESS,
viewKey: process.env.WALLET_VIEWKEY,
restoreHeight: process.env.WALLET_RESTORE_HEIGHT,
})
} catch (err) {
console.error(`Failed to open wallet: ${err}`)
}
// Set auto save
if (Number(process.env.WALLET_AUTOSAVE_SEC) > 0)
setInterval(
wallet.store(),
Number(process.env.WALLET_AUTOSAVE_SEC)
)
// Set auto refresh
try {
await wallet.rpc('auto_refresh', {
enable: true,
period: Number(process.env.WALLET_REFRESH_SEC),
})
} catch (err) {
console.error(`Failed to set auto_refresh to ${process.env.WALLET_REFRESH_SEC}: ${err}`)
}
try {
console.log(`Wallet height: ${await wallet.getHeight()}`)
} catch (err) {
console.error(`Failed to get blockchain height: ${err}`)
}
// Server
const app: Express = express()
app.use(express.json())
app.listen(80)
// Healthchecks
app.get('/wallet/height', async (req: Request, res: Response) =>
res.send((await wallet.getHeight()).toString())
)
app.get('/wallet/balance', async (req: Request, res: Response) =>
res.send(
((await wallet.getBalance()).balance * 0.000000000001)
.toString()
)
)
// New payment
//app.post('/payment', async (req: Request, res: Response) => {
// console.log(`New payment created: "${req.body.label}"`)
// return res.sendStatus(200)
//})
})()