-
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
base: main
Are you sure you want to change the base?
Conversation
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.
I like where this is going.
However, I imagined this differently.
Here you make a call to postSwapOrder
once with the equivalent to what you input in the widget form, and it will post the order.
I think is great, but if someone needs to build a UI will need the quote information.
The problem I think is interesting to improve is, its freaking hard to come up with the parameters of the order as we send them to be signed.
This is why this should be broken in 2 steps:
-
- Get order params
-
- Post order
In (1) we will:
- Make a quote
- Take slippage into account
- prepare the app data
- handle eth flow
- return
- order params, AS WE SHOULD SIGN THEM in the wallet
- convenient info to show to the user
- Mimum received
- USD amounts
- fee estimation
- fee estimations in USD
- slippage calculations
- warnings of all sort! (let people to have good protection without effort)
- etc
In essence, for me the great contribution is implementing this (1). Maybe this is herlpul:
For (2), we just need to pass the order params from (1) and pass them along the signer
Just some final 💡 I'd like to add. This logic will be made into a library, which we can reuse anywhere. I'd like to use it in BFF and implement a simple endpoint, so it allows non JS projects to easily get the trading parameters. This way, we make integrations dead simple
Here are some related notes on some similar idea:
I just want to add a plausible roadmap of this: v1: Library This way we can provide "progressive integrations" Form left to right, you get more and more control. This way, we provide an easy way to start, and then ways to gain control |
👍 Dependency issues cleared. Learn more about Socket for GitHub ↗︎ This PR previously contained dependency changes with security issues that have been resolved, removed, or ignored. |
6fab215
to
ee092bb
Compare
…at/swap-for-people
…at/swap-for-people # Conflicts: # src/index.ts
…at/swap-for-people
…at/swap-for-people # Conflicts: # src/cow-shed/CoWShedHooks.ts # src/cow-shed/contracts.ts
@anxolin thanks for the feedback!
✅
✅
✅
✅
✅
✅
✅
❌
✅
❌
✅
❌ Points that are not done (❌) - we discussed them and decided to not implement them right now. |
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.
I'll continue reviewing in another moment cause I need to leave, so far looks great and easy to use.
} | ||
|
||
export const getFormState = (): FormState => { | ||
return Object.fromEntries(new FormData(document.getElementById('form') as HTMLFormElement)) as unknown as FormState |
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 :)
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 comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't get what you want to do here
@@ -27,10 +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.281.0/crates/orderbook/openapi.yml --output src/order-book/generated --exportServices false --exportCore false", | |||
"swagger:codegen": " openapi --input https://raw.githubusercontent.com/cowprotocol/services/main/crates/orderbook/openapi.yml --output src/order-book/generated --exportServices false --exportCore false", |
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.
not sure @alfetopito will like this
@@ -95,7 +96,7 @@ export abstract class ConditionalOrder<D, S> { | |||
assertIsValid(): void { | |||
const isValidResult = this.isValid() | |||
if (!isValidResult.isValid) { | |||
throw new Error(`Invalid order: ${isValidResult.reason}`) | |||
throw new Error(`Invalid order: ${(isValidResult as IsNotValid).reason}`) |
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.
Probably this cast has to be done cause we didn't use type assertions. Not from this PR though
* @param {Order} order order to sign | ||
* @param {Pick<OrderUidParams, 'owner'>} params order unique identifier parameters. | ||
*/ | ||
export async function generateOrderId( |
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.
I would swear we had already one piece of code doing this. I remember in the watch-tower and SDK deriving the orderId form the order data
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.
This is what I meant:
cow-sdk/src/composable/ConditionalOrder.ts
Line 286 in 97d0315
const orderUid = await computeOrderUid(chainId, owner, fromStructToOrder(order)) |
Report too large to display inline |
See https://github.com/cowprotocol/cow-sdk/blob/56985986c7bf3585eed85d5b71e221f69ed763a2/src/trading/README.md
The shortest way to post an order
Get quote -> confirm -> post order