@cowprotocol/cow-sdk 0.0.1 → 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/LICENSE-APACHE +201 -0
- package/LICENSE-MIT +21 -0
- package/README.md +201 -7
- package/dist/CowSdk.d.ts +23 -0
- package/dist/api/cow/errors/OperatorError.d.ts +63 -0
- package/dist/api/cow/errors/QuoteError.d.ts +32 -0
- package/dist/api/cow/index.d.ts +38 -0
- package/dist/api/cow/types.d.ts +73 -0
- 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 +3 -0
- package/dist/api/metadata/index.d.ts +11 -0
- package/dist/api/metadata/types.d.ts +15 -0
- package/dist/appData.schema-d44994e0.js +2 -0
- package/dist/appData.schema-d44994e0.js.map +1 -0
- package/dist/appData.schema-fb2df827.js +2 -0
- package/dist/appData.schema-fb2df827.js.map +1 -0
- package/dist/constants/chains.d.ts +6 -0
- package/dist/constants/index.d.ts +4 -0
- package/dist/constants/tokens.d.ts +5 -0
- 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/types/index.d.ts +8 -0
- package/dist/utils/appData.d.ts +10 -0
- package/dist/utils/common.d.ts +7 -0
- package/dist/utils/context.d.ts +39 -0
- package/dist/utils/ipfs.d.ts +8 -0
- package/dist/utils/sign.d.ts +54 -0
- package/dist/utils/tokens.d.ts +2 -0
- package/package.json +61 -16
- package/babel.config.js +0 -6
- package/docs/images/CoW.png +0 -0
- package/src/CowSdk.ts +0 -10
- package/src/index.ts +0 -1
- package/src/schemas/appData.schema.json +0 -86
- package/src/utils/appData.spec.ts +0 -68
- package/src/utils/appData.ts +0 -34
- package/tsconfig.json +0 -11
package/LICENSE-APACHE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/LICENSE-MIT
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Gnosis Ltd
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,8 +1,206 @@
|
|
|
1
|
-
# CoW protocol SDK
|
|
2
1
|
<p align="center">
|
|
3
|
-
<img width="400" src="docs/images/CoW.png">
|
|
2
|
+
<img width="400" src="https://raw.githubusercontent.com/cowprotocol/cow-sdk/main/docs/images/CoW.png">
|
|
4
3
|
</p>
|
|
5
4
|
|
|
5
|
+
# CoW protocol SDK
|
|
6
|
+
|
|
7
|
+
[](https://prettier.io/)
|
|
8
|
+
[](https://coveralls.io/github/cowprotocol/cow-sdk?branch=main)
|
|
9
|
+
|
|
10
|
+
> ⚠️⚠️ THE SDK IS IN Beta ⚠️⚠️
|
|
11
|
+
> It is being currently develop and is a work in progress, also it's API is subjected to change.
|
|
12
|
+
> If you experience any problems, please open an issue in Github trying to describe your problem.
|
|
13
|
+
|
|
14
|
+
### Getting started
|
|
15
|
+
|
|
16
|
+
Install the SDK:
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
yarn add @cowprotocol/cow-sdk
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Instantiate the SDK:
|
|
23
|
+
|
|
24
|
+
```js
|
|
25
|
+
import { CowSdk } from 'cow-sdk'
|
|
26
|
+
|
|
27
|
+
const chainId = 4 // Rinkeby
|
|
28
|
+
const cowSdk = new CowSdk(chainId)
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
The SDK will expose the CoW API operations (`cowSdk.cowApi`) and some convenient method that will facilitate signing orders (`cowSdk.signOrder`). Future version will provide easy access to The Graph data and some other convenient utils.
|
|
32
|
+
|
|
33
|
+
```js
|
|
34
|
+
// i.e. Get last 5 orders for a given trader
|
|
35
|
+
const trades = await cowSdk.cowApi.getOrders({
|
|
36
|
+
owner: '0x00000000005ef87f8ca7014309ece7260bbcdaeb', // Trader
|
|
37
|
+
limit: 5,
|
|
38
|
+
offset: 0,
|
|
39
|
+
})
|
|
40
|
+
console.log(trades)
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Let's see a full example on how to submit an order to CowSwap.
|
|
44
|
+
|
|
45
|
+
> ⚠️ Before starting, the protocol requires you to approve the sell token before the order can be considered.
|
|
46
|
+
> For more details see https://docs.cow.fi/tutorials/how-to-submit-orders-via-the-api/1.-set-allowance-for-the-sell-token
|
|
47
|
+
|
|
48
|
+
In this example, we will:
|
|
49
|
+
|
|
50
|
+
- 1. **Instantiate the SDK and a wallet**: Used for signing orders
|
|
51
|
+
- 2. **Get a price/fee quote from the API**: Get current market price and required protocol fee to settle your trade.
|
|
52
|
+
- 3. **Sign the order using your wallet**: Only signed orders are considered by the protocol.
|
|
53
|
+
- 4. **Post the signed order to the API**: Post the order so it can be executed.
|
|
54
|
+
|
|
55
|
+
```js
|
|
56
|
+
import { Wallet } from 'ethers'
|
|
57
|
+
import { CowSdk, OrderKind } from 'cow-sdk'
|
|
58
|
+
|
|
59
|
+
// 1. Instantiate wallet and SDK
|
|
60
|
+
const mnemonic = 'fall dirt bread cactus...'
|
|
61
|
+
const wallet = Wallet.fromMnemonic(mnemonic)
|
|
62
|
+
const cowSdk = new CowSdk(4, { signer: wallet })
|
|
63
|
+
|
|
64
|
+
// 2. Get a price/fee quote from the API
|
|
65
|
+
// It will return the price and fee to "Sell 1 ETH for USDC"
|
|
66
|
+
const quoteResponse = await cowSdk.cowApi.getQuote({
|
|
67
|
+
kind: OrderKind.SELL, // Sell order (could also be BUY)
|
|
68
|
+
sellToken: '0xc778417e063141139fce010982780140aa0cd5ab', // WETH
|
|
69
|
+
buyToken: '0x4dbcdf9b62e891a7cec5a2568c3f4faf9e8abe2b', // USDC
|
|
70
|
+
amount: '1000000000000000000', // 1 WETH
|
|
71
|
+
userAddress: '0x1811be0994930fe9480eaede25165608b093ad7a', // Trader
|
|
72
|
+
validTo: 2524608000,
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
const { sellToken, buyToken, validTo, buyAmount, sellAmount, receiver, feeAmount } = quoteResponse.quote
|
|
76
|
+
const order = {
|
|
77
|
+
kind: OrderKind.SELL,
|
|
78
|
+
partiallyFillable: false, // Allow partial executions of an order (true would be for a "Fill or Kill" order, which is not yet supported but will be added soon)
|
|
79
|
+
sellToken,
|
|
80
|
+
buyToken,
|
|
81
|
+
validTo,
|
|
82
|
+
buyAmount,
|
|
83
|
+
sellAmount,
|
|
84
|
+
receiver,
|
|
85
|
+
feeAmount,
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// 3. Sign the order using your wallet
|
|
89
|
+
const signedOrder = await cowSdk.signOrder(order)
|
|
90
|
+
|
|
91
|
+
// 4. Post the signed order to the API
|
|
92
|
+
const orderId = await cowSdk.cowApi.sendOrder({
|
|
93
|
+
order: { ...order, ...signedOrder },
|
|
94
|
+
owner: '0x1811be0994930fe9480eaede25165608b093ad7a',
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
// We can inspect the Order details in the CoW Protocol Explorer
|
|
98
|
+
console.log(`https://explorer.cow.fi/rinkeby/orders/${orderId}`)
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
SDK also includes a Metadata API to interact with AppData documents and IPFS CIDs
|
|
102
|
+
|
|
103
|
+
```js
|
|
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
|
+
/* {
|
|
112
|
+
"appCode": "CowSwap",
|
|
113
|
+
"metadata": {
|
|
114
|
+
"referrer": {
|
|
115
|
+
"address": "0x1f5B740436Fc5935622e92aa3b46818906F416E9",
|
|
116
|
+
"version": "0.1.0"
|
|
117
|
+
}
|
|
118
|
+
},
|
|
119
|
+
"version": "0.1.0"
|
|
120
|
+
} */
|
|
121
|
+
|
|
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
|
|
179
|
+
|
|
180
|
+
You can query the Cow Subgraph either by running some common queries exposed by the `CowSubgraphApi` or by building your own ones:
|
|
181
|
+
|
|
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)
|
|
203
|
+
```
|
|
6
204
|
|
|
7
205
|
### Install Dependencies
|
|
8
206
|
|
|
@@ -14,11 +212,8 @@ yarn
|
|
|
14
212
|
|
|
15
213
|
```bash
|
|
16
214
|
yarn build
|
|
17
|
-
```
|
|
18
215
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
```bash
|
|
216
|
+
# Build in watch mode
|
|
22
217
|
yarn start
|
|
23
218
|
```
|
|
24
219
|
|
|
@@ -27,4 +222,3 @@ yarn start
|
|
|
27
222
|
```bash
|
|
28
223
|
yarn test
|
|
29
224
|
```
|
|
30
|
-
|
package/dist/CowSdk.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Signer } from 'ethers';
|
|
2
|
+
import { LogLevelDesc } from 'loglevel';
|
|
3
|
+
import { CowApi, CowSubgraphApi, MetadataApi } from './api';
|
|
4
|
+
import { SupportedChainId as ChainId } from './constants/chains';
|
|
5
|
+
import { validateAppDataDocument } from './utils/appData';
|
|
6
|
+
import { Context, CowContext } from './utils/context';
|
|
7
|
+
import { UnsignedOrder } from './utils/sign';
|
|
8
|
+
declare type Options = {
|
|
9
|
+
loglevel?: LogLevelDesc;
|
|
10
|
+
};
|
|
11
|
+
export declare class CowSdk<T extends ChainId> {
|
|
12
|
+
context: Context;
|
|
13
|
+
cowApi: CowApi;
|
|
14
|
+
metadataApi: MetadataApi;
|
|
15
|
+
cowSubgraphApi: CowSubgraphApi;
|
|
16
|
+
constructor(chainId: T, cowContext?: CowContext, options?: Options);
|
|
17
|
+
updateChainId: (chainId: ChainId) => void;
|
|
18
|
+
validateAppDataDocument: typeof validateAppDataDocument;
|
|
19
|
+
signOrder(order: Omit<UnsignedOrder, 'appData'>): Promise<import("./utils/sign").SigningResult>;
|
|
20
|
+
signOrderCancellation(orderId: string): Promise<import("./utils/sign").SigningResult>;
|
|
21
|
+
_checkSigner(signer?: Signer | undefined): Signer;
|
|
22
|
+
}
|
|
23
|
+
export default CowSdk;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { CowError } from '../../../utils/common';
|
|
2
|
+
declare type ApiActionType = 'get' | 'create' | 'delete';
|
|
3
|
+
export interface ApiErrorObject {
|
|
4
|
+
errorType: ApiErrorCodes;
|
|
5
|
+
description: string;
|
|
6
|
+
data?: any;
|
|
7
|
+
}
|
|
8
|
+
export declare enum ApiErrorCodes {
|
|
9
|
+
DuplicateOrder = "DuplicateOrder",
|
|
10
|
+
InvalidSignature = "InvalidSignature",
|
|
11
|
+
MissingOrderData = "MissingOrderData",
|
|
12
|
+
InsufficientValidTo = "InsufficientValidTo",
|
|
13
|
+
InsufficientAllowance = "InsufficientAllowance",
|
|
14
|
+
InsufficientBalance = "InsufficientBalance",
|
|
15
|
+
InsufficientFee = "InsufficientFee",
|
|
16
|
+
WrongOwner = "WrongOwner",
|
|
17
|
+
NotFound = "NotFound",
|
|
18
|
+
OrderNotFound = "OrderNotFound",
|
|
19
|
+
AlreadyCancelled = "AlreadyCancelled",
|
|
20
|
+
OrderFullyExecuted = "OrderFullyExecuted",
|
|
21
|
+
OrderExpired = "OrderExpired",
|
|
22
|
+
NoLiquidity = "NoLiquidity",
|
|
23
|
+
UnsupportedToken = "UnsupportedToken",
|
|
24
|
+
AmountIsZero = "AmountIsZero",
|
|
25
|
+
SellAmountDoesNotCoverFee = "SellAmountDoesNotCoverFee",
|
|
26
|
+
TransferEthToContract = "TransferEthToContract",
|
|
27
|
+
UNHANDLED_GET_ERROR = "UNHANDLED_GET_ERROR",
|
|
28
|
+
UNHANDLED_CREATE_ERROR = "UNHANDLED_CREATE_ERROR",
|
|
29
|
+
UNHANDLED_DELETE_ERROR = "UNHANDLED_DELETE_ERROR"
|
|
30
|
+
}
|
|
31
|
+
export declare enum ApiErrorCodeDetails {
|
|
32
|
+
DuplicateOrder = "There was another identical order already submitted. Please try again.",
|
|
33
|
+
InsufficientFee = "The signed fee is insufficient. It's possible that is higher now due to a change in the gas price, ether price, or the sell token price. Please try again to get an updated fee quote.",
|
|
34
|
+
InvalidSignature = "The order signature is invalid. Check whether your Wallet app supports off-chain signing.",
|
|
35
|
+
MissingOrderData = "The order has missing information",
|
|
36
|
+
InsufficientValidTo = "The order you are signing is already expired. This can happen if you set a short expiration in the settings and waited too long before signing the transaction. Please try again.",
|
|
37
|
+
InsufficientAllowance = "The account doesn't have enough funds",
|
|
38
|
+
InsufficientBalance = "The account needs to approve the selling token in order to trade",
|
|
39
|
+
WrongOwner = "The signature is invalid.\n\nIt's likely that the signing method provided by your wallet doesn't comply with the standards required by CowSwap.\n\nCheck whether your Wallet app supports off-chain signing (EIP-712 or ETHSIGN).",
|
|
40
|
+
NotFound = "Token pair selected has insufficient liquidity",
|
|
41
|
+
OrderNotFound = "The order you are trying to cancel does not exist",
|
|
42
|
+
AlreadyCancelled = "Order is already cancelled",
|
|
43
|
+
OrderFullyExecuted = "Order is already filled",
|
|
44
|
+
OrderExpired = "Order is expired",
|
|
45
|
+
NoLiquidity = "Token pair selected has insufficient liquidity",
|
|
46
|
+
UnsupportedToken = "One of the tokens you are trading is unsupported. Please read the FAQ for more info.",
|
|
47
|
+
AmountIsZero = "Amount is zero",
|
|
48
|
+
SellAmountDoesNotCoverFee = "Sell amount does not sufficiently cover the current fee",
|
|
49
|
+
TransferEthToContract = "Sending the native currency to smart contract wallets is not currently supported",
|
|
50
|
+
UNHANDLED_GET_ERROR = "Order fetch failed. This may be due to a server or network connectivity issue. Please try again later.",
|
|
51
|
+
UNHANDLED_CREATE_ERROR = "The order was not accepted by the network",
|
|
52
|
+
UNHANDLED_DELETE_ERROR = "The order cancellation was not accepted by the network"
|
|
53
|
+
}
|
|
54
|
+
export default class OperatorError extends CowError {
|
|
55
|
+
name: string;
|
|
56
|
+
description: ApiErrorObject['description'];
|
|
57
|
+
static apiErrorDetails: typeof ApiErrorCodeDetails;
|
|
58
|
+
static getErrorMessage(response: Response, action: ApiActionType): Promise<string>;
|
|
59
|
+
static getErrorFromStatusCode(response: Response, action: 'create' | 'delete'): Promise<string>;
|
|
60
|
+
constructor(apiError: ApiErrorObject);
|
|
61
|
+
}
|
|
62
|
+
export declare function isValidOperatorError(error: any): error is OperatorError;
|
|
63
|
+
export {};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { CowError } from '../../../utils/common';
|
|
2
|
+
import { ApiErrorObject } from './OperatorError';
|
|
3
|
+
export interface GpQuoteErrorObject {
|
|
4
|
+
errorType: GpQuoteErrorCodes;
|
|
5
|
+
description: string;
|
|
6
|
+
data?: any;
|
|
7
|
+
}
|
|
8
|
+
export declare enum GpQuoteErrorCodes {
|
|
9
|
+
UnsupportedToken = "UnsupportedToken",
|
|
10
|
+
InsufficientLiquidity = "InsufficientLiquidity",
|
|
11
|
+
FeeExceedsFrom = "FeeExceedsFrom",
|
|
12
|
+
ZeroPrice = "ZeroPrice",
|
|
13
|
+
UNHANDLED_ERROR = "UNHANDLED_ERROR"
|
|
14
|
+
}
|
|
15
|
+
export declare enum GpQuoteErrorDetails {
|
|
16
|
+
UnsupportedToken = "One of the tokens you are trading is unsupported. Please read the FAQ for more info.",
|
|
17
|
+
InsufficientLiquidity = "Token pair selected has insufficient liquidity",
|
|
18
|
+
FeeExceedsFrom = "Current fee exceeds entered \"from\" amount",
|
|
19
|
+
ZeroPrice = "Quoted price is zero. This is likely due to a significant price difference between the two tokens. Please try increasing amounts.",
|
|
20
|
+
UNHANDLED_ERROR = "Quote fetch failed. This may be due to a server or network connectivity issue. Please try again later."
|
|
21
|
+
}
|
|
22
|
+
export declare function mapOperatorErrorToQuoteError(error?: ApiErrorObject): GpQuoteErrorObject;
|
|
23
|
+
export default class GpQuoteError extends CowError {
|
|
24
|
+
name: string;
|
|
25
|
+
description: string;
|
|
26
|
+
data?: any;
|
|
27
|
+
static quoteErrorDetails: typeof GpQuoteErrorDetails;
|
|
28
|
+
static getErrorMessage(response: Response): Promise<string>;
|
|
29
|
+
static getErrorFromStatusCode(response: Response): Promise<string>;
|
|
30
|
+
constructor(quoteError: GpQuoteErrorObject);
|
|
31
|
+
}
|
|
32
|
+
export declare function isValidQuoteError(error: any): error is GpQuoteError;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { SupportedChainId as ChainId } from '../../constants/chains';
|
|
2
|
+
import { OrderCreation } from '../../utils/sign';
|
|
3
|
+
import { FeeQuoteParams, PriceInformation, PriceQuoteParams, SimpleGetQuoteResponse } from '../../utils/price';
|
|
4
|
+
import { GetOrdersParams, GetTradesParams, OrderCancellationParams, OrderID, OrderMetaData, ProfileData, TradeMetaData } from './types';
|
|
5
|
+
import { Context } from '../../utils/context';
|
|
6
|
+
export declare class CowApi {
|
|
7
|
+
context: Context;
|
|
8
|
+
API_NAME: string;
|
|
9
|
+
constructor(context: Context);
|
|
10
|
+
get DEFAULT_HEADERS(): {
|
|
11
|
+
'Content-Type': string;
|
|
12
|
+
'X-AppId': string;
|
|
13
|
+
};
|
|
14
|
+
get API_BASE_URL(): Partial<Record<ChainId, string>>;
|
|
15
|
+
get PROFILE_API_BASE_URL(): Partial<Record<ChainId, string>>;
|
|
16
|
+
getProfileData(address: string): Promise<ProfileData | null>;
|
|
17
|
+
getTrades(params: GetTradesParams): Promise<TradeMetaData[]>;
|
|
18
|
+
getOrders(params: GetOrdersParams): Promise<OrderMetaData[]>;
|
|
19
|
+
getTxOrders(txHash: string): Promise<OrderMetaData[]>;
|
|
20
|
+
getOrder(orderId: string): Promise<OrderMetaData | null>;
|
|
21
|
+
getPriceQuoteLegacy(params: PriceQuoteParams): Promise<PriceInformation | null>;
|
|
22
|
+
getQuote(params: FeeQuoteParams): Promise<SimpleGetQuoteResponse>;
|
|
23
|
+
sendSignedOrderCancellation(params: OrderCancellationParams): Promise<void>;
|
|
24
|
+
sendOrder(params: {
|
|
25
|
+
order: Omit<OrderCreation, 'appData'>;
|
|
26
|
+
owner: string;
|
|
27
|
+
}): Promise<OrderID>;
|
|
28
|
+
getOrderLink(orderId: OrderID): string;
|
|
29
|
+
private mapNewToLegacyParams;
|
|
30
|
+
private getApiBaseUrl;
|
|
31
|
+
private getProfileApiBaseUrl;
|
|
32
|
+
private fetch;
|
|
33
|
+
private fetchProfile;
|
|
34
|
+
private post;
|
|
35
|
+
private get;
|
|
36
|
+
private getProfile;
|
|
37
|
+
private delete;
|
|
38
|
+
}
|