-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.ts
367 lines (308 loc) · 11.8 KB
/
index.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import log from 'loglevel'
import fetch from 'cross-fetch'
import { OrderKind, QuoteQuery } from '@gnosis.pm/gp-v2-contracts'
import { SupportedChainId as ChainId } from '../../constants/chains'
import { getSigningSchemeApiValue, OrderCreation } from '../../utils/sign'
import OperatorError, { ApiErrorCodeDetails, ApiErrorCodes, ApiErrorObject } from './errors/OperatorError'
import QuoteError, {
GpQuoteErrorCodes,
GpQuoteErrorObject,
mapOperatorErrorToQuoteError,
GpQuoteErrorDetails,
} from './errors/QuoteError'
import { toErc20Address } from '../../utils/tokens'
import { FeeQuoteParams, PriceInformation, PriceQuoteParams, SimpleGetQuoteResponse } from '../../utils/price'
import { ZERO_ADDRESS } from '../../constants'
import {
GetOrdersParams,
GetTradesParams,
OrderCancellationParams,
OrderID,
OrderMetaData,
ProfileData,
TradeMetaData,
} from './types'
import { CowError, logPrefix, objectToQueryString } from '../../utils/common'
import { Context } from '../../utils/context'
function getGnosisProtocolUrl(isDev: boolean): Partial<Record<ChainId, string>> {
if (isDev) {
return {
[ChainId.MAINNET]: 'https://barn.api.cow.fi/mainnet/api',
[ChainId.RINKEBY]: 'https://barn.api.cow.fi/rinkeby/api',
[ChainId.GNOSIS_CHAIN]: 'https://barn.api.cow.fi/xdai/api',
}
}
return {
[ChainId.MAINNET]: 'https://api.cow.fi/mainnet/api',
[ChainId.RINKEBY]: 'https://api.cow.fi/rinkeby/api',
[ChainId.GNOSIS_CHAIN]: 'https://api.cow.fi/xdai/api',
}
}
function getProfileUrl(isDev: boolean): Partial<Record<ChainId, string>> {
if (isDev) {
return {
[ChainId.MAINNET]: 'https://barn.api.cow.fi/affiliate/api',
}
}
return {
[ChainId.MAINNET]: 'https://api.cow.fi/affiliate/api',
}
}
const UNHANDLED_QUOTE_ERROR: GpQuoteErrorObject = {
errorType: GpQuoteErrorCodes.UNHANDLED_ERROR,
description: GpQuoteErrorDetails.UNHANDLED_ERROR,
}
const UNHANDLED_ORDER_ERROR: ApiErrorObject = {
errorType: ApiErrorCodes.UNHANDLED_CREATE_ERROR,
description: ApiErrorCodeDetails.UNHANDLED_CREATE_ERROR,
}
async function _handleQuoteResponse<T = any, P extends QuoteQuery = QuoteQuery>(
response: Response,
params?: P
): Promise<T> {
if (!response.ok) {
const errorObj: ApiErrorObject = await response.json()
// we need to map the backend error codes to match our own for quotes
const mappedError = mapOperatorErrorToQuoteError(errorObj)
const quoteError = new QuoteError(mappedError)
if (params) {
const { sellToken, buyToken } = params
log.error(logPrefix, `Error querying fee from API - sellToken: ${sellToken}, buyToken: ${buyToken}`)
}
throw quoteError
} else {
return response.json()
}
}
export class CowApi {
context: Context
API_NAME = 'CoW Protocol'
constructor(context: Context) {
this.context = context
}
get DEFAULT_HEADERS() {
return { 'Content-Type': 'application/json', 'X-AppId': this.context.appDataHash }
}
get API_BASE_URL() {
return getGnosisProtocolUrl(this.context.isDevEnvironment)
}
get PROFILE_API_BASE_URL(): Partial<Record<ChainId, string>> {
return getProfileUrl(this.context.isDevEnvironment)
}
async getProfileData(address: string): Promise<ProfileData | null> {
const chainId = await this.context.chainId
log.debug(logPrefix, `[api:${this.API_NAME}] Get profile data for`, chainId, address)
if (chainId !== ChainId.MAINNET) {
log.info(logPrefix, 'Profile data is only available for mainnet')
return null
}
const response = await this.getProfile(`/profile/${address}`)
if (!response.ok) {
const errorResponse = await response.json()
log.error(logPrefix, errorResponse)
throw new CowError(errorResponse?.description)
} else {
return response.json()
}
}
async getTrades(params: GetTradesParams): Promise<TradeMetaData[]> {
const { owner, limit, offset } = params
const qsParams = objectToQueryString({ owner, limit, offset })
const chainId = await this.context.chainId
log.debug(logPrefix, '[util:operator] Get trades for', chainId, owner, { limit, offset })
try {
const response = await this.get(`/trades${qsParams}`)
if (!response.ok) {
const errorResponse = await response.json()
throw new CowError(errorResponse)
} else {
return response.json()
}
} catch (error) {
log.error(logPrefix, 'Error getting trades:', error)
throw new CowError('Error getting trades: ' + error)
}
}
async getOrders(params: GetOrdersParams): Promise<OrderMetaData[]> {
const { owner, limit = 1000, offset = 0 } = params
const queryString = objectToQueryString({ limit, offset })
const chainId = await this.context.chainId
log.debug(logPrefix, `[api:${this.API_NAME}] Get orders for `, chainId, owner, limit, offset)
try {
const response = await this.get(`/account/${owner}/orders/${queryString}`)
if (!response.ok) {
const errorResponse: ApiErrorObject = await response.json()
throw new OperatorError(errorResponse)
} else {
return response.json()
}
} catch (error) {
log.error(logPrefix, 'Error getting orders information:', error)
throw new OperatorError(UNHANDLED_ORDER_ERROR)
}
}
async getTxOrders(txHash: string): Promise<OrderMetaData[]> {
const chainId = await this.context.chainId
log.debug(`[api:${this.API_NAME}] Get tx orders for `, chainId, txHash)
try {
const response = await this.get(`/transactions/${txHash}/orders`)
if (!response.ok) {
const errorResponse: ApiErrorObject = await response.json()
throw new OperatorError(errorResponse)
} else {
return response.json()
}
} catch (error) {
log.error('Error getting transaction orders information:', error)
if (error instanceof OperatorError) throw error
throw new OperatorError(UNHANDLED_ORDER_ERROR)
}
}
async getOrder(orderId: string): Promise<OrderMetaData | null> {
const chainId = await this.context.chainId
log.debug(logPrefix, `[api:${this.API_NAME}] Get order for `, chainId, orderId)
try {
const response = await this.get(`/orders/${orderId}`)
if (!response.ok) {
const errorResponse: ApiErrorObject = await response.json()
throw new OperatorError(errorResponse)
} else {
return response.json()
}
} catch (error) {
log.error(logPrefix, 'Error getting order information:', error)
throw new OperatorError(UNHANDLED_ORDER_ERROR)
}
}
async getPriceQuoteLegacy(params: PriceQuoteParams): Promise<PriceInformation | null> {
const { baseToken, quoteToken, amount, kind } = params
const chainId = await this.context.chainId
log.debug(logPrefix, `[api:${this.API_NAME}] Get price from API`, params, 'for', chainId)
const response = await this.get(
`/markets/${toErc20Address(baseToken, chainId)}-${toErc20Address(quoteToken, chainId)}/${kind}/${amount}`
).catch((error) => {
log.error(logPrefix, 'Error getting price quote:', error)
throw new QuoteError(UNHANDLED_QUOTE_ERROR)
})
return _handleQuoteResponse<PriceInformation | null>(response)
}
async getQuote(params: FeeQuoteParams): Promise<SimpleGetQuoteResponse> {
const chainId = await this.context.chainId
const quoteParams = this.mapNewToLegacyParams(params, chainId)
const response = await this.post('/quote', quoteParams)
return _handleQuoteResponse<SimpleGetQuoteResponse>(response)
}
async sendSignedOrderCancellation(params: OrderCancellationParams): Promise<void> {
const { cancellation, owner: from } = params
const chainId = await this.context.chainId
log.debug(logPrefix, `[api:${this.API_NAME}] Delete signed order for network`, chainId, cancellation)
const response = await this.delete(`/orders/${cancellation.orderUid}`, {
signature: cancellation.signature,
signingScheme: getSigningSchemeApiValue(cancellation.signingScheme),
from,
})
if (!response.ok) {
// Raise an exception
const errorMessage = await OperatorError.getErrorFromStatusCode(response, 'delete')
throw new CowError(errorMessage)
}
log.debug(logPrefix, `[api:${this.API_NAME}] Cancelled order`, cancellation.orderUid, chainId)
}
async sendOrder(params: { order: Omit<OrderCreation, 'appData'>; owner: string }): Promise<OrderID> {
const fullOrder: OrderCreation = { ...params.order, appData: this.context.appDataHash }
const chainId = await this.context.chainId
const { owner } = params
log.debug(logPrefix, `[api:${this.API_NAME}] Post signed order for network`, chainId, fullOrder)
// Call API
const response = await this.post(`/orders`, {
...fullOrder,
signingScheme: getSigningSchemeApiValue(fullOrder.signingScheme),
from: owner,
})
// Handle response
if (!response.ok) {
// Raise an exception
const errorMessage = await OperatorError.getErrorFromStatusCode(response, 'create')
throw new CowError(errorMessage)
}
const uid = (await response.json()) as string
log.debug(logPrefix, `[api:${this.API_NAME}] Success posting the signed order`, uid)
return uid
}
getOrderLink(orderId: OrderID): string {
const baseUrl = this.getApiBaseUrl()
return baseUrl + `/orders/${orderId}`
}
private mapNewToLegacyParams(params: FeeQuoteParams, chainId: ChainId): QuoteQuery {
const { amount, kind, userAddress, receiver, validTo, sellToken, buyToken } = params
const fallbackAddress = userAddress || ZERO_ADDRESS
const baseParams = {
sellToken: toErc20Address(sellToken, chainId),
buyToken: toErc20Address(buyToken, chainId),
from: fallbackAddress,
receiver: receiver || fallbackAddress,
appData: this.context.appDataHash,
validTo,
partiallyFillable: false,
}
const finalParams: QuoteQuery =
kind === OrderKind.SELL
? {
kind: OrderKind.SELL,
sellAmountBeforeFee: amount,
...baseParams,
}
: {
kind: OrderKind.BUY,
buyAmountAfterFee: amount,
...baseParams,
}
return finalParams
}
private async getApiBaseUrl(): Promise<string> {
const chainId = await this.context.chainId
const baseUrl = this.API_BASE_URL[chainId]
if (!baseUrl) {
throw new CowError(`Unsupported Network. The ${this.API_NAME} API is not deployed in the Network ` + chainId)
} else {
return baseUrl + '/v1'
}
}
private async getProfileApiBaseUrl(): Promise<string> {
const chainId = await this.context.chainId
const baseUrl = this.PROFILE_API_BASE_URL[chainId]
if (!baseUrl) {
throw new CowError(`Unsupported Network. The ${this.API_NAME} API is not deployed in the Network ` + chainId)
} else {
return baseUrl + '/v1'
}
}
private async fetch(url: string, method: 'GET' | 'POST' | 'DELETE', data?: any): Promise<Response> {
const baseUrl = await this.getApiBaseUrl()
return fetch(baseUrl + url, {
headers: this.DEFAULT_HEADERS,
method,
body: data !== undefined ? JSON.stringify(data) : data,
})
}
private async fetchProfile(url: string, method: 'GET' | 'POST' | 'DELETE', data?: any): Promise<Response> {
const baseUrl = await this.getProfileApiBaseUrl()
return fetch(baseUrl + url, {
headers: this.DEFAULT_HEADERS,
method,
body: data !== undefined ? JSON.stringify(data) : data,
})
}
private post(url: string, data: any): Promise<Response> {
return this.fetch(url, 'POST', data)
}
private get(url: string): Promise<Response> {
return this.fetch(url, 'GET')
}
private getProfile(url: string): Promise<Response> {
return this.fetchProfile(url, 'GET')
}
private delete(url: string, data: any): Promise<Response> {
return this.fetch(url, 'DELETE', data)
}
}