@cowprotocol/cow-sdk 0.0.6 → 0.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +119 -7
- package/dist/CowSdk.d.ts +23 -0
- package/dist/{src/src/api → api}/cow/errors/OperatorError.d.ts +1 -1
- package/dist/{src/src/api → api}/cow/errors/QuoteError.d.ts +1 -1
- package/dist/{src/src/api → api}/cow/index.d.ts +8 -8
- package/dist/{src/src/api → api}/cow/types.d.ts +3 -2
- package/dist/api/cow-subgraph/graphql.d.ts +2603 -0
- package/dist/api/cow-subgraph/index.d.ts +17 -0
- package/dist/api/cow-subgraph/queries.d.ts +3 -0
- package/dist/api/index.d.ts +3 -0
- package/dist/api/metadata/index.d.ts +11 -0
- package/dist/api/metadata/types.d.ts +15 -0
- package/dist/{src/appData.schema-d44994e0.js → appData.schema-d44994e0.js} +0 -0
- package/dist/{src/appData.schema-d44994e0.js.map → appData.schema-d44994e0.js.map} +0 -0
- package/dist/{src/appData.schema-fb2df827.js → appData.schema-fb2df827.js} +0 -0
- package/dist/{src/appData.schema-fb2df827.js.map → appData.schema-fb2df827.js.map} +0 -0
- package/dist/{src/src/constants → constants}/chains.d.ts +0 -0
- package/dist/{src/src/constants → constants}/index.d.ts +1 -0
- package/dist/{src/src/constants → constants}/tokens.d.ts +2 -2
- package/dist/index.d.ts +5 -0
- package/dist/index.js +29 -0
- package/dist/index.js.map +1 -0
- package/dist/index.modern.js +29 -0
- package/dist/index.modern.js.map +1 -0
- package/dist/index.module.js +29 -0
- package/dist/index.module.js.map +1 -0
- package/dist/{src/src/types → types}/index.d.ts +2 -1
- package/dist/utils/appData.d.ts +10 -0
- package/dist/{src/src/utils → utils}/common.d.ts +2 -0
- package/dist/utils/context.d.ts +39 -0
- package/dist/utils/ipfs.d.ts +8 -0
- package/dist/{src/src/utils → utils}/sign.d.ts +1 -1
- package/dist/{src/src/utils → utils}/tokens.d.ts +1 -1
- package/package.json +48 -26
- package/.eslintrc.json +0 -15
- package/.github/workflows/publish.yml +0 -23
- package/.nvmrc +0 -1
- package/.prettierignore +0 -1
- package/.prettierrc +0 -5
- package/COPYRIGHT.md +0 -13
- package/LICENSE-APACHE +0 -201
- package/LICENSE-MIT +0 -21
- package/babel.config.js +0 -6
- package/dist/src/cow-sdk.esm.js +0 -2
- package/dist/src/cow-sdk.esm.js.map +0 -1
- package/dist/src/cow-sdk.js +0 -2
- package/dist/src/cow-sdk.js.map +0 -1
- package/dist/src/cow-sdk.modern.js +0 -2
- package/dist/src/cow-sdk.modern.js.map +0 -1
- package/dist/src/src/CowSdk.d.ts +0 -16
- package/dist/src/src/api/index.d.ts +0 -1
- package/dist/src/src/index.d.ts +0 -4
- package/dist/src/src/utils/appData.d.ts +0 -7
- package/dist/src/src/utils/context.d.ts +0 -24
- package/docs/images/CoW.png +0 -0
- package/src/CowSdk.ts +0 -31
- package/src/api/cow/errors/OperatorError.ts +0 -140
- package/src/api/cow/errors/QuoteError.ts +0 -114
- package/src/api/cow/index.ts +0 -343
- package/src/api/cow/types.ts +0 -82
- package/src/api/index.ts +0 -1
- package/src/constants/chains.ts +0 -11
- package/src/constants/index.ts +0 -14
- package/src/constants/tokens.ts +0 -16
- package/src/index.ts +0 -4
- package/src/schemas/appData.schema.json +0 -70
- package/src/types/index.ts +0 -5
- package/src/utils/appData.spec.ts +0 -66
- package/src/utils/appData.ts +0 -32
- package/src/utils/common.ts +0 -27
- package/src/utils/context.ts +0 -42
- package/src/utils/price.ts +0 -44
- package/src/utils/sign.ts +0 -224
- package/src/utils/tokens.ts +0 -12
- package/src/workflows/publish.sh +0 -77
- package/tsconfig.json +0 -18
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
import log from 'loglevel'
|
|
2
|
-
import { CowError } from '/utils/common'
|
|
3
|
-
import { ApiErrorCodes, ApiErrorObject } from './OperatorError'
|
|
4
|
-
|
|
5
|
-
export interface GpQuoteErrorObject {
|
|
6
|
-
errorType: GpQuoteErrorCodes
|
|
7
|
-
description: string
|
|
8
|
-
data?: any
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
// Conforms to backend API
|
|
12
|
-
// https://github.com/gnosis/gp-v2-services/blob/0bd5f7743bebaa5acd3be13e35ede2326a096f14/orderbook/openapi.yml#L562
|
|
13
|
-
export enum GpQuoteErrorCodes {
|
|
14
|
-
UnsupportedToken = 'UnsupportedToken',
|
|
15
|
-
InsufficientLiquidity = 'InsufficientLiquidity',
|
|
16
|
-
FeeExceedsFrom = 'FeeExceedsFrom',
|
|
17
|
-
ZeroPrice = 'ZeroPrice',
|
|
18
|
-
UNHANDLED_ERROR = 'UNHANDLED_ERROR',
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export enum GpQuoteErrorDetails {
|
|
22
|
-
UnsupportedToken = 'One of the tokens you are trading is unsupported. Please read the FAQ for more info.',
|
|
23
|
-
InsufficientLiquidity = 'Token pair selected has insufficient liquidity',
|
|
24
|
-
FeeExceedsFrom = 'Current fee exceeds entered "from" amount',
|
|
25
|
-
ZeroPrice = 'Quoted price is zero. This is likely due to a significant price difference between the two tokens. Please try increasing amounts.',
|
|
26
|
-
UNHANDLED_ERROR = 'Quote fetch failed. This may be due to a server or network connectivity issue. Please try again later.',
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export function mapOperatorErrorToQuoteError(error?: ApiErrorObject): GpQuoteErrorObject {
|
|
30
|
-
switch (error?.errorType) {
|
|
31
|
-
case ApiErrorCodes.NotFound:
|
|
32
|
-
case ApiErrorCodes.NoLiquidity:
|
|
33
|
-
return {
|
|
34
|
-
errorType: GpQuoteErrorCodes.InsufficientLiquidity,
|
|
35
|
-
description: GpQuoteErrorDetails.InsufficientLiquidity,
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
case ApiErrorCodes.SellAmountDoesNotCoverFee:
|
|
39
|
-
return {
|
|
40
|
-
errorType: GpQuoteErrorCodes.FeeExceedsFrom,
|
|
41
|
-
description: GpQuoteErrorDetails.FeeExceedsFrom,
|
|
42
|
-
data: error?.data,
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
case ApiErrorCodes.UnsupportedToken:
|
|
46
|
-
return {
|
|
47
|
-
errorType: GpQuoteErrorCodes.UnsupportedToken,
|
|
48
|
-
description: error.description,
|
|
49
|
-
}
|
|
50
|
-
case ApiErrorCodes.SellAmountDoesNotCoverFee:
|
|
51
|
-
return {
|
|
52
|
-
errorType: GpQuoteErrorCodes.FeeExceedsFrom,
|
|
53
|
-
description: error.description,
|
|
54
|
-
}
|
|
55
|
-
default:
|
|
56
|
-
return { errorType: GpQuoteErrorCodes.UNHANDLED_ERROR, description: GpQuoteErrorDetails.UNHANDLED_ERROR }
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export default class GpQuoteError extends CowError {
|
|
61
|
-
name = 'QuoteErrorObject'
|
|
62
|
-
description: string
|
|
63
|
-
// any data attached
|
|
64
|
-
data?: any
|
|
65
|
-
|
|
66
|
-
// Status 400 errors
|
|
67
|
-
// https://github.com/gnosis/gp-v2-services/blob/9014ae55412a356e46343e051aefeb683cc69c41/orderbook/openapi.yml#L563
|
|
68
|
-
static quoteErrorDetails = GpQuoteErrorDetails
|
|
69
|
-
|
|
70
|
-
public static async getErrorMessage(response: Response) {
|
|
71
|
-
try {
|
|
72
|
-
const orderPostError: GpQuoteErrorObject = await response.json()
|
|
73
|
-
|
|
74
|
-
if (orderPostError.errorType) {
|
|
75
|
-
const errorMessage = GpQuoteError.quoteErrorDetails[orderPostError.errorType]
|
|
76
|
-
// shouldn't fall through as this error constructor expects the error code to exist but just in case
|
|
77
|
-
return errorMessage || orderPostError.errorType
|
|
78
|
-
} else {
|
|
79
|
-
log.error('Unknown reason for bad quote fetch', orderPostError)
|
|
80
|
-
return orderPostError.description
|
|
81
|
-
}
|
|
82
|
-
} catch (error) {
|
|
83
|
-
log.error('Error handling 400/404 error. Likely a problem deserialising the JSON response')
|
|
84
|
-
return GpQuoteError.quoteErrorDetails.UNHANDLED_ERROR
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
static async getErrorFromStatusCode(response: Response) {
|
|
89
|
-
switch (response.status) {
|
|
90
|
-
case 400:
|
|
91
|
-
case 404:
|
|
92
|
-
return this.getErrorMessage(response)
|
|
93
|
-
|
|
94
|
-
case 500:
|
|
95
|
-
default:
|
|
96
|
-
log.error(
|
|
97
|
-
'[QuoteError::getErrorFromStatusCode] Error fetching quote, status code:',
|
|
98
|
-
response.status || 'unknown'
|
|
99
|
-
)
|
|
100
|
-
return 'Error fetching quote'
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
constructor(quoteError: GpQuoteErrorObject) {
|
|
104
|
-
super(quoteError.description, quoteError.errorType)
|
|
105
|
-
|
|
106
|
-
this.description = quoteError.description
|
|
107
|
-
this.message = GpQuoteError.quoteErrorDetails[quoteError.errorType]
|
|
108
|
-
this.data = quoteError?.data
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export function isValidQuoteError(error: any): error is GpQuoteError {
|
|
113
|
-
return error instanceof GpQuoteError
|
|
114
|
-
}
|
package/src/api/cow/index.ts
DELETED
|
@@ -1,343 +0,0 @@
|
|
|
1
|
-
import log from 'loglevel'
|
|
2
|
-
import fetch from 'cross-fetch'
|
|
3
|
-
import { OrderKind, QuoteQuery } from '@gnosis.pm/gp-v2-contracts'
|
|
4
|
-
import { SupportedChainId as ChainId } from '/constants/chains'
|
|
5
|
-
import { getSigningSchemeApiValue, OrderCreation } from '/utils/sign'
|
|
6
|
-
import OperatorError, { ApiErrorCodeDetails, ApiErrorCodes, ApiErrorObject } from './errors/OperatorError'
|
|
7
|
-
import QuoteError, {
|
|
8
|
-
GpQuoteErrorCodes,
|
|
9
|
-
GpQuoteErrorObject,
|
|
10
|
-
mapOperatorErrorToQuoteError,
|
|
11
|
-
GpQuoteErrorDetails,
|
|
12
|
-
} from './errors/QuoteError'
|
|
13
|
-
import { toErc20Address } from '/utils/tokens'
|
|
14
|
-
import { FeeQuoteParams, PriceInformation, PriceQuoteParams, SimpleGetQuoteResponse } from '/utils/price'
|
|
15
|
-
|
|
16
|
-
import { ZERO_ADDRESS } from '/constants'
|
|
17
|
-
import {
|
|
18
|
-
GetOrdersParams,
|
|
19
|
-
GetTradesParams,
|
|
20
|
-
OrderCancellationParams,
|
|
21
|
-
OrderID,
|
|
22
|
-
OrderMetaData,
|
|
23
|
-
ProfileData,
|
|
24
|
-
TradeMetaData,
|
|
25
|
-
} from '/api/cow/types'
|
|
26
|
-
import { CowError, objectToQueryString } from '/utils/common'
|
|
27
|
-
import { Context } from '/utils/context'
|
|
28
|
-
|
|
29
|
-
function getGnosisProtocolUrl(isDev: boolean): Partial<Record<ChainId, string>> {
|
|
30
|
-
if (isDev) {
|
|
31
|
-
return {
|
|
32
|
-
[ChainId.MAINNET]: 'https://barn.api.cow.fi/mainnet/api',
|
|
33
|
-
[ChainId.RINKEBY]: 'https://barn.api.cow.fi/rinkeby/api',
|
|
34
|
-
[ChainId.GNOSIS_CHAIN]: 'https://barn.api.cow.fi/xdai/api',
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
return {
|
|
39
|
-
[ChainId.MAINNET]: 'https://api.cow.fi/mainnet/api',
|
|
40
|
-
[ChainId.RINKEBY]: 'https://api.cow.fi/rinkeby/api',
|
|
41
|
-
[ChainId.GNOSIS_CHAIN]: 'https://api.cow.fi/xdai/api',
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function getProfileUrl(isDev: boolean): Partial<Record<ChainId, string>> {
|
|
46
|
-
if (isDev) {
|
|
47
|
-
return {
|
|
48
|
-
[ChainId.MAINNET]: 'https://barn.api.cow.fi/affiliate/api',
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
return {
|
|
53
|
-
[ChainId.MAINNET]: 'https://api.cow.fi/affiliate/api',
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const UNHANDLED_QUOTE_ERROR: GpQuoteErrorObject = {
|
|
58
|
-
errorType: GpQuoteErrorCodes.UNHANDLED_ERROR,
|
|
59
|
-
description: GpQuoteErrorDetails.UNHANDLED_ERROR,
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
const UNHANDLED_ORDER_ERROR: ApiErrorObject = {
|
|
63
|
-
errorType: ApiErrorCodes.UNHANDLED_CREATE_ERROR,
|
|
64
|
-
description: ApiErrorCodeDetails.UNHANDLED_CREATE_ERROR,
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
async function _handleQuoteResponse<T = any, P extends QuoteQuery = QuoteQuery>(
|
|
68
|
-
response: Response,
|
|
69
|
-
params?: P
|
|
70
|
-
): Promise<T> {
|
|
71
|
-
if (!response.ok) {
|
|
72
|
-
const errorObj: ApiErrorObject = await response.json()
|
|
73
|
-
|
|
74
|
-
// we need to map the backend error codes to match our own for quotes
|
|
75
|
-
const mappedError = mapOperatorErrorToQuoteError(errorObj)
|
|
76
|
-
const quoteError = new QuoteError(mappedError)
|
|
77
|
-
|
|
78
|
-
if (params) {
|
|
79
|
-
const { sellToken, buyToken } = params
|
|
80
|
-
log.error(`Error querying fee from API - sellToken: ${sellToken}, buyToken: ${buyToken}`)
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
throw quoteError
|
|
84
|
-
} else {
|
|
85
|
-
return response.json()
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
export class CowApi<T extends ChainId> {
|
|
90
|
-
chainId: T
|
|
91
|
-
context: Context
|
|
92
|
-
|
|
93
|
-
API_NAME = 'CoW Protocol'
|
|
94
|
-
|
|
95
|
-
constructor(chainId: T, context: Context) {
|
|
96
|
-
this.chainId = chainId
|
|
97
|
-
this.context = context
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
get DEFAULT_HEADERS() {
|
|
101
|
-
return { 'Content-Type': 'application/json', 'X-AppId': this.context.appDataHash }
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
get API_BASE_URL() {
|
|
105
|
-
return getGnosisProtocolUrl(this.context.isDevEnvironment)
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
get PROFILE_API_BASE_URL(): Partial<Record<ChainId, string>> {
|
|
109
|
-
return getProfileUrl(this.context.isDevEnvironment)
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
async getProfileData(address: string): Promise<ProfileData | null> {
|
|
113
|
-
log.debug(`[api:${this.API_NAME}] Get profile data for`, this.chainId, address)
|
|
114
|
-
if (this.chainId !== ChainId.MAINNET) {
|
|
115
|
-
log.info('Profile data is only available for mainnet')
|
|
116
|
-
return null
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
const response = await this.getProfile(`/profile/${address}`)
|
|
120
|
-
|
|
121
|
-
if (!response.ok) {
|
|
122
|
-
const errorResponse = await response.json()
|
|
123
|
-
log.error(errorResponse)
|
|
124
|
-
throw new CowError(errorResponse?.description)
|
|
125
|
-
} else {
|
|
126
|
-
return response.json()
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
async getTrades(params: GetTradesParams): Promise<TradeMetaData[]> {
|
|
131
|
-
const { owner, limit, offset } = params
|
|
132
|
-
const qsParams = objectToQueryString({ owner, limit, offset })
|
|
133
|
-
log.debug('[util:operator] Get trades for', this.chainId, owner, { limit, offset })
|
|
134
|
-
try {
|
|
135
|
-
const response = await this.get(`/trades${qsParams}`)
|
|
136
|
-
|
|
137
|
-
if (!response.ok) {
|
|
138
|
-
const errorResponse = await response.json()
|
|
139
|
-
throw new CowError(errorResponse)
|
|
140
|
-
} else {
|
|
141
|
-
return response.json()
|
|
142
|
-
}
|
|
143
|
-
} catch (error) {
|
|
144
|
-
log.error('Error getting trades:', error)
|
|
145
|
-
throw new CowError('Error getting trades: ' + error)
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
async getOrders(params: GetOrdersParams): Promise<OrderMetaData[]> {
|
|
150
|
-
const { owner, limit = 1000, offset = 0 } = params
|
|
151
|
-
const queryString = objectToQueryString({ limit, offset })
|
|
152
|
-
log.debug(`[api:${this.API_NAME}] Get orders for `, this.chainId, owner, limit, offset)
|
|
153
|
-
|
|
154
|
-
try {
|
|
155
|
-
const response = await this.get(`/account/${owner}/orders/${queryString}`)
|
|
156
|
-
|
|
157
|
-
if (!response.ok) {
|
|
158
|
-
const errorResponse: ApiErrorObject = await response.json()
|
|
159
|
-
throw new OperatorError(errorResponse)
|
|
160
|
-
} else {
|
|
161
|
-
return response.json()
|
|
162
|
-
}
|
|
163
|
-
} catch (error) {
|
|
164
|
-
log.error('Error getting orders information:', error)
|
|
165
|
-
throw new OperatorError(UNHANDLED_ORDER_ERROR)
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
async getOrder(orderId: string): Promise<OrderMetaData | null> {
|
|
170
|
-
log.debug(`[api:${this.API_NAME}] Get order for `, this.chainId, orderId)
|
|
171
|
-
try {
|
|
172
|
-
const response = await this.get(`/orders/${orderId}`)
|
|
173
|
-
|
|
174
|
-
if (!response.ok) {
|
|
175
|
-
const errorResponse: ApiErrorObject = await response.json()
|
|
176
|
-
throw new OperatorError(errorResponse)
|
|
177
|
-
} else {
|
|
178
|
-
return response.json()
|
|
179
|
-
}
|
|
180
|
-
} catch (error) {
|
|
181
|
-
log.error('Error getting order information:', error)
|
|
182
|
-
throw new OperatorError(UNHANDLED_ORDER_ERROR)
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
async getPriceQuoteLegacy(params: PriceQuoteParams): Promise<PriceInformation | null> {
|
|
187
|
-
const { baseToken, quoteToken, amount, kind } = params
|
|
188
|
-
log.debug(`[api:${this.API_NAME}] Get price from API`, params)
|
|
189
|
-
|
|
190
|
-
const response = await this.get(
|
|
191
|
-
`/markets/${toErc20Address(baseToken, this.chainId)}-${toErc20Address(
|
|
192
|
-
quoteToken,
|
|
193
|
-
this.chainId
|
|
194
|
-
)}/${kind}/${amount}`
|
|
195
|
-
).catch((error) => {
|
|
196
|
-
log.error('Error getting price quote:', error)
|
|
197
|
-
throw new QuoteError(UNHANDLED_QUOTE_ERROR)
|
|
198
|
-
})
|
|
199
|
-
|
|
200
|
-
return _handleQuoteResponse<PriceInformation | null>(response)
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
async getQuote(params: FeeQuoteParams): Promise<SimpleGetQuoteResponse> {
|
|
204
|
-
const quoteParams = this.mapNewToLegacyParams(params, this.chainId)
|
|
205
|
-
const response = await this.post('/quote', quoteParams)
|
|
206
|
-
|
|
207
|
-
return _handleQuoteResponse<SimpleGetQuoteResponse>(response)
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
async sendSignedOrderCancellation(params: OrderCancellationParams): Promise<void> {
|
|
211
|
-
const { cancellation, owner: from } = params
|
|
212
|
-
|
|
213
|
-
log.debug(`[api:${this.API_NAME}] Delete signed order for network`, this.chainId, cancellation)
|
|
214
|
-
|
|
215
|
-
const response = await this.delete(`/orders/${cancellation.orderUid}`, {
|
|
216
|
-
signature: cancellation.signature,
|
|
217
|
-
signingScheme: getSigningSchemeApiValue(cancellation.signingScheme),
|
|
218
|
-
from,
|
|
219
|
-
})
|
|
220
|
-
|
|
221
|
-
if (!response.ok) {
|
|
222
|
-
// Raise an exception
|
|
223
|
-
const errorMessage = await OperatorError.getErrorFromStatusCode(response, 'delete')
|
|
224
|
-
throw new CowError(errorMessage)
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
log.debug(`[api:${this.API_NAME}] Cancelled order`, cancellation.orderUid, this.chainId)
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
async sendOrder(params: { order: Omit<OrderCreation, 'appData'>; owner: string }): Promise<OrderID> {
|
|
231
|
-
const fullOrder: OrderCreation = { ...params.order, appData: this.context.appDataHash }
|
|
232
|
-
const { owner } = params
|
|
233
|
-
log.debug(`[api:${this.API_NAME}] Post signed order for network`, this.chainId, fullOrder)
|
|
234
|
-
|
|
235
|
-
// Call API
|
|
236
|
-
const response = await this.post(`/orders`, {
|
|
237
|
-
...fullOrder,
|
|
238
|
-
signingScheme: getSigningSchemeApiValue(fullOrder.signingScheme),
|
|
239
|
-
from: owner,
|
|
240
|
-
})
|
|
241
|
-
|
|
242
|
-
// Handle response
|
|
243
|
-
if (!response.ok) {
|
|
244
|
-
// Raise an exception
|
|
245
|
-
const errorMessage = await OperatorError.getErrorFromStatusCode(response, 'create')
|
|
246
|
-
throw new CowError(errorMessage)
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
const uid = (await response.json()) as string
|
|
250
|
-
log.debug(`[api:${this.API_NAME}] Success posting the signed order`, uid)
|
|
251
|
-
return uid
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
getOrderLink(orderId: OrderID): string {
|
|
255
|
-
const baseUrl = this.getApiBaseUrl()
|
|
256
|
-
|
|
257
|
-
return baseUrl + `/orders/${orderId}`
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
private mapNewToLegacyParams(params: FeeQuoteParams, chainId: ChainId): QuoteQuery {
|
|
261
|
-
const { amount, kind, userAddress, receiver, validTo, sellToken, buyToken } = params
|
|
262
|
-
const fallbackAddress = userAddress || ZERO_ADDRESS
|
|
263
|
-
|
|
264
|
-
const baseParams = {
|
|
265
|
-
sellToken: toErc20Address(sellToken, chainId),
|
|
266
|
-
buyToken: toErc20Address(buyToken, chainId),
|
|
267
|
-
from: fallbackAddress,
|
|
268
|
-
receiver: receiver || fallbackAddress,
|
|
269
|
-
appData: this.context.appDataHash,
|
|
270
|
-
validTo,
|
|
271
|
-
partiallyFillable: false,
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
const finalParams: QuoteQuery =
|
|
275
|
-
kind === OrderKind.SELL
|
|
276
|
-
? {
|
|
277
|
-
kind: OrderKind.SELL,
|
|
278
|
-
sellAmountBeforeFee: amount,
|
|
279
|
-
...baseParams,
|
|
280
|
-
}
|
|
281
|
-
: {
|
|
282
|
-
kind: OrderKind.BUY,
|
|
283
|
-
buyAmountAfterFee: amount,
|
|
284
|
-
...baseParams,
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
return finalParams
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
private getApiBaseUrl(): string {
|
|
291
|
-
const baseUrl = this.API_BASE_URL[this.chainId]
|
|
292
|
-
|
|
293
|
-
if (!baseUrl) {
|
|
294
|
-
throw new CowError(`Unsupported Network. The ${this.API_NAME} API is not deployed in the Network ` + this.chainId)
|
|
295
|
-
} else {
|
|
296
|
-
return baseUrl + '/v1'
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
private getProfileApiBaseUrl(): string {
|
|
301
|
-
const baseUrl = this.PROFILE_API_BASE_URL[this.chainId]
|
|
302
|
-
|
|
303
|
-
if (!baseUrl) {
|
|
304
|
-
throw new CowError(`Unsupported Network. The ${this.API_NAME} API is not deployed in the Network ` + this.chainId)
|
|
305
|
-
} else {
|
|
306
|
-
return baseUrl + '/v1'
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
private fetch(url: string, method: 'GET' | 'POST' | 'DELETE', data?: any): Promise<Response> {
|
|
311
|
-
const baseUrl = this.getApiBaseUrl()
|
|
312
|
-
return fetch(baseUrl + url, {
|
|
313
|
-
headers: this.DEFAULT_HEADERS,
|
|
314
|
-
method,
|
|
315
|
-
body: data !== undefined ? JSON.stringify(data) : data,
|
|
316
|
-
})
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
private fetchProfile(url: string, method: 'GET' | 'POST' | 'DELETE', data?: any): Promise<Response> {
|
|
320
|
-
const baseUrl = this.getProfileApiBaseUrl()
|
|
321
|
-
return fetch(baseUrl + url, {
|
|
322
|
-
headers: this.DEFAULT_HEADERS,
|
|
323
|
-
method,
|
|
324
|
-
body: data !== undefined ? JSON.stringify(data) : data,
|
|
325
|
-
})
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
private post(url: string, data: any): Promise<Response> {
|
|
329
|
-
return this.fetch(url, 'POST', data)
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
private get(url: string): Promise<Response> {
|
|
333
|
-
return this.fetch(url, 'GET')
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
private getProfile(url: string): Promise<Response> {
|
|
337
|
-
return this.fetchProfile(url, 'GET')
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
private delete(url: string, data: any): Promise<Response> {
|
|
341
|
-
return this.fetch(url, 'DELETE', data)
|
|
342
|
-
}
|
|
343
|
-
}
|
package/src/api/cow/types.ts
DELETED
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
import { OrderKind } from '@gnosis.pm/gp-v2-contracts'
|
|
2
|
-
import { SupportedChainId as ChainId } from '/constants/chains'
|
|
3
|
-
import { OrderCancellation, SigningSchemeValue } from '/utils/sign'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Unique identifier for the order, calculated by keccak256(orderDigest, ownerAddress, validTo),
|
|
7
|
-
* where orderDigest = keccak256(orderStruct). bytes32.
|
|
8
|
-
*/
|
|
9
|
-
export type OrderID = string
|
|
10
|
-
export type ApiOrderStatus = 'fulfilled' | 'expired' | 'cancelled' | 'presignaturePending' | 'open'
|
|
11
|
-
|
|
12
|
-
export interface OrderMetaData {
|
|
13
|
-
creationDate: string
|
|
14
|
-
owner: string
|
|
15
|
-
uid: OrderID
|
|
16
|
-
availableBalance: string
|
|
17
|
-
executedBuyAmount: string
|
|
18
|
-
executedSellAmount: string
|
|
19
|
-
executedSellAmountBeforeFees: string
|
|
20
|
-
executedFeeAmount: string
|
|
21
|
-
invalidated: false
|
|
22
|
-
sellToken: string
|
|
23
|
-
buyToken: string
|
|
24
|
-
sellAmount: string
|
|
25
|
-
buyAmount: string
|
|
26
|
-
validTo: number
|
|
27
|
-
appData: number
|
|
28
|
-
feeAmount: string
|
|
29
|
-
kind: OrderKind
|
|
30
|
-
partiallyFillable: false
|
|
31
|
-
signature: string
|
|
32
|
-
signingScheme: SigningSchemeValue
|
|
33
|
-
status: ApiOrderStatus
|
|
34
|
-
receiver: string
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export interface TradeMetaData {
|
|
38
|
-
blockNumber: number
|
|
39
|
-
logIndex: number
|
|
40
|
-
orderUid: OrderID
|
|
41
|
-
owner: string
|
|
42
|
-
sellToken: string
|
|
43
|
-
buyToken: string
|
|
44
|
-
sellAmount: string
|
|
45
|
-
buyAmount: string
|
|
46
|
-
sellAmountBeforeFees: string
|
|
47
|
-
txHash: string
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export interface UnsupportedToken {
|
|
51
|
-
[token: string]: {
|
|
52
|
-
address: string
|
|
53
|
-
dateAdded: number
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export type PaginationParams = {
|
|
58
|
-
limit?: number
|
|
59
|
-
offset?: number
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export type OrderCancellationParams = {
|
|
63
|
-
chainId: ChainId
|
|
64
|
-
cancellation: OrderCancellation
|
|
65
|
-
owner: string
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
export type GetOrdersParams = {
|
|
69
|
-
owner: string
|
|
70
|
-
} & PaginationParams
|
|
71
|
-
|
|
72
|
-
export type GetTradesParams = {
|
|
73
|
-
owner: string
|
|
74
|
-
} & PaginationParams
|
|
75
|
-
|
|
76
|
-
export type ProfileData = {
|
|
77
|
-
totalTrades: number
|
|
78
|
-
totalReferrals: number
|
|
79
|
-
tradeVolumeUsd: number
|
|
80
|
-
referralVolumeUsd: number
|
|
81
|
-
lastUpdated: string
|
|
82
|
-
}
|
package/src/api/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './cow'
|
package/src/constants/chains.ts
DELETED
package/src/constants/index.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import contractNetworks from '@gnosis.pm/gp-v2-contracts/networks.json'
|
|
2
|
-
import { SupportedChainId as ChainId } from './chains'
|
|
3
|
-
|
|
4
|
-
const { GPv2Settlement } = contractNetworks
|
|
5
|
-
|
|
6
|
-
export const GP_SETTLEMENT_CONTRACT_ADDRESS: Partial<Record<number, string>> = {
|
|
7
|
-
[ChainId.MAINNET]: GPv2Settlement[ChainId.MAINNET].address,
|
|
8
|
-
[ChainId.RINKEBY]: GPv2Settlement[ChainId.RINKEBY].address,
|
|
9
|
-
[ChainId.GNOSIS_CHAIN]: GPv2Settlement[ChainId.GNOSIS_CHAIN].address,
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'
|
|
13
|
-
|
|
14
|
-
export const DEFAULT_APP_DATA_HASH = '0x0000000000000000000000000000000000000000000000000000000000000000'
|
package/src/constants/tokens.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { SupportedChainId as ChainId } from '/constants/chains'
|
|
2
|
-
import { Token } from '/types'
|
|
3
|
-
|
|
4
|
-
export const XDAI_SYMBOL = 'XDAI'
|
|
5
|
-
|
|
6
|
-
export const WRAPPED_NATIVE_TOKEN: Record<ChainId, Token> = {
|
|
7
|
-
[ChainId.MAINNET]: new Token('WETH', '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'),
|
|
8
|
-
[ChainId.RINKEBY]: new Token('WETH', '0xc778417E063141139Fce010982780140Aa0cD5Ab'),
|
|
9
|
-
[ChainId.GNOSIS_CHAIN]: new Token('WXDAI', '0xe91D153E0b41518A2Ce8Dd3D7944Fa863463a97d'),
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export const NATIVE: Record<ChainId, string> = {
|
|
13
|
-
[ChainId.MAINNET]: 'ETH',
|
|
14
|
-
[ChainId.RINKEBY]: 'ETH',
|
|
15
|
-
[ChainId.GNOSIS_CHAIN]: XDAI_SYMBOL,
|
|
16
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"$id": "https://cowswap.exchange/appdata.schema.json",
|
|
3
|
-
"$schema": "http://json-schema.org/draft-07/schema",
|
|
4
|
-
"description": "Metadata JSON document for adding information to orders.",
|
|
5
|
-
"required": ["version", "metadata"],
|
|
6
|
-
"title": "AppData Root Schema",
|
|
7
|
-
"type": "object",
|
|
8
|
-
"properties": {
|
|
9
|
-
"version": {
|
|
10
|
-
"$id": "#/properties/version",
|
|
11
|
-
"description": "Semantic versioning of document",
|
|
12
|
-
"examples": ["1.0.0", "1.2.3"],
|
|
13
|
-
"title": "Semantic Versioning",
|
|
14
|
-
"type": "string"
|
|
15
|
-
},
|
|
16
|
-
"appCode": {
|
|
17
|
-
"$id": "#/properties/appCode",
|
|
18
|
-
"description": "The code identifying the CLI, UI, service generating the order.",
|
|
19
|
-
"examples": ["CowSwap"],
|
|
20
|
-
"title": "App Code",
|
|
21
|
-
"type": "string"
|
|
22
|
-
},
|
|
23
|
-
"metadata": {
|
|
24
|
-
"$id": "#/properties/metadata",
|
|
25
|
-
"default": {},
|
|
26
|
-
"description": "Each metadata will specify one aspect of the order.",
|
|
27
|
-
"required": [],
|
|
28
|
-
"title": "Metadata descriptors",
|
|
29
|
-
"type": "object",
|
|
30
|
-
"properties": {
|
|
31
|
-
"referrer": {
|
|
32
|
-
"$ref": "#/definitions/kindMetadata/referrer"
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
},
|
|
37
|
-
"definitions": {
|
|
38
|
-
"version": {
|
|
39
|
-
"$id": "#/definitions/version",
|
|
40
|
-
"description": "Semantic versioning of document",
|
|
41
|
-
"examples": ["1.0.0", "1.2.3"],
|
|
42
|
-
"title": "Semantic Versioning",
|
|
43
|
-
"type": "string"
|
|
44
|
-
},
|
|
45
|
-
"ethereumAddress": {
|
|
46
|
-
"$id": "#/definitions/ethereumAddress",
|
|
47
|
-
"pattern": "^0x[a-fA-F0-9]{40}$",
|
|
48
|
-
"title": "Ethereum compatible address",
|
|
49
|
-
"examples": ["0xb6BAd41ae76A11D10f7b0E664C5007b908bC77C9"],
|
|
50
|
-
"type": "string"
|
|
51
|
-
},
|
|
52
|
-
"kindMetadata": {
|
|
53
|
-
"referrer": {
|
|
54
|
-
"$id": "#/definitions/referrer",
|
|
55
|
-
"required": ["version", "address"],
|
|
56
|
-
"title": "Referrer",
|
|
57
|
-
"type": "object",
|
|
58
|
-
"properties": {
|
|
59
|
-
"version": {
|
|
60
|
-
"$ref": "#/definitions/version"
|
|
61
|
-
},
|
|
62
|
-
"address": {
|
|
63
|
-
"$ref": "#/definitions/ethereumAddress",
|
|
64
|
-
"title": "Referrer address"
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
}
|