update: extend response API for get-assets-price-rates endpoint
This commit is contained in:
parent
fe8ba5e8a9
commit
abdb4a6b4b
4 changed files with 79 additions and 51 deletions
|
|
@ -3,6 +3,10 @@ import UserData from '@/interfaces/common/UserData.js';
|
|||
import Currency from '@/schemes/Currency.js';
|
||||
import Pair from '@/schemes/Pair.js';
|
||||
import { Op } from 'sequelize';
|
||||
import GetAssetsPriceRatesBody from '@/interfaces/bodies/dex/GetAssetsPriceRatesBody.js';
|
||||
import GetAssetsPriceRatesRes, {
|
||||
GetAssetsPriceRatesResPriceRate,
|
||||
} from '@/interfaces/responses/dex/GetAssetsPriceRatesRes.js';
|
||||
import User from '../schemes/User.js';
|
||||
import ordersModel from '../models/Orders.js';
|
||||
import dexModel from '../models/Dex.js';
|
||||
|
|
@ -104,10 +108,10 @@ class DexController {
|
|||
return res.status(200).send(result);
|
||||
}
|
||||
|
||||
async getAssetsPriceRates(req: Request, res: Response) {
|
||||
const { assetsIds } = req.body;
|
||||
getAssetsPriceRates = async (req: Request, res: Response<GetAssetsPriceRatesRes>) => {
|
||||
const { assetsIds } = req.body as GetAssetsPriceRatesBody;
|
||||
|
||||
const currencysRows = await Currency.findAll({
|
||||
const currenciesRows = await Currency.findAll({
|
||||
where: {
|
||||
asset_id: {
|
||||
[Op.in]: assetsIds,
|
||||
|
|
@ -115,17 +119,9 @@ class DexController {
|
|||
},
|
||||
});
|
||||
|
||||
if (!currencysRows) {
|
||||
return res.status(200).send({
|
||||
success: false,
|
||||
data: 'Assets with this id doesn`t exists',
|
||||
});
|
||||
}
|
||||
const currencyIds = currenciesRows.map((currency) => currency.id);
|
||||
|
||||
const currencyIds = currencysRows.map((currency) => currency.id);
|
||||
|
||||
const pairsRows = (
|
||||
(await Pair.findAll({
|
||||
const pairsRows = (await Pair.findAll({
|
||||
where: {
|
||||
first_currency_id: {
|
||||
[Op.in]: currencyIds,
|
||||
|
|
@ -139,37 +135,26 @@ class DexController {
|
|||
attributes: ['asset_id'],
|
||||
},
|
||||
],
|
||||
})) || []
|
||||
).map((pair) => ({
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
asset_id: pair?.first_currency?.asset_id,
|
||||
rate: pair.rate,
|
||||
}));
|
||||
})) as (Pair & { first_currency: Currency })[];
|
||||
|
||||
if (!pairsRows || pairsRows.length === 0) {
|
||||
return res.status(200).send({
|
||||
success: false,
|
||||
data: 'Assets with this id doesn`t exists',
|
||||
const priceRates: GetAssetsPriceRatesResPriceRate[] = pairsRows.map((pairRow) => {
|
||||
const assetId = pairRow.first_currency.asset_id;
|
||||
|
||||
return {
|
||||
asset_id: assetId,
|
||||
rate: pairRow?.rate ?? null,
|
||||
day_change: pairRow?.coefficient ?? null,
|
||||
day_volume: pairRow?.volume ?? null,
|
||||
day_high: pairRow?.high ?? null,
|
||||
day_low: pairRow?.low ?? null,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
// const priceRates = await Promise.all(pairsRows.map(async (pair) => {
|
||||
// const currency = await Currency.findOne({ where: {
|
||||
// id: pair.first_currency_id
|
||||
// }})
|
||||
|
||||
// return {
|
||||
// asset_id: currency?.asset_id,
|
||||
// rate: pair.rate
|
||||
// }
|
||||
// }))
|
||||
|
||||
return res.status(200).send({
|
||||
success: true,
|
||||
priceRates: pairsRows,
|
||||
priceRates,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
async findPairID(req: Request, res: Response) {
|
||||
const { first, second } = req.body;
|
||||
|
|
|
|||
14
src/interfaces/bodies/dex/GetAssetsPriceRatesBody.ts
Normal file
14
src/interfaces/bodies/dex/GetAssetsPriceRatesBody.ts
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import { body } from 'express-validator';
|
||||
|
||||
interface GetAssetsPriceRatesBody {
|
||||
assetsIds: string[];
|
||||
}
|
||||
|
||||
export const getAssetsPriceRatesValidator = [
|
||||
body('assetsIds')
|
||||
.isArray({ min: 1 })
|
||||
.withMessage('assetsIds must be a non-empty array of strings'),
|
||||
body('assetsIds.*').isString().withMessage('Each assetId must be a string'),
|
||||
];
|
||||
|
||||
export default GetAssetsPriceRatesBody;
|
||||
24
src/interfaces/responses/dex/GetAssetsPriceRatesRes.ts
Normal file
24
src/interfaces/responses/dex/GetAssetsPriceRatesRes.ts
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
export type GetAssetsPriceRatesResPriceRate = {
|
||||
asset_id: string;
|
||||
rate: number | null;
|
||||
day_change: number | null;
|
||||
day_volume: number | null;
|
||||
day_high: number | null;
|
||||
day_low: number | null;
|
||||
};
|
||||
|
||||
export type GetAssetsPriceRatesSuccessRes = {
|
||||
success: true;
|
||||
priceRates: GetAssetsPriceRatesResPriceRate[];
|
||||
};
|
||||
|
||||
export enum GetAssetsPriceRatesErrorCode {}
|
||||
|
||||
export type GetAssetsPriceRatesErrorRes = {
|
||||
success: false;
|
||||
data: GetAssetsPriceRatesErrorCode;
|
||||
};
|
||||
|
||||
type GetAssetsPriceRatesRes = GetAssetsPriceRatesSuccessRes | GetAssetsPriceRatesErrorRes;
|
||||
|
||||
export default GetAssetsPriceRatesRes;
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
import express from 'express';
|
||||
import { getAssetsPriceRatesValidator } from '@/interfaces/bodies/dex/GetAssetsPriceRatesBody.js';
|
||||
import dexController from '../controllers/dex.controller.js';
|
||||
import middleware from '../middleware/middleware.js';
|
||||
|
||||
|
|
@ -9,7 +10,11 @@ dexRouter.post('/dex/get-pairs-pages-amount', dexController.getPairsPagesAmount)
|
|||
dexRouter.post('/dex/get-pair', dexController.getPair);
|
||||
dexRouter.post('/dex/renew-bot', middleware.verifyToken, dexController.registerBot);
|
||||
dexRouter.post('/dex/volume-stats', dexController.volumeStats);
|
||||
dexRouter.post('/dex/get-assets-price-rates', dexController.getAssetsPriceRates);
|
||||
dexRouter.post(
|
||||
'/dex/get-assets-price-rates',
|
||||
middleware.expressValidator(getAssetsPriceRatesValidator),
|
||||
dexController.getAssetsPriceRates,
|
||||
);
|
||||
dexRouter.post('/dex/find-pair', dexController.findPairID);
|
||||
|
||||
export default dexRouter;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue