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

[SDK dogfooding] Refactoring, split API's, new assembling, Swagger #97

Merged
merged 49 commits into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
9b4b8de
New architecture (split API's, webpack)
shoom3301 Feb 14, 2023
0251d91
Removed 0x and paraswap APIs
shoom3301 Feb 14, 2023
7fd266f
Moved subgraph and metadata APIs to src
shoom3301 Feb 14, 2023
bd9b725
Renamed cow-subgraph to subgraph
shoom3301 Feb 14, 2023
474842b
Moved order-book tests and utils to new directory
shoom3301 Feb 14, 2023
75ad165
Renamed order-book tests
shoom3301 Feb 14, 2023
fc5ea8a
OrderBook API
shoom3301 Feb 14, 2023
a05e90e
Removed old order-book API
shoom3301 Feb 14, 2023
4279bb0
Order-book code generated from Swagger
shoom3301 Feb 14, 2023
b22403f
SubgraphApi refactoring
shoom3301 Feb 14, 2023
7c1db6f
MetaDataApi refactoring
shoom3301 Feb 14, 2023
5400fe4
Removed outdated files
shoom3301 Feb 14, 2023
a9803cb
Fix code style
shoom3301 Feb 15, 2023
1c6719d
Remove chainId from SubgraphApi constructor
shoom3301 Feb 23, 2023
5c73283
Remove chainId from OrderBookApi constructor
shoom3301 Feb 23, 2023
1d72bd3
Fix tests
shoom3301 Feb 23, 2023
0483355
Remove @cowprotocol/app-data usage
shoom3301 Feb 24, 2023
39b0a58
Exclude tests
shoom3301 Feb 24, 2023
a665f48
Fix OrderQuoteSide
shoom3301 Feb 24, 2023
90295ae
Fix OrderQuoteResponse
shoom3301 Feb 24, 2023
5baa639
getNativePrice method to order-book
shoom3301 Feb 24, 2023
95ee0ff
Merge pull request #102 from cowprotocol/refactoring/dogfooding-5
shoom3301 Feb 28, 2023
ccbe4e5
Merge pull request #101 from cowprotocol/refactoring/dogfooding-4
shoom3301 Feb 28, 2023
48bd75e
Merge pull request #100 from cowprotocol/refactoring/dogfooding-3
shoom3301 Feb 28, 2023
e9db1e4
Merge pull request #99 from cowprotocol/refactoring/dogfooding-2
shoom3301 Feb 28, 2023
b3d8c27
Merge pull request #98 from cowprotocol/refactoring/dogfooding-1
shoom3301 Feb 28, 2023
c37b0a0
Adjust assembling
shoom3301 Feb 28, 2023
b89ef83
Merge branch 'main' into refactoring/dogfooding
shoom3301 Feb 28, 2023
0a13171
Fix readme
shoom3301 Feb 28, 2023
75b328b
Merge remote-tracking branch 'origin/refactoring/dogfooding' into ref…
shoom3301 Feb 28, 2023
4326b8b
Add ApiContext for SubgraphApi
shoom3301 Mar 2, 2023
2c45457
Add ApiContext for OrderBookApi
shoom3301 Mar 2, 2023
23224a0
Move back app-data to MetadataApi with Lazy-loading
shoom3301 Mar 2, 2023
93c9ce4
getOrderMultiEnv() for order-book API
shoom3301 Mar 3, 2023
6c7ed68
Fix getContextWithOverride()
shoom3301 Mar 3, 2023
c204541
Remove webpack config
shoom3301 Mar 3, 2023
48ccd6c
customEnvConfigs for order-book and subgraph APIs
shoom3301 Mar 3, 2023
d832595
customEnvConfigs for order-book and subgraph APIs
shoom3301 Mar 3, 2023
6972448
2.0.0-alpha.0
shoom3301 Mar 3, 2023
38b2382
prepare script
shoom3301 Mar 3, 2023
8f9ce7a
2.0.0-alpha.1
shoom3301 Mar 3, 2023
743cbdd
One entry point
shoom3301 Mar 3, 2023
6f0f33a
2.0.0-alpha.2
shoom3301 Mar 3, 2023
247f607
microbundle for assembling
shoom3301 Mar 3, 2023
296f784
2.0.0-alpha.3
shoom3301 Mar 3, 2023
5013ac3
generateAppDataDoc() example
shoom3301 Mar 3, 2023
cfcdb17
Removed Metadata since now it's in @cowprotocol/app-data
shoom3301 Mar 6, 2023
0c8f681
2.0.0-alpha.4
shoom3301 Mar 6, 2023
afc8f72
Update swagger URL
shoom3301 Mar 6, 2023
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
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
"prettier/prettier": "error"
},

"ignorePatterns": ["dist", "node_modules", "src/api/cow-subgraph/graphql.ts"]
"ignorePatterns": ["dist", "node_modules", "src/subgraph/graphql.ts"]
}
7 changes: 5 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ on:
release:
types: [published]

env:
NODE_VERSION: lts/gallium

jobs:
build:
name: Build Package
Expand All @@ -30,7 +33,7 @@ jobs:
- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: 14
node-version: ${{ env.NODE_VERSION }}

- uses: actions/cache@v2
with:
Expand All @@ -45,6 +48,6 @@ jobs:
yarn install --cache-folder ./.yarncache --frozen-lockfile
rm -rf .yarncache
yarn cache clean

- name: Build app
run: yarn build
7 changes: 5 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ on:
release:
types: [published]

env:
NODE_VERSION: lts/gallium

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 14.x
node-version: ${{ env.NODE_VERSION }}
registry-url: 'https://registry.npmjs.org'
scope: '@cowprotocol'
- run: yarn --frozen-lockfile
- run: bash src/workflows/publish.sh
- run: bash workflows/publish.sh
env:
NODE_AUTH_TOKEN: ${{ secrets.NODE_AUTH_TOKEN }}
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ name: Unit tests & Coverage
on: [push, pull_request]

env:
NODE_VERSION: lts/gallium
COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}

jobs:
Expand All @@ -19,7 +20,7 @@ jobs:
- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: 14
node-version: ${{ env.NODE_VERSION }}

- name: Yarn cache
uses: actions/cache@v2
Expand Down
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
dist/
src/api/cow-subgraph/graphql.ts
src/subgraph/graphql.ts
src/order-book/generated
38 changes: 19 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ console.log(trades)
## Sign and Post orders
In order to trade, you will need to create a valid order first.

On the contraty to other decentralised exchanges, creating orders is free in CoW Protocol. This is because, one of the
On the contraty to other decentralised exchanges, creating orders is free in CoW Protocol. This is because, one of the
most common ways to do it is by created offchain signed messages (meta-transactions, uses `EIP-712` or `EIP-1271`).

Posting orders is a three steps process:
Expand All @@ -71,9 +71,9 @@ The next sections will guide you through the process of creating a valid order.

### Enable tokens (token approval)
Because of the use of off-chain signing (meta-transactions), users will need to **Enable the sell token** before signed
orders can be considered as valid ones.
orders can be considered as valid ones.

This enabling is technically an `ERC-20` approval, and is something that needs to be done only once. After this all
This enabling is technically an `ERC-20` approval, and is something that needs to be done only once. After this all
order creation can be done for free using offchain signing.

> For more details see https://docs.cow.fi/tutorials/how-to-submit-orders-via-the-api/1.-set-allowance-for-the-sell-token
Expand All @@ -90,18 +90,18 @@ const wallet = Wallet.fromMnemonic(mnemonic)
const cowSdk = new CowSdk(
100, { // Leaving chainId empty will default to MAINNET
signer: wallet // Provide a signer, so you can sign order
})
})
```

### STEP 1: Get Market Price
To create an order, you need to get a price/fee quote first:

* The SDK will give you easy access to the API, which returns the `Market Price` and the `Fee` for any given trade you intent to do.
* The returned `Market Price` is not strictly needed, you can use your own pricing logic.
* The returned `Market Price` is not strictly needed, you can use your own pricing logic.
* You can choose a price that is below this Market price (**Market Order**), or above Market Price (**Limit Order**).
* The `Fee` however is very important.
* It is the required amount in sell token the trader agrees on paying for executing the order onchain.
* Normally, its value is proportional to the current Gas Price of the network.
* The `Fee` however is very important.
* It is the required amount in sell token the trader agrees on paying for executing the order onchain.
* Normally, its value is proportional to the current Gas Price of the network.
* This fee is never charged if you don't trade.

To get the quote, you simply specify the trade you intent to do:
Expand Down Expand Up @@ -133,7 +133,7 @@ const { sellToken, buyToken, validTo, buyAmount, sellAmount, receiver, feeAmount

// Prepare the RAW order
const order = {
kind: OrderKind.SELL, // SELL || BUY
kind: OrderKind.SELL, // SELL || BUY
receiver, // Your account or any other
sellToken,
buyToken,
Expand All @@ -144,10 +144,10 @@ const order = {
validTo,

// Limit Price
// You can apply some slippage tolerance here to make sure the trade is executed.
// You can apply some slippage tolerance here to make sure the trade is executed.
// CoW protocol protects from MEV, so it can work with higher slippages
sellAmount,
buyAmount,
buyAmount,

// Use the fee you received from the API
feeAmount,
Expand All @@ -162,8 +162,8 @@ const signedOrder = await cowSdk.signOrder(order)

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.

## STEP 3: **Post the signed order to the API**:

## STEP 3: **Post the signed order to the API**:
Once you have a signed order, last step is to send it to the API.
* The API will accept the order if its correctly signed, the deadline is correct, and the fee is enough to settle it
* Once accepted, the order will be `OPEN` until the specified `validTo` date (expiration)
Expand All @@ -187,7 +187,7 @@ const orderId = await cowSdk.cowApi.sendOrder({


### BONUS: Show link to Explorer
Once the order is posted, its good to allow to check the state of it.
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`:

Expand Down Expand Up @@ -216,7 +216,7 @@ This will create a document similar to:
"version": "0.4.0",
"appCode": "YourApp",
"metadata": {}
}
}
```

After creating the most basic document, you can see how to attach additional meta-data items.
Expand Down Expand Up @@ -264,7 +264,7 @@ You can calculate the `AppData` Hex, and its corresponding `cidV0` using the SDK
const { appDataHash, cidv0 } = await cowSdk.metadataApi.calculateAppDataHash(appDataDoc)
```

Note how this operation is deterministic, so given the same document, it will always generate the same hash.
Note how this operation is deterministic, so given the same document, it will always generate the same hash.

This method can be used to calculate the actual hash before uploading the document to IPFS.

Expand Down Expand Up @@ -299,8 +299,8 @@ Alternatively, you can upload the document on your own using any other service.
```js
// Make sure you provide the IPFS params when instantiating the SDK
const cowSdk = new CowSdk(100, {
ipfs: {
pinataApiKey: 'YOUR_PINATA_API_KEY',
ipfs: {
pinataApiKey: 'YOUR_PINATA_API_KEY',
pinataApiSecret: 'YOUR_PINATA_API_SECRET'
},
})
Expand All @@ -310,7 +310,7 @@ const uploadedAppDataHash = await cowSdk.metadataApi.uploadMetadataDocToIpfs(app
```

## Convert IPFS CIDv0 to AppData (and back)
Given an IPFS CIDv0 you can convert it to an `AppData`
Given an IPFS CIDv0 you can convert it to an `AppData`

```js
const decodedAppDataHex = await cowSdk.metadataApi.cidToAppDataHex('QmUf2TrpSANVXdgcYfAAACe6kg551cY3rAemB7xfEMjYvs')
Expand Down
33 changes: 0 additions & 33 deletions __mocks__/paraswap.js

This file was deleted.

4 changes: 2 additions & 2 deletions graphql-codegen.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
overwrite: true
schema: "https://api.thegraph.com/subgraphs/name/cowprotocol/cow"
documents: './src/api/cow-subgraph/queries.ts'
documents: './src/subgraph/queries.ts'
generates:
src/api/cow-subgraph/graphql.ts:
src/subgraph/graphql.ts:
plugins:
- "typescript"
- "typescript-operations"
57 changes: 27 additions & 30 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,24 @@
"name": "@cowprotocol/cow-sdk",
"version": "1.0.2-RC.0",
"license": "(MIT OR Apache-2.0)",
"source": "src/index.ts",
"main": "./dist/index.js",
"module": "./dist/index.module.js",
"exports": {
"require": "./dist/index.js",
"default": "./dist/index.modern.mjs"
},
"types": "dist/index.d.ts",
"files": [
"/dist"
],
"scripts": {
"build": "microbundle -f modern,esm,cjs",
"start": "microbundle -f modern,esm,cjs watch",
"prebuild": "rm -rf dist && yarn run codegen",
"build": "tsc --module commonjs",
"postbuild": "cp package.json dist && cp README.md dist",
"start:metadata": "tsc-watch --noClear -p ./tsconfig.json --onSuccess \"node ./dist/metadata/index.js\"",
"start:order-book": "tsc-watch --noClear -p ./tsconfig.json --onSuccess \"node ./dist/order-book/index.js\"",
"start:subgraph": "tsc-watch --noClear -p ./tsconfig.json --onSuccess \"node ./dist/subgraph/index.js\"",
"lint": "eslint src",
"format": "prettier --write \"src/**/*.+(ts|json)\"",
"test": "jest",
"test:coverage": "jest --coverage --json --outputFile=jest.results.json && cat ./coverage/lcov.info | coveralls",
"prepare": "npm run build && npm run graphql:codegen",
"codegen": "npm run graphql:codegen && npm run swagger:codegen",
"prepublishOnly": "npm test && npm run lint",
"graphql:codegen": "graphql-codegen --config graphql-codegen.yml"
"graphql:codegen": "graphql-codegen --config graphql-codegen.yml",
"swagger:codegen": " openapi --input https://raw.githubusercontent.com/cowprotocol/services/fix/orderbook-openapi/crates/orderbook/openapi.yml --output src/order-book/generated --client fetch --name OrderBookClient"
},
"dependencies": {
"@cowprotocol/app-data": "^0.0.3-RC.0",
Expand All @@ -32,30 +29,31 @@
"graphql": "^16.3.0",
"graphql-request": "^4.3.0",
"ipfs-only-hash": "^4.0.0",
"loglevel": "^1.8.0",
"multiformats": "^9.6.4",
"paraswap": "^5.2.0",
"paraswap-core": "^1.0.2"
"multiformats": "^9.6.4"
},
"devDependencies": {
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.18.6",
"@babel/plugin-proposal-private-methods": "^7.18.6",
"@babel/preset-env": "^7.20.2",
"@babel/preset-typescript": "^7.18.6",
"@graphql-codegen/cli": "2.6.2",
"@graphql-codegen/typescript": "2.4.8",
"@graphql-codegen/typescript-operations": "^2.3.5",
"@types/jest": "^27.0.3",
"@typescript-eslint/eslint-plugin": "^5.9.0",
"@typescript-eslint/parser": "^5.9.0",
"@graphql-codegen/cli": "3.0.0",
"@graphql-codegen/typescript": "3.0.0",
"@graphql-codegen/typescript-operations": "^3.0.0",
"@types/jest": "^29.4.0",
"@types/node": "^18.13.0",
"@typescript-eslint/eslint-plugin": "^5.51.0",
"@typescript-eslint/parser": "^5.51.0",
"babel-plugin-inline-import": "^3.0.0",
"coveralls": "^3.1.1",
"eslint": "^8.6.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
"jest": "^27.3.1",
"eslint": "^8.33.0",
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-prettier": "^4.2.1",
"jest": "^29.4.2",
"jest-fetch-mock": "^3.0.3",
"microbundle": "^0.15.0",
"prettier": "^2.5.1"
"openapi-typescript-codegen": "^0.23.0",
"prettier": "^2.5.1",
"tsc-watch": "^6.0.0",
"typescript": "^4.9.5"
},
"jest": {
"automock": false,
Expand All @@ -64,8 +62,7 @@
"./setupTests.js"
],
"collectCoverageFrom": [
"src/**/*.{ts,tsx}",
"!src/api/cow/errors/*.{ts,tsx}"
"src/**/*.{ts,tsx}"
]
},
"keywords": [
Expand Down
6 changes: 0 additions & 6 deletions setupTests.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import fetchMock from 'jest-fetch-mock'
import log from 'loglevel'

fetchMock.enableMocks()

jest.setMock('cross-fetch', fetchMock)

global.window = global

log.info = jest.fn
log.debug = jest.fn
log.error = jest.fn
log.setLevel = jest.fn
Loading