trade-frontend/src/utils/methods.ts

421 lines
11 KiB
TypeScript
Raw Normal View History

2025-06-02 11:40:07 +05:00
import CurrencyContentRow from '@/interfaces/common/CurrencyContentRow';
import CurrencyRow from '@/interfaces/common/CurrencyRow';
import Period from '@/interfaces/common/Period';
import ApplyOrderData from '@/interfaces/fetch-data/apply-order/ApplyOrderData';
import GetPageData from '@/interfaces/fetch-data/get-page/GetPageData';
import UpdateOfferData from '@/interfaces/fetch-data/update-offer/UpdateOfferData';
import ErrorRes from '@/interfaces/responses/ErrorRes';
import GetStatsRes from '@/interfaces/responses/offers/GetStatsRes';
import GetConfigRes from '@/interfaces/responses/config/GetConfigRes';
import GetPairsPageRes from '@/interfaces/responses/dex/GetPairsPageRes';
import GetPairRes from '@/interfaces/responses/dex/GetPairRes';
import GetPageRes from '@/interfaces/responses/offers/GetPageRes';
import GetOrdersPageRes from '@/interfaces/responses/orders/GetOrdersPageRes';
import GetUserRes from '@/interfaces/responses/user/GetUserRes';
import GetUserOrdersRes from '@/interfaces/responses/orders/GetUserOrdersRes';
import GetCandlesRes from '@/interfaces/responses/candles/GetCandlesRes';
import GetChartOrdersRes from '@/interfaces/responses/orders/GetChartOrdersRes';
import GetPairStatsRes from '@/interfaces/responses/orders/GetPairStatsRes';
import GetUserOrdersPageRes from '@/interfaces/responses/orders/GetUserOrdersPageRes';
import GetChatRes from '@/interfaces/responses/chats/GetChatRes';
import GetAllChatsRes from '@/interfaces/responses/chats/GetAllChatsRes';
import CreateOrderData from '@/interfaces/fetch-data/create-order/CreateOrderData';
import GetChatChunkRes from '@/interfaces/responses/chats/GetChatChunkRes';
import axios from 'axios';
import GetPairsPagesAmountRes from '@/interfaces/responses/dex/GetPairsPagesAmountRes';
import { PairSortOption } from '@/interfaces/enum/pair';
2026-01-09 20:18:35 +05:00
import { API_URL } from '@/constants';
import { GetUserOrdersData } from '@/interfaces/fetch-data/get-user-orders/GetUserOrdersData';
import GetUserOrdersAllPairsRes from '@/interfaces/responses/orders/GetUserOrdersAllPairsRes';
2026-02-18 03:16:32 +03:00
import { CancelAllData } from '@/interfaces/fetch-data/cancel-all-orders/CancelAllData';
import CancelAllRes from '@/interfaces/responses/orders/CancelAllRes';
2026-01-09 20:18:35 +05:00
const isServer = typeof window === 'undefined';
const baseUrl = isServer ? process.env.API_URL || API_URL || '' : '';
2025-06-02 11:40:07 +05:00
export async function getUser(): Promise<ErrorRes | GetUserRes> {
return axios
.post('/api/user/get-user', {
token: sessionStorage.getItem('token'),
})
.then((res) => res.data);
}
export async function getConfig(): Promise<ErrorRes | GetConfigRes> {
return axios.get('/api/config').then((res) => res.data);
}
export async function updateOffer(
offerData: UpdateOfferData,
): Promise<ErrorRes | { success: true }> {
return axios
.post('/api/offers/update', {
token: sessionStorage.getItem('token'),
offerData,
})
.then((res) => res.data);
}
export async function deleteOffer(number: string): Promise<ErrorRes | { success: true }> {
return axios
.post('/api/offers/delete', {
token: sessionStorage.getItem('token'),
offerData: {
number: number ?? null,
},
})
.then((res) => res.data);
}
export async function getPage(
params: GetPageData,
host: string | undefined = undefined,
): Promise<ErrorRes | GetPageRes> {
return axios
.post(`${host ? `http://${host}` : ''}/api/offers/get-page`, {
data: params,
})
.then((res) => res.data);
}
2026-01-17 19:16:10 +05:00
export async function getStats(): Promise<GetStatsRes> {
return (await fetch(`${baseUrl}/api/offers/get-stats`).then((res) =>
2025-06-02 11:40:07 +05:00
res.json(),
)) as GetStatsRes;
}
export async function findPairID(
first: string,
second: string,
host: string | undefined = undefined,
): Promise<number | undefined> {
const findPairURL = `${host ?? ''}/api/dex/find-pair`;
2025-06-02 11:40:07 +05:00
console.log('Find pair URL:', findPairURL);
return (await fetch(findPairURL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
first,
second,
}),
})
.then((res) => res.json())
.then((res) => parseInt(res?.data, 10) || undefined)) as number | undefined;
}
export function getFormattedCurrencies(currencies: CurrencyRow[]): CurrencyContentRow[] {
const currenciesElements = [
{
name: 'Crypto currencies',
data: currencies
.filter((e) => e.type === 'crypto')
.map((e) => ({
...e,
})),
},
{
name: 'Fiat currencies',
data: currencies
.filter((e) => e.type === 'fiat')
.map((e) => ({
...e,
})),
},
];
return currenciesElements;
}
export async function sendFavouriteCurrencies(
currs: string[],
): Promise<ErrorRes | { success: true }> {
return axios
.post('/api/user/set-favourite-currencies', {
token: sessionStorage.getItem('token'),
data: currs,
})
.then((res) => res.data);
}
export async function createChat(
number: string,
pay: string,
receive: string,
): Promise<ErrorRes | { success: true; data: number }> {
return axios
.post('/api/chats/create', {
token: sessionStorage.getItem('token'),
number,
chatData: {
pay,
receive,
},
})
.then((res) => res.data);
}
export async function getChat(id: string): Promise<ErrorRes | GetChatRes> {
return axios
.post('/api/chats/get-chat', {
token: sessionStorage.getItem('token'),
id,
})
.then((res) => res.data);
}
export async function getAllChats(): Promise<ErrorRes | GetAllChatsRes> {
return axios
.post('/api/chats/get-all-chats', {
token: sessionStorage.getItem('token'),
})
.then((res) => res.data);
}
export async function deleteChat(
id: string,
): Promise<ErrorRes | { success: true; data?: undefined }> {
return axios
.post('/api/chats/delete-chat', {
token: sessionStorage.getItem('token'),
id,
})
.then((res) => res.data);
}
export async function getPairsPage(
page: number,
searchText: string,
whitelistedOnly: boolean,
sortOption: PairSortOption,
): Promise<ErrorRes | GetPairsPageRes> {
return axios
2026-01-09 20:18:35 +05:00
.post(`${baseUrl}/api/dex/get-pairs-page`, {
2025-06-02 11:40:07 +05:00
page,
searchText,
whitelistedOnly,
sortOption,
})
.then((res) => res.data);
}
export async function getPairsPagesAmount(
searchText: string,
whitelistedOnly: boolean,
): Promise<ErrorRes | GetPairsPagesAmountRes> {
return axios
.post('/api/dex/get-pairs-pages-amount', {
searchText,
whitelistedOnly,
})
.then((res) => res.data);
}
export async function getPair(id: string): Promise<ErrorRes | GetPairRes> {
return axios
2026-01-09 20:18:35 +05:00
.post(`${baseUrl}/api/dex/get-pair`, {
2025-06-02 11:40:07 +05:00
id,
})
.then((res) => res.data);
}
export async function createOrder(
orderData: CreateOrderData,
): Promise<ErrorRes | { success: true; data: { immediateMatch: boolean } }> {
return axios
.post('/api/orders/create', {
token: sessionStorage.getItem('token'),
orderData,
})
.then((res) => res.data);
}
export async function getOrdersPage(pairId: string): Promise<ErrorRes | GetOrdersPageRes> {
return axios
2026-01-09 20:18:35 +05:00
.post(`${baseUrl}/api/orders/get-page`, {
2025-06-02 11:40:07 +05:00
pairId,
})
.then((res) => res.data);
}
export async function getUserOrdersPage(pairId: string): Promise<ErrorRes | GetUserOrdersPageRes> {
return axios
.post('/api/orders/get-user-page', {
token: sessionStorage.getItem('token'),
pairId,
})
.then((res) => res.data);
}
export async function getUserOrders({
limit,
offset,
2026-02-18 03:28:02 +03:00
filterInfo: { pairId, status, type, date },
}: GetUserOrdersData): Promise<ErrorRes | GetUserOrdersRes> {
2025-06-02 11:40:07 +05:00
return axios
.patch('/api/orders/get', {
2025-06-02 11:40:07 +05:00
token: sessionStorage.getItem('token'),
limit,
offset,
filterInfo: {
2026-02-18 03:28:02 +03:00
pairId,
status,
type,
date: date
? {
from: date.from,
to: date.to,
}
: undefined,
},
2025-06-02 11:40:07 +05:00
})
.then((res) => res.data);
}
export async function getUserOrdersAllPairs(): Promise<ErrorRes | GetUserOrdersAllPairsRes> {
return axios
.patch('/api/orders/get-user-orders-pairs', {
token: sessionStorage.getItem('token'),
})
.then((res) => res.data);
}
2025-06-02 11:40:07 +05:00
export async function cancelOrder(id: string): Promise<ErrorRes | { success: true }> {
return axios
.post('/api/orders/cancel', {
token: sessionStorage.getItem('token'),
orderId: id,
})
.then((res) => res.data);
}
2025-09-14 18:16:48 +07:00
export async function cancelTransaction(id: string): Promise<ErrorRes | { success: true }> {
return axios
.post('/api/transactions/cancel', {
token: sessionStorage.getItem('token'),
transactionId: id,
})
.then((res) => res.data);
}
2025-06-02 11:40:07 +05:00
export async function getCandles(
pairId: string,
period: Period,
): Promise<ErrorRes | GetCandlesRes> {
return axios
2026-01-09 20:18:35 +05:00
.post(`${baseUrl}/api/orders/get-candles`, {
2025-06-02 11:40:07 +05:00
pairId,
period,
})
.then((res) => res.data);
}
export async function getChartOrders(pairId: string): Promise<ErrorRes | GetChartOrdersRes> {
return axios
.post('/api/orders/get-chart-orders', {
pairId,
})
.then((res) => res.data);
}
export async function getPairStats(pairId: string): Promise<ErrorRes | GetPairStatsRes> {
return axios
2026-01-09 20:18:35 +05:00
.post(`${baseUrl}/api/orders/get-pair-stats`, {
2025-06-02 11:40:07 +05:00
pairId,
})
.then((res) => res.data);
}
export async function applyOrder(orderData: ApplyOrderData): Promise<ErrorRes | { success: true }> {
return axios
.post('/api/orders/apply-order', {
token: sessionStorage.getItem('token'),
orderData,
})
.then((res) => res.data);
}
2026-02-18 03:16:32 +03:00
export async function cancelAllOrders({
filterInfo: { pairId, type, date },
}: CancelAllData): Promise<ErrorRes | CancelAllRes> {
return axios
.patch('/api/orders/cancel-all', {
token: sessionStorage.getItem('token'),
filterInfo: {
pairId,
type,
date: date
? {
from: date.from,
to: date.to,
}
: undefined,
},
})
.then((res) => res.data);
}
2025-06-02 11:40:07 +05:00
export async function confirmTransaction(
transactionId: string,
): Promise<ErrorRes | { success: true }> {
return axios
.post('/api/transactions/confirm', {
transactionId,
token: sessionStorage.getItem('token'),
})
.then((res) => res.data);
}
export async function getChatChunk(
chatId: string,
chunkNumber: number,
): Promise<ErrorRes | GetChatChunkRes> {
return axios
.post('/api/chats/get-chat-chunk', {
token: sessionStorage.getItem('token'),
id: chatId,
chunkNumber,
})
.then((res) => res.data);
}
export async function getTrades(pairId: string) {
2025-06-02 11:40:07 +05:00
return axios
2026-01-09 20:18:35 +05:00
.post(`${baseUrl}/api/orders/get-trades`, {
pairId,
})
2025-06-02 11:40:07 +05:00
.then((res) => res.data);
}
2025-08-02 15:27:26 +05:00
export async function getUserPendings() {
2025-08-02 15:27:26 +05:00
return axios
.post('/api/transactions/get-my-pending', {
token: sessionStorage.getItem('token'),
2025-08-02 15:27:26 +05:00
})
.then((res) => res.data);
}
2025-08-08 16:40:10 +05:00
export async function getLetheanPrice() {
return axios
.get(
'https://explorer.lethean.io/api/price?asset_id=d6329b5b1f7c0805b5c345f4957554002a2f557845f64d7645dae0e051a6498a',
)
.then((res) => res.data);
}
2025-08-08 16:40:10 +05:00
export async function getMatrixAddresses(addresses: string[]) {
try {
const { data } = await axios.post('https://messenger.lethean.io/api/get-addresses', {
2025-08-08 16:40:10 +05:00
addresses,
});
return data;
} catch (error) {
console.log(error);
}
}