add: my trades api
This commit is contained in:
parent
c7660640bc
commit
4f0d9fda53
8 changed files with 162 additions and 38 deletions
|
|
@ -239,6 +239,27 @@ class OrdersController {
|
|||
res.status(500).send({ success: false, data: 'Unhandled error' });
|
||||
}
|
||||
}
|
||||
|
||||
async getTrades(req: Request, res: Response) {
|
||||
try {
|
||||
const { pairId } = req.body;
|
||||
|
||||
if (!pairId) {
|
||||
return res.status(400).send({ success: false, data: 'Invalid pair data' });
|
||||
}
|
||||
|
||||
const result = await ordersModel.getTrades(Number(pairId));
|
||||
|
||||
if (result.data === 'Invalid pair data') return res.status(400).send(result);
|
||||
|
||||
if (result.data === 'Internal error') return res.status(500).send(result);
|
||||
|
||||
res.status(200).send(result);
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
res.status(500).send({ success: false, data: 'Unhandled error' });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const ordersController = new OrdersController();
|
||||
|
|
|
|||
15
src/interfaces/common/Transaction.ts
Normal file
15
src/interfaces/common/Transaction.ts
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
import Order from "@/schemes/Order";
|
||||
import Transaction from "@/schemes/Transaction";
|
||||
import User from "@/schemes/User";
|
||||
|
||||
|
||||
interface TransactionWithOrders extends Transaction {
|
||||
buy_order: Order & {
|
||||
user: User;
|
||||
};
|
||||
sell_order: Order & {
|
||||
user: User;
|
||||
};
|
||||
}
|
||||
|
||||
export default TransactionWithOrders;
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
import { Op } from 'sequelize';
|
||||
import Decimal from 'decimal.js';
|
||||
import TransactionWithOrders from '@/interfaces/common/Transaction.js';
|
||||
import configModel from './Config.js';
|
||||
import dexModel from './Dex.js';
|
||||
import userModel from './User.js';
|
||||
|
|
@ -573,6 +574,68 @@ class OrdersModel {
|
|||
return { success: false, data: 'Internal error' };
|
||||
}
|
||||
}
|
||||
|
||||
async getTrades(pairId: number) {
|
||||
try {
|
||||
const pair = await dexModel.getPairRow(pairId);
|
||||
|
||||
if (!pair) {
|
||||
return { success: false, data: 'Invalid pair data' };
|
||||
}
|
||||
|
||||
const transactions = (await Transaction.findAll({
|
||||
where: { status: 'confirmed' },
|
||||
include: [
|
||||
{
|
||||
model: Order,
|
||||
as: 'buy_order',
|
||||
where: { pair_id: pairId },
|
||||
include: [
|
||||
{
|
||||
model: User,
|
||||
as: 'user',
|
||||
attributes: ['address'],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
model: Order,
|
||||
as: 'sell_order',
|
||||
where: { pair_id: pairId },
|
||||
include: [
|
||||
{
|
||||
model: User,
|
||||
as: 'user',
|
||||
attributes: ['address'],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
order: [['timestamp', 'DESC']],
|
||||
})) as TransactionWithOrders[];
|
||||
|
||||
const trades = transactions.map((tx) => ({
|
||||
id: tx.id,
|
||||
timestamp: tx.timestamp,
|
||||
amount: tx.amount,
|
||||
price: tx.buy_order.price,
|
||||
type: tx.creator,
|
||||
buyer: {
|
||||
address: tx.buy_order.user.address,
|
||||
id: tx.buy_order.user_id,
|
||||
},
|
||||
seller: {
|
||||
address: tx.sell_order.user.address,
|
||||
id: tx.sell_order.user_id,
|
||||
},
|
||||
}));
|
||||
|
||||
return { success: true, data: trades };
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
return { success: false, data: 'Internal error' };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const ordersModel = new OrdersModel();
|
||||
|
|
|
|||
|
|
@ -24,5 +24,6 @@ ordersRouter.post('/orders/get-candles', ordersController.getCandles);
|
|||
ordersRouter.post('/orders/get-chart-orders', ordersController.getChartOrders);
|
||||
ordersRouter.post('/orders/get-pair-stats', ordersController.getPairStats);
|
||||
ordersRouter.post('/orders/apply-order', ordersController.applyOrder);
|
||||
ordersRouter.post('/orders/get-trades', ordersController.getTrades);
|
||||
|
||||
export default ordersRouter;
|
||||
|
|
|
|||
60
src/schemes/Associations.ts
Normal file
60
src/schemes/Associations.ts
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
import Transaction from './Transaction';
|
||||
import Order from './Order';
|
||||
import User from './User';
|
||||
import Pair from './Pair';
|
||||
|
||||
export function setupAssociations() {
|
||||
Transaction.belongsTo(Order, {
|
||||
foreignKey: 'buy_order_id',
|
||||
as: 'buy_order',
|
||||
onDelete: 'CASCADE',
|
||||
onUpdate: 'CASCADE',
|
||||
hooks: true,
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
Transaction.belongsTo(Order, {
|
||||
foreignKey: 'sell_order_id',
|
||||
as: 'sell_order',
|
||||
onDelete: 'CASCADE',
|
||||
onUpdate: 'CASCADE',
|
||||
hooks: true,
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
Order.hasMany(Transaction, {
|
||||
foreignKey: 'buy_order_id',
|
||||
as: 'buy_orders',
|
||||
onDelete: 'CASCADE',
|
||||
onUpdate: 'CASCADE',
|
||||
hooks: true,
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
Order.hasMany(Transaction, {
|
||||
foreignKey: 'sell_order_id',
|
||||
as: 'sell_orders',
|
||||
onDelete: 'CASCADE',
|
||||
onUpdate: 'CASCADE',
|
||||
hooks: true,
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
Order.belongsTo(Pair, {
|
||||
foreignKey: 'pair_id',
|
||||
as: 'pair',
|
||||
onDelete: 'CASCADE',
|
||||
onUpdate: 'CASCADE',
|
||||
hooks: true,
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
Order.belongsTo(User, {
|
||||
foreignKey: 'user_id',
|
||||
as: 'user',
|
||||
onDelete: 'CASCADE',
|
||||
onUpdate: 'CASCADE',
|
||||
hooks: true,
|
||||
constraints: false,
|
||||
});
|
||||
}
|
||||
|
|
@ -1,7 +1,5 @@
|
|||
import { Model, DataTypes } from 'sequelize';
|
||||
import sequelize from '../sequelize';
|
||||
import Transaction from './Transaction';
|
||||
import Pair from './Pair';
|
||||
|
||||
class Order extends Model {
|
||||
declare readonly id: number;
|
||||
|
|
@ -89,31 +87,4 @@ Order.init(
|
|||
},
|
||||
);
|
||||
|
||||
Order.hasMany(Transaction, {
|
||||
foreignKey: 'buy_order_id',
|
||||
as: 'buy_orders',
|
||||
onDelete: 'CASCADE',
|
||||
onUpdate: 'CASCADE',
|
||||
hooks: true,
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
Order.hasMany(Transaction, {
|
||||
foreignKey: 'sell_order_id',
|
||||
as: 'sell_orders',
|
||||
onDelete: 'CASCADE',
|
||||
onUpdate: 'CASCADE',
|
||||
hooks: true,
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
Order.belongsTo(Pair, {
|
||||
foreignKey: 'pair_id',
|
||||
as: 'pair',
|
||||
onDelete: 'CASCADE',
|
||||
onUpdate: 'CASCADE',
|
||||
hooks: true,
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
export default Order;
|
||||
|
|
|
|||
|
|
@ -67,13 +67,4 @@ User.hasMany(Order, {
|
|||
hooks: true,
|
||||
});
|
||||
|
||||
Order.belongsTo(User, {
|
||||
foreignKey: 'user_id',
|
||||
as: 'user',
|
||||
onDelete: 'CASCADE',
|
||||
onUpdate: 'CASCADE',
|
||||
hooks: true,
|
||||
constraints: false,
|
||||
});
|
||||
|
||||
export default User;
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ import Currency, { Asset } from './schemes/Currency';
|
|||
import User from './schemes/User';
|
||||
import statsRouter from './routes/stats.router';
|
||||
import exchangeModel from './models/ExchangeTransactions';
|
||||
import { setupAssociations } from './schemes/Associations';
|
||||
|
||||
const PORT = process.env.PORT || 3000;
|
||||
|
||||
|
|
@ -42,6 +43,7 @@ process.on('unhandledRejection', (reason, promise) => {
|
|||
await initdb();
|
||||
await sequelize.authenticate();
|
||||
await sequelize.sync();
|
||||
await setupAssociations();
|
||||
|
||||
const zanoRow = await Currency.findOne({ where: { asset_id: ZANO_ASSET_ID } });
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue