CoW protocol SDK
⚠️⚠️ THE SDK IS IN Beta ⚠️⚠️
It is being currently develop and is a work in progress, also it's API is subjected to change.
If you experience any problems, please open an issue in Github trying to describe your problem.
Getting started
Install the SDK:
yarn add @cowprotocol/cow-sdk
Instantiate the SDK:
import { CowSdk } from 'cow-sdk'
const chainId = 4
const cowSdk = new CowSdk(chainId)
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.
const trades = await cowSdk.cowApi.getOrders({
owner: '0x00000000005ef87f8ca7014309ece7260bbcdaeb',
limit: 5,
offset: 0
})
console.log(trades)
Let's see a full example on how to submit an order to CowSwap.
⚠️ Before starting, the protocol requires you to approve the sell token before the order can be considered.
For more details see https://docs.cow.fi/tutorials/how-to-submit-orders-via-the-api/1.-set-allowance-for-the-sell-token
In this example, we will:
-
- Instantiate the SDK and a wallet: Used for signing orders
-
- Get a price/fee quote from the API: Get current market price and required protocol fee to settle your trade.
-
- Sign the order using your wallet: Only signed orders are considered by the protocol.
-
- Post the signed order to the API: Post the order so it can be executed.
import { Wallet } from 'ethers'
import { CowSdk, OrderKind } from 'cow-sdk'
const mnemonic = 'fall dirt bread cactus...'
const wallet = Wallet.fromMnemonic(mnemonic)
const cowSdk = new CowSdk(4, { signer: wallet })
const quoteResponse = await cowSdk.cowApi.getQuote({
kind: OrderKind.SELL,
sellToken: '0xc778417e063141139fce010982780140aa0cd5ab',
buyToken: '0x4dbcdf9b62e891a7cec5a2568c3f4faf9e8abe2b',
amount: '1000000000000000000',
userAddress: '0x1811be0994930fe9480eaede25165608b093ad7a',
validTo: 2524608000,
})
const { sellToken, buyToken, validTo, buyAmount, sellAmount, receiver, feeAmount } = quoteResponse.quote
const order = {
kind: OrderKind.SELL,
partiallyFillable: false,
sellToken,
buyToken,
validTo,
buyAmount,
sellAmount,
receiver,
feeAmount,
}
const signedOrder = await cowSdk.signOrder(order)
const orderId = await cowSdk.cowApi.sendOrder({
order: { ...order, ...signedOrder },
owner: '0x1811be0994930fe9480eaede25165608b093ad7a',
})
console.log(`https://explorer.cow.fi/rinkeby/orders/${orderId}`)
Install Dependencies
yarn
Build
yarn build
yarn start
Unit testing
yarn test