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: sdk extension for people to swap #213

Open
wants to merge 32 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
6788008
feat: SDK extension for people to swap
shoom3301 Jun 26, 2024
e8927e2
feat: SDK extension for people to trade
shoom3301 Jun 26, 2024
59f550b
refactor: move files to trading dir
shoom3301 Jun 27, 2024
ee092bb
feat: js example for trade-sdk
shoom3301 Jun 27, 2024
775e687
chore: trade sdk examples
shoom3301 Jun 28, 2024
bbece96
docs: trading SDK docs
shoom3301 Jul 5, 2024
f27a94d
chore: encapsulate networkCostsAmount
shoom3301 Jul 5, 2024
d98230a
Merge branch 'main' of https://github.com/cowprotocol/cow-sdk into fe…
shoom3301 Jul 5, 2024
f82841d
chore: fix build
shoom3301 Jul 5, 2024
5639517
chore: fix build
shoom3301 Jul 5, 2024
92c7d14
Merge branch 'main' of https://github.com/cowprotocol/cow-sdk into fe…
shoom3301 Sep 2, 2024
6896b20
Merge branch 'main' of https://github.com/cowprotocol/cow-sdk into fe…
shoom3301 Nov 5, 2024
94b21ff
chore: make networkCostsAmount optional
shoom3301 Nov 5, 2024
bf87d83
feat: calculate unique OrderId util
shoom3301 Nov 6, 2024
9c23fae
feat: support on-chain trades
shoom3301 Nov 7, 2024
6fcff58
chore: reduce getOrderToSign parameters
shoom3301 Nov 7, 2024
490e7d4
Merge branch 'main' of https://github.com/cowprotocol/cow-sdk into fe…
shoom3301 Nov 7, 2024
4a1f35e
feat: support on-chain orders
shoom3301 Nov 7, 2024
240ebfb
test: app data utils
shoom3301 Nov 7, 2024
a8d1ec9
test: test calculateUniqueOrderId
shoom3301 Nov 7, 2024
59fd04b
test: test getOrderToSign
shoom3301 Nov 7, 2024
6351ae7
test: test getQuote
shoom3301 Nov 8, 2024
56692ab
test: test postCoWProtocolTrade
shoom3301 Nov 8, 2024
9d828bf
test: test postLimitOrder
shoom3301 Nov 8, 2024
f106189
test: test postOnChainTrade
shoom3301 Nov 8, 2024
99a1cad
docs: docs for postOnChainTrade
shoom3301 Nov 8, 2024
5698598
Merge branch 'main' of https://github.com/cowprotocol/cow-sdk into fe…
shoom3301 Nov 8, 2024
03d212e
chore: up docs
shoom3301 Nov 8, 2024
867b05e
fix: update eth-flow addresses
shoom3301 Nov 8, 2024
62ee519
chore: fix tests
shoom3301 Nov 8, 2024
4f788ed
chore: export helpers
shoom3301 Nov 8, 2024
953f848
chore: rc version
shoom3301 Nov 12, 2024
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
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"
}
]
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
46 changes: 46 additions & 0 deletions examples/vanilla/src/formState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { OrderKind, SupportedChainId, SwapParameters, TradeParameters } from '../../../src'
import { TOKENS } from './tokens'

interface FormState {
privateKey: string
chainId: string
sellToken: string
buyToken: string
amount: string
slippageBps: string
kind: 'sell' | 'buy'
}

export const getFormState = (): FormState => {
return Object.fromEntries(new FormData(document.getElementById('form') as HTMLFormElement)) as unknown as FormState
Copy link
Contributor

Choose a reason for hiding this comment

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

Good old vanilla :)

}

export const getTradeParameters = (): TradeParameters => {
const {
slippageBps: _slippageBps,
chainId: _chainId,
sellToken: _sellToken,
buyToken: _buyToken,
amount: _amount,
kind,
} = getFormState()

const chainId: SupportedChainId = +_chainId
const isSell = kind === 'sell'
const sellToken = TOKENS[chainId].find((t) => t.address === _sellToken)
const buyToken = TOKENS[chainId].find((t) => t.address === _buyToken)
const decimals = isSell ? sellToken.decimals : buyToken.decimals
const multiplicator = decimals > 3 ? 3 : 0
const amount = BigInt(+_amount * 10 ** multiplicator) * BigInt(10 ** (decimals - multiplicator))
Copy link
Contributor

Choose a reason for hiding this comment

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

I didn't get what you want to do here

const slippageBps = _slippageBps ? +_slippageBps : undefined

return {
sellToken: sellToken.address,
sellTokenDecimals: sellToken.decimals,
buyToken: buyToken.address,
buyTokenDecimals: buyToken.decimals,
amount: amount.toString(),
slippageBps,
kind: isSell ? OrderKind.SELL : OrderKind.BUY,
}
}
Loading
Loading