diff --git a/docker-compose.yml b/docker-compose.yml index 0668e93..ca7df85 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -50,6 +50,7 @@ services: --rpc-bind-port=18082 --password ${WALLET_PASSWORD} --daemon-host=${MONERO_DAEMON_HOST} + --tx-notify "/usr/bin/curl -s http://pago_api/new_tx/%s" networks: monero: @@ -73,6 +74,8 @@ services: container_name: pago_api build: . restart: unless-stopped + depends_on: + - wallet command: start networks: monero: diff --git a/main.js b/main.js index d3025aa..b1fe671 100644 --- a/main.js +++ b/main.js @@ -1,6 +1,9 @@ const dotenv = require('dotenv') const express = require('express') +const cors = require('cors') const Wallet = require('monero-wallet-rpc-js') +const {createServer} = require('node:http') +const {Server} = require('socket.io') ;(async () => { @@ -61,7 +64,7 @@ const Wallet = require('monero-wallet-rpc-js') } return getAccount(i) } else { - console.log(`Account #${i} exists with a balance of ${deAtomize(account.balance)} XMR.`) + console.log(`Using account #${i} (balance: ${deAtomize(account.balance)} XMR).`) return account } }; getAccount(WALLET_ACCOUNT_INDEX) @@ -70,8 +73,20 @@ const Wallet = require('monero-wallet-rpc-js') // Server const app = express() app.use(express.json()) - app.listen(80) + app.use(cors({ + // origin: 'http://example.com', + })) + const server = createServer(app) + const io = new Server(server) + server.listen(80) + // Test websockets + io.on('connection', (socket) => { + console.log('a user connected') + socket.on('disconnect', () => { + console.log('user disconnected') + }) + }) // Healthchecks app.get('/wallet/height', async (req, res) => @@ -107,16 +122,16 @@ const Wallet = require('monero-wallet-rpc-js') // Check payment app.get('/payment/:addr', async (req, res) => { let subaddr_index; try { - subaddr_index = (await wallet.getAddressIndex({ - address: req.params.addr - })).index.minor + subaddr_index = (await wallet.getAddressIndex(req.params.addr)).index.minor } catch (err) { console.error(`Failed to get index of subaddress: ${err}`) - return res.status(500).sendText('Failed to determine subaddress index!') + return res.sendStatus(500) } let transfers; try { transfers = Object.values(await wallet.getTransfers({ + all_accounts: true, in: true, + out: false, pending: true, failed: true, pool: true, @@ -125,9 +140,28 @@ const Wallet = require('monero-wallet-rpc-js') })).flat() } catch (err) { console.error(`Failed to get transactions for subaddress: ${err}`) - return res.status(500).sendText('Failed to get transactions!') + return res.sendStatus(500) } return res.json(transfers) }) + // Listen for new transactions + app.get('/new_tx/:txid', async (req, res) => { + // TODO: Retry, sleep, retry, give up + console.log(`New transaction: ${req.params.txid}`) + let result; try { + result = await wallet.getTransfersByTxid({ + account_index: WALLET_ACCOUNT_INDEX, + txid: req.params.txid, + }) + if (result.transfer===undefined) throw new Error('Multiple transfers?') + } catch (err) { + console.error(`Failed to query transaction: ${err}`) + return res.sendStatus(500) + } + console.log(`Transfer received at ${result.transfer.subaddr_index.minor} for ${deAtomize(result.transfer.amount)} XMR to ${result.transfer.address}`) + // TODO: Send websocket + return res.sendStatus(200) + }) + })() diff --git a/merch/index.html b/merch/index.html new file mode 100644 index 0000000..93aa831 --- /dev/null +++ b/merch/index.html @@ -0,0 +1,11 @@ + +
+Date | +Time | +Amt | +Confs | +Block | +Stat | + +
---|---|---|---|---|---|
+ | + | + | + | + | + ⏲️ + ⛔ + ✅ + | +