@cowprotocol/sdk-trading 0.1.0-monorepo.0
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 +576 -0
- package/dist/index.d.mts +326 -0
- package/dist/index.d.ts +326 -0
- package/dist/index.js +1067 -0
- package/dist/index.mjs +1025 -0
- package/package.json +51 -0
package/README.md
ADDED
|
@@ -0,0 +1,576 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img width="400" src="https://github.com/cowprotocol/cow-sdk/raw/main/docs/images/CoW.png" />
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
# Trading SDK
|
|
6
|
+
|
|
7
|
+
CoW Protocol is intent based, decentralized trading protocol that allows users to trade ERC-20 tokens.
|
|
8
|
+
This SDK makes it easier to interact with CoW Protocol by handling order parameters, calculating amounts, and signing orders.
|
|
9
|
+
|
|
10
|
+
## Basic Trading Flow
|
|
11
|
+
|
|
12
|
+
1. 🔎 Get a quote (price) for a trade
|
|
13
|
+
2. ✍️ Sign the order
|
|
14
|
+
3. ✅ Post the order to the order-book
|
|
15
|
+
|
|
16
|
+
The CoW Protocol provides very flexible and powerful trading capabilities. However, this flexibility comes with a cost: the complexity of the protocol. This SDK serves to simplify the interaction with the CoW Protocol. It will put all necessary parameters to your order, calculates proper amounts, and signs the order.
|
|
17
|
+
|
|
18
|
+
## Why Use This SDK?
|
|
19
|
+
|
|
20
|
+
- **App-data** - Order metadata handling
|
|
21
|
+
- **Order signing** - EIP-712 signature management
|
|
22
|
+
- **Network costs** - Automatic fee and slippage calculations
|
|
23
|
+
- **Order parameters** - Validity, partial fills, etc.
|
|
24
|
+
- **Quote API settings** - Price quality, signing scheme, etc.
|
|
25
|
+
- **Order types** - Market, limit, on-chain trades, etc.
|
|
26
|
+
|
|
27
|
+
> See the [examples](https://github.com/cowprotocol/cow-sdk/tree/main/examples) for usage.
|
|
28
|
+
|
|
29
|
+
## Installation
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
npm install @cowprotocol/sdk-trading
|
|
33
|
+
or
|
|
34
|
+
pnpm add @cowprotocol/sdk-trading
|
|
35
|
+
or
|
|
36
|
+
yarn add @cowprotocol/sdk-trading
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## TradingSdk Functions
|
|
40
|
+
|
|
41
|
+
Main functions:
|
|
42
|
+
|
|
43
|
+
- `postSwapOrder` - Get a quote and create a swap order.
|
|
44
|
+
- `postLimitOrder` - Create a limit order.
|
|
45
|
+
- `getQuote` - Fetch a quote for a swap order.
|
|
46
|
+
|
|
47
|
+
Special cases:
|
|
48
|
+
|
|
49
|
+
- 'setTraderParams' - In case if you work with different chains and need to switch between them in runtime.
|
|
50
|
+
- `postSellNativeCurrencyOrder` - Sell blockchain native tokens (e.g., ETH on Ethereum).
|
|
51
|
+
- `getPreSignTransaction` - Sign an order using a smart contract wallet.
|
|
52
|
+
|
|
53
|
+
### Setup
|
|
54
|
+
|
|
55
|
+
You need:
|
|
56
|
+
|
|
57
|
+
- `chainId` - Supported chain ID ([see list](https://docs.cow.fi/cow-protocol/reference/sdks/core-utilities/sdk-config)).
|
|
58
|
+
- `appCode` - Unique app identifier for tracking orders.
|
|
59
|
+
- `signer` - Private key, ethers signer, or `Eip1193` provider (optional - will use global adapter's signer if not provided).
|
|
60
|
+
|
|
61
|
+
## Usage
|
|
62
|
+
|
|
63
|
+
### Individual package usage
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
import { SupportedChainId, OrderKind, TradeParameters, TradingSdk } from '@cowprotocol/sdk-trading'
|
|
67
|
+
import { EthersV6Adapter } from '@cowprotocol/sdk-ethers-v6-adapter'
|
|
68
|
+
import { JsonRpcProvider, Wallet } from 'ethers'
|
|
69
|
+
|
|
70
|
+
// Configure the adapter
|
|
71
|
+
const provider = new JsonRpcProvider('YOUR_RPC_URL')
|
|
72
|
+
const wallet = new Wallet('YOUR_PRIVATE_KEY', provider)
|
|
73
|
+
const adapter = new EthersV6Adapter({ provider, signer: wallet })
|
|
74
|
+
|
|
75
|
+
// Initialize the SDK
|
|
76
|
+
const sdk = new TradingSdk(
|
|
77
|
+
{
|
|
78
|
+
appCode: 'YOUR_APP_CODE',
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
chainId: SupportedChainId.SEPOLIA,
|
|
82
|
+
},
|
|
83
|
+
adapter, // Pass the adapter
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
// Define trade parameters
|
|
87
|
+
const parameters: TradeParameters = {
|
|
88
|
+
kind: OrderKind.BUY,
|
|
89
|
+
sellToken: '0xfff9976782d46cc05630d1f6ebab18b2324d6b14',
|
|
90
|
+
sellTokenDecimals: 18,
|
|
91
|
+
buyToken: '0x0625afb445c3b6b7b929342a04a22599fd5dbb59',
|
|
92
|
+
buyTokenDecimals: 18,
|
|
93
|
+
amount: '120000000000000000',
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Post the order
|
|
97
|
+
const orderId = await sdk.postSwapOrder(parameters)
|
|
98
|
+
console.log('Order created, id: ', orderId)
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### Usage with Umbrella SDK
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
import { CowSdk, SupportedChainId, OrderKind, TradeParameters } from '@cowprotocol/cow-sdk'
|
|
105
|
+
import { EthersV6Adapter } from '@cowprotocol/sdk-ethers-v6-adapter'
|
|
106
|
+
import { JsonRpcProvider, Wallet } from 'ethers'
|
|
107
|
+
|
|
108
|
+
// Configure the adapter
|
|
109
|
+
const provider = new JsonRpcProvider('YOUR_RPC_URL')
|
|
110
|
+
const wallet = new Wallet('YOUR_PRIVATE_KEY', provider)
|
|
111
|
+
const adapter = new EthersV6Adapter({ provider, signer: wallet })
|
|
112
|
+
|
|
113
|
+
// Initialize the unified SDK
|
|
114
|
+
const sdk = new CowSdk({
|
|
115
|
+
chainId: SupportedChainId.SEPOLIA,
|
|
116
|
+
adapter,
|
|
117
|
+
tradingOptions: {
|
|
118
|
+
traderParams: {
|
|
119
|
+
appCode: 'YOUR_APP_CODE',
|
|
120
|
+
},
|
|
121
|
+
options: {
|
|
122
|
+
chainId: SupportedChainId.SEPOLIA,
|
|
123
|
+
},
|
|
124
|
+
},
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
// Define trade parameters
|
|
128
|
+
const parameters: TradeParameters = {
|
|
129
|
+
kind: OrderKind.BUY,
|
|
130
|
+
sellToken: '0xfff9976782d46cc05630d1f6ebab18b2324d6b14',
|
|
131
|
+
sellTokenDecimals: 18,
|
|
132
|
+
buyToken: '0x0625afb445c3b6b7b929342a04a22599fd5dbb59',
|
|
133
|
+
buyTokenDecimals: 18,
|
|
134
|
+
amount: '120000000000000000',
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Post the order
|
|
138
|
+
const orderId = await sdk.trading.postSwapOrder(parameters)
|
|
139
|
+
console.log('Order created, id: ', orderId)
|
|
140
|
+
|
|
141
|
+
// Or you can just initialize the SDK and import the trading class in another file:
|
|
142
|
+
// you don't necessarily need to use the trading from inside of CowSdk:
|
|
143
|
+
//...
|
|
144
|
+
const sdk = new CowSdk({
|
|
145
|
+
chainId: SupportedChainId.SEPOLIA,
|
|
146
|
+
adapter,
|
|
147
|
+
})
|
|
148
|
+
// Other file:
|
|
149
|
+
import { TradingSdk } from '@cowprotocol/cow-sdk'
|
|
150
|
+
// parameters without passing the adapter. the adapter will be controlled by the umbrella
|
|
151
|
+
const trading = TradingSdk(...)
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### Options
|
|
155
|
+
|
|
156
|
+
For detailed information about trading steps you can enable the SDK logging and configure UTM tracking:
|
|
157
|
+
|
|
158
|
+
```typescript
|
|
159
|
+
import { SupportedChainId, TradingSdk, TradingSdkOptions } from '@cowprotocol/cow-sdk'
|
|
160
|
+
|
|
161
|
+
const traderParams = {
|
|
162
|
+
chainId: SupportedChainId.SEPOLIA,
|
|
163
|
+
appCode: '<YOUR_APP_CODE>',
|
|
164
|
+
signer: '<privateKeyOrEthersSigner>', // signer is optional - will use global adapter's signer if not provided
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const sdkOptions: TradingSdkOptions = {
|
|
168
|
+
enableLogging: true, // enables detailed logging of trading steps
|
|
169
|
+
utmContent: '🐮 moo-ving to defi 🐮', // custom UTM content for tracking
|
|
170
|
+
disableUtm: false, // set to true to disable UTM tracking completely
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const sdk = new TradingSdk(traderParams, sdkOptions)
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### getQuote
|
|
177
|
+
|
|
178
|
+
In case if you want to get a quote and only then create an order, you can use the `getQuote` function.
|
|
179
|
+
|
|
180
|
+
The parameters required are the same as for the `postSwapOrder` function.
|
|
181
|
+
|
|
182
|
+
The function returns `quoteResults` object with the following properties:
|
|
183
|
+
|
|
184
|
+
- `tradeParameters` - trade type, assets, amounts and other optional parameters
|
|
185
|
+
- `amountsAndCosts` - the order sell/buy amounts including network costs, fees and slippage
|
|
186
|
+
- `orderToSign` - order parameters to sign (see [order signing](https://docs.cow.fi/cow-protocol/reference/sdks/cow-sdk/classes/OrderSigningUtils))
|
|
187
|
+
- `quoteResponse` - DTO from [quote API](https://api.cow.fi/docs/#/default/post_api_v1_quote)
|
|
188
|
+
- `appDataInfo` - [order's metadata](https://docs.cow.fi/cow-protocol/reference/sdks/app-data)
|
|
189
|
+
- `orderTypedData` - EIP-712 typed data for signing
|
|
190
|
+
|
|
191
|
+
Another parameter is returned by this function is `postSwapOrderFromQuote`.
|
|
192
|
+
It can be used to create an order from the received quote.
|
|
193
|
+
|
|
194
|
+
#### Example
|
|
195
|
+
|
|
196
|
+
```typescript
|
|
197
|
+
import { SupportedChainId, OrderKind, TradeParameters, TradingSdk } from '@cowprotocol/cow-sdk'
|
|
198
|
+
|
|
199
|
+
// Proper adapter initialization (see setup example above)
|
|
200
|
+
const sdk = new TradingSdk({
|
|
201
|
+
chainId: SupportedChainId.SEPOLIA,
|
|
202
|
+
appCode: '<YOUR_APP_CODE>',
|
|
203
|
+
})
|
|
204
|
+
|
|
205
|
+
const parameters: TradeParameters = {
|
|
206
|
+
kind: OrderKind.BUY,
|
|
207
|
+
sellToken: '0xfff9976782d46cc05630d1f6ebab18b2324d6b14',
|
|
208
|
+
sellTokenDecimals: 18,
|
|
209
|
+
buyToken: '0x0625afb445c3b6b7b929342a04a22599fd5dbb59',
|
|
210
|
+
buyTokenDecimals: 18,
|
|
211
|
+
amount: '120000000000000000',
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const { quoteResults, postSwapOrderFromQuote } = await sdk.getQuote(parameters)
|
|
215
|
+
|
|
216
|
+
const buyAmount = quoteResults.amountsAndCosts.afterSlippage.buyAmount
|
|
217
|
+
|
|
218
|
+
if (confirm(`You will get at least: ${buyAmount}, ok?`)) {
|
|
219
|
+
const orderId = await postSwapOrderFromQuote()
|
|
220
|
+
|
|
221
|
+
console.log('Order created, id: ', orderId)
|
|
222
|
+
}
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
### postSwapOrder
|
|
226
|
+
|
|
227
|
+
This function fetches a quote for a swap order and just creates the order.
|
|
228
|
+
|
|
229
|
+
The parameters required are:
|
|
230
|
+
|
|
231
|
+
- `kind` - the order kind (sell/buy)
|
|
232
|
+
- `sellToken` - the sell token address
|
|
233
|
+
- `sellTokenDecimals` - the sell token decimals
|
|
234
|
+
- `buyToken` - the buy token address
|
|
235
|
+
- `buyTokenDecimals` - the buy token decimals
|
|
236
|
+
- `amount` - the amount to sell/buy in atoms
|
|
237
|
+
|
|
238
|
+
> When sell token is a blockchain native token (ETH for Ethereum), then order will be created as an on-chain transaction. See the `postSellNativeCurrencyOrder` method below for more details.
|
|
239
|
+
|
|
240
|
+
#### Example
|
|
241
|
+
|
|
242
|
+
```typescript
|
|
243
|
+
import { SupportedChainId, OrderKind, TradeParameters, TradingSdk } from '@cowprotocol/cow-sdk'
|
|
244
|
+
|
|
245
|
+
// Proper adapter initialization (see setup example above)
|
|
246
|
+
const sdk = new TradingSdk({
|
|
247
|
+
chainId: SupportedChainId.SEPOLIA,
|
|
248
|
+
appCode: '<YOUR_APP_CODE>',
|
|
249
|
+
})
|
|
250
|
+
|
|
251
|
+
const parameters: TradeParameters = {
|
|
252
|
+
kind: OrderKind.BUY,
|
|
253
|
+
sellToken: '0xfff9976782d46cc05630d1f6ebab18b2324d6b14',
|
|
254
|
+
sellTokenDecimals: 18,
|
|
255
|
+
buyToken: '0x0625afb445c3b6b7b929342a04a22599fd5dbb59',
|
|
256
|
+
buyTokenDecimals: 18,
|
|
257
|
+
amount: '120000000000000000',
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const { orderId } = await sdk.postSwapOrder(parameters)
|
|
261
|
+
|
|
262
|
+
console.log('Order created, id: ', orderId)
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
### postLimitOrder
|
|
266
|
+
|
|
267
|
+
This main difference between this function and `postSwapOrder` is that here you need to specify both sell and buy amounts.
|
|
268
|
+
|
|
269
|
+
You need to provide the following parameters:
|
|
270
|
+
|
|
271
|
+
- `kind` - the order kind (sell/buy)
|
|
272
|
+
- `sellToken` - the sell token address
|
|
273
|
+
- `sellTokenDecimals` - the sell token decimals
|
|
274
|
+
- `buyToken` - the buy token address
|
|
275
|
+
- `buyTokenDecimals` - the buy token decimals
|
|
276
|
+
- `sellAmount` - the amount to sell in atoms
|
|
277
|
+
- `buyAmount` - the amount to buy in atoms
|
|
278
|
+
|
|
279
|
+
And optional parameters:
|
|
280
|
+
|
|
281
|
+
- `quoteId` - id of the quote from the quote API (see getQuote function)
|
|
282
|
+
- `validTo` - the order expiration time in seconds
|
|
283
|
+
|
|
284
|
+
```typescript
|
|
285
|
+
import { SupportedChainId, OrderKind, LimitTradeParameters, TradingSdk } from '@cowprotocol/cow-sdk'
|
|
286
|
+
|
|
287
|
+
// Proper adapter initialization (see setup example above)
|
|
288
|
+
const sdk = new TradingSdk({
|
|
289
|
+
chainId: SupportedChainId.SEPOLIA,
|
|
290
|
+
appCode: '<YOUR_APP_CODE>',
|
|
291
|
+
})
|
|
292
|
+
|
|
293
|
+
const limitOrderParameters: LimitTradeParameters = {
|
|
294
|
+
kind: OrderKind.BUY,
|
|
295
|
+
sellToken: '0xfff9976782d46cc05630d1f6ebab18b2324d6b14',
|
|
296
|
+
sellTokenDecimals: 18,
|
|
297
|
+
buyToken: '0x0625afb445c3b6b7b929342a04a22599fd5dbb59',
|
|
298
|
+
buyTokenDecimals: 18,
|
|
299
|
+
sellAmount: '120000000000000000',
|
|
300
|
+
buyAmount: '66600000000000000000',
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
const { orderId } = await sdk.postLimitOrder(limitOrderParameters)
|
|
304
|
+
|
|
305
|
+
console.log('Order created, id: ', orderId)
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
### postSellNativeCurrencyOrder
|
|
309
|
+
|
|
310
|
+
CoW Protocol supports on-chain trades for selling blockchain native tokens (ETH for Ethereum).
|
|
311
|
+
In this case, the order is created as an on-chain transaction.
|
|
312
|
+
You don't have to think about the case when you use `postSwapOrder` function, it will be handled automatically.
|
|
313
|
+
But if you need more flexible way to create an order to sell native token, you can use the `postSellNativeCurrencyOrder` function.
|
|
314
|
+
|
|
315
|
+
> We consider the order as native token selling order if the sell token has '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee' address.
|
|
316
|
+
|
|
317
|
+
```typescript
|
|
318
|
+
import { SupportedChainId, OrderKind, TradeParameters, TradingSdk } from '@cowprotocol/cow-sdk'
|
|
319
|
+
|
|
320
|
+
// Proper adapter initialization (see setup example above)
|
|
321
|
+
const sdk = new TradingSdk({
|
|
322
|
+
chainId: SupportedChainId.SEPOLIA,
|
|
323
|
+
appCode: '<YOUR_APP_CODE>',
|
|
324
|
+
})
|
|
325
|
+
|
|
326
|
+
const parameters: TradeParameters = {
|
|
327
|
+
kind: OrderKind.BUY,
|
|
328
|
+
sellToken: '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE',
|
|
329
|
+
sellTokenDecimals: 18,
|
|
330
|
+
buyToken: '0x0625afb445c3b6b7b929342a04a22599fd5dbb59',
|
|
331
|
+
buyTokenDecimals: 18,
|
|
332
|
+
amount: '120000000000000000',
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
const { orderId } = await sdk.postSellNativeCurrencyOrder(parameters)
|
|
336
|
+
|
|
337
|
+
console.log('Order created, id: ', orderId)
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
### Get quote for a smart-contract wallet
|
|
341
|
+
|
|
342
|
+
If you want to use a smart-contract wallet to sign the order, you should specify the `signingScheme` parameter in order to get more accurate quote in terms of gas efficiency.
|
|
343
|
+
Smart-contract wallets are supported by using a different signing scheme - `SigningScheme.PRESIGN`.
|
|
344
|
+
|
|
345
|
+
#### Example
|
|
346
|
+
|
|
347
|
+
```typescript
|
|
348
|
+
import {
|
|
349
|
+
SupportedChainId,
|
|
350
|
+
OrderKind,
|
|
351
|
+
TradeParameters,
|
|
352
|
+
SwapAdvancedSettings,
|
|
353
|
+
SigningScheme,
|
|
354
|
+
TradingSdk,
|
|
355
|
+
} from '@cowprotocol/cow-sdk'
|
|
356
|
+
|
|
357
|
+
// Proper adapter initialization (see setup example above)
|
|
358
|
+
const sdk = new TradingSdk({
|
|
359
|
+
chainId: SupportedChainId.SEPOLIA,
|
|
360
|
+
appCode: '<YOUR_APP_CODE>',
|
|
361
|
+
})
|
|
362
|
+
|
|
363
|
+
const parameters: TradeParameters = {
|
|
364
|
+
kind: OrderKind.BUY,
|
|
365
|
+
sellToken: '0xfff9976782d46cc05630d1f6ebab18b2324d6b14',
|
|
366
|
+
sellTokenDecimals: 18,
|
|
367
|
+
buyToken: '0x0625afb445c3b6b7b929342a04a22599fd5dbb59',
|
|
368
|
+
buyTokenDecimals: 18,
|
|
369
|
+
amount: '120000000000000000',
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
const advancedParameters: SwapAdvancedSettings = {
|
|
373
|
+
quoteRequest: {
|
|
374
|
+
// Specify the signing scheme
|
|
375
|
+
signingScheme: SigningScheme.PRESIGN,
|
|
376
|
+
},
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
const { quoteResults } = await sdk.getQuote(parameters)
|
|
380
|
+
|
|
381
|
+
console.log('Quote:', quoteResults)
|
|
382
|
+
```
|
|
383
|
+
|
|
384
|
+
### Create an order with smart-contract wallet
|
|
385
|
+
|
|
386
|
+
If you want to create an order with a smart-contract wallet, you should specify the `signingScheme` parameter in the `postSwapOrder` function.
|
|
387
|
+
And then you need to send a transaction from `getPreSignTransaction` result in order to sign the order.
|
|
388
|
+
|
|
389
|
+
#### Example of Swap order
|
|
390
|
+
|
|
391
|
+
```typescript
|
|
392
|
+
import { SupportedChainId, OrderKind, TradeParameters, TradingSdk } from '@cowprotocol/cow-sdk'
|
|
393
|
+
|
|
394
|
+
// Proper adapter initialization (see setup example above)
|
|
395
|
+
const sdk = new TradingSdk({
|
|
396
|
+
chainId: SupportedChainId.SEPOLIA,
|
|
397
|
+
appCode: '<YOUR_APP_CODE>',
|
|
398
|
+
})
|
|
399
|
+
|
|
400
|
+
const parameters: TradeParameters = {
|
|
401
|
+
kind: OrderKind.BUY,
|
|
402
|
+
sellToken: '0xfff9976782d46cc05630d1f6ebab18b2324d6b14',
|
|
403
|
+
sellTokenDecimals: 18,
|
|
404
|
+
buyToken: '0x0625afb445c3b6b7b929342a04a22599fd5dbb59',
|
|
405
|
+
buyTokenDecimals: 18,
|
|
406
|
+
amount: '120000000000000000',
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
const advancedParameters: SwapAdvancedSettings = {
|
|
410
|
+
quoteRequest: {
|
|
411
|
+
// Specify the signing scheme
|
|
412
|
+
signingScheme: SigningScheme.PRESIGN,
|
|
413
|
+
},
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
const smartContractWalletAddress = '0x<smartContractWalletAddress>'
|
|
417
|
+
const { orderId } = await sdk.postSwapOrder(parameters, advancedParameters)
|
|
418
|
+
const preSignTransaction = await sdk.getPreSignTransaction({ orderId, account: smartContractWalletAddress })
|
|
419
|
+
|
|
420
|
+
console.log('Order created with "pre-sign" state, id: ', orderId)
|
|
421
|
+
console.log('Execute the transaction to sign the order', preSignTransaction)
|
|
422
|
+
```
|
|
423
|
+
|
|
424
|
+
#### Example of Limit order
|
|
425
|
+
|
|
426
|
+
```typescript
|
|
427
|
+
import {
|
|
428
|
+
SupportedChainId,
|
|
429
|
+
OrderKind,
|
|
430
|
+
LimitTradeParameters,
|
|
431
|
+
LimitOrderAdvancedSettings,
|
|
432
|
+
SigningScheme,
|
|
433
|
+
TradingSdk,
|
|
434
|
+
} from '@cowprotocol/cow-sdk'
|
|
435
|
+
|
|
436
|
+
// Proper adapter initialization (see setup example above)
|
|
437
|
+
const sdk = new TradingSdk({
|
|
438
|
+
chainId: SupportedChainId.SEPOLIA,
|
|
439
|
+
appCode: '<YOUR_APP_CODE>',
|
|
440
|
+
})
|
|
441
|
+
|
|
442
|
+
const smartContractWalletAddress = '0x<smartContractWalletAddress>'
|
|
443
|
+
|
|
444
|
+
const limitOrderParameters: LimitTradeParameters = {
|
|
445
|
+
kind: OrderKind.BUY,
|
|
446
|
+
sellToken: '0xfff9976782d46cc05630d1f6ebab18b2324d6b14',
|
|
447
|
+
sellTokenDecimals: 18,
|
|
448
|
+
buyToken: '0x0625afb445c3b6b7b929342a04a22599fd5dbb59',
|
|
449
|
+
buyTokenDecimals: 18,
|
|
450
|
+
sellAmount: '120000000000000000',
|
|
451
|
+
buyAmount: '66600000000000000000',
|
|
452
|
+
owner: smartContractWalletAddress, // See note bellow why you need to specify the parameter
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
const advancedParameters: LimitOrderAdvancedSettings = {
|
|
456
|
+
additionalParams: {
|
|
457
|
+
signingScheme: SigningScheme.PRESIGN,
|
|
458
|
+
},
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
const { orderId } = await sdk.postLimitOrder(limitOrderParameters, advancedParameters)
|
|
462
|
+
const preSignTransaction = await sdk.getPreSignTransaction({ orderId, account: smartContractWalletAddress })
|
|
463
|
+
|
|
464
|
+
console.log('Order created with "pre-sign" state, id: ', orderId)
|
|
465
|
+
console.log('Execute the transaction to sign the order', preSignTransaction)
|
|
466
|
+
```
|
|
467
|
+
|
|
468
|
+
> **Note:** it's important to specify the `owner` parameter if you create an order with a smart-contract wallet, and it differs from the signer (for example Safe).
|
|
469
|
+
> CoW Protocol will use `owner` in order to check the order owner balance, allowance and other things.
|
|
470
|
+
|
|
471
|
+
### Optional parameters
|
|
472
|
+
|
|
473
|
+
Both `postSwapOrder` and `postLimitOrder` functions have optional parameters.
|
|
474
|
+
See `TradeOptionalParameters` type for more details.
|
|
475
|
+
|
|
476
|
+
| **Parameter** | **Type** | **Default Value** | **Description** |
|
|
477
|
+
| ------------------- | ------------ | ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
478
|
+
| `env` | `Env` | `prod` | The environment to use (`prod` or `staging`). |
|
|
479
|
+
| `partiallyFillable` | `boolean` | `false` | Indicates whether the order is fill-or-kill or partially fillable. |
|
|
480
|
+
| `slippageBps` | `number` | 50 | Slippage tolerance applied to the order to get the limit price. Expressed in Basis Points (BPS). One basis point is equivalent to 0.01% (1/100th of a percent). |
|
|
481
|
+
| `receiver` | `string` | order creator | The address that will receive the order's tokens. |
|
|
482
|
+
| `validFor` | `number` | 30 mins | The order expiration time in seconds. |
|
|
483
|
+
| `partnerFee` | `PartnerFee` | - | Partners of the protocol can specify their fee for the order, including the fee in basis points (BPS) and the fee recipient address. [Read more](https://docs.cow.fi/governance/fees/partner-fee) |
|
|
484
|
+
|
|
485
|
+
##### Example
|
|
486
|
+
|
|
487
|
+
```typescript
|
|
488
|
+
import { SupportedChainId, OrderKind, TradeParameters, TradingSdk } from '@cowprotocol/cow-sdk'
|
|
489
|
+
|
|
490
|
+
// Proper adapter initialization (see setup example above)
|
|
491
|
+
const sdk = new TradingSdk({
|
|
492
|
+
chainId: SupportedChainId.SEPOLIA,
|
|
493
|
+
appCode: '<YOUR_APP_CODE>',
|
|
494
|
+
})
|
|
495
|
+
|
|
496
|
+
const parameters: TradeParameters = {
|
|
497
|
+
kind: OrderKind.BUY,
|
|
498
|
+
sellToken: '0xfff9976782d46cc05630d1f6ebab18b2324d6b14',
|
|
499
|
+
sellTokenDecimals: 18,
|
|
500
|
+
buyToken: '0x0625afb445c3b6b7b929342a04a22599fd5dbb59',
|
|
501
|
+
buyTokenDecimals: 18,
|
|
502
|
+
amount: '120000000000000000',
|
|
503
|
+
// Optional parameters
|
|
504
|
+
slippageBps: 200, // 2%
|
|
505
|
+
validFor: 1200, // 20 mins
|
|
506
|
+
receiver: '0xdef1ca1fb7f1232777520aa7f396b4e015f497ab', // Just a random address, don't use it!
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
const { orderId } = await sdk.postSwapOrder(parameters)
|
|
510
|
+
|
|
511
|
+
console.log('Order created, id: ', orderId)
|
|
512
|
+
```
|
|
513
|
+
|
|
514
|
+
### Advanced swap order creation
|
|
515
|
+
|
|
516
|
+
By default, the SDK requires only the basic parameters to create an order.
|
|
517
|
+
However, you can provide additional parameters to customize the order creation.
|
|
518
|
+
|
|
519
|
+
#### Swap
|
|
520
|
+
|
|
521
|
+
1. `quoteRequest` - the quote request object. It is used to get a quote from the quote API ([read more](https://docs.cow.fi/cow-protocol/reference/sdks/cow-sdk/modules#orderquoterequest))
|
|
522
|
+
2. `appData` - the order's metadata ([read more](https://docs.cow.fi/cow-protocol/reference/sdks/app-data/modules#appdataparams))
|
|
523
|
+
|
|
524
|
+
##### Example
|
|
525
|
+
|
|
526
|
+
```typescript
|
|
527
|
+
import {
|
|
528
|
+
SupportedChainId,
|
|
529
|
+
OrderKind,
|
|
530
|
+
TradeParameters,
|
|
531
|
+
TradingSdk,
|
|
532
|
+
SwapAdvancedSettings,
|
|
533
|
+
PriceQuality,
|
|
534
|
+
} from '@cowprotocol/cow-sdk'
|
|
535
|
+
|
|
536
|
+
// Proper adapter initialization (see setup example above)
|
|
537
|
+
const sdk = new TradingSdk({
|
|
538
|
+
chainId: SupportedChainId.SEPOLIA,
|
|
539
|
+
appCode: '<YOUR_APP_CODE>',
|
|
540
|
+
})
|
|
541
|
+
|
|
542
|
+
const parameters: TradeParameters = {
|
|
543
|
+
kind: OrderKind.BUY,
|
|
544
|
+
sellToken: '0xfff9976782d46cc05630d1f6ebab18b2324d6b14',
|
|
545
|
+
sellTokenDecimals: 18,
|
|
546
|
+
buyToken: '0x0625afb445c3b6b7b929342a04a22599fd5dbb59',
|
|
547
|
+
buyTokenDecimals: 18,
|
|
548
|
+
amount: '120000000000000000',
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
const advancedSettings: SwapAdvancedSettings = {
|
|
552
|
+
quoteRequest: {
|
|
553
|
+
priceQuality: PriceQuality.FAST,
|
|
554
|
+
validFor: 120,
|
|
555
|
+
},
|
|
556
|
+
appData: {
|
|
557
|
+
hooks: {
|
|
558
|
+
version: 1,
|
|
559
|
+
pre: [
|
|
560
|
+
{
|
|
561
|
+
target: '0xdef1ca1fb7fbcdc777520aa7f396b4e015f497ab',
|
|
562
|
+
callData: '0x70a08231000000000000000000000000d8da6bf26964af9d7eed9e03e53415d37aa96045',
|
|
563
|
+
gasLimit: 21000,
|
|
564
|
+
},
|
|
565
|
+
],
|
|
566
|
+
},
|
|
567
|
+
},
|
|
568
|
+
}
|
|
569
|
+
const { orderId } = await sdk.postSwapOrder(parameters, advancedSettings)
|
|
570
|
+
|
|
571
|
+
console.log('Order created, id: ', orderId)
|
|
572
|
+
```
|
|
573
|
+
|
|
574
|
+
#### Limit order
|
|
575
|
+
|
|
576
|
+
Same as for the swap order but without the `quoteRequest` parameter.
|