forked from igorek17000/alunajs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBitfinexHttp.ts
200 lines (134 loc) · 3.7 KB
/
BitfinexHttp.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import axios from 'axios'
import crypto from 'crypto'
import {
IAlunaHttp,
IAlunaHttpAuthedParams,
IAlunaHttpPublicParams,
IAlunaHttpRequestCount,
} from '../../lib/core/IAlunaHttp'
import { AlunaHttpVerbEnum } from '../../lib/enums/AlunaHtttpVerbEnum'
import { IAlunaCredentialsSchema } from '../../lib/schemas/IAlunaCredentialsSchema'
import { IAlunaSettingsSchema } from '../../lib/schemas/IAlunaSettingsSchema'
import { assembleRequestConfig } from '../../utils/axios/assembleRequestConfig'
import { AlunaCache } from '../../utils/cache/AlunaCache'
import { handleBitfinexRequestError } from './errors/handleBitfinexRequestError'
export const BITFINEX_HTTP_CACHE_KEY_PREFIX = 'BitfinexHttp.publicRequest'
export class BitfinexHttp implements IAlunaHttp {
public settings: IAlunaSettingsSchema
public requestWeight: IAlunaHttpRequestCount
constructor(settings: IAlunaSettingsSchema) {
this.requestWeight = {
authed: 0,
public: 0,
}
this.settings = settings
}
public async publicRequest <T>(
params: IAlunaHttpPublicParams,
): Promise<T> {
const {
url,
body,
verb = AlunaHttpVerbEnum.GET,
weight = 1,
} = params
const settings = (params.settings || this.settings)
const {
disableCache = false,
cacheTtlInSeconds = 60,
} = settings
const cacheKey = AlunaCache.hashCacheKey({
args: params,
prefix: BITFINEX_HTTP_CACHE_KEY_PREFIX,
})
if (!disableCache && AlunaCache.cache.has(cacheKey)) {
return AlunaCache.cache.get<T>(cacheKey) as T
}
const { proxySettings } = settings
const { requestConfig } = assembleRequestConfig({
url,
method: verb,
data: body,
proxySettings,
})
this.requestWeight.public += weight
try {
const { data } = await axios.create().request<T>(requestConfig)
if (!disableCache) {
AlunaCache.cache.set<T>(cacheKey, data, cacheTtlInSeconds)
}
return data
} catch (error) {
throw handleBitfinexRequestError({ error })
}
}
public async authedRequest <T>(
params: IAlunaHttpAuthedParams,
): Promise<T> {
const {
url,
body = {},
verb = AlunaHttpVerbEnum.POST,
credentials,
weight = 1,
} = params
const settings = (params.settings || this.settings)
const signedHash = generateAuthHeader({
credentials,
body,
url,
})
const { proxySettings } = settings
const { requestConfig } = assembleRequestConfig({
url,
method: verb,
data: body,
headers: signedHash,
proxySettings,
})
this.requestWeight.authed += weight
try {
const { data } = await axios.create().request<T>(requestConfig)
return data
} catch (error) {
throw handleBitfinexRequestError({ error })
}
}
}
interface ISignedHashParams {
url: string
body: Record<any, any>
credentials: IAlunaCredentialsSchema
}
export interface IBitfinexSignedHeaders {
'Content-Type': string
'bfx-nonce': string
'bfx-apikey': string
'bfx-signature': string
}
export const generateAuthHeader = (
params: ISignedHashParams,
): IBitfinexSignedHeaders => {
const {
credentials,
body,
url,
} = params
const {
key,
secret,
} = credentials
const path = new URL(url).pathname
const nonce = (Date.now() * 1000).toString()
const payload = `/api${path}${nonce}${JSON.stringify(body)}`
const sig = crypto.createHmac('sha384', secret)
.update(payload)
.digest('hex')
const headers: IBitfinexSignedHeaders = {
'Content-Type': 'application/json',
'bfx-nonce': nonce,
'bfx-apikey': key,
'bfx-signature': sig,
}
return headers
}