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

chore(wallet): refactor swap service interface #21406

Merged
merged 6 commits into from
Dec 20, 2023

Conversation

onyb
Copy link
Member

@onyb onyb commented Dec 15, 2023

No functional changes in this PR, just some refactoring of the SwapService interface.

Resolves brave/brave-browser#34932

Submitter Checklist:

  • I confirm that no security/privacy review is needed and no other type of reviews are needed, or that I have requested them
  • There is a ticket for my issue
  • Used Github auto-closing keywords in the PR description above
  • Wrote a good PR/commit description
  • Squashed any review feedback or "fixup" commits before merge, so that history is a record of what happened in the repo, not your PR
  • Added appropriate labels (QA/Yes or QA/No; release-notes/include or release-notes/exclude; OS/...) to the associated issue
  • Checked the PR locally:
    • npm run test -- brave_browser_tests, npm run test -- brave_unit_tests wiki
    • npm run lint, npm run presubmit wiki, npm run gn_check, npm run tslint
  • Ran git rebase master (if needed)

Reviewer Checklist:

  • A security review is not needed, or a link to one is included in the PR description
  • New files have MPL-2.0 license header
  • Adequate test coverage exists to prevent regressions
  • Major classes, functions and non-trivial code blocks are well-commented
  • Changes in component dependencies are properly reflected in gn
  • Code follows the style guide
  • Test plan is specified in PR before merging

After-merge Checklist:

Test Plan:

N/A

@onyb onyb self-assigned this Dec 15, 2023
@onyb onyb requested review from a team as code owners December 15, 2023 19:24
@github-actions github-actions bot added CI/storybook-url Deploy storybook and provide a unique URL for each build feature/web3/wallet feature/web3/wallet/core puLL-Merge labels Dec 15, 2023
@@ -138,7 +139,7 @@ export function useJupiter(params: SwapParams) {
outputToken:
overriddenParams.toToken.contractAddress ||
WRAPPED_SOL_CONTRACT_ADDRESS,
taker: overriddenParams.fromAddress
taker: overriddenParams.fromAccountId.address
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's get address from AccountInfo. Address field in AccountId was/is a temporary measure and to be removed soon™

const fromAccount = useAccountQuery(fromAccountId)
fromAccount.address

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed here, but without the useAccountQuery() hook.

@brave-builds
Copy link
Collaborator

A Storybook has been deployed to preview UI for the latest push

@brave-builds
Copy link
Collaborator

A Storybook has been deployed to preview UI for the latest push

@brave-builds
Copy link
Collaborator

A Storybook has been deployed to preview UI for the latest push

@onyb onyb force-pushed the f/wallet/swap-service-refactoring branch from 63d0349 to bea0de1 Compare December 18, 2023 17:48
Copy link
Contributor

@josheleonard josheleonard left a comment

Choose a reason for hiding this comment

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

frontend ++

@brave-builds
Copy link
Collaborator

A Storybook has been deployed to preview UI for the latest push

Copy link
Collaborator

@supermassive supermassive left a comment

Choose a reason for hiding this comment

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

wallet core LGTM.
Added some minor notes.

Copy link
Contributor

[puLL-Merge] - brave/brave-core@21406

Description

The PR introduces a change in the wallet swap feature within the Brave browser for both Android and Java components. Specifically, it involves refactoring the swap feature's implementation to use a new class name in obfuscation and introduces a new structure to handle swap transaction parameters and responses more generically.

Changes

Changes

android/java/apk_for_test.flags

  • Renamed obfuscation class name from SwapParams to SwapQuoteParams to prevent obfuscation on that class as it seems to be required in a readable form for tests.

android/javatests/org/chromium/chrome/browser/brave_wallet/BraveWalletUtilsTest.java

  • Refactored unit tests to use the new SwapQuoteParams instead of the old SwapParams.
  • Added the new AccountId and RoutePriority imports as they're now required for SwapQuoteParams creation and assertions in tests.

components/brave_wallet/browser/swap_request_helper.cc and .h

  • Modified the EncodeJupiterTransactionParams function to take a const reference instead of a pointer, and removed the DCHECK macro call that was checking for a non-null pointer. This suggests a more C++ idiomatic way of handling non-optional parameters and avoiding raw pointers when possible.

components/brave_wallet/browser/swap_request_helper_unittest.cc

  • Updated unit tests to reflect the new function signature of EncodeJupiterTransactionParams.
  • Deleted DCHECK death tests as they're no longer relevant due to the removal of pointer parameters.

components/brave_wallet/browser/swap_response_parser.cc and .h

  • Renamed various functions to better reflect their purpose (ParseSwapResponse was changed to ParseZeroExQuoteResponse, etc.).
  • Updated the parameter and return types of various functions to match the new naming and structures introduced in the wallet's swap system.

components/brave_wallet/browser/swap_service.cc

  • Updated methods GetPriceQuoteURL and GetTransactionPayloadURL to GetZeroExQuoteURL and GetZeroExTransactionURL, reflecting the switch to the new SwapQuoteParams type.
  • Implemented GetTransaction and GetQuote methods to use the new SwapTransactionParamsUnion and SwapQuoteUnion types, offering a more flexible swap service interface that can handle different swap providers and transactions.

components/brave_wallet/common/brave_wallet.mojom

  • Added new enums and structs (RoutePriority, SwapQuoteParams, SwapTransactionParamsUnion, SwapQuoteUnion, SwapTransactionUnion, ZeroExQuote, ZeroExErrorItem, ZeroExError).
  • Removed old structures that have been superseded by the new types, like SwapParams and SwapResponse.

components/brave_wallet_ui/common/async/__mocks__/bridge.ts

  • Updated mock implementations to use the new naming conventions and types (mockZeroExQuote, mockZeroExTransaction).

components/brave_wallet_ui/page/screens/swap/constants/types.ts

  • Adjusted SwapParams type definition to replace fromAddress with fromAccount to reflect the new data structure used to represent account information.

components/brave_wallet_ui/page/screens/swap/hooks/useJupiter.ts and useZeroEx.ts

  • Refactored the hooks to use the new MOJO union types (SwapTransactionParamsUnion, SwapQuoteUnion), which support handling both Jupiter and ZeroEx swaps in a generic way.

Other Files

  • Changes in several files to accommodate the new data types and structures introduced in the MOJO definitions.

Security Hotspots

  • Modification of serialization and deserialization logic of transaction parameters could potentially introduce vulnerabilities if not properly handled, creating the risk of malformed transactions or transaction tampering.

Overall, the changes appear to focus on improving the swap system's extensibility, maintainability, and safety by using more robust data types and avoiding raw pointers. The removal of DCHECK macros in favor of using references can also be considered a security and stability improvement, as it enforces the presence of valid objects at compile-time rather than runtime. However, care must be taken to ensure serialization and deserialization routines are robust and secure.

@brave-builds
Copy link
Collaborator

A Storybook has been deployed to preview UI for the latest push

Copy link
Collaborator

@simoarpe simoarpe left a comment

Choose a reason for hiding this comment

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

👍 Java parts look good!

Copy link
Contributor

@nuo-xu nuo-xu left a comment

Choose a reason for hiding this comment

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

iOS++

@onyb onyb merged commit 091ce3b into master Dec 20, 2023
17 checks passed
@onyb onyb deleted the f/wallet/swap-service-refactoring branch December 20, 2023 11:38
@github-actions github-actions bot added this to the 1.63.x - Nightly milestone Dec 20, 2023
kjozwiak pushed a commit that referenced this pull request Jan 9, 2024
* chore(wallet): refactor swap service interface (#21406)

* chore(wallet): migrate to Jupiter API v6 (#21475)

* chore(wallet): migrate to Jupiter API v6

* add referral account to collect platform fees

* switch to processed preflight commitment for swaps

* review(doug): remove unused dependencies in hook

* add production referral key

* Fixes BraveWalletUtilsTest Android unit tests

---------

Co-authored-by: Serg <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI/storybook-url Deploy storybook and provide a unique URL for each build feature/web3/wallet/core feature/web3/wallet puLL-Merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor SwapService interface to allow cross-chain swaps
6 participants