@cowprotocol/cow-sdk 0.0.8-RC.0 → 0.0.11

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.
Files changed (56) hide show
  1. package/README.md +102 -23
  2. package/dist/CowSdk.d.ts +2 -1
  3. package/dist/api/cow/index.d.ts +1 -1
  4. package/dist/api/cow/types.d.ts +34 -1
  5. package/dist/api/cow-subgraph/graphql.d.ts +2603 -0
  6. package/dist/api/cow-subgraph/index.d.ts +17 -0
  7. package/dist/api/cow-subgraph/queries.d.ts +3 -0
  8. package/dist/api/index.d.ts +1 -0
  9. package/dist/api/metadata/index.d.ts +3 -1
  10. package/dist/index.d.ts +3 -2
  11. package/dist/index.js +28 -1
  12. package/dist/index.js.map +1 -1
  13. package/dist/index.modern.js +28 -1
  14. package/dist/index.modern.js.map +1 -1
  15. package/dist/index.module.js +28 -1
  16. package/dist/index.module.js.map +1 -1
  17. package/dist/utils/context.d.ts +12 -3
  18. package/dist/utils/ipfs.d.ts +8 -0
  19. package/package.json +27 -6
  20. package/.babelrc +0 -4
  21. package/.eslintrc.json +0 -15
  22. package/.github/workflows/build.yml +0 -50
  23. package/.github/workflows/lint.yml +0 -19
  24. package/.github/workflows/publish.yml +0 -20
  25. package/.github/workflows/test.yml +0 -47
  26. package/.nvmrc +0 -1
  27. package/.prettierignore +0 -1
  28. package/.prettierrc +0 -5
  29. package/COPYRIGHT.md +0 -13
  30. package/LICENSE-APACHE +0 -201
  31. package/LICENSE-MIT +0 -21
  32. package/babel.config.js +0 -3
  33. package/docs/images/CoW.png +0 -0
  34. package/src/CowSdk.ts +0 -53
  35. package/src/api/cow/errors/OperatorError.ts +0 -142
  36. package/src/api/cow/errors/QuoteError.ts +0 -115
  37. package/src/api/cow/index.ts +0 -367
  38. package/src/api/cow/types.ts +0 -82
  39. package/src/api/index.ts +0 -2
  40. package/src/api/metadata/index.ts +0 -37
  41. package/src/api/metadata/types.ts +0 -17
  42. package/src/constants/chains.ts +0 -11
  43. package/src/constants/index.ts +0 -16
  44. package/src/constants/tokens.ts +0 -16
  45. package/src/index.ts +0 -4
  46. package/src/schemas/appData.schema.json +0 -70
  47. package/src/types/index.ts +0 -6
  48. package/src/utils/appData.spec.ts +0 -109
  49. package/src/utils/appData.ts +0 -58
  50. package/src/utils/common.ts +0 -35
  51. package/src/utils/context.ts +0 -89
  52. package/src/utils/price.ts +0 -44
  53. package/src/utils/sign.ts +0 -224
  54. package/src/utils/tokens.ts +0 -12
  55. package/src/workflows/publish.sh +0 -49
  56. 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/gnosis/cow-sdk/main/docs/images/CoW.png">
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
- [![Styled With Prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://prettier.io/)
7
6
 
7
+ [![Styled With Prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://prettier.io/)
8
+ [![Coverage Status](https://coveralls.io/repos/github/cowprotocol/cow-sdk/badge.svg?branch=main)](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', // USDC
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
- const chainId = 4 // Rinkeby
103
- const cowSdk = new CowSdk(chainId)
104
- let hash = '0xa6c81f4ca727252a05b108f1742a07430f28d474d2a3492d8f325746824d22e5'
105
-
106
- // Decode AppData document given a CID hash
107
- const appDataDoc = await cowSdk.metadataApi.decodeAppData(hash)
108
- console.log(appDataDoc)
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,94 @@ 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
- const cid = 'QmUf2TrpSANVXdgcYfAAACe6kg551cY3rAemB7xfEMjYvs'
121
-
122
- // Decode CID hash to AppData Hex
123
- const decodedAppDataHex = await cowSdk.metadataApi.cidToAppDataHex(cid)
124
- console.log(decodedAppDataHex) //0x5ddb2c8207c10b96fac92cb934ef9ba004bc007a073c9e5b13edc422f209ed80
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
+ */
125
168
 
126
- hash = '0x5ddb2c8207c10b96fac92cb934ef9ba004bc007a073c9e5b13edc422f209ed80'
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)
127
185
 
128
- // Decode AppData Hex to CID
129
- const decodedAppDataHex = await cowSdk.metadataApi.appDataHexToCid(hash)
130
- console.log(decodedAppDataHex) //QmUf2TrpSANVXdgcYfAAACe6kg551cY3rAemB7xfEMjYvs
186
+ // Get Cow Protocol totals
187
+ const { tokens, orders, traders, settlements, volumeUsd, volumeEth, feesUsd, feesEth } = await cowSdk.cowSubgraphApi.getTotals()
188
+ console.log({ tokens, orders, traders, settlements, volumeUsd, volumeEth, feesUsd, feesEth })
189
+
190
+ // Get last 24 hours volume in usd
191
+ const { hourlyTotals } = await cowSdk.cowSubgraphApi.getLastHoursVolume(24)
192
+ console.log(hourlyTotals)
193
+
194
+ // Get last week volume in usd
195
+ const { dailyTotals } = await cowSdk.cowSubgraphApi.getLastDaysVolume(7)
196
+ console.log(dailyTotals)
197
+
198
+ // Get the last 5 batches
199
+ const query = `
200
+ query LastBatches($n: Int!) {
201
+ settlements(orderBy: firstTradeTimestamp, orderDirection: desc, first: $n) {
202
+ txHash
203
+ firstTradeTimestamp
204
+ }
205
+ }
206
+ `
207
+ const variables = { n: 5 }
208
+ const response = await cowSdk.cowSubgraphApi.runQuery(query, variables)
209
+ console.log(response)
131
210
  ```
132
211
 
133
212
  ### 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;
@@ -1,6 +1,6 @@
1
1
  import { SupportedChainId as ChainId } from '../../constants/chains';
2
2
  import { OrderCreation } from '../../utils/sign';
3
- import { FeeQuoteParams, PriceInformation, PriceQuoteParams, SimpleGetQuoteResponse } from '../../utils/price';
3
+ import { FeeQuoteParams, PriceInformation, PriceQuoteParams, SimpleGetQuoteResponse } from './types';
4
4
  import { GetOrdersParams, GetTradesParams, OrderCancellationParams, OrderID, OrderMetaData, ProfileData, TradeMetaData } from './types';
5
5
  import { Context } from '../../utils/context';
6
6
  export declare class CowApi {
@@ -1,4 +1,4 @@
1
- import { OrderKind } from '@gnosis.pm/gp-v2-contracts';
1
+ import { GetQuoteResponse, OrderKind } from '@gnosis.pm/gp-v2-contracts';
2
2
  import { SupportedChainId as ChainId } from '../../constants/chains';
3
3
  import { OrderCancellation, SigningSchemeValue } from '../../utils/sign';
4
4
  /**
@@ -71,3 +71,36 @@ export declare type ProfileData = {
71
71
  referralVolumeUsd: number;
72
72
  lastUpdated: string;
73
73
  };
74
+ export interface QuoteParams {
75
+ quoteParams: FeeQuoteParams;
76
+ fetchFee: boolean;
77
+ previousFee?: FeeInformation;
78
+ isPriceRefresh: boolean;
79
+ }
80
+ export interface FeeInformation {
81
+ expirationDate: string;
82
+ amount: string;
83
+ }
84
+ export interface PriceInformation {
85
+ token: string;
86
+ amount: string | null;
87
+ }
88
+ export declare type SimpleGetQuoteResponse = Pick<GetQuoteResponse, 'from'> & {
89
+ quote: Omit<GetQuoteResponse['quote'], 'sellAmount' | 'buyAmount' | 'feeAmount' | 'validTo'> & {
90
+ sellAmount: string;
91
+ buyAmount: string;
92
+ validTo: string;
93
+ feeAmount: string;
94
+ };
95
+ expiration: string;
96
+ };
97
+ export declare type FeeQuoteParams = Pick<OrderMetaData, 'sellToken' | 'buyToken' | 'kind'> & {
98
+ amount: string;
99
+ userAddress?: string | null;
100
+ receiver?: string | null;
101
+ validTo: number;
102
+ };
103
+ export declare type PriceQuoteParams = Omit<FeeQuoteParams, 'sellToken' | 'buyToken'> & {
104
+ baseToken: string;
105
+ quoteToken: string;
106
+ };