-
Notifications
You must be signed in to change notification settings - Fork 9
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
shoom3301
wants to merge
32
commits into
main
Choose a base branch
from
feat/swap-for-people
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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 e8927e2
feat: SDK extension for people to trade
shoom3301 59f550b
refactor: move files to trading dir
shoom3301 ee092bb
feat: js example for trade-sdk
shoom3301 775e687
chore: trade sdk examples
shoom3301 bbece96
docs: trading SDK docs
shoom3301 f27a94d
chore: encapsulate networkCostsAmount
shoom3301 d98230a
Merge branch 'main' of https://github.com/cowprotocol/cow-sdk into fe…
shoom3301 f82841d
chore: fix build
shoom3301 5639517
chore: fix build
shoom3301 92c7d14
Merge branch 'main' of https://github.com/cowprotocol/cow-sdk into fe…
shoom3301 6896b20
Merge branch 'main' of https://github.com/cowprotocol/cow-sdk into fe…
shoom3301 94b21ff
chore: make networkCostsAmount optional
shoom3301 bf87d83
feat: calculate unique OrderId util
shoom3301 9c23fae
feat: support on-chain trades
shoom3301 6fcff58
chore: reduce getOrderToSign parameters
shoom3301 490e7d4
Merge branch 'main' of https://github.com/cowprotocol/cow-sdk into fe…
shoom3301 4a1f35e
feat: support on-chain orders
shoom3301 240ebfb
test: app data utils
shoom3301 a8d1ec9
test: test calculateUniqueOrderId
shoom3301 59fd04b
test: test getOrderToSign
shoom3301 6351ae7
test: test getQuote
shoom3301 56692ab
test: test postCoWProtocolTrade
shoom3301 9d828bf
test: test postLimitOrder
shoom3301 f106189
test: test postOnChainTrade
shoom3301 99a1cad
docs: docs for postOnChainTrade
shoom3301 5698598
Merge branch 'main' of https://github.com/cowprotocol/cow-sdk into fe…
shoom3301 03d212e
chore: up docs
shoom3301 867b05e
fix: update eth-flow addresses
shoom3301 62ee519
chore: fix tests
shoom3301 4f788ed
chore: export helpers
shoom3301 953f848
chore: rc version
shoom3301 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ | |
"isolatedModules": true, | ||
"noEmit": true, | ||
"jsx": "react-jsx" | ||
}, | ||
}, | ||
"include": [ | ||
"src" | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}, | ||
}, | ||
}, | ||
} | ||
) | ||
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good old vanilla :)