@cowprotocol/cow-sdk 7.0.5-beta.0 → 7.0.5
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 +228 -244
- package/dist/CHANGELOG.md +162 -0
- package/dist/README.md +228 -244
- package/dist/index.d.mts +3 -68
- package/dist/index.d.ts +3 -68
- package/dist/index.js +36 -70
- package/dist/index.mjs +3 -45
- package/dist/package.json +3 -8
- package/package.json +9 -14
package/README.md
CHANGED
|
@@ -2,269 +2,265 @@
|
|
|
2
2
|
<img width="400" src="https://github.com/cowprotocol/cow-sdk/raw/main/docs/images/CoW.png" />
|
|
3
3
|
</p>
|
|
4
4
|
|
|
5
|
-
# CoW SDK
|
|
5
|
+
# CoW SDK
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
## 📚 [Docs website](https://docs.cow.fi/)
|
|
8
8
|
|
|
9
|
-
##
|
|
9
|
+
## Test coverage
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
-
|
|
14
|
-
- **Future-Proof** - Automatically includes new features and modules as they're added
|
|
11
|
+
| Statements | Branches | Functions | Lines |
|
|
12
|
+
| ------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- |
|
|
13
|
+
|  |  |  |  |
|
|
15
14
|
|
|
16
|
-
## 📚 **Documentation Hub**
|
|
17
15
|
|
|
18
|
-
|
|
16
|
+
## 🌐 **Supported Networks**
|
|
19
17
|
|
|
20
|
-
|
|
18
|
+
The SDK supports all CoW Protocol enabled networks:
|
|
21
19
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
20
|
+
- **Ethereum** (1) - `SupportedChainId.MAINNET`
|
|
21
|
+
- **Gnosis Chain** (100) - `SupportedChainId.GNOSIS_CHAIN`
|
|
22
|
+
- **Arbitrum One** (42161) - `SupportedChainId.ARBITRUM_ONE`
|
|
23
|
+
- **Base** (8453) - `SupportedChainId.BASE`
|
|
24
|
+
- **Polygon** (137) - `SupportedChainId.POLYGON`
|
|
25
|
+
- **Avalanche** (43114) - `SupportedChainId.AVALANCHE`
|
|
26
|
+
- **Lens** (232) - `SupportedChainId.LENS`
|
|
27
|
+
- **BNB** (56) - `SupportedChainId.BNB`
|
|
28
|
+
- **Sepolia** (11155111) - `SupportedChainId.SEPOLIA` (Testnet)
|
|
29
|
+
|
|
30
|
+
## 🔗 **Related Resources**
|
|
31
|
+
|
|
32
|
+
- **[CoW Protocol Documentation](https://docs.cow.fi/)**
|
|
33
|
+
- **[API Reference](https://api.cow.fi/docs/)**
|
|
34
|
+
- **[CoW Protocol Website](https://cow.fi/)**
|
|
35
|
+
- **[Examples Repository](https://github.com/cowprotocol/cow-sdk/tree/main/examples)**
|
|
36
|
+
- **Issues**: [GitHub Issues](https://github.com/cowprotocol/cow-sdk/issues)
|
|
37
|
+
- **Discord**: [CoW Protocol Discord](https://discord.com/invite/cowprotocol)
|
|
38
|
+
- **Documentation**: [docs.cow.fi](https://docs.cow.fi/)
|
|
39
|
+
|
|
40
|
+
## Getting Started
|
|
41
|
+
|
|
42
|
+
### Usage Examples
|
|
43
|
+
|
|
44
|
+
**React Examples:**
|
|
45
|
+
- [React (viem)](https://github.com/cowprotocol/cow-sdk/tree/main/examples/react/viem)
|
|
46
|
+
- [React (wagmi)](https://github.com/cowprotocol/cow-sdk/tree/main/examples/react/wagmi)
|
|
47
|
+
- [React (ethers v6)](https://github.com/cowprotocol/cow-sdk/tree/main/examples/react/ethers6)
|
|
48
|
+
- [React (ethers v5)](https://github.com/cowprotocol/cow-sdk/tree/main/examples/react/ethers5)
|
|
48
49
|
|
|
49
|
-
|
|
50
|
+
**Node.js Examples:**
|
|
51
|
+
- [Node.js (viem)](https://github.com/cowprotocol/cow-sdk/blob/main/examples/nodejs/viem/src/index.ts)
|
|
52
|
+
- [Node.js (ethers v6)](https://github.com/cowprotocol/cow-sdk/blob/main/examples/nodejs/ethers6/src/index.ts)
|
|
53
|
+
- [Node.js (ethers v5)](https://github.com/cowprotocol/cow-sdk/blob/main/examples/nodejs/ethers5/src/index.ts)
|
|
50
54
|
|
|
51
55
|
### Installation
|
|
52
56
|
|
|
53
57
|
```bash
|
|
54
|
-
npm install @cowprotocol/cow-sdk
|
|
55
|
-
# or
|
|
56
58
|
pnpm add @cowprotocol/cow-sdk
|
|
57
|
-
# or
|
|
58
|
-
yarn add @cowprotocol/cow-sdk
|
|
59
59
|
```
|
|
60
60
|
|
|
61
|
-
You'll also need one of the adapter packages:
|
|
62
|
-
|
|
63
61
|
```bash
|
|
64
|
-
|
|
65
|
-
npm install @cowprotocol/sdk-ethers-v6-adapter
|
|
66
|
-
npm install @cowprotocol/sdk-ethers-v5-adapter
|
|
67
|
-
npm install @cowprotocol/sdk-viem-adapter
|
|
62
|
+
yarn add @cowprotocol/cow-sdk
|
|
68
63
|
```
|
|
69
64
|
|
|
70
|
-
##
|
|
65
|
+
## CoW SDK
|
|
71
66
|
|
|
72
|
-
|
|
67
|
+
Using CoW Protocol, you can perform swaps, limit orders, TWAP orders, and many other operations.
|
|
68
|
+
The `@cowprotocol/cow-sdk` provides tools at different abstraction levels, allowing you to conveniently implement basic scenarios while maintaining the flexibility to control all possible cases.
|
|
73
69
|
|
|
74
|
-
|
|
70
|
+
> In most cases, you will only need to use the **[Trading SDK](https://github.com/cowprotocol/cow-sdk/tree/main/packages/trading/README.md)**
|
|
75
71
|
|
|
76
|
-
|
|
72
|
+
## SDK Components
|
|
77
73
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
OrderSigningUtils,
|
|
84
|
-
setGlobalAdapter,
|
|
85
|
-
SupportedChainId
|
|
86
|
-
} from '@cowprotocol/cow-sdk'
|
|
87
|
-
import { ViemAdapter } from '@cowprotocol/sdk-viem-adapter'
|
|
88
|
-
import { createPublicClient, http, privateKeyToAccount } from 'viem'
|
|
89
|
-
import { sepolia } from 'viem/chains'
|
|
74
|
+
### Core
|
|
75
|
+
- **[`TradingSdk`](https://github.com/cowprotocol/cow-sdk/tree/main/packages/trading/README.md)** - Main tool for swap and limit orders with built-in quote fetching, order signing, and posting
|
|
76
|
+
- **[`OrderSigningUtils`](https://github.com/cowprotocol/cow-sdk/tree/main/packages/order-signing/README.md)** - Utilities for signing orders and cancellations using cryptographic algorithms
|
|
77
|
+
- **[`OrderBookApi`](https://github.com/cowprotocol/cow-sdk/tree/main/packages/order-book/README.md)** - Provides the ability to retrieve orders and trades from the CoW Protocol order book, as well as add and cancel them
|
|
78
|
+
- **[`MetadataApi`](https://github.com/cowprotocol/cow-sdk/tree/main/packages/app-data/README.md)** - API for accessing order metadata and additional information
|
|
90
79
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
provider: createPublicClient({
|
|
96
|
-
chain: sepolia,
|
|
97
|
-
transport: http('YOUR_RPC_URL')
|
|
98
|
-
}),
|
|
99
|
-
signer: privateKeyToAccount('YOUR_PRIVATE_KEY' as `0x${string}`)
|
|
100
|
-
})
|
|
80
|
+
### Advanced
|
|
81
|
+
- **[`BridgingSdk`](https://github.com/cowprotocol/cow-sdk/tree/main/packages/bridging/README.md)** - Cross-chain token transfers and bridging functionality
|
|
82
|
+
- **[`ConditionalOrder`](https://github.com/cowprotocol/cow-sdk/tree/main/packages/composable/README.md)** - SDK for Programmatic Orders such as TWAP ([Read more](https://docs.cow.fi/cow-protocol/concepts/order-types/programmatic-orders))
|
|
83
|
+
- **[`CowShedSdk`](https://github.com/cowprotocol/cow-sdk/tree/main/packages/cow-shed/README.md)** - Account abstraction that leverages EOA with smart contract capabilities
|
|
101
84
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
chainId: SupportedChainId.SEPOLIA,
|
|
105
|
-
adapter,
|
|
106
|
-
tradingOptions: {
|
|
107
|
-
traderParams: {
|
|
108
|
-
appCode: 'YOUR_APP_CODE',
|
|
109
|
-
...
|
|
110
|
-
},
|
|
111
|
-
...
|
|
112
|
-
},
|
|
113
|
-
})
|
|
85
|
+
## v6 → v7 Migration Guide
|
|
86
|
+
The versions are 99% backward compatible. The only difference is that in v7 you need to set an adapter corresponding to the library you use: `Viem`, `Ethers6`, or `Ethers5`.
|
|
114
87
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
const totals = await sdk.subgraph?.getTotals()
|
|
119
|
-
const signature = await sdk.orderSigning.signOrder(order, chainId, signer)
|
|
120
|
-
```
|
|
88
|
+
**Before (v6):**
|
|
89
|
+
```typescript
|
|
90
|
+
import { SupportedChainId, TradingSdk, MetadataApi, OrderBookApi } from '@cowprotocol/cow-sdk'
|
|
121
91
|
|
|
122
|
-
|
|
92
|
+
const options = {}
|
|
123
93
|
|
|
124
|
-
|
|
94
|
+
const sdk = new TradingSdk({
|
|
95
|
+
chainId: SupportedChainId.SEPOLIA,
|
|
96
|
+
appCode: 'YOUR_APP_CODE',
|
|
97
|
+
}, options)
|
|
98
|
+
```
|
|
125
99
|
|
|
100
|
+
**After (v7):**
|
|
126
101
|
```typescript
|
|
127
|
-
import {
|
|
102
|
+
import { SupportedChainId, OrderKind, TradeParameters, TradingSdk } from '@cowprotocol/cow-sdk'
|
|
128
103
|
import { ViemAdapter } from '@cowprotocol/sdk-viem-adapter'
|
|
129
104
|
import { createPublicClient, http, privateKeyToAccount } from 'viem'
|
|
130
105
|
import { sepolia } from 'viem/chains'
|
|
131
106
|
|
|
132
|
-
//
|
|
133
|
-
// There are EthersV5Adapter and EthersV6Adapter as well
|
|
134
|
-
// @cowprotocol/sdk-ethers-v5-adapter, @cowprotocol/sdk-ethers-v6-adapter
|
|
107
|
+
// NEW: Instantiate and set adapter
|
|
135
108
|
const adapter = new ViemAdapter({
|
|
136
109
|
provider: createPublicClient({
|
|
137
110
|
chain: sepolia,
|
|
138
111
|
transport: http('YOUR_RPC_URL')
|
|
139
112
|
}),
|
|
113
|
+
// You can also set `walletClient` instead of `signer` using `useWalletClient` from wagmi
|
|
140
114
|
signer: privateKeyToAccount('YOUR_PRIVATE_KEY' as `0x${string}`)
|
|
141
115
|
})
|
|
142
116
|
|
|
143
|
-
|
|
144
|
-
|
|
117
|
+
const options = {}
|
|
118
|
+
|
|
119
|
+
const sdk = new TradingSdk({
|
|
120
|
+
chainId: SupportedChainId.SEPOLIA,
|
|
121
|
+
appCode: 'YOUR_APP_CODE',
|
|
122
|
+
}, options, adapter)
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
Most other packages (e.g., `MetadataApi` or `BridgingSdk`) have a parameter to set an adapter.
|
|
126
|
+
You can also set an adapter using `setGlobalAdapter`:
|
|
127
|
+
|
|
128
|
+
```typescript
|
|
129
|
+
import { setGlobalAdapter, CowShedSdk } from '@cowprotocol/cow-sdk'
|
|
130
|
+
|
|
131
|
+
const adapter = {...}
|
|
145
132
|
|
|
146
|
-
|
|
133
|
+
// Set global adapter
|
|
134
|
+
setGlobalAdapter(adapter)
|
|
147
135
|
|
|
148
|
-
|
|
149
|
-
const orderId = await trading.postSwapOrder(parameters)
|
|
150
|
-
const orders = await orderBook.getOrders({ owner: address })
|
|
136
|
+
const cowShedSdk = new CowShedSdk()
|
|
151
137
|
```
|
|
152
138
|
|
|
153
|
-
|
|
139
|
+
You will likely also need to bind the SDK to your app's account state.
|
|
140
|
+
When the account or network changes in the app/wallet, it should be updated in the SDK as well.
|
|
141
|
+
Here's an example for `WAGMI` (see `examples/react/wagmi`):
|
|
142
|
+
|
|
143
|
+
```typescript
|
|
144
|
+
import { useAccount, usePublicClient, useWalletClient } from 'wagmi'
|
|
145
|
+
import { useEffect, useState } from 'react'
|
|
146
|
+
import { ViemAdapter, ViemAdapterOptions } from '@cowprotocol/sdk-viem-adapter'
|
|
147
|
+
import { tradingSdk } from '../cowSdk.ts'
|
|
148
|
+
import { setGlobalAdapter } from '@cowprotocol/cow-sdk'
|
|
149
|
+
|
|
150
|
+
export function useBindCoWSdkToWagmi(): boolean {
|
|
151
|
+
const { chainId } = useAccount()
|
|
152
|
+
const { data: walletClient } = useWalletClient()
|
|
153
|
+
const publicClient = usePublicClient()
|
|
154
|
+
|
|
155
|
+
const [isSdkReady, setIsSdkReady] = useState(false)
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Sync Trading SDK with wagmi account state (chainId and signer)
|
|
159
|
+
*/
|
|
160
|
+
useEffect(() => {
|
|
161
|
+
if (!walletClient || !chainId) return
|
|
162
|
+
|
|
163
|
+
setGlobalAdapter(
|
|
164
|
+
new ViemAdapter({
|
|
165
|
+
provider: publicClient,
|
|
166
|
+
walletClient,
|
|
167
|
+
} as unknown as ViemAdapterOptions),
|
|
168
|
+
)
|
|
169
|
+
|
|
170
|
+
tradingSdk.setTraderParams({ chainId })
|
|
171
|
+
|
|
172
|
+
setIsSdkReady(true)
|
|
173
|
+
}, [publicClient, walletClient, chainId])
|
|
174
|
+
|
|
175
|
+
return isSdkReady
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
```
|
|
154
179
|
|
|
155
|
-
|
|
180
|
+
## Basic Use Case
|
|
181
|
+
This example demonstrates the main use case of creating a swap and shows how to:
|
|
182
|
+
- Get a quote
|
|
183
|
+
- Verify amounts
|
|
184
|
+
- Adjust swap parameters
|
|
185
|
+
- Sign and post an order
|
|
156
186
|
|
|
157
187
|
```typescript
|
|
158
|
-
import {
|
|
188
|
+
import { SupportedChainId, OrderKind, TradeParameters, TradingSdk } from '@cowprotocol/cow-sdk'
|
|
159
189
|
import { ViemAdapter } from '@cowprotocol/sdk-viem-adapter'
|
|
160
190
|
import { createPublicClient, http, privateKeyToAccount } from 'viem'
|
|
161
191
|
import { sepolia } from 'viem/chains'
|
|
162
192
|
|
|
163
|
-
//
|
|
164
|
-
// There are EthersV5Adapter and EthersV6Adapter as well
|
|
193
|
+
// EthersV5Adapter and EthersV6Adapter are also available
|
|
165
194
|
// @cowprotocol/sdk-ethers-v5-adapter, @cowprotocol/sdk-ethers-v6-adapter
|
|
166
195
|
const adapter = new ViemAdapter({
|
|
167
196
|
provider: createPublicClient({
|
|
168
197
|
chain: sepolia,
|
|
169
198
|
transport: http('YOUR_RPC_URL')
|
|
170
199
|
}),
|
|
200
|
+
// You can also set `walletClient` instead of `signer` using `useWalletClient` from wagmi
|
|
171
201
|
signer: privateKeyToAccount('YOUR_PRIVATE_KEY' as `0x${string}`)
|
|
172
202
|
})
|
|
173
203
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
const
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
All modules are accessible through the umbrella package:
|
|
188
|
-
|
|
189
|
-
> **📖 Documentation**: All links below point to the official documentation at [docs.cow.fi](https://docs.cow.fi). For source code, visit the [GitHub repository](https://github.com/cowprotocol/cow-sdk).
|
|
190
|
-
|
|
191
|
-
### **Trading**
|
|
192
|
-
|
|
193
|
-
```typescript
|
|
194
|
-
import { TradingSdk } from '@cowprotocol/cow-sdk'
|
|
195
|
-
```
|
|
196
|
-
|
|
197
|
-
Create and manage orders, get quotes, handle swaps and limit orders.
|
|
198
|
-
[📖 Trading Documentation](https://docs.cow.fi/cow-protocol/reference/sdks/protocol-components/sdk-trading)
|
|
199
|
-
|
|
200
|
-
### **Order Book**
|
|
201
|
-
|
|
202
|
-
```typescript
|
|
203
|
-
import { OrderBookApi } from '@cowprotocol/cow-sdk'
|
|
204
|
-
```
|
|
205
|
-
|
|
206
|
-
Interact with CoW Protocol's order book API for order management.
|
|
207
|
-
[📖 Order Book Documentation](https://docs.cow.fi/cow-protocol/reference/sdks/protocol-components/sdk-order-book)
|
|
208
|
-
|
|
209
|
-
### **Order Signing**
|
|
204
|
+
const sdk = new TradingSdk({
|
|
205
|
+
chainId: SupportedChainId.SEPOLIA,
|
|
206
|
+
appCode: 'YOUR_APP_CODE',
|
|
207
|
+
}, {}, adapter)
|
|
208
|
+
|
|
209
|
+
const parameters: TradeParameters = {
|
|
210
|
+
kind: OrderKind.SELL,
|
|
211
|
+
sellToken: '0xfff9976782d46cc05630d1f6ebab18b2324d6b14',
|
|
212
|
+
sellTokenDecimals: 18,
|
|
213
|
+
buyToken: '0x0625afb445c3b6b7b929342a04a22599fd5dbb59',
|
|
214
|
+
buyTokenDecimals: 18,
|
|
215
|
+
amount: '120000000000000',
|
|
216
|
+
}
|
|
210
217
|
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
```
|
|
218
|
+
// Get quote
|
|
219
|
+
const { quoteResults, postSwapOrderFromQuote } = await sdk.getQuote(parameters)
|
|
214
220
|
|
|
215
|
-
|
|
216
|
-
[📖 Order Signing Documentation](https://docs.cow.fi/cow-protocol/reference/sdks/protocol-components/sdk-order-signing)
|
|
221
|
+
const buyAmount = quoteResults.amountsAndCosts.afterSlippage.buyAmount
|
|
217
222
|
|
|
218
|
-
|
|
223
|
+
// Verify amount
|
|
224
|
+
if (confirm(`You will receive at least: ${buyAmount}. Proceed?`)) {
|
|
225
|
+
// Sign and post order
|
|
226
|
+
const orderId = await postSwapOrderFromQuote()
|
|
219
227
|
|
|
220
|
-
|
|
221
|
-
|
|
228
|
+
console.log('Order created, ID:', orderId)
|
|
229
|
+
}
|
|
222
230
|
```
|
|
223
231
|
|
|
224
|
-
|
|
225
|
-
[📖 App Data Documentation](https://docs.cow.fi/cow-protocol/reference/sdks/app-data)
|
|
232
|
+
This example demonstrates the simplest way to trade on CoW Protocol.
|
|
226
233
|
|
|
227
|
-
|
|
234
|
+
For more advanced use cases, you can use additional parameters such as `receiver`, `partiallyFillable`, `validTo`, and others.
|
|
235
|
+
Refer to the [Trading SDK documentation](https://github.com/cowprotocol/cow-sdk/tree/main/packages/trading/README.md) for comprehensive details.
|
|
228
236
|
|
|
229
|
-
```typescript
|
|
230
|
-
import { SubgraphApi } from '@cowprotocol/cow-sdk'
|
|
231
|
-
```
|
|
232
237
|
|
|
233
|
-
|
|
234
|
-
[📖 Subgraph Documentation](https://docs.cow.fi/cow-protocol/reference/sdks/protocol-components/sdk-subgraph)
|
|
238
|
+
## Adapters
|
|
235
239
|
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
```typescript
|
|
239
|
-
import { ContractsTs } from '@cowprotocol/cow-sdk'
|
|
240
|
-
```
|
|
240
|
+
The CoW SDK supports multiple blockchain adapters to work with different Web3 libraries. You need to install and configure one of the following adapters:
|
|
241
241
|
|
|
242
|
-
|
|
243
|
-
[📖 Contracts Documentation](https://docs.cow.fi/cow-protocol/reference/sdks/core-utilities/sdk-contracts-ts)
|
|
242
|
+
### Available Adapters
|
|
244
243
|
|
|
245
|
-
|
|
244
|
+
- **EthersV6Adapter** - For ethers.js v6
|
|
245
|
+
- **EthersV5Adapter** - For ethers.js v5
|
|
246
|
+
- **ViemAdapter** - For viem
|
|
246
247
|
|
|
247
|
-
|
|
248
|
-
import { ConditionalOrderFactory, Multiplexer } from '@cowprotocol/cow-sdk'
|
|
249
|
-
```
|
|
248
|
+
### Installation
|
|
250
249
|
|
|
251
|
-
|
|
252
|
-
|
|
250
|
+
```bash
|
|
251
|
+
# For ethers v6
|
|
252
|
+
pnpm add @cowprotocol/sdk-ethers-v6-adapter ethers
|
|
253
253
|
|
|
254
|
-
|
|
254
|
+
# For ethers v5
|
|
255
|
+
pnpm add @cowprotocol/sdk-ethers-v5-adapter ethers@^5.7.0
|
|
255
256
|
|
|
256
|
-
|
|
257
|
-
|
|
257
|
+
# For viem
|
|
258
|
+
pnpm add @cowprotocol/sdk-viem-adapter viem
|
|
258
259
|
```
|
|
259
260
|
|
|
260
|
-
|
|
261
|
-
[📖 CoW Shed Documentation](https://docs.cow.fi/cow-protocol/reference/sdks/protocol-components/sdk-cow-shed)
|
|
262
|
-
|
|
263
|
-
## ⚙️ **Adapter Support**
|
|
264
|
-
|
|
265
|
-
The umbrella package works with all supported blockchain adapters:
|
|
261
|
+
### Adapter Setup Examples
|
|
266
262
|
|
|
267
|
-
|
|
263
|
+
#### EthersV6Adapter
|
|
268
264
|
|
|
269
265
|
```typescript
|
|
270
266
|
import { EthersV6Adapter } from '@cowprotocol/sdk-ethers-v6-adapter'
|
|
@@ -275,7 +271,7 @@ const wallet = new Wallet('YOUR_PRIVATE_KEY', provider)
|
|
|
275
271
|
const adapter = new EthersV6Adapter({ provider, signer: wallet })
|
|
276
272
|
```
|
|
277
273
|
|
|
278
|
-
|
|
274
|
+
#### EthersV5Adapter
|
|
279
275
|
|
|
280
276
|
```typescript
|
|
281
277
|
import { EthersV5Adapter } from '@cowprotocol/sdk-ethers-v5-adapter'
|
|
@@ -286,87 +282,75 @@ const wallet = new ethers.Wallet('YOUR_PRIVATE_KEY', provider)
|
|
|
286
282
|
const adapter = new EthersV5Adapter({ provider, signer: wallet })
|
|
287
283
|
```
|
|
288
284
|
|
|
289
|
-
|
|
285
|
+
#### ViemAdapter
|
|
290
286
|
|
|
291
287
|
```typescript
|
|
292
288
|
import { ViemAdapter } from '@cowprotocol/sdk-viem-adapter'
|
|
293
|
-
import { http,
|
|
289
|
+
import { http, createPublicClient, privateKeyToAccount } from 'viem'
|
|
294
290
|
import { sepolia } from 'viem/chains'
|
|
295
291
|
|
|
296
292
|
const account = privateKeyToAccount('YOUR_PRIVATE_KEY' as `0x${string}`)
|
|
297
293
|
const transport = http('YOUR_RPC_URL')
|
|
298
|
-
const
|
|
294
|
+
const provider = createPublicClient({ chain: sepolia, transport })
|
|
295
|
+
|
|
296
|
+
// You can also set `walletClient` instead of `signer` using `useWalletClient` from wagmi
|
|
297
|
+
const adapter = new ViemAdapter({ provider, signer: account })
|
|
299
298
|
```
|
|
300
299
|
|
|
301
|
-
##
|
|
300
|
+
## Low-Level SDK Usage Example
|
|
302
301
|
|
|
303
|
-
|
|
302
|
+
This example demonstrates low-level API usage with a practical scenario:
|
|
303
|
+
exchanging `0.4 GNO` for `WETH` on the Gnosis Chain network.
|
|
304
304
|
|
|
305
|
-
|
|
306
|
-
- **Gnosis Chain** (100) - `SupportedChainId.GNOSIS_CHAIN`
|
|
307
|
-
- **Arbitrum One** (42161) - `SupportedChainId.ARBITRUM_ONE`
|
|
308
|
-
- **Base** (8453) - `SupportedChainId.BASE`
|
|
309
|
-
- **Polygon** (137) - `SupportedChainId.POLYGON`
|
|
310
|
-
- **Avalanche** (43114) - `SupportedChainId.AVALANCHE`
|
|
311
|
-
- **Sepolia** (11155111) - `SupportedChainId.SEPOLIA` (Testnet)
|
|
305
|
+
We will perform the following operations:
|
|
312
306
|
|
|
313
|
-
|
|
307
|
+
1. Get a quote
|
|
308
|
+
2. Sign the order
|
|
309
|
+
3. Send the order to the order book
|
|
310
|
+
4. Get the data of the created order
|
|
311
|
+
5. Get trades for the order
|
|
312
|
+
6. Cancel the order (signing + sending)
|
|
314
313
|
|
|
315
314
|
```typescript
|
|
316
|
-
import {
|
|
315
|
+
import { OrderBookApi, OrderSigningUtils, SupportedChainId, OrderKind } from '@cowprotocol/cow-sdk'
|
|
317
316
|
import { EthersV6Adapter } from '@cowprotocol/sdk-ethers-v6-adapter'
|
|
318
317
|
import { JsonRpcProvider, Wallet } from 'ethers'
|
|
319
318
|
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
const sdk = new CowSdk({
|
|
327
|
-
chainId: SupportedChainId.SEPOLIA,
|
|
328
|
-
adapter,
|
|
329
|
-
tradingOptions: {
|
|
330
|
-
traderParams: { appCode: 'MY_APP' },
|
|
331
|
-
},
|
|
332
|
-
})
|
|
319
|
+
const account = 'YOUR_WALLET_ADDRESS'
|
|
320
|
+
const chainId = 100 // Gnosis Chain
|
|
321
|
+
const provider = new JsonRpcProvider('YOUR_RPC_URL')
|
|
322
|
+
const wallet = new Wallet('YOUR_PRIVATE_KEY', provider)
|
|
323
|
+
const adapter = new EthersV6Adapter({ provider, signer: wallet })
|
|
333
324
|
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
amount: '100000000000000000', // 0.1 WETH
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
// Execute trade
|
|
345
|
-
const orderId = await sdk.trading.postSwapOrder(tradeParams)
|
|
346
|
-
console.log('Order created:', orderId)
|
|
347
|
-
|
|
348
|
-
// Monitor order
|
|
349
|
-
const order = await sdk.orderBook.getOrder(orderId)
|
|
350
|
-
console.log('Order status:', order.status)
|
|
351
|
-
|
|
352
|
-
// Get historical data
|
|
353
|
-
const stats = await sdk.subgraph?.getTotals()
|
|
354
|
-
console.log('Protocol stats:', stats)
|
|
325
|
+
const quoteRequest = {
|
|
326
|
+
sellToken: '0x6a023ccd1ff6f2045c3309768ead9e68f978f6e1', // WETH on Gnosis Chain
|
|
327
|
+
buyToken: '0x9c58bacc331c9aa871afd802db6379a98e80cedb', // GNO on Gnosis Chain
|
|
328
|
+
from: account,
|
|
329
|
+
receiver: account,
|
|
330
|
+
sellAmountBeforeFee: (0.4 * 10 ** 18).toString(), // 0.4 WETH
|
|
331
|
+
kind: OrderKind.SELL,
|
|
355
332
|
}
|
|
356
333
|
|
|
357
|
-
|
|
358
|
-
```
|
|
334
|
+
const orderBookApi = new OrderBookApi({ chainId: SupportedChainId.GNOSIS_CHAIN })
|
|
359
335
|
|
|
360
|
-
|
|
336
|
+
async function main() {
|
|
337
|
+
const { quote } = await orderBookApi.getQuote(quoteRequest)
|
|
361
338
|
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
339
|
+
const orderSigningResult = await OrderSigningUtils.signOrder(quote, chainId, adapter)
|
|
340
|
+
|
|
341
|
+
const orderId = await orderBookApi.sendOrder({ ...quote, ...orderSigningResult })
|
|
342
|
+
|
|
343
|
+
const order = await orderBookApi.getOrder(orderId)
|
|
344
|
+
|
|
345
|
+
const trades = await orderBookApi.getTrades({ orderId })
|
|
369
346
|
|
|
370
|
-
|
|
347
|
+
const orderCancellationSigningResult = await OrderSigningUtils.signOrderCancellations([orderId], chainId, adapter)
|
|
371
348
|
|
|
372
|
-
|
|
349
|
+
const cancellationResult = await orderBookApi.sendSignedOrderCancellations({
|
|
350
|
+
...orderCancellationSigningResult,
|
|
351
|
+
orderUids: [orderId],
|
|
352
|
+
})
|
|
353
|
+
|
|
354
|
+
console.log('Results:', { orderId, order, trades, orderCancellationSigningResult, cancellationResult })
|
|
355
|
+
}
|
|
356
|
+
```
|