Skip to content
This repository has been archived by the owner on Jan 9, 2024. It is now read-only.

Commit

Permalink
SDK docs update corresponding to the new version (v2) (#6)
Browse files Browse the repository at this point in the history
* SDK docs update corresponding to the new version (v2)

* Fix links
  • Loading branch information
shoom3301 authored Mar 31, 2023
1 parent 7d8e8ae commit acb52ac
Show file tree
Hide file tree
Showing 16 changed files with 117 additions and 88 deletions.
1 change: 0 additions & 1 deletion SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
* [CoW API](cow-sdk/cow-api.md)
* [Sign and Post orders](cow-sdk/sign-and-post-orders/README.md)
* [Enable Tokens](cow-sdk/sign-and-post-orders/enable-tokens.md)
* [Instantiate SDK with a signer](cow-sdk/sign-and-post-orders/instantiate-sdk-with-a-signer.md)
* [STEP 1: Get Market Price](cow-sdk/sign-and-post-orders/step-1-get-market-price.md)
* [STEP 2: Sign the order](cow-sdk/sign-and-post-orders/step-2-sign-the-order.md)
* [STEP 3: Post the signed order to the API](cow-sdk/sign-and-post-orders/step-3-post-the-signed-order-to-the-api.md)
Expand Down
4 changes: 1 addition & 3 deletions cow-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ CoW Swap uses CoW protocol. One easy way to get familiar with the protocol is by

* [https://swap.cow.fi](https://swap.cow.fi/)

You can use it in **Rinkeby** test net if you want. Otherwise, it is avaiable in **Mainnet** and **Gnosis Chain**.
You can use it in **Goerli** test net if you want. Otherwise, it is avaiable in **Mainnet** and **Gnosis Chain**.

> 💡 If you need Rinkeby Ether, you can use this [Faucet](https://www.rinkeby.io/#faucet)

\
Now you are more familiar with the protocol and CoW Swap, let's introduce the SDK in the next section.
6 changes: 5 additions & 1 deletion cow-sdk/cow-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ The SDK provides access to the CoW API. The CoW API allows you:
For example, you can easily get the last 5 order of a trader:

```typescript
import { OrderBookApi, SupportedChainId } from '@cowprotocol/cow-sdk'

const orderBookApi = new OrderBookApi({ chainId: SupportedChainId.MAINNET })

// i.e. Get last 5 orders for a given trader
const trades = await cowSdk.cowApi.getOrders({
const trades = orderBookApi.getOrders({
owner: '0x00000000005ef87f8ca7014309ece7260bbcdaeb', // Trader
limit: 5,
offset: 0,
Expand Down
17 changes: 10 additions & 7 deletions cow-sdk/getting-started-with-the-sdk.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@ yarn add @cowprotocol/cow-sdk
Instantiate the SDK:

```javascript
import { CowSdk } from '@cowprotocol/cow-sdk'
import { OrderBookApi, OrderSigningUtils, SubgraphApi } from '@cowprotocol/cow-sdk'

const chainId = 4 // Rinkeby
const cowSdk = new CowSdk(chainId)
const chainId = 100 // Gnosis chain

const orderBookApi = new OrderBookApi({ chainId })
const subgraphApi = new SubgraphApi({ chainId })
const orderSigningUtils = new OrderSigningUtils()
```

The SDK will expose:

* The CoW API (`cowSdk.cowApi`)
* The CoW Subgraph (`cowSdk.cowSubgraphApi`)
* Convenient method to facilitate signing orders (i.e `cowSdk.signOrder`)
- `OrderBookApi` - provides the ability to retrieve orders and trades from the CowSap order-book, as well as add and cancel them
- `OrderSigningUtils` - serves to sign orders and cancel them using [EIP-712](https://eips.ethereum.org/EIPS/eip-712)
- `SubgraphApi` - provides statistics data about CoW protocol from [Subgraph](https://github.com/cowprotocol/subgraph), such as trading volume, trades count and others

> 💡 For a quick snippet with the full process on posting an order see the [Post an Order Example](https://github.com/cowprotocol/cow-sdk/blob/e086d9edeb24b25bb873a11c462019fa1ea4c021/docs/post-order-example.ts)
> 💡 For a quick snippet with the full process on posting an order see the [Quick start example](https://github.com/cowprotocol/cow-sdk/blob/main/examples/cra/src/pages/quickStart/index.tsx)
Now you should have a ready to use SDK instance. Let's explore in the next section, how we can use it to query the CoW API.
12 changes: 10 additions & 2 deletions cow-sdk/order-meta-data-appdata/bonus-cidv0-and-appdata.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ As explained before, the `AppData` points to an IPFS document. So given any `CID
Given an IPFS CIDv0 you can convert it to an `AppData`

```typescript
const decodedAppDataHex = await cowSdk.metadataApi.cidToAppDataHex('QmUf2TrpSANVXdgcYfAAACe6kg551cY3rAemB7xfEMjYvs')
import { MetadataApi } from '@cowprotocol/app-data'

export const metadataApi = new MetadataApi()

const decodedAppDataHex = await metadataApi.cidToAppDataHex('QmUf2TrpSANVXdgcYfAAACe6kg551cY3rAemB7xfEMjYvs')
```

This will return an `AppData` hex: `0x5ddb2c8207c10b96fac92cb934ef9ba004bc007a073c9e5b13edc422f209ed80`
Expand All @@ -15,7 +19,11 @@ This will return an `AppData` hex: `0x5ddb2c8207c10b96fac92cb934ef9ba004bc007a07
Similarly, you can do the opposite and convert an `AppData` into an IPFS document:

```typescript
const decodedAppDataHex = await cowSdk.metadataApi.appDataHexToCid(hash)
import { MetadataApi } from '@cowprotocol/app-data'

export const metadataApi = new MetadataApi()

const decodedAppDataHex = await metadataApi.appDataHexToCid(hash)
```

This will return an IPFS CIDv0: `QmUf2TrpSANVXdgcYfAAACe6kg551cY3rAemB7xfEMjYvs`
Expand Down
49 changes: 30 additions & 19 deletions cow-sdk/order-meta-data-appdata/create-a-meta-data-document.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,26 @@ The SDK facilitates the creation of these documents, and getting the `AppData` H
The most important thing to define in the meta-data is the name of your app, so the order-flow can be credited to it.

```typescript
const appDataDoc = cowSdk.metadataApi.generateAppDataDoc({}, {
appCode: 'YourApp'
import { MetadataApi } from '@cowprotocol/app-data'

export const metadataApi = new MetadataApi()

const appCode = 'YOUR_APP_CODE'

const appDataDoc = await metadataApi.generateAppDataDoc({
appDataParams: { appCode },
})

console.log(appDataDoc)
```

This will create a document similar to:

```json
{
"version": "0.1.0",
"appCode": "YourApp",
"metadata": {},
"version": "0.5.0",
"appCode": "YOUR_APP_CODE",
"metadata": {}
}
```

Expand All @@ -28,30 +36,33 @@ For example, you could give information about who reffered the user creating the

{% code title="" %}
```typescript
const appDataDoc = cowSdk.metadataApi.generateAppDataDoc(
{
referrer: {
address: '0x1f5B740436Fc5935622e92aa3b46818906F416E9',
version: '0.1.0',
},
},
{
appCode: 'YourApp',
}
)
import { MetadataApi } from '@cowprotocol/app-data'

export const metadataApi = new MetadataApi()

const appCode = 'YOUR_APP_CODE'
const referrerParams = { address: '0x1f5B740436Fc5935622e92aa3b46818906F416E9' }

const appDataDoc = await metadataApi.generateAppDataDoc({
appDataParams: { appCode },
metadataParams: { referrerParams },
})

console.log(appDataDoc)

```
{% endcode %}

This will create a document similar to:

```json
{
"version": "0.1.0",
"appCode": "YourApp",
"version": "0.5.0",
"appCode": "YOUR_APP_CODE",
"metadata": {
"referrer": {
"address": "0x1f5B740436Fc5935622e92aa3b46818906F416E9",
"version": "0.1.0",
"version": "0.2.0"
},
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@
Given the `AppData` of a document that has been uploaded to IPFS, you can easily retrieve the document.

```javascript
const appDataDoc = await cowSdk.metadataApi.decodeAppData('0x5ddb2c8207c10b96fac92cb934ef9ba004bc007a073c9e5b13edc422f209ed80')
import { MetadataApi } from '@cowprotocol/app-data'

export const metadataApi = new MetadataApi()

const appDataDoc = await metadataApi.decodeAppData('0x5ddb2c8207c10b96fac92cb934ef9ba004bc007a073c9e5b13edc422f209ed80')
```

This will return a document similar to:

```json
{
"version": "0.1.0",
"version": "0.5.0",
"appCode": "YourApp",
"metadata": {
"referrer": {
"address": "0x1f5B740436Fc5935622e92aa3b46818906F416E9",
"version": "0.1.0",
},
},
"version": "0.2.0"
}
}
}
```
6 changes: 5 additions & 1 deletion cow-sdk/order-meta-data-appdata/get-the-appdata-hex.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ The `AppData` Hex points to an IPFS document with the meta-data attached to the
You can calculate the `AppData` Hex, and its corresponding `cidV0` using the SDK:

```javascript
const { appDataHash, cidv0 } = await cowSdk.metadataApi.calculateAppDataHash(appDataDoc)
import { MetadataApi } from '@cowprotocol/app-data'

export const metadataApi = new MetadataApi()

const { appDataHash, cidv0 } = await metadataApi.calculateAppDataHash(appDataDoc)
```

Note how this operation is deterministic, so given the same document, it will always generate the same hash. Also, this operation is done locally, so it's not uploading anything to IPFS, its just calculating what will be the hash that maps to the provided document.
Expand Down
23 changes: 14 additions & 9 deletions cow-sdk/order-meta-data-appdata/upload-document-to-ipfs.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ The SDK uses Pinata to upload it to IPFS, so you will need an API Key in order t
Alternativelly, you can upload the document on your own using any other service.

```typescript
import { MetadataApi } from '@cowprotocol/app-data'

export const metadataApi = new MetadataApi()

const IPFS_OPTIONS = {
pinataApiKey: `PINATA_API_KEY`,
pinataApiSecret: `PINATA_SECRET_API_KEY`,
}

const appDataDoc = await metadataApi.generateAppDataDoc(...)

// Make sure you provide the IPFS params when instantiating the SDK
const cowSdk = new CowSdk(4, {
ipfs: {
pinataApiKey: 'YOUR_PINATA_API_KEY',
pinataApiSecret: 'YOUR_PINATA_API_SECRET'
},
})

// Upload to IPFS
const uploadedAppDataHash = await cowSdk.metadataApi.uploadMetadataDocToIpfs(appDataDoc)
const uploadedAppDataHash = await metadataApi.uploadMetadataDocToIpfs(appDataDoc, IPFS_OPTIONS)

console.log(uploadedAppDataHash)
```
13 changes: 7 additions & 6 deletions cow-sdk/querying-the-cow-subgraph.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@ The SDK provides just an easy way to access all this information.
You can query the Cow Subgraph either by running some common queries exposed by the `CowSubgraphApi` or by building your own ones:

```typescript
const chainId = 1 // Mainnet
const cowSdk = new CowSdk(chainId)
import { SubgraphApi, SupportedChainId } from '@cowprotocol/cow-sdk'

const subgraphApi = new SubgraphApi({ chainId: SupportedChainId.MAINNET })

// Get CoW Protocol totals
const { tokens, orders, traders, settlements, volumeUsd, volumeEth, feesUsd, feesEth } =
await cowSdk.cowSubgraphApi.getTotals()
await csubgraphApi.getTotals()
console.log({ tokens, orders, traders, settlements, volumeUsd, volumeEth, feesUsd, feesEth })

// Get last 24 hours volume in usd
const { hourlyTotals } = await cowSdk.cowSubgraphApi.getLastHoursVolume(24)
const { hourlyTotals } = await cowSubgraphApi.getLastHoursVolume(24)
console.log(hourlyTotals)

// Get last week volume in usd
const { dailyTotals } = await cowSdk.cowSubgraphApi.getLastDaysVolume(7)
const { dailyTotals } = await cowSubgraphApi.getLastDaysVolume(7)
console.log(dailyTotals)

// Get the last 5 batches
Expand All @@ -33,6 +34,6 @@ const query = `
}
`
const variables = { n: 5 }
const response = await cowSdk.cowSubgraphApi.runQuery(query, variables)
const response = await cowSubgraphApi.runQuery(query, variables)
console.log(response)
```
2 changes: 1 addition & 1 deletion cow-sdk/sign-and-post-orders/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ Posting orders is a three steps process:
2. **Sign the order**: Using off-chain signing or Meta-transactions
3. **Post the signed order to the API**: So, the order becomes `OPEN`

The next sections will guide you through the process of creating a valid order step by step. If you feel impatient and you want just a quick example, please out the the [Post an Order Example](https://github.com/cowprotocol/cow-sdk/blob/e086d9edeb24b25bb873a11c462019fa1ea4c021/docs/post-order-example.ts).
The next sections will guide you through the process of creating a valid order step by step. If you feel impatient and you want just a quick example, please out the the [Quick start example](https://github.com/cowprotocol/cow-sdk/blob/main/examples/cra/src/pages/quickStart/index.tsx).
8 changes: 6 additions & 2 deletions cow-sdk/sign-and-post-orders/bonus-show-link-to-explorer.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ Once the order is posted, its good to allow to check the state of it.

One easy is to check in the CoW Explorer. You can create a CoW Explorer link if you have the `orderId`:

For example, you can see one valid order in the explorer by following the link [https://explorer.cow.fi/rinkeby/orders/0xa0a77033edd3ce261f6c7d2afa6c59f07b126e328ebaa8284aca01e4f30437d8](https://explorer.cow.fi/rinkeby/orders/0xa0a77033edd3ce261f6c7d2afa6c59f07b126e328ebaa8284aca01e4f30437d8)
For example, you can see one valid order in the explorer by following the link [https://explorer.cow.fi/goerli/orders/0xa0a77033edd3ce261f6c7d2afa6c59f07b126e328ebaa8284aca01e4f30437d8](https://explorer.cow.fi/goerli/orders/0xa0a77033edd3ce261f6c7d2afa6c59f07b126e328ebaa8284aca01e4f30437d8)

```javascript
import { OrderBookApi, SupportedChainId } from '@cowprotocol/cow-sdk'

const orderBookApi = new OrderBookApi({ chainId: SupportedChainId.GOERLI })

// View order in explorer
console.log(`https://explorer.cow.fi/rinkeby/orders/${orderId}`)
console.log(orderBookApi.getOrderLink(orderId)) // https://explorer.cow.fi/goerli/orders/${orderId}
```

🍾 Congrats, you've made it to the end of SDK's Sign and Post Orders tutorial.
Expand Down
25 changes: 0 additions & 25 deletions cow-sdk/sign-and-post-orders/instantiate-sdk-with-a-signer.md

This file was deleted.

6 changes: 5 additions & 1 deletion cow-sdk/sign-and-post-orders/step-1-get-market-price.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ To create an order, you need to get a price/fee quote first:
To get the quote, you simply specify the trade you intent to do:

```typescript
const quoteResponse = await cowSdk.cowApi.getQuote({
import { OrderBookApi, SupportedChainId } from '@cowprotocol/cow-sdk'

const orderBookApi = new OrderBookApi({ chainId: SupportedChainId.MAINNET })

const quoteResponse = await orderBookApi.getQuote({
kind: OrderKind.SELL, // Sell order (could also be BUY)
sellToken: '0xc778417e063141139fce010982780140aa0cd5ab', // WETH
buyToken: '0x4dbcdf9b62e891a7cec5a2568c3f4faf9e8abe2b', // USDC
Expand Down
10 changes: 9 additions & 1 deletion cow-sdk/sign-and-post-orders/step-2-sign-the-order.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ Once you know the price and fee, we can create the order and sign it:
* The SDK will provide an easy way to sign orders given the raw data

```typescript
import { OrderSigningUtils, OrderKind, SupportedChainId } from '@cowprotocol/cow-sdk'
import { Web3Provider } from '@ethersproject/providers'

const provider = new Web3Provider(window.ethereum)
const signer = provider.getSigner()

const { sellToken, buyToken, validTo, buyAmount, sellAmount, receiver, feeAmount } = quoteResponse.quote

// Prepare the RAW order
Expand Down Expand Up @@ -34,7 +40,9 @@ const order = {
}

// Sign the order
const signedOrder = await cowSdk.signOrder(order)
const signedOrder = await OrderSigningUtils.signOrder(order, SupportedChainId.MAINNET, signer)
```

At this point, you have a signed order. So next step will be to post it to the API so it's considered by the solvers and executed.

> 📚 Read more about `appData` [here](https://github.com/cowprotocol/app-data)
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ Once you have a signed order, last step is to send it to the API.
Post an order using the SDK:

```javascript
const orderId = await cowSdk.cowApi.sendOrder({
order: { ...order, ...signedOrder },
owner: '0x1811be0994930fe9480eaede25165608b093ad7a',
})
import { OrderBookApi, SupportedChainId } from '@cowprotocol/cow-sdk'

const orderBookApi = new OrderBookApi({ chainId: SupportedChainId.MAINNET })

const orderId = await orderBookApi.sendOrder({ ...order, ...signedOrder })
```

Success 🎉! You managed to post a new valid order. Note that even though you awaited for the `sendOrder` call, this only signals that the order was accepted by the protocol.\
Expand Down

0 comments on commit acb52ac

Please sign in to comment.