From 05499106f89d2f1300210cf1dc0075afa45936dd Mon Sep 17 00:00:00 2001 From: Michael Ilerioluwa Adeniyi <77995341+mickey4653@users.noreply.github.com> Date: Fri, 30 May 2025 08:57:43 -0400 Subject: [PATCH 1/2] Feat: - added get all wallets and get wallet id endpoints --- package-lock.json | 1 + src/controllers/wallet.js | 85 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/package-lock.json b/package-lock.json index 334a196..0eff332 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3144,6 +3144,7 @@ "version": "0.2.3", "resolved": "https://registry.npmjs.org/hpp/-/hpp-0.2.3.tgz", "integrity": "sha512-4zDZypjQcxK/8pfFNR7jaON7zEUpXZxz4viyFmqjb3kWNWAHsLEUmWXcdn25c5l76ISvnD6hbOGO97cXUI3Ryw==", + "license": "ISC", "dependencies": { "lodash": "^4.17.12", "type-is": "^1.6.12" diff --git a/src/controllers/wallet.js b/src/controllers/wallet.js index c1bc156..e691362 100644 --- a/src/controllers/wallet.js +++ b/src/controllers/wallet.js @@ -114,3 +114,88 @@ export const createWallet = async (req, res) => { } }; +// Get all wallets +export const getAllWallets = async (req, res) => { + try { + if (!BITNOB_API_KEY) { + return res.status(500).json({ + status: false, + message: 'API key is not configured' + }); + } + + const response = await axios({ + method: 'GET', + url: `${BITNOB_API_URL}/wallets`, + headers: { + 'accept': 'application/json', + 'Authorization': `Bearer ${BITNOB_API_KEY.trim()}` + } + }); + + return res.status(200).json(response.data); + } catch (error) { + console.error('Error fetching wallets:', error.message); + + if (error.response) { + return res.status(error.response.status).json({ + status: false, + message: error.response.data?.message || 'Failed to fetch wallets', + details: error.response.data + }); + } + + return res.status(500).json({ + status: false, + message: 'Error fetching wallets' + }); + } +}; + +// Get wallet by coin +export const getWalletByCoin = async (req, res) => { + try { + const { coin } = req.params; + + if (!coin || !['trx', 'bnb'].includes(coin)) { + return res.status(400).json({ + status: false, + message: 'Invalid coin type. Must be either "trx" or "bnb"' + }); + } + + if (!BITNOB_API_KEY) { + return res.status(500).json({ + status: false, + message: 'API key is not configured' + }); + } + + const response = await axios({ + method: 'GET', + url: `${BITNOB_API_URL}/wallets/crypto-wallet/${coin}`, + headers: { + 'accept': 'application/json', + 'Authorization': `Bearer ${BITNOB_API_KEY.trim()}` + } + }); + + return res.status(200).json(response.data); + } catch (error) { + console.error('Error fetching wallet:', error.message); + + if (error.response) { + return res.status(error.response.status).json({ + status: false, + message: error.response.data?.message || 'Failed to fetch wallet', + details: error.response.data + }); + } + + return res.status(500).json({ + status: false, + message: 'Error fetching wallet' + }); + } +}; + From b4b58dcf5ba1f31fcefc9a603ecd84a828b9642a Mon Sep 17 00:00:00 2001 From: Zinhle Nhlapo Date: Fri, 30 May 2025 16:05:24 +0200 Subject: [PATCH 2/2] Added two more routes for the wallet controller. --- src/index.js | 6 ++---- src/routes/wallet.js | 0 src/routes/walletRoute.js | 9 ++++++++- 3 files changed, 10 insertions(+), 5 deletions(-) delete mode 100644 src/routes/wallet.js diff --git a/src/index.js b/src/index.js index aed2f10..4020e04 100644 --- a/src/index.js +++ b/src/index.js @@ -3,7 +3,7 @@ import hpp from 'hpp'; import helmet from 'helmet'; import cors from 'cors'; import compression from 'compression'; -import { createWallet } from './controllers/wallet.js'; +import walletRoute from './routes/walletRoute.js'; import { config } from './configs/config.env.js'; const app = express(); @@ -22,9 +22,7 @@ app.use(compression()); app.use(json({ limit: '200mb' })); app.use(urlencoded({ extended: true, limit: '200mb' })); -createWallet(); // Example usage of createWallet function - - +app.use('/api/wallet', walletRoute); app.listen(SERVER_PORT, ()=> { diff --git a/src/routes/wallet.js b/src/routes/wallet.js deleted file mode 100644 index e69de29..0000000 diff --git a/src/routes/walletRoute.js b/src/routes/walletRoute.js index 8a2dc81..a76aef9 100644 --- a/src/routes/walletRoute.js +++ b/src/routes/walletRoute.js @@ -1,8 +1,15 @@ import express from 'express'; -import { createWallet } from '../controllers/wallet.js'; +import { createWallet, getAllWallets, getWalletByCoin } from '../controllers/wallet.js'; const router = express.Router(); +// Create wallet router.post('/create', createWallet); +// List all wallets +router.get('/', getAllWallets); + +// Get wallet by Coin +router.get('/coin/:coin', getWalletByCoin); + export default router; \ No newline at end of file