Skip to content

Commit 7bf8859

Browse files
committed
adding transaction routes
1 parent 13970df commit 7bf8859

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

src/db/transactions.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import db from './config.js';
2+
3+
const createTransaction = async (signature, ip_address, wallet_address, github_username, timestamp) => {
4+
const query = `
5+
INSERT INTO faucet.transactions (signature, ip_address, wallet_address, github_username, timestamp)
6+
VALUES ($1, $2, $3, $4, $5)
7+
RETURNING *;
8+
`;
9+
const values = [signature, ip_address, wallet_address, github_username, timestamp];
10+
const result = await db.query(query, values);
11+
return result.rows[0];
12+
};
13+
14+
const getLastTransaction = async ({ wallet_address, github_username, ip_address }) => {
15+
const query = `
16+
SELECT * FROM faucet.transactions
17+
WHERE
18+
(wallet_address = $1) OR
19+
(github_username = $2) OR
20+
(ip_address = $3)
21+
ORDER BY timestamp DESC
22+
LIMIT 1;
23+
`;
24+
25+
const values = [wallet_address || null, github_username || null, ip_address || null];
26+
const result = await db.query(query, values);
27+
return result.rows[0]; // Returns the most recent transaction found
28+
};
29+
30+
// Not currently needed, but may be used for implementing TTL
31+
const deleteTransaction = async (signature) => {
32+
const query = `
33+
DELETE FROM faucet.transactions
34+
WHERE signature = $1
35+
RETURNING *;
36+
`;
37+
const values = [signature];
38+
const result = await db.query(query, values);
39+
return result.rows[0];
40+
};
41+
42+
export default {
43+
createTransaction,
44+
getLastTransaction,
45+
deleteTransaction
46+
};

src/routes/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import express from 'express';
22
import rateLimitRoute from './rateLimitRoute.js';
3+
import transactionsRoute from './transactionsRoute.js';
34
import solanaBalancesRoute from "./solanaBalancesRoute.js";
45
import githubValidationRoute from "./githubValidationRoute.js";
56

@@ -8,6 +9,9 @@ const router = express.Router();
89
// Use rate limit routes
910
router.use(rateLimitRoute);
1011

12+
// Use transactions routes
13+
router.use(transactionsRoute);
14+
1115
// Use Solana balances routes
1216
router.use(solanaBalancesRoute);
1317

src/routes/transactionsRoute.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import express from 'express';
2+
import transactions from '../db/transactions.js';
3+
4+
const router = express.Router();
5+
6+
// POST a new transaction
7+
router.post('/transactions', async (req, res, next) => {
8+
const { signature, ip_address, wallet_address, github_username, timestamp } = req.body;
9+
10+
if (!signature || !ip_address || !wallet_address || !timestamp) {
11+
return res.status(400).json({ message: 'Missing required fields (signature, ip_address, wallet_address, timestamp).' });
12+
}
13+
14+
try {
15+
const newTransaction = await transactions.createTransaction(signature, ip_address, wallet_address, github_username ?? '', timestamp);
16+
res.status(201).json(newTransaction);
17+
} catch (error) {
18+
console.error('Error creating transaction:', error);
19+
next(error);
20+
}
21+
});
22+
23+
// GET the most recent transaction based on wallet, GitHub or IP
24+
router.get('/transactions/last', async (req, res, next) => {
25+
const { wallet_address, github_username, ip_address } = req.query;
26+
27+
if (!wallet_address && !github_username && !ip_address) {
28+
return res.status(400).json({ message: 'At least one parameter (wallet_address, github_username, or ip_address) is required.' });
29+
}
30+
31+
try {
32+
const lastTransaction = await transactions.getLastTransaction({ wallet_address, github_username, ip_address });
33+
34+
if (lastTransaction) {
35+
res.status(200).json(lastTransaction);
36+
} else {
37+
res.status(204).json({ message: 'No transaction found for the given criteria.' });
38+
}
39+
} catch (error) {
40+
console.error('Error fetching last transaction:', error);
41+
next(error);
42+
}
43+
});
44+
45+
// DELETE a transaction by signature
46+
router.delete('/transactions/:signature', async (req, res, next) => {
47+
const { signature } = req.params;
48+
49+
try {
50+
const deletedTransaction = await transactions.deleteTransaction(signature);
51+
if (deletedTransaction) {
52+
res.status(200).json(deletedTransaction);
53+
} else {
54+
console.warn(`Transaction not found for signature "${signature}"`);
55+
res.status(404).json({ message: 'Transaction not found' });
56+
}
57+
} catch (error) {
58+
console.error(`Error deleting transaction for signature "${signature}":`, error);
59+
next(error);
60+
}
61+
});
62+
63+
export default router;

0 commit comments

Comments
 (0)