Skip to content

Commit

Permalink
feat(wallet): introduce swap protocol fees
Browse files Browse the repository at this point in the history
  • Loading branch information
onyb committed Aug 23, 2023
1 parent 282a459 commit 1360135
Show file tree
Hide file tree
Showing 17 changed files with 764 additions and 197 deletions.
6 changes: 4 additions & 2 deletions components/brave_wallet/browser/brave_wallet_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,7 @@ constexpr webui::LocalizedString kLocalizedStrings[] = {
IDS_BRAVE_SWAP_MINIMUM_RECEIVED_AFTER_SLIPPAGE},
{"braveSwapNetworkFee", IDS_BRAVE_SWAP_NETWORK_FEE},
{"braveSwapBraveFee", IDS_BRAVE_SWAP_BRAVE_FEE},
{"braveSwapProtocolFee", IDS_BRAVE_SWAP_PROTOCOL_FEE},
{"braveSwapFree", IDS_BRAVE_SWAP_FREE},
{"braveSwapLiquidityProvider", IDS_BRAVE_SWAP_LIQUIDITY_PROVIDER},
{"braveSwapSwapAndSend", IDS_BRAVE_SWAP_SWAP_AND_SEND},
Expand Down Expand Up @@ -1329,7 +1330,8 @@ constexpr char kOptimismSwapBaseAPIURL[] = "https://optimism.api.0x.org/";
constexpr char kArbitrumSwapBaseAPIURL[] = "https://arbitrum.api.0x.org/";
constexpr char kBaseSwapBaseAPIURL[] = "https://base.api.0x.org/";
constexpr char kSwapBaseAPIURL[] = "https://api.0x.org/";
constexpr char kBuyTokenPercentageFee[] = "0.00875";
constexpr double k0xBuyTokenFeePercentage = 0.875;
constexpr double k0xProtocolFeePercentage = 0.15;
constexpr char kEVMFeeRecipient[] =
"0xbd9420A98a7Bd6B89765e5715e169481602D9c3d";
constexpr char kAffiliateAddress[] =
Expand All @@ -1338,7 +1340,7 @@ constexpr char k0xAPIKeyHeader[] = "0x-api-key";

// Jupiter swap constants
constexpr char kSolanaSwapBaseAPIURL[] = "https://quote-api.jup.ag/";
constexpr char kSolanaBuyTokenFeeBps[] = "85";
constexpr double kSolanaBuyTokenFeePercentage = 0.85;
constexpr char kSolanaFeeRecipient[] =
"3NUW8hWoCnLgJwWCVnwdFo2Dsz8bKwLac9A3VgS2jLUQ";

Expand Down
5 changes: 3 additions & 2 deletions components/brave_wallet/browser/swap_request_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
namespace brave_wallet {

absl::optional<std::string> EncodeJupiterTransactionParams(
mojom::JupiterSwapParamsPtr params) {
mojom::JupiterSwapParamsPtr params,
bool has_fee) {
DCHECK(params);
base::Value::Dict tx_params;

Expand All @@ -35,7 +36,7 @@ absl::optional<std::string> EncodeJupiterTransactionParams(
// If the if-condition below is false, associated_token_account is unused,
// but the originating call to SolanaKeyring::GetAssociatedTokenAccount()
// is still done to ensure output_mint is always valid.
if (HasJupiterFeesForTokenMint(params->output_mint)) {
if (has_fee) {
// feeAccount is the ATA account for the output mint where the fee will be
// sent to.
tx_params.Set("feeAccount", *associated_token_account);
Expand Down
3 changes: 2 additions & 1 deletion components/brave_wallet/browser/swap_request_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
namespace brave_wallet {

absl::optional<std::string> EncodeJupiterTransactionParams(
mojom::JupiterSwapParamsPtr params);
mojom::JupiterSwapParamsPtr params,
bool has_fee);

} // namespace brave_wallet

Expand Down
11 changes: 7 additions & 4 deletions components/brave_wallet/browser/swap_request_helper_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ TEST(SwapRequestHelperUnitTest, EncodeJupiterTransactionParams) {
params.route = swap_quote->routes.at(0).Clone();
params.user_public_key = "mockPubKey";
params.output_mint = "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"; // USDC
auto encoded_params = EncodeJupiterTransactionParams(params.Clone());
auto encoded_params = EncodeJupiterTransactionParams(params.Clone(), true);

std::string expected_params(R"(
{
Expand Down Expand Up @@ -115,7 +115,7 @@ TEST(SwapRequestHelperUnitTest, EncodeJupiterTransactionParams) {

// OK: Jupiter transaction params WITHOUT feeAccount
params.output_mint = "SHDWyBxihqiCj6YekG2GUr7wqKLeLAMK1gHZck9pL6y"; // SHDW
encoded_params = EncodeJupiterTransactionParams(params.Clone());
encoded_params = EncodeJupiterTransactionParams(params.Clone(), false);
expected_params = R"(
{
"route": {
Expand Down Expand Up @@ -156,11 +156,14 @@ TEST(SwapRequestHelperUnitTest, EncodeJupiterTransactionParams) {
ASSERT_EQ(*encoded_params, GetJSON(expected_params_value));

// KO: empty params
EXPECT_DCHECK_DEATH(EncodeJupiterTransactionParams(nullptr));
EXPECT_DCHECK_DEATH(EncodeJupiterTransactionParams(nullptr, true));
EXPECT_DCHECK_DEATH(EncodeJupiterTransactionParams(nullptr, false));

// KO: invalid output mint
params.output_mint = "invalid output mint";
encoded_params = EncodeJupiterTransactionParams(params.Clone());
encoded_params = EncodeJupiterTransactionParams(params.Clone(), true);
ASSERT_EQ(encoded_params, absl::nullopt);
encoded_params = EncodeJupiterTransactionParams(params.Clone(), false);
ASSERT_EQ(encoded_params, absl::nullopt);
}
} // namespace brave_wallet
44 changes: 43 additions & 1 deletion components/brave_wallet/browser/swap_response_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,33 @@ absl::optional<double> ParsePriceImpactPct(const base::Value& value) {

namespace brave_wallet {

namespace {
mojom::ZeroExFeePtr ParseZeroExFee(const base::Value& value) {
if (value.is_none()) {
return nullptr;
}

if (!value.is_dict()) {
return nullptr;
}

auto zero_ex_fee_value =
swap_responses::ZeroExFee::FromValue(value.GetDict());
if (!zero_ex_fee_value) {
return nullptr;
}

auto zero_ex_fee = mojom::ZeroExFee::New();
zero_ex_fee->fee_type = zero_ex_fee_value->fee_type;
zero_ex_fee->fee_token = zero_ex_fee_value->fee_token;
zero_ex_fee->fee_amount = zero_ex_fee_value->fee_amount;
zero_ex_fee->billing_type = zero_ex_fee_value->billing_type;

return zero_ex_fee;
}

} // namespace

mojom::SwapResponsePtr ParseSwapResponse(const base::Value& json_value,
bool expect_transaction_data) {
// {
Expand Down Expand Up @@ -81,7 +108,15 @@ mojom::SwapResponsePtr ParseSwapResponse(const base::Value& json_value,
// "name": "Curve",
// "proportion": "0",
// }
// ]
// ],
// "fees": {
// "zeroExFee": {
// "feeType": "volume",
// "feeToken": "0x8f3cf7ad23cd3cadbd9735aff958023239c6a063",
// "feeAmount": "148470027512868522",
// "billingType": "on-chain"
// }
// }
// }

auto swap_response_value =
Expand Down Expand Up @@ -134,6 +169,13 @@ mojom::SwapResponsePtr ParseSwapResponse(const base::Value& json_value,
mojom::ZeroExSource::New(source_value.name, source_value.proportion));
}

auto fees = mojom::ZeroExFees::New();
if (auto zero_ex_fee = ParseZeroExFee(swap_response_value->fees.zero_ex_fee);
zero_ex_fee) {
fees->zero_ex_fee = std::move(zero_ex_fee);
}
swap_response->fees = std::move(fees);

return swap_response;
}

Expand Down
Loading

0 comments on commit 1360135

Please sign in to comment.