Skip to content

Commit

Permalink
feat: sdk extension for people to swap (#213)
Browse files Browse the repository at this point in the history
* feat: SDK extension for people to swap

* feat: SDK extension for people to trade

* refactor: move files to trading dir

* feat: js example for trade-sdk

* chore: trade sdk examples

* docs: trading SDK docs

* chore: encapsulate networkCostsAmount

* chore: fix build

* chore: fix build

* chore: make networkCostsAmount optional

* feat: calculate unique OrderId util

* feat: support on-chain trades

* chore: reduce getOrderToSign parameters

* feat: support on-chain orders

* test: app data utils

* test: test calculateUniqueOrderId

* test: test getOrderToSign

* test: test getQuote

* test: test postCoWProtocolTrade

* test: test postLimitOrder

* test: test postOnChainTrade

* docs: docs for postOnChainTrade

* chore: up docs

* fix: update eth-flow addresses

* chore: fix tests

* chore: export helpers

* chore: rc version

* chore: fix readme

* feat: avoid using signer in getQuote

* chore: rc

* chore: move QuoteResults to types

* refactor: make QuoteResults serializable

* feat: add json schemas for trading entities

* chore: rc

* chore: up node v

* chore: remove schema gen call

* chore: up app-data version

* chore: bump version

* feat: add order typed data to quote results

* fix: fix order signing

* fix: override sellToken with wrapped for eth-flow

* feat(trading): function to generate eth-flow transaction

* chore: export swapParamsToLimitOrderParams

* fix: use hex numbers in transaction

* fix(trading): fix order amounts calculation

* chore: bump rc

* chore: fix tests

* chore: add mapQuoteAmountsAndCosts util

* chore: fix types

* chore: do not cache ethflow contract

* test: test getEthFlowTransaction

* feat(trading): add getPreSignTransaction function

* chore: add more description to trading types

* chore: fix test

* chore: update trading sdk readme

* chore: up rc

* chore: fix base contract addresses

* fix: make quoteId optional for limit orders

* refactor: rename postOnChainTrade -> postSellNativeCurrencyTrade

* docs: optional params

* chore: fix tests

* fix: add additional params for eth-flow quote req

* feat: add getPreSignTransaction to TradingSdk

* docs: get quest for smart-contract wallet

* chore: rename postSellNativeCurrencyOrder

* docs: create an order with smart-contract wallet

* chore: fix tests
  • Loading branch information
shoom3301 authored Dec 12, 2024
1 parent 8a8ce30 commit 9cc41f1
Show file tree
Hide file tree
Showing 55 changed files with 4,097 additions and 463 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@
]
},

"ignorePatterns": ["dist", "node_modules", "src/subgraph/graphql.ts", "examples"]
"ignorePatterns": ["dist", "node_modules", "src/subgraph/graphql.ts", "examples", "schemas"]
}
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ on:
types: [published]

env:
NODE_VERSION: lts/gallium
NODE_VERSION: lts/hydrogen

jobs:
build:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
types: [published]

env:
NODE_VERSION: lts/gallium
NODE_VERSION: lts/hydrogen

jobs:
deploy:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Unit tests & Coverage
on: [push, pull_request]

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

jobs:
Expand Down
152 changes: 152 additions & 0 deletions abi/EthFlow.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
[
{
"inputs": [
{
"components": [
{
"internalType": "contract IERC20",
"name": "buyToken",
"type": "address"
},
{
"internalType": "address",
"name": "receiver",
"type": "address"
},
{
"internalType": "uint256",
"name": "sellAmount",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "buyAmount",
"type": "uint256"
},
{
"internalType": "bytes32",
"name": "appData",
"type": "bytes32"
},
{
"internalType": "uint256",
"name": "feeAmount",
"type": "uint256"
},
{
"internalType": "uint32",
"name": "validTo",
"type": "uint32"
},
{
"internalType": "bool",
"name": "partiallyFillable",
"type": "bool"
},
{
"internalType": "int64",
"name": "quoteId",
"type": "int64"
}
],
"internalType": "struct EthFlowOrder.Data",
"name": "order",
"type": "tuple"
}
],
"name": "createOrder",
"outputs": [
{
"internalType": "bytes32",
"name": "orderHash",
"type": "bytes32"
}
],
"stateMutability": "payable",
"type": "function"
},
{
"inputs": [
{
"components": [
{
"internalType": "contract IERC20",
"name": "buyToken",
"type": "address"
},
{
"internalType": "address",
"name": "receiver",
"type": "address"
},
{
"internalType": "uint256",
"name": "sellAmount",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "buyAmount",
"type": "uint256"
},
{
"internalType": "bytes32",
"name": "appData",
"type": "bytes32"
},
{
"internalType": "uint256",
"name": "feeAmount",
"type": "uint256"
},
{
"internalType": "uint32",
"name": "validTo",
"type": "uint32"
},
{
"internalType": "bool",
"name": "partiallyFillable",
"type": "bool"
},
{
"internalType": "int64",
"name": "quoteId",
"type": "int64"
}
],
"internalType": "struct EthFlowOrder.Data",
"name": "order",
"type": "tuple"
}
],
"name": "invalidateOrder",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "bytes32",
"name": "",
"type": "bytes32"
}
],
"name": "orders",
"outputs": [
{
"internalType": "address",
"name": "owner",
"type": "address"
},
{
"internalType": "uint32",
"name": "validTo",
"type": "uint32"
}
],
"stateMutability": "view",
"type": "function"
}
]
89 changes: 89 additions & 0 deletions abi/GPv2Settlement.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
[
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "owner",
"type": "address"
},
{
"indexed": false,
"internalType": "contract IERC20",
"name": "sellToken",
"type": "address"
},
{
"indexed": false,
"internalType": "contract IERC20",
"name": "buyToken",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "sellAmount",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint256",
"name": "buyAmount",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint256",
"name": "feeAmount",
"type": "uint256"
},
{
"indexed": false,
"internalType": "bytes",
"name": "orderUid",
"type": "bytes"
}
],
"name": "Trade",
"type": "event"
},
{
"inputs": [
{
"internalType": "bytes",
"name": "orderUid",
"type": "bytes"
},
{
"internalType": "bool",
"name": "signed",
"type": "bool"
}
],
"name": "setPreSignature",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "bytes",
"name": "orderUid",
"type": "bytes"
}
],
"name": "invalidateOrder",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "domainSeparator",
"outputs": [{ "name": "", "type": "bytes32" }],
"stateMutability": "nonpayable",
"type": "function"
}
]
2 changes: 1 addition & 1 deletion examples/cra/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx"
},
},
"include": [
"src"
]
Expand Down
73 changes: 63 additions & 10 deletions examples/nodejs/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,69 @@
import { OrderBookApi, SubgraphApi, SupportedChainId } from '@cowprotocol/cow-sdk'
import { SupportedChainId, OrderKind, postSwapOrder, postLimitOrder } from '../../../src'

// See more examples in /examples/cra
const privateKey = 'xxx'

// Swap
;(async function () {
const orderBookApi = new OrderBookApi({ chainId: SupportedChainId.MAINNET })
const subgraphApi = new SubgraphApi({ chainId: SupportedChainId.MAINNET })
return

const order = await orderBookApi.getOrder(
'0xff2e2e54d178997f173266817c1e9ed6fee1a1aae4b43971c53b543cffcc2969845c6f5599fbb25dbdd1b9b013daf85c03f3c63763e4bc4a'
)
postSwapOrder({
appCode: 'cow-sdk-example',
signer: privateKey,
chainId: SupportedChainId.SEPOLIA,

kind: OrderKind.SELL,
sellToken: '0xfff9976782d46cc05630d1f6ebab18b2324d6b14',
sellTokenDecimals: 18,
buyToken: '0x0625afb445c3b6b7b929342a04a22599fd5dbb59',
buyTokenDecimals: 18,
amount: '120000000000000000',
})
})()

// Limit order
;(async function () {
return

const lastDaysVolume = await subgraphApi.getTotals()
postLimitOrder({
appCode: 'cow-sdk-example',
signer: privateKey,
chainId: SupportedChainId.SEPOLIA,

console.log('[orderBookApi] Order: ', order)
console.log('[subgraphApi] Last day volume: ', lastDaysVolume)
kind: OrderKind.BUY,
sellToken: '0xfff9976782d46cc05630d1f6ebab18b2324d6b14',
sellTokenDecimals: 18,
buyToken: '0x0625afb445c3b6b7b929342a04a22599fd5dbb59',
buyTokenDecimals: 18,
sellAmount: '120000000000000000',
buyAmount: '66600000000000000000',
networkCostsAmount: '0',
})
})()

// Swap with partner fee
;(async function () {
postSwapOrder(
{
appCode: 'cow-sdk-example',
signer: privateKey,
chainId: SupportedChainId.SEPOLIA,

kind: OrderKind.SELL,
sellToken: '0xfff9976782d46cc05630d1f6ebab18b2324d6b14',
sellTokenDecimals: 18,
buyToken: '0x0625afb445c3b6b7b929342a04a22599fd5dbb59',
buyTokenDecimals: 18,
amount: '120000000000000000',
},
{
appData: {
metadata: {
partnerFee: {
bps: 100,
recipient: '0xfb3c7eb936cAA12B5A884d612393969A557d4307',
},
},
},
}
)
})()
6 changes: 3 additions & 3 deletions examples/vanilla/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
"start": "webpack serve --mode=development",
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"@cowprotocol/cow-sdk": "^4.0.3"
},
"dependencies": {},
"author": "",
"license": "ISC",
"devDependencies": {
"css-loader": "^7.1.2",
"html-webpack-plugin": "^5.5.0",
"style-loader": "^4.0.0",
"ts-loader": "^9.4.2",
"webpack": "^5.76.3",
"webpack-cli": "^5.0.1",
Expand Down
Loading

0 comments on commit 9cc41f1

Please sign in to comment.