@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
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,
|
|
@@ -96,6 +98,117 @@ const orderId = await cowSdk.cowApi.sendOrder({
|
|
|
96
98
|
console.log(`https://explorer.cow.fi/rinkeby/orders/${orderId}`)
|
|
97
99
|
```
|
|
98
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 { 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)
|
|
210
|
+
```
|
|
211
|
+
|
|
99
212
|
### Install Dependencies
|
|
100
213
|
|
|
101
214
|
```bash
|
|
@@ -116,4 +229,3 @@ yarn start
|
|
|
116
229
|
```bash
|
|
117
230
|
yarn test
|
|
118
231
|
```
|
|
119
|
-
|
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;
|
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
import { SupportedChainId as ChainId } from '
|
|
2
|
-
import { OrderCreation } from '
|
|
3
|
-
import { FeeQuoteParams, PriceInformation, PriceQuoteParams, SimpleGetQuoteResponse } from '
|
|
4
|
-
import { GetOrdersParams, GetTradesParams, OrderCancellationParams, OrderID, OrderMetaData, ProfileData, TradeMetaData } from '
|
|
5
|
-
import { Context } from '
|
|
6
|
-
export declare class CowApi
|
|
7
|
-
chainId: T;
|
|
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 {
|
|
8
7
|
context: Context;
|
|
9
8
|
API_NAME: string;
|
|
10
|
-
constructor(
|
|
9
|
+
constructor(context: Context);
|
|
11
10
|
get DEFAULT_HEADERS(): {
|
|
12
11
|
'Content-Type': string;
|
|
13
12
|
'X-AppId': string;
|
|
@@ -17,6 +16,7 @@ export declare class CowApi<T extends ChainId> {
|
|
|
17
16
|
getProfileData(address: string): Promise<ProfileData | null>;
|
|
18
17
|
getTrades(params: GetTradesParams): Promise<TradeMetaData[]>;
|
|
19
18
|
getOrders(params: GetOrdersParams): Promise<OrderMetaData[]>;
|
|
19
|
+
getTxOrders(txHash: string): Promise<OrderMetaData[]>;
|
|
20
20
|
getOrder(orderId: string): Promise<OrderMetaData | null>;
|
|
21
21
|
getPriceQuoteLegacy(params: PriceQuoteParams): Promise<PriceInformation | null>;
|
|
22
22
|
getQuote(params: FeeQuoteParams): Promise<SimpleGetQuoteResponse>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { OrderKind } from '@gnosis.pm/gp-v2-contracts';
|
|
2
|
-
import { SupportedChainId as ChainId } from '
|
|
3
|
-
import { OrderCancellation, SigningSchemeValue } from '
|
|
2
|
+
import { SupportedChainId as ChainId } from '../../constants/chains';
|
|
3
|
+
import { OrderCancellation, SigningSchemeValue } from '../../utils/sign';
|
|
4
4
|
/**
|
|
5
5
|
* Unique identifier for the order, calculated by keccak256(orderDigest, ownerAddress, validTo),
|
|
6
6
|
* where orderDigest = keccak256(orderStruct). bytes32.
|
|
@@ -71,3 +71,4 @@ export declare type ProfileData = {
|
|
|
71
71
|
referralVolumeUsd: number;
|
|
72
72
|
lastUpdated: string;
|
|
73
73
|
};
|
|
74
|
+
export { FeeQuoteParams, PriceQuoteParams, QuoteParams, FeeInformation, PriceInformation, SimpleGetQuoteResponse, } from '../../utils/price';
|