-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdexapi.js
More file actions
113 lines (102 loc) · 4.19 KB
/
dexapi.js
File metadata and controls
113 lines (102 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { getConfig } from './utils.js';
// Contains methods for interacting with the off-chain DEX API
const apiConfig = getConfig().api;
/**
* Generic GET request to one of the APIs
* @param {string} root - root url for the API, ex. https://metal-dexdb.global.binfra.one/dex
* @param {string} path - path for data, ex. /v1/markets/all
* @param {bool} returnData - true to return data property contents
* @returns {Promise<object>} - json data
*/
const fetchFromAPI = async (root, path, returnData = true) => {
const response = await fetch(`${root}${path}`);
const responseJson = await response.json();
if (returnData) {
return responseJson.data;
}
return responseJson;
};
/**
* Get all available markets
* @returns {Promise<array>} - list of all markets available on ProtonDEX
*/
export const fetchMarkets = async () => {
const marketData = await fetchFromAPI(apiConfig.apiRoot, '/v1/markets/all');
return marketData;
};
/**
* Return an orderbook for the provided market. Use a higher step number for low priced currencies
* @param {string} symbol - market symbol
* @param {number} limit - maximum number of records to return
* @param {number} step - controls aggregation by price; ex. 0.01, 0.1, 1, 10, 100
* @returns {Promise<object>} - asks and bids for the market
*/
export const fetchOrderBook = async (symbol, limit = 100, step = 1000) => {
const orderBook = await fetchFromAPI(apiConfig.apiRoot, `/v1/orders/depth?symbol=${symbol}&limit=${limit}&step=${step}`);
return orderBook;
};
/**
* Get all open orders for a given user
* @param {string} username - name of proton user/account to retrieve orders for
* @returns {Promise<array>} - list of all open orders
*/
export const fetchOpenOrders = async (username) => {
const openOrders = await fetchFromAPI(apiConfig.apiRoot, `/v1/orders/open?limit=100&offset=0&account=${username}`);
return openOrders;
};
/**
* Return history of unopened orders for a given user
* @param {string} username - name of proton user/account to retrieve history for
* @param {number} limit - maximum number of records to return
* @param {number} offset - where to start in the list - used for paging
* @returns {Promise<array>} - returns an array of orders, most recent first
*/
export const fetchOrderHistory = async (username, limit = 100, offset = 0) => {
const orderHistory = await fetchFromAPI(apiConfig.apiRoot, `/v1/orders/history?limit=${limit}&offset=${offset}&account=${username}`);
return orderHistory;
};
/**
* Given a market symbol, return the most recent trades to have executed in that market
* @param {string} symbol - market symbol
* @param {number} count - how many trades to return
* @param {number} offset - where to start, used for paging
* @returns a list of recent trades
*/
export const fetchTrades = async (symbol, count = 100, offset = 0) => {
const response = await fetchFromAPI(apiConfig.apiRoot, `/v1/trades/recent?symbol=${symbol}&limit=${count}&offset=${offset}`);
return response;
};
/**
* Given a market symbol, get the price for it
* @param {string} symbol - market symbol
* @returns returns the price of the most recently executed trade
*/
export const fetchLatestPrice = async (symbol) => {
const trades = await fetchTrades(symbol, 1);
return trades[0].price;
};
/**
*
* @param {string} username - name of proton user/account to retrieve history for
* @returns {Promise<array>} - array of balances,
* ex. {"decimals":"4","contract":"eosio.token","amount":"123.4567","currency":"XPR"}
*/
export const fetchBalances = async (username) => {
const chain = process.env.NODE_ENV === 'test' ? 'protontest' : 'proton';
const response = await fetchFromAPI(apiConfig.lightApiRoot, `/balances/${chain}/${username}`, false);
return response.balances;
};
const markets = { byId: {}, bySymbol: {} };
export const getMarketById = (id) => markets.byId[id];
export const getMarketBySymbol = (symbol) => markets.bySymbol[symbol];
/**
* Initialize. Gets and stores all dex markets
*/
export const initialize = async () => {
// load all markets for later use
const allMarkets = await fetchMarkets();
allMarkets.forEach((market) => {
markets.byId[market.market_id] = market;
markets.bySymbol[market.symbol] = market;
});
};