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

Add Solana priority fees #23214

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open

Add Solana priority fees #23214

wants to merge 23 commits into from

Conversation

nvonpentz
Copy link
Member

@nvonpentz nvonpentz commented Apr 22, 2024

Resolves brave/brave-browser#35866

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 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:

@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 labels Apr 22, 2024
@nvonpentz nvonpentz force-pushed the solana-priority-fees branch 3 times, most recently from d9d87c2 to a9c030f Compare April 29, 2024 22:58
@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

@nvonpentz nvonpentz marked this pull request as ready for review May 2, 2024 14:09
@nvonpentz nvonpentz requested review from a team as code owners May 2, 2024 14:09
@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

@brave-builds
Copy link
Collaborator

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

Copy link
Collaborator

@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.

desktop frontend ++

@brave-builds
Copy link
Collaborator

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

Copy link
Collaborator

@StephenHeaps StephenHeaps left a comment

Choose a reason for hiding this comment

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

iOS ++

@@ -2382,6 +2392,8 @@ struct SolanaTxData {
// signAllTransactions provider APIs, which includes serialized message and
// signatures from partial_sign.
SolanaSignTransactionParam? sign_transaction_param;

SolanaGasEstimation? gas_estimation;
Copy link
Member

Choose a reason for hiding this comment

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

nit: let's use fee estimation instead.

@@ -20,6 +20,9 @@ class PrefService;

namespace brave_wallet {

// https://docs.rs/solana-program/1.18.10/src/solana_program/clock.rs.html#129-131
extern const int kValidBlockHeightThreshold;
Copy link
Member

Choose a reason for hiding this comment

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

Let's move the constant to a shared file rather than use extern here, and rename to kSolanaValidBlockHeightThreshold if needed.

@@ -155,6 +161,9 @@ class SolanaTransaction {
// Currently might be specified by solana.signAndSendTransaction provider
// API as the options to be passed to sendTransaction RPC call.
std::optional<SendOptions> send_options_;

// Gas estimation result
mojom::SolanaGasEstimationPtr gas_estimation_;
Copy link
Member

Choose a reason for hiding this comment

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

add a check in SolanaTransaction::operator==

Copy link
Member Author

Choose a reason for hiding this comment

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

This was addressed after you wrote your comment but before it was posted to the pull request.

Comment on lines 1883 to 1884
GetSolanaGasEstimation(string chain_id, string tx_meta_id)
=> (SolanaGasEstimation estimation, SolanaProviderError error, string error_message);
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need two instead of just changing fee to the new struct in GetEstimatedTxFee?

Comment on lines 777 to 795
// Rearrange the static account keys and generate a new SolanaMessageHeader
uint16_t num_required_signatures = 0;
uint16_t num_readonly_signed_accounts = 0;
uint16_t num_readonly_unsigned_accounts = 0;
std::vector<SolanaAccountMeta> unique_account_metas;
GetUniqueAccountMetas(fee_payer_, instructions_, &unique_account_metas);
std::vector<SolanaAddress> static_accounts;
for (const auto& meta : unique_account_metas) {
auto addr = SolanaAddress::FromBase58(meta.pubkey);
if (!addr) {
return false;
}

if (meta.is_signer) {
num_required_signatures++;
}
if (meta.is_signer && !meta.is_writable) {
num_readonly_signed_accounts++;
}
if (!meta.is_signer && !meta.is_writable) {
num_readonly_unsigned_accounts++;
}

if (num_required_signatures > UINT8_MAX ||
num_readonly_signed_accounts > UINT8_MAX ||
num_readonly_unsigned_accounts > UINT8_MAX ||
static_accounts.size() == UINT8_MAX) {
return false;
}
static_accounts.emplace_back(*addr);
}
static_account_keys_ = static_accounts;

Copy link
Member

Choose a reason for hiding this comment

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

We should extract and share code in CreateLegacyMessage rather than copy the logic.

@nvonpentz nvonpentz marked this pull request as draft May 14, 2024 00:12
@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

* Fix to support AddPriorityFEe for v0 transactions

* Test case for AddPriorityFee for v0 transactions

* Test case for UsesPriorityFee
* If fetching the base fee fails, don't set a gas estimate or modify the
  priority fee instructions, but still add the transaction. The client
  will need to fetch the estimate again on it's own as it currently
  does.

* If only setting the priority fee fails (e.g. simulation), we set the
  gas estimate, but don't modify the instructions. We do this because
  the frontend can still use base fee from the estimate, but we don't
  have enough info to set the priority fee instruction.
Copy link
Contributor

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

Here is a review of the pull request:

Description

This PR makes several changes to improve Solana transaction handling in Brave Wallet:

  • Adds support for fetching fee estimations for Solana transactions, including simulating transactions to get compute unit usage estimates and fetching recent prioritization fees.
  • Updates the Solana transaction manager to automatically add compute budget instructions and priority fee to transactions when possible.
  • Propagates the fee estimation results through the UI.

The motivation seems to be to provide more accurate and complete fee estimations for Solana transactions and enable users to add priority fees.

Changes

Changes

Changes are organized by area:

Core:

  • brave_wallet_constants.h: Adds some new Solana-related constants
  • json_rpc_service.cc/h: Implements new methods to simulate Solana transactions and get recent prioritization fees
  • solana_instruction_builder.cc/h: Adds functions to create Solana compute budget program instructions
  • solana_instruction_data_decoder.cc/h: Adds function to get the type of a compute budget instruction
  • solana_message.cc/h: Updates to support adding priority fee instructions to a Solana message
  • solana_requests.cc/h: Adds requests to simulate a transaction and get recent prioritization fees
  • solana_response_parser.cc/h: Adds functions to parse responses for simulating transactions and getting recent fees
  • solana_transaction.cc/h: Updates serialization to include fee estimation results
  • solana_tx_manager.cc/h: Major changes to fetch fee estimations when adding unapproved transactions. Will automatically add compute budget instructions and priority fee if possible.

Tests:

  • brave_wallet/browser/*_unittest.cc files: Adds unit tests for the new functionality
  • brave_wallet_p3a_unittest.cc: Updates test expectations
  • wallet_button_notification_source_browsertest.cc: Makes test transaction more realistic

UI:

  • Updates mock bridge data to match new fee estimation format
  • Updates transaction confirmation to calculate total fee from base fee and priority fee
  • Updates stores and extensions to use new solanaTxFeeEstimation method

Security Hotspots

No major security issues identified. The changes primarily impact already-existing Solana transaction handling rather than opening up new attack surface.

One note is that user-provided compute unit limits in transactions are now respected. However, this seems to be the intended behavior, and there are still limits in place (max transaction size, etc).

Let me know if you have any other questions!

@brave-builds
Copy link
Collaborator

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

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
5 participants