@cowprotocol/cow-sdk 0.0.8-RC.0 → 0.0.9
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 +95 -23
- package/dist/CowSdk.d.ts +2 -1
- package/dist/api/cow-subgraph/graphql.d.ts +2576 -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 +1 -0
- package/dist/api/metadata/index.d.ts +3 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.js +28 -1
- package/dist/index.js.map +1 -1
- package/dist/index.modern.js +28 -1
- package/dist/index.modern.js.map +1 -1
- package/dist/index.module.js +28 -1
- package/dist/index.module.js.map +1 -1
- package/dist/utils/context.d.ts +12 -3
- package/dist/utils/ipfs.d.ts +8 -0
- package/package.json +24 -7
- package/.babelrc +0 -4
- package/.eslintrc.json +0 -15
- package/.github/workflows/build.yml +0 -50
- package/.github/workflows/lint.yml +0 -19
- package/.github/workflows/publish.yml +0 -20
- package/.github/workflows/test.yml +0 -47
- package/.nvmrc +0 -1
- package/.prettierignore +0 -1
- package/.prettierrc +0 -5
- package/COPYRIGHT.md +0 -13
- package/babel.config.js +0 -3
- package/docs/images/CoW.png +0 -0
- package/src/CowSdk.ts +0 -53
- package/src/api/cow/errors/OperatorError.ts +0 -142
- package/src/api/cow/errors/QuoteError.ts +0 -115
- package/src/api/cow/index.ts +0 -367
- package/src/api/cow/types.ts +0 -82
- package/src/api/index.ts +0 -2
- package/src/api/metadata/index.ts +0 -37
- package/src/api/metadata/types.ts +0 -17
- package/src/constants/chains.ts +0 -11
- package/src/constants/index.ts +0 -16
- 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 -6
- package/src/utils/appData.spec.ts +0 -109
- package/src/utils/appData.ts +0 -58
- package/src/utils/common.ts +0 -35
- package/src/utils/context.ts +0 -89
- 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 -49
- package/tsconfig.json +0 -17
package/README.md
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
<p align="center">
|
|
2
|
-
<img width="400" src="https://raw.githubusercontent.com/
|
|
2
|
+
<img width="400" src="https://raw.githubusercontent.com/cowprotocol/cow-sdk/main/docs/images/CoW.png">
|
|
3
3
|
</p>
|
|
4
4
|
|
|
5
5
|
# CoW protocol SDK
|
|
6
|
-
[](https://prettier.io/)
|
|
7
6
|
|
|
7
|
+
[](https://prettier.io/)
|
|
8
|
+
[](https://coveralls.io/github/cowprotocol/cow-sdk?branch=main)
|
|
8
9
|
|
|
9
10
|
> ⚠️⚠️ THE SDK IS IN Beta ⚠️⚠️
|
|
10
|
-
> It is being currently develop and is a work in progress, also it's API is subjected to change.
|
|
11
|
+
> It is being currently develop and is a work in progress, also it's API is subjected to change.
|
|
11
12
|
> If you experience any problems, please open an issue in Github trying to describe your problem.
|
|
12
13
|
|
|
13
14
|
### Getting started
|
|
@@ -34,17 +35,18 @@ The SDK will expose the CoW API operations (`cowSdk.cowApi`) and some convenient
|
|
|
34
35
|
const trades = await cowSdk.cowApi.getOrders({
|
|
35
36
|
owner: '0x00000000005ef87f8ca7014309ece7260bbcdaeb', // Trader
|
|
36
37
|
limit: 5,
|
|
37
|
-
offset: 0
|
|
38
|
+
offset: 0,
|
|
38
39
|
})
|
|
39
40
|
console.log(trades)
|
|
40
41
|
```
|
|
41
42
|
|
|
42
43
|
Let's see a full example on how to submit an order to CowSwap.
|
|
43
44
|
|
|
44
|
-
> ⚠️ Before starting, the protocol requires you to approve the sell token before the order can be considered.
|
|
45
|
+
> ⚠️ Before starting, the protocol requires you to approve the sell token before the order can be considered.
|
|
45
46
|
> For more details see https://docs.cow.fi/tutorials/how-to-submit-orders-via-the-api/1.-set-allowance-for-the-sell-token
|
|
46
47
|
|
|
47
48
|
In this example, we will:
|
|
49
|
+
|
|
48
50
|
- 1. **Instantiate the SDK and a wallet**: Used for signing orders
|
|
49
51
|
- 2. **Get a price/fee quote from the API**: Get current market price and required protocol fee to settle your trade.
|
|
50
52
|
- 3. **Sign the order using your wallet**: Only signed orders are considered by the protocol.
|
|
@@ -64,7 +66,7 @@ const cowSdk = new CowSdk(4, { signer: wallet })
|
|
|
64
66
|
const quoteResponse = await cowSdk.cowApi.getQuote({
|
|
65
67
|
kind: OrderKind.SELL, // Sell order (could also be BUY)
|
|
66
68
|
sellToken: '0xc778417e063141139fce010982780140aa0cd5ab', // WETH
|
|
67
|
-
buyToken: '0x4dbcdf9b62e891a7cec5a2568c3f4faf9e8abe2b',
|
|
69
|
+
buyToken: '0x4dbcdf9b62e891a7cec5a2568c3f4faf9e8abe2b', // USDC
|
|
68
70
|
amount: '1000000000000000000', // 1 WETH
|
|
69
71
|
userAddress: '0x1811be0994930fe9480eaede25165608b093ad7a', // Trader
|
|
70
72
|
validTo: 2524608000,
|
|
@@ -99,14 +101,14 @@ console.log(`https://explorer.cow.fi/rinkeby/orders/${orderId}`)
|
|
|
99
101
|
SDK also includes a Metadata API to interact with AppData documents and IPFS CIDs
|
|
100
102
|
|
|
101
103
|
```js
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
104
|
+
const chainId = 4 // Rinkeby
|
|
105
|
+
const cowSdk = new CowSdk(chainId)
|
|
106
|
+
let hash = '0xa6c81f4ca727252a05b108f1742a07430f28d474d2a3492d8f325746824d22e5'
|
|
107
|
+
|
|
108
|
+
// Decode AppData document given a CID hash
|
|
109
|
+
const appDataDoc = await cowSdk.metadataApi.decodeAppData(hash)
|
|
110
|
+
console.log(appDataDoc)
|
|
111
|
+
/* {
|
|
110
112
|
"appCode": "CowSwap",
|
|
111
113
|
"metadata": {
|
|
112
114
|
"referrer": {
|
|
@@ -117,17 +119,87 @@ SDK also includes a Metadata API to interact with AppData documents and IPFS CID
|
|
|
117
119
|
"version": "0.1.0"
|
|
118
120
|
} */
|
|
119
121
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
122
|
+
const cid = 'QmUf2TrpSANVXdgcYfAAACe6kg551cY3rAemB7xfEMjYvs'
|
|
123
|
+
|
|
124
|
+
// Decode CID hash to AppData Hex
|
|
125
|
+
const decodedAppDataHex = await cowSdk.metadataApi.cidToAppDataHex(cid)
|
|
126
|
+
console.log(decodedAppDataHex) //0x5ddb2c8207c10b96fac92cb934ef9ba004bc007a073c9e5b13edc422f209ed80
|
|
127
|
+
|
|
128
|
+
hash = '0x5ddb2c8207c10b96fac92cb934ef9ba004bc007a073c9e5b13edc422f209ed80'
|
|
129
|
+
|
|
130
|
+
// Decode AppData Hex to CID
|
|
131
|
+
const decodedAppDataHex = await cowSdk.metadataApi.appDataHexToCid(hash)
|
|
132
|
+
console.log(decodedAppDataHex) //QmUf2TrpSANVXdgcYfAAACe6kg551cY3rAemB7xfEMjYvs
|
|
133
|
+
|
|
134
|
+
/*Create an AppData Document with empty metadata and default appCode
|
|
135
|
+
generateAppDataDoc receives as parameters:
|
|
136
|
+
- metadata: MetadataDoc (Default: {})
|
|
137
|
+
- appCode: string (Default: 'Cowswap')
|
|
138
|
+
*/
|
|
139
|
+
const appDataDoc = cowSdk.metadataApi.generateAppDataDoc({})
|
|
140
|
+
/* {
|
|
141
|
+
version: '0.1.0',
|
|
142
|
+
appCode: 'CowSwap',
|
|
143
|
+
metadata: {},
|
|
144
|
+
}
|
|
145
|
+
*/
|
|
146
|
+
|
|
147
|
+
// Create an AppData Document with custom metadata and appCode
|
|
148
|
+
const appDataDoc = cowSdk.metadataApi.generateAppDataDoc(
|
|
149
|
+
{
|
|
150
|
+
referrer: {
|
|
151
|
+
address: '0x1f5B740436Fc5935622e92aa3b46818906F416E9',
|
|
152
|
+
version: '0.1.0',
|
|
153
|
+
},
|
|
154
|
+
},
|
|
155
|
+
'CowApp'
|
|
156
|
+
)
|
|
157
|
+
/* {
|
|
158
|
+
version: '0.1.0',
|
|
159
|
+
appCode: 'CowApp',
|
|
160
|
+
metadata: {
|
|
161
|
+
referrer: {
|
|
162
|
+
address: '0x1f5B740436Fc5935622e92aa3b46818906F416E9',
|
|
163
|
+
version: '0.1.0',
|
|
164
|
+
},
|
|
165
|
+
},
|
|
166
|
+
}
|
|
167
|
+
*/
|
|
168
|
+
|
|
169
|
+
// Upload AppDataDoc to IPFS (Pinata)
|
|
170
|
+
const cowSdk = new CowSdk(4, {
|
|
171
|
+
ipfs: { pinataApiKey: 'YOUR_PINATA_API_KEY', pinataApiSecret: 'YOUR_PINATA_API_SECRET' },
|
|
172
|
+
})
|
|
173
|
+
|
|
174
|
+
await cowSdk.metadataApi.uploadMetadataDocToIpfs(appDataDoc)
|
|
175
|
+
/* 0x5ddb2c8207c10b96fac92cb934ef9ba004bc007a073c9e5b13edc422f209ed80 */
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
#### Querying the Cow Subgraph
|
|
125
179
|
|
|
126
|
-
|
|
180
|
+
You can query the Cow Subgraph either by running some common queries exposed by the `CowSubgraphApi` or by building your own ones:
|
|
127
181
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
182
|
+
```js
|
|
183
|
+
const chainId = 1 // Mainnet
|
|
184
|
+
const cowSdk = new CowSdk(chainId)
|
|
185
|
+
|
|
186
|
+
// Get Cow Protocol totals
|
|
187
|
+
const { totals } = await cowSdk.cowSubgraphApi.getTotals()
|
|
188
|
+
const { tokens, orders, traders, settlements, volumeUsd, volumeEth, feesUsd, feesEth } = totals
|
|
189
|
+
console.log({ tokens, orders, traders, settlements, volumeUsd, volumeEth, feesUsd, feesEth })
|
|
190
|
+
|
|
191
|
+
// Get the last 5 batches
|
|
192
|
+
const query = `
|
|
193
|
+
query LastBatches($n: Int!) {
|
|
194
|
+
settlements(orderBy: timestamp, orderDirection: desc, first: $n) {
|
|
195
|
+
txHash
|
|
196
|
+
timestamp
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
`
|
|
200
|
+
const variables = { n: 5 }
|
|
201
|
+
const response = await cowSdk.cowSubgraphApi.runQuery(query, variables)
|
|
202
|
+
console.log(response.data.settlements)
|
|
131
203
|
```
|
|
132
204
|
|
|
133
205
|
### Install Dependencies
|
package/dist/CowSdk.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Signer } from 'ethers';
|
|
2
2
|
import { LogLevelDesc } from 'loglevel';
|
|
3
|
-
import { CowApi, MetadataApi } from './api';
|
|
3
|
+
import { CowApi, CowSubgraphApi, MetadataApi } from './api';
|
|
4
4
|
import { SupportedChainId as ChainId } from './constants/chains';
|
|
5
5
|
import { validateAppDataDocument } from './utils/appData';
|
|
6
6
|
import { Context, CowContext } from './utils/context';
|
|
@@ -12,6 +12,7 @@ export declare class CowSdk<T extends ChainId> {
|
|
|
12
12
|
context: Context;
|
|
13
13
|
cowApi: CowApi;
|
|
14
14
|
metadataApi: MetadataApi;
|
|
15
|
+
cowSubgraphApi: CowSubgraphApi;
|
|
15
16
|
constructor(chainId: T, cowContext?: CowContext, options?: Options);
|
|
16
17
|
updateChainId: (chainId: ChainId) => void;
|
|
17
18
|
validateAppDataDocument: typeof validateAppDataDocument;
|