Skip to content

Commit

Permalink
feat(wallet): add fee waiver for swaps (uplift to 1.67.x) (#23975)
Browse files Browse the repository at this point in the history
feat(wallet): add fee waiver for swaps (#23960)

* feat(wallet): add fee waiver for swaps

* review(StephenHeaps): remove unused code
  • Loading branch information
onyb authored Jun 4, 2024
1 parent 18f8cbd commit 0392fdc
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 91 deletions.
16 changes: 0 additions & 16 deletions components/brave_wallet/browser/brave_wallet_constants.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,6 @@ GetEthBalanceScannerContractAddresses() {
return *contract_addresses;
}

bool HasJupiterFeesForTokenMint(const std::string& mint) {
static std::vector<std::string> mints(
{"So11111111111111111111111111111111111111112", // wSOL
"EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v", // USDC
"Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB", // USDT
"7vfCXTUXx5WJV5JADk17DUJ4ksgau7utNKj4b963voxs", // WETH (Wormhole)
"2FPyTwcZLUg1MDrwsyoP4D6s1tM7hAkHYRjkNb5w6Pxk", // ETH (Sollet)
"9n4nbM75f5Ui33ZbPYXn59EwSgE8CGsHtAeTH5YFeJ9E", // BTC (Sollet)
"qfnqNqs3nCAHjnyCgLRDbBtq4p2MtHZxw8YjSyYhPoL", // wWBTC (Wormhole)
"7dHbWXmci3dT8UFYWYZweBLXgycu7Y3iL6trKn1Y7ARj", // stSOL
"mSoLzYCxHdYgdzU16g5QSh3i5K3z3KZK7ytfqcJm7So", // mSOL
"FYpdBuyAHSbdaAyD1sKkxyLWbAP8uUW9h6uvdhK74ij1"}); // DAI

return base::Contains(mints, mint);
}

const std::vector<std::string>& GetEthSupportedNftInterfaces() {
static base::NoDestructor<std::vector<std::string>> interfaces({
kERC721InterfaceId,
Expand Down
4 changes: 0 additions & 4 deletions components/brave_wallet/browser/brave_wallet_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -1473,7 +1473,6 @@ inline constexpr char kZeroExBaseBaseAPIURL[] =
"https://base.api.0x.wallet.brave.com";
inline constexpr char kZeroExEthereumBaseAPIURL[] =
"https://api.0x.wallet.brave.com";
inline constexpr double kZeroExBuyTokenFeePercentage = 0.875;
inline constexpr char kEVMFeeRecipient[] =
"0xbd9420A98a7Bd6B89765e5715e169481602D9c3d";
inline constexpr char kAffiliateAddress[] =
Expand All @@ -1483,7 +1482,6 @@ inline constexpr char kZeroExNativeAssetContractAddress[] =

// Jupiter swap constants
inline constexpr char kJupiterBaseAPIURL[] = "https://jupiter.wallet.brave.com";
inline constexpr double kSolanaBuyTokenFeePercentage = 0.85;
inline constexpr char kJupiterReferralKey[] =
"7yke2kxg6ewNsun61qBkdsLdxuXcUiB8CMB47Zv39Aoy";
inline constexpr char kJupiterReferralProgram[] =
Expand All @@ -1505,7 +1503,6 @@ inline constexpr char kLiFiNativeEVMAssetContractAddress[] =
"0x0000000000000000000000000000000000000000";
inline constexpr char kLiFiNativeSVMAssetContractAddress[] =
"So11111111111111111111111111111111111111112";
inline constexpr double kLiFiFeePercentage = 0.875;

constexpr int64_t kBlockTrackerDefaultTimeInSeconds = 20;
constexpr int64_t kLogTrackerDefaultTimeInSeconds = 20;
Expand Down Expand Up @@ -1549,7 +1546,6 @@ const std::string GetSardineNetworkName(const std::string& chain_id);
const base::flat_map<std::string, std::string>& GetInfuraChainEndpoints();
const base::flat_map<std::string, std::string>&
GetEthBalanceScannerContractAddresses();
bool HasJupiterFeesForTokenMint(const std::string& mint);
const std::vector<std::string>& GetEthSupportedNftInterfaces();
// Returns the URL for the Ratios service.
const std::string GetAssetRatioBaseURL();
Expand Down
70 changes: 8 additions & 62 deletions components/brave_wallet/browser/swap_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,67 +99,13 @@ std::string GetAffiliateAddress(const std::string& chain_id) {
return "";
}

mojom::SwapFeesPtr GetJupiterSwapFee(const mojom::SwapQuoteParams& params) {
mojom::SwapFeesPtr GetZeroSwapFee() {
mojom::SwapFeesPtr response = mojom::SwapFees::New();
bool has_fees = HasJupiterFeesForTokenMint(
params.to_token.empty() ? kWrappedSolanaMintAddress : params.to_token);

auto fee_pct = kSolanaBuyTokenFeePercentage;
response->fee_pct = base::NumberToString(fee_pct);

auto discount = has_fees ? 0.0 : 100.0;
response->discount_pct = base::NumberToString(discount);

auto effective_fee_pct = (100.0 - discount) / 100.0 * fee_pct;
response->effective_fee_pct = base::NumberToString(effective_fee_pct);

// Jupiter swap fee is specified in basis points
response->fee_param =
effective_fee_pct != 0.0
? base::NumberToString(static_cast<int>(effective_fee_pct * 100.0))
: "";

response->discount_code =
has_fees ? mojom::SwapDiscountCode::kNone
: mojom::SwapDiscountCode::kUnknownJupiterOutputMint;

return response;
}

mojom::SwapFeesPtr GetLiFiSwapFee() {
mojom::SwapFeesPtr response = mojom::SwapFees::New();

response->fee_pct = base::NumberToString(kLiFiFeePercentage);

// We currently do not offer discounts on LiFi Brave fees.
response->fee_pct = "0";
response->discount_pct = "0";
response->effective_fee_pct = "0";
response->fee_param = "";
response->discount_code = mojom::SwapDiscountCode::kNone;
response->effective_fee_pct = response->fee_pct;

// LiFi swap fee is specified as a multiplier
response->fee_param = response->effective_fee_pct != "0"
? base::NumberToString(kLiFiFeePercentage / 100.0)
: "";

return response;
}

mojom::SwapFeesPtr GetZeroExSwapFee() {
mojom::SwapFeesPtr response = mojom::SwapFees::New();

response->fee_pct = base::NumberToString(kZeroExBuyTokenFeePercentage);

// We currently do not offer discounts on 0x Brave fees.
response->discount_pct = "0";
response->discount_code = mojom::SwapDiscountCode::kNone;
response->effective_fee_pct = response->fee_pct;

// 0x swap fee is specified as a multiplier
response->fee_param =
response->effective_fee_pct != "0"
? base::NumberToString(kZeroExBuyTokenFeePercentage / 100.0)
: "";

return response;
}

Expand Down Expand Up @@ -385,7 +331,7 @@ void SwapService::GetQuote(mojom::SwapQuoteParamsPtr params,

if (params->from_chain_id == params->to_chain_id &&
IsNetworkSupportedByZeroEx(params->from_chain_id)) {
auto swap_fee = GetZeroExSwapFee();
auto swap_fee = GetZeroSwapFee();
auto fee_param = swap_fee->fee_param;

auto internal_callback = base::BindOnce(
Expand All @@ -402,7 +348,7 @@ void SwapService::GetQuote(mojom::SwapQuoteParamsPtr params,

if (params->from_chain_id == params->to_chain_id &&
IsNetworkSupportedByJupiter(params->from_chain_id)) {
auto swap_fee = GetJupiterSwapFee(*params);
auto swap_fee = GetZeroSwapFee();
auto fee_param = swap_fee->fee_param;

auto internal_callback = base::BindOnce(
Expand All @@ -419,7 +365,7 @@ void SwapService::GetQuote(mojom::SwapQuoteParamsPtr params,

if (IsNetworkSupportedByLiFi(params->from_chain_id) &&
IsNetworkSupportedByLiFi(params->to_chain_id)) {
auto swap_fee = GetLiFiSwapFee();
auto swap_fee = GetZeroSwapFee();
auto fee_param = swap_fee->fee_param;

auto encoded_params = lifi::EncodeQuoteParams(std::move(params), fee_param);
Expand Down Expand Up @@ -548,7 +494,7 @@ void SwapService::GetTransaction(mojom::SwapTransactionParamsUnionPtr params,
auto conversion_callback = base::BindOnce(&ConvertAllNumbersToString);

if (params->is_zero_ex_transaction_params()) {
auto swap_fee = GetZeroExSwapFee();
auto swap_fee = GetZeroSwapFee();

auto internal_callback =
base::BindOnce(&SwapService::OnGetZeroExTransaction,
Expand Down
18 changes: 9 additions & 9 deletions components/brave_wallet/browser/swap_service_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -465,11 +465,11 @@ TEST_F(SwapServiceUnitTest, GetZeroExQuote) {
expected_zero_ex_quote->fees = std::move(fees);

auto expected_swap_fees = mojom::SwapFees::New();
expected_swap_fees->fee_pct = "0.875";
expected_swap_fees->fee_pct = "0";
expected_swap_fees->discount_pct = "0";
expected_swap_fees->effective_fee_pct = "0";
expected_swap_fees->discount_code = mojom::SwapDiscountCode::kNone;
expected_swap_fees->effective_fee_pct = "0.875";
expected_swap_fees->fee_param = "0.00875";
expected_swap_fees->fee_param = "";

base::MockCallback<mojom::SwapService::GetQuoteCallback> callback;
EXPECT_CALL(callback, Run(EqualsMojo(mojom::SwapQuoteUnion::NewZeroExQuote(
Expand Down Expand Up @@ -1090,11 +1090,11 @@ TEST_F(SwapServiceUnitTest, GetJupiterQuote) {
auto& expected_quote = params->get_jupiter_transaction_params()->quote;

auto expected_swap_fees = mojom::SwapFees::New();
expected_swap_fees->fee_pct = "0.85";
expected_swap_fees->fee_pct = "0";
expected_swap_fees->discount_pct = "0";
expected_swap_fees->effective_fee_pct = "0.85";
expected_swap_fees->effective_fee_pct = "0";
expected_swap_fees->discount_code = mojom::SwapDiscountCode::kNone;
expected_swap_fees->fee_param = "85";
expected_swap_fees->fee_param = "";

base::MockCallback<mojom::SwapService::GetQuoteCallback> callback;
EXPECT_CALL(callback, Run(EqualsMojo(mojom::SwapQuoteUnion::NewJupiterQuote(
Expand Down Expand Up @@ -1380,11 +1380,11 @@ TEST_F(SwapServiceUnitTest, GetLiFiQuote) {
)");

auto expected_swap_fees = mojom::SwapFees::New();
expected_swap_fees->fee_pct = "0.875";
expected_swap_fees->fee_pct = "0";
expected_swap_fees->discount_pct = "0";
expected_swap_fees->effective_fee_pct = "0.875";
expected_swap_fees->effective_fee_pct = "0";
expected_swap_fees->discount_code = mojom::SwapDiscountCode::kNone;
expected_swap_fees->fee_param = "0.00875";
expected_swap_fees->fee_param = "";

base::MockCallback<mojom::SwapService::GetQuoteCallback> callback;
EXPECT_CALL(
Expand Down

0 comments on commit 0392fdc

Please sign in to comment.