@cowprotocol/cow-sdk 2.0.0-alpha.4 → 2.0.0-alpha.6
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 +43 -293
- package/dist/README.md +43 -293
- package/dist/common/configs.d.ts +2 -1
- package/dist/common/consts.d.ts +3 -0
- package/dist/index-c9cad3d4.js +29 -0
- package/dist/index-c9cad3d4.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/index.modern.mjs +1 -28
- package/dist/index.modern.mjs.map +1 -1
- package/dist/index.module.js +2 -2
- package/dist/index.module.js.map +1 -1
- package/dist/order-book/api.d.ts +7 -8
- package/dist/order-book/generated/models/OrderMetaData.d.ts +2 -1
- package/dist/order-signing/index.d.ts +2 -0
- package/dist/order-signing/orderSigningUtils.d.ts +10 -0
- package/dist/order-signing/types.d.ts +28 -0
- package/dist/order-signing/utils.d.ts +27 -0
- package/dist/package.json +5 -2
- package/dist/subgraph/api.d.ts +4 -5
- package/dist/utils-204fe1d3.js +2 -0
- package/dist/utils-204fe1d3.js.map +1 -0
- package/dist/utils-474dc9c3.js +2 -0
- package/dist/utils-474dc9c3.js.map +1 -0
- package/dist/utils-fa9c8b98.js +2 -0
- package/dist/utils-fa9c8b98.js.map +1 -0
- package/package.json +5 -2
package/README.md
CHANGED
|
@@ -4,8 +4,11 @@
|
|
|
4
4
|
|
|
5
5
|
# CoW SDK
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
## Test coverage
|
|
8
|
+
|
|
9
|
+
| Statements | Branches | Functions | Lines |
|
|
10
|
+
| ------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- |
|
|
11
|
+
|  |  |  |  |
|
|
9
12
|
|
|
10
13
|
## Getting started
|
|
11
14
|
|
|
@@ -53,153 +56,51 @@ const trades = await cowSdk.cowApi.getOrders({
|
|
|
53
56
|
console.log(trades)
|
|
54
57
|
```
|
|
55
58
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
## Sign and Post orders
|
|
59
|
-
|
|
60
|
-
In order to trade, you will need to create a valid order first.
|
|
61
|
-
|
|
62
|
-
On the contraty to other decentralised exchanges, creating orders is free in CoW Protocol. This is because, one of the
|
|
63
|
-
most common ways to do it is by created offchain signed messages (meta-transactions, uses `EIP-712` or `EIP-1271`).
|
|
64
|
-
|
|
65
|
-
Posting orders is a three steps process:
|
|
66
|
-
|
|
67
|
-
- 1. **Get Market Pricea**: Fee & Price
|
|
68
|
-
- 2. **Sign the order**: Using off-chain signing or Meta-transactions
|
|
69
|
-
- 3. **Post the signed order to the API**: So, the order becomes `OPEN`
|
|
70
|
-
|
|
71
|
-
The next sections will guide you through the process of creating a valid order.
|
|
72
|
-
|
|
73
|
-
> For a quick snippet with the full process on posting an order see the [Post an Order Example](./docs/post-order-example.ts).
|
|
74
|
-
|
|
75
|
-
### Enable tokens (token approval)
|
|
76
|
-
Because of the use of off-chain signing (meta-transactions), users will need to **Enable the sell token** before signed
|
|
77
|
-
orders can be considered as valid ones.
|
|
78
|
-
|
|
79
|
-
This enabling is technically an `ERC-20` approval, and is something that needs to be done only once. After this all
|
|
80
|
-
order creation can be done for free using offchain signing.
|
|
81
|
-
|
|
82
|
-
> For more details see https://docs.cow.fi/tutorials/how-to-submit-orders-via-the-api/1.-set-allowance-for-the-sell-token
|
|
83
|
-
|
|
84
|
-
### Instantiate SDK with a signer
|
|
85
|
-
|
|
86
|
-
Before you can sign any transaction, you have to instantiate the SDK with a [Ethers.JS signer](https://docs.ethers.io/v5/api/signer/):
|
|
87
|
-
|
|
88
|
-
```js
|
|
89
|
-
import { Wallet } from 'ethers'
|
|
90
|
-
import { CowSdk, OrderKind } from '@cowprotocol/cow-sdk'
|
|
91
|
-
|
|
92
|
-
const mnemonic = 'fall dirt bread cactus...'
|
|
93
|
-
const wallet = Wallet.fromMnemonic(mnemonic)
|
|
94
|
-
|
|
95
|
-
const cowSdk = new CowSdk(
|
|
96
|
-
100, { // Leaving chainId empty will default to MAINNET
|
|
97
|
-
signer: wallet // Provide a signer, so you can sign order
|
|
98
|
-
})
|
|
99
|
-
```
|
|
100
|
-
|
|
101
|
-
### STEP 1: Get Market Price
|
|
102
|
-
|
|
103
|
-
To create an order, you need to get a price/fee quote first:
|
|
104
|
-
|
|
105
|
-
* The SDK will give you easy access to the API, which returns the `Market Price` and the `Fee` for any given trade you intent to do.
|
|
106
|
-
* The returned `Market Price` is not strictly needed, you can use your own pricing logic.
|
|
107
|
-
* You can choose a price that is below this Market price (**Market Order**), or above Market Price (**Limit Order**).
|
|
108
|
-
* The `Fee` however is very important.
|
|
109
|
-
* It is the required amount in sell token the trader agrees on paying for executing the order onchain.
|
|
110
|
-
* Normally, its value is proportional to the current Gas Price of the network.
|
|
111
|
-
* This fee is never charged if you don't trade.
|
|
112
|
-
|
|
113
|
-
To get the quote, you simply specify the trade you intent to do:
|
|
114
|
-
|
|
115
|
-
```js
|
|
116
|
-
const quoteResponse = await cowSdk.cowApi.getQuote({
|
|
117
|
-
kind: OrderKind.SELL, // Sell order (could also be BUY)
|
|
118
|
-
sellToken: '0xc778417e063141139fce010982780140aa0cd5ab', // WETH
|
|
119
|
-
buyToken: '0x4dbcdf9b62e891a7cec5a2568c3f4faf9e8abe2b', // USDC
|
|
120
|
-
amount: '1000000000000000000', // 1 WETH
|
|
121
|
-
userAddress: '0x1811be0994930fe9480eaede25165608b093ad7a', // Trader
|
|
122
|
-
validTo: 2524608000,
|
|
123
|
-
})
|
|
124
|
-
|
|
125
|
-
const { sellToken, buyToken, validTo, buyAmount, sellAmount, receiver, feeAmount } = quoteResponse.quote
|
|
126
|
-
```
|
|
127
|
-
|
|
128
|
-
### STEP 2: Sign the order
|
|
129
|
-
|
|
130
|
-
Once you know the price and fee, we can create the order and sign it:
|
|
59
|
+
The SDK will expose:
|
|
131
60
|
|
|
132
|
-
-
|
|
133
|
-
-
|
|
134
|
-
|
|
135
|
-
- The SDK will provide an easy way to sign orders given the raw data
|
|
61
|
+
- The CoW API (`cowSdk.cowApi`)
|
|
62
|
+
- The CoW Subgraph (`cowSdk.cowSubgraphApi`)
|
|
63
|
+
- Convenient method to facilitate signing orders (i.e `cowSdk.signOrder`)
|
|
136
64
|
|
|
137
|
-
|
|
138
|
-
const { sellToken, buyToken, validTo, buyAmount, sellAmount, receiver, feeAmount } = quoteResponse.quote
|
|
65
|
+
> ✨ For a quick snippet with the full process on posting an order see the [Post an Order Example](./docs/post-order-example.ts)
|
|
139
66
|
|
|
140
|
-
|
|
141
|
-
const order = {
|
|
142
|
-
kind: OrderKind.SELL, // SELL || BUY
|
|
143
|
-
receiver, // Your account or any other
|
|
144
|
-
sellToken,
|
|
145
|
-
buyToken,
|
|
67
|
+
> 📚 Read more about [How to get started with the SDK](https://docs.cow.fi/cow-sdk/getting-started-with-the-sdk)
|
|
146
68
|
|
|
147
|
-
|
|
69
|
+
## Architecture
|
|
148
70
|
|
|
149
|
-
|
|
150
|
-
validTo,
|
|
71
|
+
One way to make the most out of the SDK is to get familiar to its architecture.
|
|
151
72
|
|
|
152
|
-
|
|
153
|
-
// You can apply some slippage tolerance here to make sure the trade is executed.
|
|
154
|
-
// CoW protocol protects from MEV, so it can work with higher slippages
|
|
155
|
-
sellAmount,
|
|
156
|
-
buyAmount,
|
|
73
|
+
> See [SDK Architecture](./docs/architecture.md)
|
|
157
74
|
|
|
158
|
-
|
|
159
|
-
feeAmount,
|
|
75
|
+
## CoW API
|
|
160
76
|
|
|
161
|
-
|
|
162
|
-
appData: '0x0000000000000000000000000000000000000000000000000000000000000000',
|
|
163
|
-
}
|
|
77
|
+
The SDK provides access to the CoW API. The CoW API allows you:
|
|
164
78
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
79
|
+
- Post orders
|
|
80
|
+
- Get fee quotes
|
|
81
|
+
- Get order details
|
|
82
|
+
- Get history of orders: i.e. filtering by account, transaction hash, etc.
|
|
168
83
|
|
|
169
|
-
|
|
84
|
+
> 📚 Read more about [How to Query the API](https://docs.cow.fi/cow-sdk/cow-api)
|
|
170
85
|
|
|
171
|
-
##
|
|
86
|
+
## Sign and Post orders
|
|
172
87
|
|
|
173
|
-
|
|
88
|
+
In order to trade, you will need to create a valid order first.
|
|
174
89
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
- The possible outcomes once accepted is:
|
|
178
|
-
- The order is `EXECUTED`: you will pay the signed fee, and get at least the `buyAmount` tokens you specified, although you will probably get more! (you will probably get a so-called **Surplus**).
|
|
179
|
-
- The order `EXPIRES`: If your price is not good enough, and the order is out of the market price before
|
|
180
|
-
expiration, your order will expire. This doesn't have any cost to the user, which **only pays the fee if the trade is executed**.
|
|
181
|
-
- You cancel the order, so it becomes `CANCELLED`. Cancelling an order can be done both as a free meta-transaction
|
|
182
|
-
(**soft cancelations**) or as a regular on-chain transaction (**hard cancelations**).
|
|
183
|
-
- The API will return an `orderId` which identifies the order, and is created as a summary (hash) of it. In other words, the `orderId` is deterministic given all the order parameters.
|
|
90
|
+
On the contraty to other decentralised exchanges, creating orders is free in CoW Protocol. This is because, one of the
|
|
91
|
+
most common ways to do it is by created offchain signed messages (meta-transactions, uses `EIP-712` or `EIP-1271`).
|
|
184
92
|
|
|
185
|
-
|
|
93
|
+
Posting orders is a three steps process:
|
|
186
94
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
owner: '0x1811be0994930fe9480eaede25165608b093ad7a',
|
|
191
|
-
})
|
|
192
|
-
```
|
|
95
|
+
- 1. **Get Market Pricea**: Fee & Price
|
|
96
|
+
- 2. **Sign the order**: Using off-chain signing or Meta-transactions
|
|
97
|
+
- 3. **Post the signed order to the API**: So, the order becomes `OPEN`
|
|
193
98
|
|
|
194
|
-
|
|
195
|
-
Once the order is posted, its good to allow to check the state of it.
|
|
99
|
+
The next sections will guide you through the process of creating a valid order.
|
|
196
100
|
|
|
197
|
-
|
|
101
|
+
> For a quick snippet with the full process on posting an order see the [Post an Order Example](./docs/post-order-example.ts).
|
|
198
102
|
|
|
199
|
-
|
|
200
|
-
// View order in explorer
|
|
201
|
-
console.log(`https://explorer.cow.fi/gc/orders/${orderId}`)
|
|
202
|
-
```
|
|
103
|
+
> 📚 Read more about [How to Sign and Post your orders](https://docs.cow.fi/cow-sdk/sign-and-post-orders)
|
|
203
104
|
|
|
204
105
|
## Create a meta-data document for attaching to an order
|
|
205
106
|
|
|
@@ -225,169 +126,9 @@ This will create a document similar to:
|
|
|
225
126
|
}
|
|
226
127
|
```
|
|
227
128
|
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
For example, you could give information about who referred the user creating the order.
|
|
231
|
-
|
|
232
|
-
```js
|
|
233
|
-
const appDataDoc = cowSdk.metadataApi.generateAppDataDoc({
|
|
234
|
-
appDataParams: { appCode: 'YourApp' },
|
|
235
|
-
metadataParas: {
|
|
236
|
-
referrerParams: {
|
|
237
|
-
address: '0x1f5B740436Fc5935622e92aa3b46818906F416E9',
|
|
238
|
-
},
|
|
239
|
-
},
|
|
240
|
-
})
|
|
241
|
-
```
|
|
242
|
-
|
|
243
|
-
This will create a document similar to:
|
|
244
|
-
|
|
245
|
-
```json
|
|
246
|
-
{
|
|
247
|
-
"version": "0.4.0",
|
|
248
|
-
"appCode": "YourApp",
|
|
249
|
-
"metadata": {
|
|
250
|
-
"referrer": {
|
|
251
|
-
"address": "0x1f5B740436Fc5935622e92aa3b46818906F416E9",
|
|
252
|
-
"version": "0.1.0"
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
```
|
|
257
|
-
|
|
258
|
-
The method `cowSdk.metadataApi.generateAppDataDoc` will always create the latest schema version.
|
|
259
|
-
|
|
260
|
-
For a complete list of metadata that can be attached and for previous versions
|
|
261
|
-
check `@cowprotocol/app-data` [repository](https://github.com/cowprotocol/app-data)
|
|
262
|
-
and [NPM package](https://www.npmjs.com/package/@cowprotocol/app-data)
|
|
263
|
-
|
|
264
|
-
## Get the AppData Hex
|
|
265
|
-
|
|
266
|
-
The `AppData` Hex points to an IPFS document with the meta-data attached to the order.
|
|
267
|
-
|
|
268
|
-
You can calculate the `AppData` Hex, and its corresponding `cidV0` using the SDK:
|
|
269
|
-
|
|
270
|
-
```js
|
|
271
|
-
const { appDataHash, cidv0 } = await cowSdk.metadataApi.calculateAppDataHash(appDataDoc)
|
|
272
|
-
```
|
|
129
|
+
> 📚 Read more about [How to Create/Encode/Decode your own Meta-data for orders](https://docs.cow.fi/cow-sdk/order-meta-data-appdata)
|
|
273
130
|
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
This method can be used to calculate the actual hash before uploading the document to IPFS.
|
|
277
|
-
|
|
278
|
-
## Get meta-data document uploaded to IPFS from AppData
|
|
279
|
-
|
|
280
|
-
Given the `AppData` of a document that has been uploaded to IPFS, you can easily retrieve the document.
|
|
281
|
-
|
|
282
|
-
```js
|
|
283
|
-
const appDataDoc = await cowSdk.metadataApi.decodeAppData(
|
|
284
|
-
'0x5ddb2c8207c10b96fac92cb934ef9ba004bc007a073c9e5b13edc422f209ed80'
|
|
285
|
-
)
|
|
286
|
-
```
|
|
287
|
-
|
|
288
|
-
This will return a document similar to:
|
|
289
|
-
|
|
290
|
-
```json
|
|
291
|
-
{
|
|
292
|
-
"version": "0.1.0",
|
|
293
|
-
"appCode": "YourApp",
|
|
294
|
-
"metadata": {
|
|
295
|
-
"referrer": {
|
|
296
|
-
"address": "0x1f5B740436Fc5935622e92aa3b46818906F416E9",
|
|
297
|
-
"version": "0.1.0"
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
```
|
|
302
|
-
|
|
303
|
-
## Upload document to IPFS
|
|
304
|
-
|
|
305
|
-
The SDK uses Pinata to upload it to IPFS, so you will need an API Key in order to upload it using the SDK.
|
|
306
|
-
|
|
307
|
-
Alternatively, you can upload the document on your own using any other service.
|
|
308
|
-
|
|
309
|
-
```js
|
|
310
|
-
// Make sure you provide the IPFS params when instantiating the SDK
|
|
311
|
-
const cowSdk = new CowSdk(100, {
|
|
312
|
-
ipfs: {
|
|
313
|
-
pinataApiKey: 'YOUR_PINATA_API_KEY',
|
|
314
|
-
pinataApiSecret: 'YOUR_PINATA_API_SECRET',
|
|
315
|
-
},
|
|
316
|
-
})
|
|
317
|
-
|
|
318
|
-
// Upload to IPFS
|
|
319
|
-
const uploadedAppDataHash = await cowSdk.metadataApi.uploadMetadataDocToIpfs(appDataDoc)
|
|
320
|
-
```
|
|
321
|
-
|
|
322
|
-
## Convert IPFS CIDv0 to AppData (and back)
|
|
323
|
-
Given an IPFS CIDv0 you can convert it to an `AppData`
|
|
324
|
-
|
|
325
|
-
```js
|
|
326
|
-
const decodedAppDataHex = await cowSdk.metadataApi.cidToAppDataHex('QmUf2TrpSANVXdgcYfAAACe6kg551cY3rAemB7xfEMjYvs')
|
|
327
|
-
```
|
|
328
|
-
|
|
329
|
-
This will return an `AppData` hex: `0x5ddb2c8207c10b96fac92cb934ef9ba004bc007a073c9e5b13edc422f209ed80`
|
|
330
|
-
|
|
331
|
-
> This might be handy if you decide to upload the document to IPFS yourself and then you need the AppData to post your order
|
|
332
|
-
|
|
333
|
-
Similarly, you can do the opposite and convert an `AppData` into an IPFS document:
|
|
334
|
-
|
|
335
|
-
```js
|
|
336
|
-
const decodedAppDataHex = await cowSdk.metadataApi.appDataHexToCid(hash)
|
|
337
|
-
```
|
|
338
|
-
|
|
339
|
-
This will return an IPFS CIDv0: `QmUf2TrpSANVXdgcYfAAACe6kg551cY3rAemB7xfEMjYvs`
|
|
340
|
-
|
|
341
|
-
## Getting appData schema files
|
|
342
|
-
|
|
343
|
-
It's possible to get schema files directly for each exported version using `getAppDataSchema`
|
|
344
|
-
|
|
345
|
-
```js
|
|
346
|
-
const schema = await cowSdk.metadataApi.getAppDataSchema('0.4.0')
|
|
347
|
-
```
|
|
348
|
-
|
|
349
|
-
## Validating appDataDocs
|
|
350
|
-
|
|
351
|
-
If for some reason you decide to create an `appDataDoc` without using the helper function, you can use `validateAppDataDoc` to validate it against the schema
|
|
352
|
-
|
|
353
|
-
```js
|
|
354
|
-
const { success, error } = await cowSdk.metadataApi.validateAppDataDoc({ version: '0.1.0', ... })
|
|
355
|
-
```
|
|
356
|
-
|
|
357
|
-
## Querying the Cow Subgraph
|
|
358
|
-
|
|
359
|
-
You can query the Cow Subgraph either by running some common queries exposed by the `CowSubgraphApi` or by building your own ones:
|
|
360
|
-
|
|
361
|
-
```js
|
|
362
|
-
const chainId = 1 // Mainnet
|
|
363
|
-
const cowSdk = new CowSdk(chainId)
|
|
364
|
-
|
|
365
|
-
// Get Cow Protocol totals
|
|
366
|
-
const { tokens, orders, traders, settlements, volumeUsd, volumeEth, feesUsd, feesEth } =
|
|
367
|
-
await cowSdk.cowSubgraphApi.getTotals()
|
|
368
|
-
console.log({ tokens, orders, traders, settlements, volumeUsd, volumeEth, feesUsd, feesEth })
|
|
369
|
-
|
|
370
|
-
// Get last 24 hours volume in usd
|
|
371
|
-
const { hourlyTotals } = await cowSdk.cowSubgraphApi.getLastHoursVolume(24)
|
|
372
|
-
console.log(hourlyTotals)
|
|
373
|
-
|
|
374
|
-
// Get last week volume in usd
|
|
375
|
-
const { dailyTotals } = await cowSdk.cowSubgraphApi.getLastDaysVolume(7)
|
|
376
|
-
console.log(dailyTotals)
|
|
377
|
-
|
|
378
|
-
// Get the last 5 batches
|
|
379
|
-
const query = `
|
|
380
|
-
query LastBatches($n: Int!) {
|
|
381
|
-
settlements(orderBy: firstTradeTimestamp, orderDirection: desc, first: $n) {
|
|
382
|
-
txHash
|
|
383
|
-
firstTradeTimestamp
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
`
|
|
387
|
-
const variables = { n: 5 }
|
|
388
|
-
const response = await cowSdk.cowSubgraphApi.runQuery(query, variables)
|
|
389
|
-
console.log(response)
|
|
390
|
-
```
|
|
131
|
+
## Development
|
|
391
132
|
|
|
392
133
|
### Install Dependencies
|
|
393
134
|
|
|
@@ -409,3 +150,12 @@ yarn start
|
|
|
409
150
|
```bash
|
|
410
151
|
yarn test
|
|
411
152
|
```
|
|
153
|
+
|
|
154
|
+
### Code generation
|
|
155
|
+
|
|
156
|
+
Some parets of the SDK are automatically generated. This is the case for the Order Book APU and the Subgraph API
|
|
157
|
+
|
|
158
|
+
```bash
|
|
159
|
+
# Re-create automatically generated code
|
|
160
|
+
yarn codegen
|
|
161
|
+
```
|