-
Notifications
You must be signed in to change notification settings - Fork 873
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(wallet): migrate 0x Swap API to v2 #26231
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.
++
@@ -448,74 +448,71 @@ union SwapTransactionParamsUnion { | |||
|
|||
union SwapQuoteUnion { | |||
JupiterQuote jupiter_quote; | |||
ZeroExQuote zero_ex_quote; | |||
ZeroExQuoteInfo zero_ex_quote; |
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.
should we change this to zero_ex_quote_info
to distinguish from ZeroExQuote
? it seems ZeroExQuoteInfo
now contains an optional of ZeroExQuote
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.
The refactoring here should fix this.
// 0x specific validations | ||
if (quoteUnion?.zeroExQuote?.liquidityAvailable === false) { | ||
return 'insufficientLiquidity' | ||
} | ||
|
||
if (quoteErrorUnion?.zeroExError) { |
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.
In this case, shouldn't quoteErrorUnion.zeroExError. isInsufficientLiquidity == true
?
checking both QuoteUnion
and QuoteErrorUnion
for errors seems odd to me. Also the quoteErrorUnion.zeroExError.isInsufficientAllowance
case seems to be ignored.
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 was actually done because 0x API v2 now returns isInsufficientLiquidity
as part of the quote instead of an error response. But now that I think about it, retaining the old interface is cleaner. You can find this change here: review(nuo-xu): reuse ZeroExQuote and drop ZeroExQuoteInfo struct
Also the quoteErrorUnion.zeroExError.isInsufficientAllowance case seems to be ignored
Allowance checks are no longer returned as part of the error response from the API. If this changes in future, will add this back. Meanwhile, the desktop code already handles allowance checks just like LiFi and Squid.
1471c24
to
1bbee57
Compare
A Storybook has been deployed to preview UI for the latest push |
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.
wallet core lgtm
}; | ||
|
||
struct ZeroExFees { | ||
ZeroExFee? zero_ex_fee; | ||
}; | ||
|
||
struct ZeroExQuote { | ||
string price; |
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.
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.
Is it possible to infer this on this client-side like we do on desktop?
brave-core/components/brave_wallet_ui/page/screens/swap/swap.utils.ts
Lines 83 to 85 in 369f9d6
rate: new Amount(quote.buyAmount) | |
.divideByDecimals(toToken.decimals) | |
.div(new Amount(quote.sellAmount).divideByDecimals(fromToken.decimals)), |
In the backend, we no longer have this field and we don't know about the buy/sell token decimals to be able to correctly compute the rate.
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.
we have no problem with sell amount and buy amount. just the market price for the sell token.
i think iOS can do one of the following:
1. fetch market price for sell token using assetRatioService
api instead of from quote response.
2. remove this market price label but need to move that refresh button else where for users to refetch quote.
cc @jamesmudgett and design @kleantzogu
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.
My understanding is that "Market Price in ETH" is the exchange rate between buyToken
and sellToken
. I don't think using the assetRatioService
will be correct here, since it's the CEX/global exchange rate and not the one as per the AMM swap quote.
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.
you are right. i think this leave us removing that label. i just need to figure out where to put that refetch button.
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.
Highlighting again (see #26231 (comment)) that the "Market price in <sellToken>" can be determined using buyAmount
and sellAmount
formatted with their respective decimals.
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.
Note that it's "Market price in ETH" and not "Market price of ETH".
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.
ah! i mistakenly thought the sell amount and buy amount are from the ZeroExTransaction
. ok yeah i think we can using those two values to determine the market price. Thanks!
[puLL-Merge] - brave/brave-core@26231 DescriptionThis PR updates the 0x API integration in the Brave Wallet to use the newer allowance-holder API. It modifies the request and response handling for quotes and transactions, updates the data models, and adjusts the UI components to work with the new data structure. Possible Issues
Security Hotspots
ChangesChangescomponents/brave_wallet/browser/brave_wallet_constants.cc:
components/brave_wallet/browser/brave_wallet_constants.h:
components/brave_wallet/browser/swap_response_parser.cc:
components/brave_wallet/browser/swap_response_parser.h:
components/brave_wallet/browser/swap_response_parser_unittest.cc:
components/brave_wallet/browser/swap_service.cc:
components/brave_wallet/browser/swap_service.h:
components/brave_wallet/browser/swap_service_unittest.cc:
components/brave_wallet/common/brave_wallet.mojom:
components/brave_wallet_ui/common/async/mocks/bridge.ts:
components/brave_wallet_ui/page/screens/swap/hooks/useSwap.ts:
components/brave_wallet_ui/page/screens/swap/hooks/useZeroEx.ts:
components/brave_wallet_ui/page/screens/swap/swap.utils.ts:
ios/brave-ios/Sources/BraveWallet/Crypto/Stores/SwapTokenStore.swift:
ios/brave-ios/Sources/BraveWallet/Preview MockContent.swift:
ios/brave-ios/Sources/BraveWallet/Preview MockSwapService.swift:
ios/brave-ios/Tests/BraveWalletTests/SwapTokenStoreTests.swift:
|
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.
proportionBps: "10000" | ||
)] | ||
), | ||
sellAmount: "1000000000000000000", |
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.
nite: restore the 1 ETH
comment
@StephenHeaps Yes this is expected. The contracts are not public yet, so I'm unable to add the parsers right now. Will follow it up in a future PR. |
Released in v1.75.1 |
Resolves brave/brave-browser#41891
Submitter Checklist:
QA/Yes
orQA/No
;release-notes/include
orrelease-notes/exclude
;OS/...
) to the associated issuenpm run test -- brave_browser_tests
,npm run test -- brave_unit_tests
wikinpm run presubmit
wiki,npm run gn_check
,npm run tslint
git rebase master
(if needed)Reviewer Checklist:
gn
After-merge Checklist:
changes has landed on
Test Plan:
See STR in linked issue