/
api.ts
170 lines (147 loc) · 4.29 KB
/
api.ts
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import * as requests from './requests'
// Config
import { Namespace, endpoints } from '../config'
import { ApiOptionsArguments, ApiOptions } from './types'
// Utils
import {
EIP712Message,
Orderbook,
Tickers,
Orders,
Trade,
Meta,
Wallet
} from '../Utils/types'
export enum ApiError {
Unauthorized = 'Unauthorized',
IncorrectauthAddress = 'IncorrectauthAddress',
ExpiredToken = 'ExpiredToken'
}
/**
* OEX HTTP
*/
export default class Api {
private readonly _options: ApiOptions
protected readonly _endpoint: string
protected _authAddress: string | null = null
protected _signature: string | null = null
public constructor (options?: ApiOptionsArguments) {
// Options
const defaultOptions: ApiOptions = {
namespace: Namespace.production
}
this._options = {
...defaultOptions,
...options
}
this._endpoint = endpoints[this._options.namespace]
}
// ================= HTTP API =================
// Auth
public getAuthLoginData (): Promise<EIP712Message> {
return requests.auth.get(this._endpoint)
}
// Orderbook
public postOrderForm (
order: Orderbook.PostOrderbookForm.RequestBody,
authAddress: string = this._authAddress!,
signature: string = this._signature!
): Promise<Orderbook.PostOrderbookForm.ResponseBody> {
return requests.orderbook.form(
this._endpoint,
order,
authAddress,
signature
)
}
public postOrderSign (
order: Orderbook.PostOrderbook.RequestBody,
authAddress: string = this._authAddress!,
signature: string = this._signature!
): Promise<unknown> {
return requests.orderbook.sign(
this._endpoint,
order,
authAddress,
signature
)
}
public putOrdersCancel (
query: Orderbook.PutOrderbookCancel.RequestQuery,
authAddress: string = this._authAddress!,
signature: string = this._signature!
): Promise<unknown> {
return requests.orderbook.cancel(
this._endpoint,
query,
authAddress,
signature
)
}
public getOrderPreSubmit (
query: Orderbook.GetOrderbookPreSubmit.RequestQuery,
authAddress: string = this._authAddress!,
signature: string = this._signature!
): Promise<Orderbook.GetOrderbookPreSubmit.ResponseBody> {
return requests.orderbook.preSubmit(
this._endpoint,
query,
authAddress,
signature
)
}
// Helpers
public set authAddress (authAddress: string) {
this._authAddress = authAddress
}
public set signature (signature: string) {
this._signature = signature
}
// meta
public metaConfig (): Promise<Meta.GetMetaConfig.ResponseBody> {
return requests.meta.config(this._endpoint)
}
public metaTickersSearch (
query: Partial<Meta.GetMetaTickersSearchParams.RequestQuery>
): Promise<Meta.GetMetaTickersSearchParams.ResponseBody> {
return requests.meta.tickersSearchParams(this._endpoint, query)
}
// wallet
public walletTokenBalance (
authAddress: string = this._authAddress!,
signature: string = this._signature!
): Promise<Wallet.GetWalletBalanceTokens.ResponseBody> {
return requests.wallet.tokenBalance(this._endpoint, authAddress, signature)
}
// tickers
public tickersGetAll (
query: Partial<Tickers.GetTickers.RequestQuery>
): Promise<Tickers.GetTickers.ResponseBody> {
return requests.tickers.all(this._endpoint, query)
}
public tickersGetData (
hash: string
): Promise<Tickers.GetTickersData.ResponseBody> {
return requests.tickers.data(this._endpoint, { hash })
}
public tickersGetDerivatives (
query: Partial<Tickers.GetTickersDerivatives.RequestQuery>
): Promise<unknown> {
return requests.tickers.derivatives(this._endpoint, query)
}
// trades
public tradesGetAllTradesByAddress (
authAddress: string = this._authAddress!,
signature: string = this._signature!
): Promise<Trade.GetAllTradesByAddress.ResponseBody> {
return requests.trades.allAddress(this._endpoint, authAddress, signature)
}
// orders
public ordersGetAllOrdersByAddress (
authAddress: string = this._authAddress!,
signature: string = this._signature!
): Promise<Orders.GetAllOrdersByAddress.ResponseBody> {
return requests.orders.allAddresses(this._endpoint, authAddress, signature)
}
}