import { API_FUTURES_PRIVATE } from './index';
import { TYPE_SIDE } from 'app/screens/futures_onus/constants/order';
import { APIRunBroker, withToken } from '../common';
import moment from 'moment';
import { updateOrdersBySocket } from 'app/store/actions/futures/orders';
const getAccount = () => {
return API_FUTURES_PRIVATE.get('/account'); //assets:[],setting,symbolSettings
};
//get thông tin nạp rút
export const getTotalTrans = () => {
return API_FUTURES_PRIVATE.get('/totalTrans');
};
export const getReward = () => {
return API_FUTURES_PRIVATE.get('/reward');
};
//get thông tin pnl
export const getDailyPnL = (from = 0, to = 0) => {
//set mặc định là timestamp 7 ngày trước
if (!from) {
from = moment()
.subtract(9, 'days')
.valueOf();
}
if (!to) {
to = moment().valueOf();
}
return API_FUTURES_PRIVATE.get('/dailyPnL', {
params: {
from,
to,
},
});
};
const getAccountSetting = () => {
return API_FUTURES_PRIVATE.get('/accountSetting');
};
const getAccountSettingSymbol = ({ symbol }) => {
return API_FUTURES_PRIVATE.get(`/accountSymbolSetting?symbol=${symbol}`);
};
const getBalance = ({ asset }) => {
return API_FUTURES_PRIVATE.get(`/balance?asset=${asset}`);
};
// tạo lệnh
const createOrder = orderBody => {
return API_FUTURES_PRIVATE.post('/order', orderBody);
};
// tạo lệnh có sl tp
const createStrategyOrder = ordersBody => {
return API_FUTURES_PRIVATE.post('/strategyOrder', {
orders: ordersBody,
});
};
// get lệnh có sl tp
const getStrategyOrder = idOrder => {
return API_FUTURES_PRIVATE.get(`/strategyOrder?id=${idOrder}`);
};
//get danh sách vị thế
const getPositions = () => {
return API_FUTURES_PRIVATE.get(`/positions?status=OPEN`);
};
//get danh sách vị thế đóng
const getPositionsClose = ({
symbol,
orderId,
limit,
fromTime,
endTime,
side,
}) => {
let params = {};
if (symbol) params.symbol = symbol;
if (orderId) params.orderId = orderId;
if (limit) params.limit = limit;
if (fromTime) params.fromTime = fromTime;
if (endTime) params.endTime = endTime;
if (side && side !== TYPE_SIDE.ALL) params.side = side;
return API_FUTURES_PRIVATE.get(`/fills`, {
params: params,
});
};
//get danh sách order, lệnh mở(OPEN) và lệnh chờ(UNTRIGGERED)
const getOrders = ({ symbol } = { symbol: '' }) => {
if (!symbol) {
return API_FUTURES_PRIVATE.get(`/orders?status=OPEN&status=UNTRIGGERED`);
}
return API_FUTURES_PRIVATE.get(
`/orders?symbol=${symbol}&status=OPEN&status=UNTRIGGERED`,
);
};
//get danh sách lịch sử order
const getOrdersHistory = ({
symbol,
type,
limit,
fromTime,
endTime,
status,
}) => {
let params = {};
if (symbol) params.symbol = symbol;
if (type) params.type = type;
if (status) params.status = status;
if (limit) params.limit = limit;
if (fromTime) params.fromTime = fromTime;
if (endTime) params.endTime = endTime;
return API_FUTURES_PRIVATE.get(`/history/orderfills`, {
params: params,
});
};
//get danh sách lịch sử transaction
export const getTransactionHistory = ({
type,
limit,
startTime,
endTime,
asset,
}) => {
let params = {};
if (type) params.type = type;
if (asset) params.asset = asset;
if (limit) params.limit = limit;
if (startTime) params.startTime = startTime;
if (endTime) params.endTime = endTime;
return API_FUTURES_PRIVATE.get(`/history/transactions`, {
params: params,
});
};
const changeLeverage = ({ symbol, leverage }) => {
return API_FUTURES_PRIVATE.post('/leverage', {
symbol,
leverage,
});
};
const changeMarginType = ({ symbol, marginType }) => {
return API_FUTURES_PRIVATE.post('/marginType', {
symbol,
marginType,
});
};
//get listen key để socket user
const getKeyStreamUser = () => {
return API_FUTURES_PRIVATE.post('/listenKey');
};
//refresh listen key để socket user
const upadteKeyStreamUser = userId => {
API_FUTURES_PRIVATE.interceptors.request.use(req => {
req.headers['X-ONX-USERID'] = userId;
return req;
});
return API_FUTURES_PRIVATE.put('/listenKey');
};
const cancelOrder = ({ id, symbol, userId }) => {
return API_FUTURES_PRIVATE.delete('/order', {
data: {
symbol,
id,
clientOrderId: userId,
},
});
};
const cancelAllOrder = () => {
return API_FUTURES_PRIVATE.delete('/activeOrders');
};
const cancelTpSl = async ({ dataOrderTp, dataOrderSl, cbSuccess, cbError }) => {
try {
let promiseTp = null;
let promiseSl = null;
if (dataOrderTp) {
promiseTp = cancelOrder(dataOrderTp);
}
if (dataOrderSl) {
promiseSl = cancelOrder(dataOrderSl);
}
if (dataOrderTp && dataOrderSl) {
const res = await Promise.all([promiseTp, promiseSl]);
if (res[0].status === 200 && res[1].status === 200) {
cbSuccess();
if (res[0].data) {
updateOrdersBySocket(res[0].data, false);
}
if (res[1].data) {
updateOrdersBySocket(res[1].data, false);
}
} else {
if (res[0]?.status !== 200) {
cbError(res[0]);
} else {
cbError(res[1]);
}
}
} else if (dataOrderTp) {
const res = await promiseTp;
if (res.status === 200) {
if (res.data) {
updateOrdersBySocket(res.data, false);
}
cbSuccess();
} else {
cbError(res);
}
} else if (dataOrderSl) {
const res = await promiseSl;
if (res.status === 200) {
if (res.data) {
updateOrdersBySocket(res.data, false);
}
cbSuccess();
} else {
cbError(res);
}
}
if (!dataOrderTp && !dataOrderSl) {
cbError(null);
}
} catch (error) {
cbError(null);
console.log('error cancelTpSl', error);
}
};
const createTPAndSL = async ({
dataOrderTp,
dataOrderSl,
cbSuccess,
cbError,
}) => {
try {
let promiseTp = null;
let promiseSl = null;
if (dataOrderTp) {
delete dataOrderTp.size;
promiseTp = createOrder(dataOrderTp);
}
if (dataOrderSl) {
delete dataOrderSl.size;
promiseSl = createOrder(dataOrderSl);
}
if (dataOrderTp && dataOrderSl) {
const res = await Promise.all([promiseTp, promiseSl]);
if (res[0].status === 200 && res[1].status === 200) {
if (res[0].data) {
updateOrdersBySocket(res[0].data, false);
}
if (res[1].data) {
updateOrdersBySocket(res[1].data, false);
}
cbSuccess();
} else {
cbError();
}
} else if (dataOrderTp) {
const res = await promiseTp;
if (res.status === 200) {
if (res.data) {
updateOrdersBySocket(res.data, false);
}
cbSuccess();
} else {
cbError();
}
} else if (dataOrderSl) {
const res = await promiseSl;
if (res.status === 200) {
if (res.data) {
updateOrdersBySocket(res.data, false);
}
cbSuccess();
} else {
cbError();
}
}
} catch (error) {
cbError();
console.log('error cancelTpSl', error);
}
};
const changeMarginPosition = async ({ amount, symbol, type, positionSide }) => {
return API_FUTURES_PRIVATE.post('/positionMargin', {
positionSide,
amount,
symbol,
type,
});
};
const getTradeInfo = () => {
return API_FUTURES_PRIVATE.get(`/tradeInfo`);
};
const apiWithdraw = ({ amount, asset }) => {
return API_FUTURES_PRIVATE.post(`/withdraw`, {
type: 'WITHDRAW',
amount,
asset,
});
};
const getCommissionFutures = token =>
withToken(token, APIRunBroker.get, '/run/futures/onuspro/commissions');
const getTotalVolumeTrade = () => {
return API_FUTURES_PRIVATE.get('/history/vol/lifetime');
};
export {
getAccountSetting,
getAccountSettingSymbol,
getBalance,
createOrder,
cancelOrder,
getPositions,
changeLeverage,
getOrders,
getKeyStreamUser,
getOrdersHistory,
getPositionsClose,
upadteKeyStreamUser,
cancelTpSl,
createTPAndSL,
changeMarginType,
getAccount,
createStrategyOrder,
getStrategyOrder,
changeMarginPosition,
getTradeInfo,
apiWithdraw,
cancelAllOrder,
getCommissionFutures,
getTotalVolumeTrade,
};