Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add arbitrum #206

Merged
merged 6 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 26 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ yarn add @cowprotocol/cow-sdk
- `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, trade count and others


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

Expand All @@ -47,6 +46,7 @@ For clarity, let's look at the use of the API with a practical example:
Exchanging `0.4 GNO` to `WETH` on `Gnosis chain` network.

We will do the following operations:

1. Get a quote
2. Sign the order
3. Send the order to the order-book
Expand All @@ -56,7 +56,6 @@ We will do the following operations:

[You also can check this code in the CRA example](https://github.com/cowprotocol/cow-sdk/blob/main/examples/cra/src/pages/quickStart/index.tsx)


```typescript
import { OrderBookApi, OrderSigningUtils, SupportedChainId } from '@cowprotocol/cow-sdk'
import { Web3Provider } from '@ethersproject/providers'
Expand All @@ -78,21 +77,24 @@ const quoteRequest = {
const orderBookApi = new OrderBookApi({ chainId: SupportedChainId.GNOSIS_CHAIN })

async function main() {
const { quote } = await orderBookApi.getQuote(quoteRequest)
const { quote } = await orderBookApi.getQuote(quoteRequest)

const orderSigningResult = await OrderSigningUtils.signOrder(quote, chainId, signer)
const orderSigningResult = await OrderSigningUtils.signOrder(quote, chainId, signer)

const orderId = await orderBookApi.sendOrder({ ...quote, ...orderSigningResult })
const orderId = await orderBookApi.sendOrder({ ...quote, ...orderSigningResult })

const order = await orderBookApi.getOrder(orderId)
const order = await orderBookApi.getOrder(orderId)

const trades = await orderBookApi.getTrades({ orderId })
const trades = await orderBookApi.getTrades({ orderId })

const orderCancellationSigningResult = await OrderSigningUtils.signOrderCancellations([orderId], chainId, signer)
const orderCancellationSigningResult = await OrderSigningUtils.signOrderCancellations([orderId], chainId, signer)

const cancellationResult = await orderBookApi.sendSignedOrderCancellations({...orderCancellationSigningResult, orderUids: [orderId] })
const cancellationResult = await orderBookApi.sendSignedOrderCancellations({
...orderCancellationSigningResult,
orderUids: [orderId],
})

console.log('Results: ', { orderId, order, trades, orderCancellationSigningResult, cancellationResult })
console.log('Results: ', { orderId, order, trades, orderCancellationSigningResult, cancellationResult })
}
```

Expand All @@ -103,18 +105,19 @@ Since the API supports different networks and environments, there are some optio

#### Environment configuration

`chainId` - can be one of `SupportedChainId.MAINNET`, `SupportedChainId.GNOSIS_CHAIN`, or `SupportedChainId.SEPOLIA`
`chainId` - can be one of `SupportedChainId.MAINNET`, `SupportedChainId.GNOSIS_CHAIN`, `SupportedChainId.ARBITRUM` or `SupportedChainId.SEPOLIA`

`env` - this parameter affects which environment will be used:
- `https://api.cow.fi` for `prod` (default)
- `https://barn.api.cow.fi` for `staging`

- `https://api.cow.fi` for `prod` (default)
- `https://barn.api.cow.fi` for `staging`

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

const orderBookApi = new OrderBookApi({
chainId: SupportedChainId.GNOSIS_CHAIN,
env: 'staging' // <-----
env: 'staging', // <-----
})
```

Expand All @@ -127,19 +130,22 @@ import { OrderBookApi } from '@cowprotocol/cow-sdk'

const orderBookApi = new OrderBookApi({
chainId: SupportedChainId.GNOSIS_CHAIN,
baseUrls: { // <-----
baseUrls: {
// <-----
[SupportedChainId.MAINNET]: 'https://YOUR_ENDPOINT/mainnet',
[SupportedChainId.GNOSIS_CHAIN]: 'https://YOUR_ENDPOINT/gnosis-chain',
[SupportedChainId.GNOSIS_CHAIN]: 'https://YOUR_ENDPOINT/gnosis_chain',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As requested, your Highness.

[SupportedChainId.ARBITRUM]: 'https://YOUR_ENDPOINT/arbitrum_one',
[SupportedChainId.SEPOLIA]: 'https://YOUR_ENDPOINT/sepolia',
}
},
})
```

The [CoW Protocol API](https://api.cow.fi/docs/#/) has restrictions on the backend side to protect against DDOS and other issues.

>The main restriction is request rate limit of: **5 requests per second for each IP address**
> The main restriction is request rate limit of: **5 requests per second for each IP address**

The _client's_ limiter settings can be configured as well:

The *client's* limiter settings can be configured as well:
```typescript
import { OrderBookApi } from '@cowprotocol/cow-sdk'
import { BackoffOptions } from 'exponential-backoff'
Expand All @@ -156,9 +162,7 @@ const backOffOpts: BackoffOptions = {
jitter: 'none',
}

const orderBookApi = new OrderBookApi(
{chainId: SupportedChainId.GNOSIS_CHAIN, limiterOpts, backOffOpts},
)
const orderBookApi = new OrderBookApi({ chainId: SupportedChainId.GNOSIS_CHAIN, limiterOpts, backOffOpts })
```

### Querying the CoW Subgraph
Expand Down Expand Up @@ -201,7 +205,6 @@ const response = await cowSubgraphApi.runQuery(query, variables)
console.log(response)
```


## Architecture

One way to make the most out of the SDK is to get familiar with its architecture.
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
"prepare": "npm run build",
"prepublishOnly": "npm test && npm run lint",
"graphql:codegen": "graphql-codegen --config graphql-codegen.yml",
"swagger:codegen": " openapi --input https://raw.githubusercontent.com/cowprotocol/services/v2.252.0/crates/orderbook/openapi.yml --output src/order-book/generated --exportServices false --exportCore false",
"swagger:codegen": " openapi --input https://raw.githubusercontent.com/cowprotocol/services/varbitrum/crates/orderbook/openapi.yml --output src/order-book/generated --exportServices false --exportCore false",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we will need to remember to come back here, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really, every backend release we need to update it, and this is the latest

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but i thought we do tagged versions. But anyways, fine, next version we change it

"typechain:codegen": "typechain --target ethers-v5 --out-dir ./src/composable/generated './abi/*.json'"
},
"dependencies": {
"@cowprotocol/contracts": "^1.4.0",
"@cowprotocol/contracts": "^1.6.0",
"@ethersproject/abstract-signer": "^5.7.0",
"@openzeppelin/merkle-tree": "^1.0.5",
"cross-fetch": "^3.1.5",
Expand Down Expand Up @@ -100,4 +100,4 @@
"typescript",
"subgraph"
]
}
}
1 change: 1 addition & 0 deletions src/common/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
export enum SupportedChainId {
MAINNET = 1,
GNOSIS_CHAIN = 100,
ARBITRUM = 42161,
SEPOLIA = 11155111,
}
28 changes: 15 additions & 13 deletions src/order-book/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
import 'cross-fetch/polyfill'
import { RateLimiter } from 'limiter'
import { SupportedChainId } from '../common/chains'
import {
ApiBaseUrls,
ApiContext,
CowEnv,
DEFAULT_COW_API_CONTEXT,
ENVS_LIST,
PartialApiContext,
RequestOptions,
} from '../common/configs'
import { CowError } from '../common/cow-error'
import {
Address,
AppDataHash,
Expand All @@ -15,21 +27,9 @@ import {
TransactionHash,
UID,
} from './generated'
import { CowError } from '../common/cow-error'
import {
ApiBaseUrls,
ApiContext,
CowEnv,
DEFAULT_COW_API_CONTEXT,
ENVS_LIST,
PartialApiContext,
RequestOptions,
} from '../common/configs'
import { DEFAULT_BACKOFF_OPTIONS, DEFAULT_LIMITER_OPTIONS, FetchParams, OrderBookApiError, request } from './request'
import { transformOrder } from './transformOrder'
import { EnrichedOrder } from './types'
import { SupportedChainId } from '../common/chains'
import { RateLimiter } from 'limiter'
import { DEFAULT_BACKOFF_OPTIONS, DEFAULT_LIMITER_OPTIONS, FetchParams, OrderBookApiError, request } from './request'

/**
* An object containing *production* environment base URLs for each supported `chainId`.
Expand All @@ -38,6 +38,7 @@ import { DEFAULT_BACKOFF_OPTIONS, DEFAULT_LIMITER_OPTIONS, FetchParams, OrderBoo
export const ORDER_BOOK_PROD_CONFIG: ApiBaseUrls = {
[SupportedChainId.MAINNET]: 'https://api.cow.fi/mainnet',
[SupportedChainId.GNOSIS_CHAIN]: 'https://api.cow.fi/xdai',
[SupportedChainId.ARBITRUM]: 'https://api.cow.fi/arbitrum_one',
[SupportedChainId.SEPOLIA]: 'https://api.cow.fi/sepolia',
}

Expand All @@ -47,6 +48,7 @@ export const ORDER_BOOK_PROD_CONFIG: ApiBaseUrls = {
export const ORDER_BOOK_STAGING_CONFIG: ApiBaseUrls = {
[SupportedChainId.MAINNET]: 'https://barn.api.cow.fi/mainnet',
[SupportedChainId.GNOSIS_CHAIN]: 'https://barn.api.cow.fi/xdai',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

weren't these names changing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No changes yet afaict, just the proposal for a standardization.

[SupportedChainId.ARBITRUM]: 'https://barn.api.cow.fi/arbitrum_one',
[SupportedChainId.SEPOLIA]: 'https://barn.api.cow.fi/sepolia',
}

Expand Down
4 changes: 0 additions & 4 deletions src/order-book/generated/models/AuctionOrder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ export type AuctionOrder = {
* see `OrderParameters::buyAmount`
*/
buyAmount: TokenAmount;
/**
* see `OrderParameters::feeAmount`
*/
userFee: TokenAmount;
/**
* see `OrderParameters::validTo`
*/
Expand Down
10 changes: 6 additions & 4 deletions src/subgraph/api.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Variables, request } from 'graphql-request'
import { DocumentNode } from 'graphql/index'
import { SupportedChainId } from '../common/chains'
import { ApiContext, CowEnv, DEFAULT_COW_API_CONTEXT } from '../common/configs'
import { CowError } from '../common/cow-error'
import { LastDaysVolumeQuery, LastHoursVolumeQuery, TotalsQuery } from './graphql'
import { LAST_DAYS_VOLUME_QUERY, LAST_HOURS_VOLUME_QUERY, TOTALS_QUERY } from './queries'
import { DocumentNode } from 'graphql/index'
import { request, Variables } from 'graphql-request'
import { ApiContext, CowEnv, DEFAULT_COW_API_CONTEXT } from '../common/configs'
import { SupportedChainId } from '../common/chains'

const SUBGRAPH_BASE_URL = 'https://api.thegraph.com/subgraphs/name/cowprotocol'

Expand All @@ -24,6 +24,7 @@ type PartialSubgraphApiContext = Partial<SubgraphApiContext>
export const SUBGRAPH_PROD_CONFIG: SubgraphApiBaseUrls = {
[SupportedChainId.MAINNET]: SUBGRAPH_BASE_URL + '/cow',
[SupportedChainId.GNOSIS_CHAIN]: SUBGRAPH_BASE_URL + '/cow-gc',
[SupportedChainId.ARBITRUM]: null,
[SupportedChainId.SEPOLIA]: null,
}

Expand All @@ -36,6 +37,7 @@ export const SUBGRAPH_PROD_CONFIG: SubgraphApiBaseUrls = {
export const SUBGRAPH_STAGING_CONFIG: SubgraphApiBaseUrls = {
[SupportedChainId.MAINNET]: SUBGRAPH_BASE_URL + '/cow-staging',
[SupportedChainId.GNOSIS_CHAIN]: SUBGRAPH_BASE_URL + '/cow-gc-staging',
[SupportedChainId.ARBITRUM]: null,
[SupportedChainId.SEPOLIA]: null,
}

Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1761,10 +1761,10 @@
resolved "https://registry.yarnpkg.com/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz#75a2e8b51cb758a7553d6804a5932d7aace75c39"
integrity sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw==

"@cowprotocol/contracts@^1.4.0":
version "1.4.0"
resolved "https://registry.yarnpkg.com/@cowprotocol/contracts/-/contracts-1.4.0.tgz#e93e5f25aac76feeaa348fa57231903274676247"
integrity sha512-XLs3SlPmXD4lbiWIO7mxxuCn1eE5isuO6EUlE1cj17HqN/wukDAN0xXYPx6umOH/XdjGS33miMiPHELEyY9siw==
"@cowprotocol/contracts@^1.6.0":
version "1.6.0"
resolved "https://registry.yarnpkg.com/@cowprotocol/contracts/-/contracts-1.6.0.tgz#d0fc83ed8c624b968d1a68bb5c74712c11ec81e0"
integrity sha512-+UKhYRzkvnqqviBru5D3btTLYc743n0O5YTG+wpYwGl4fb7VNKBkFHe28C5Mf1DF/kOfmqfu+0IAvX9Vuq5Dqw==

"@cspotcode/source-map-support@^0.8.0":
version "0.8.1"
Expand Down
Loading