Skip to content

Commit

Permalink
chore: blowfish enums (#19960)
Browse files Browse the repository at this point in the history
  • Loading branch information
josheleonard authored Oct 11, 2023
1 parent 1bb725c commit 2061576
Show file tree
Hide file tree
Showing 9 changed files with 1,334 additions and 518 deletions.
583 changes: 555 additions & 28 deletions components/brave_wallet/browser/simulation_response_parser.cc

Large diffs are not rendered by default.

236 changes: 153 additions & 83 deletions components/brave_wallet/browser/simulation_response_parser_unittest.cc

Large diffs are not rendered by default.

188 changes: 174 additions & 14 deletions components/brave_wallet/browser/simulation_responses.idl
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,129 @@

// Dummy comments to make the linter happy.
namespace simulation_responses {

enum WarningSeverity {
CRITICAL,
WARNING
};

// UI should defer to the supplied message if the kind isn't recognized
enum WarningKind {
APPROVAL_TO_E_O_A,
BLOCKLISTED_DOMAIN_CROSS_ORIGIN,
BULK_APPROVALS_REQUEST,
COMPROMISED_AUTHORITY_UPGRADE,
COPY_CAT_DOMAIN,
COPY_CAT_IMAGE_UNRESPONSIVE_DOMAIN,
DANGLING_APPROVAL,
DEVTOOLS_DISABLED,
ETH_SIGN_TX_HASH,
KNOWN_MALICIOUS,
MAINNET_REPLAY_POSSIBLE,
MULTI_COPY_CAT_DOMAIN,
NEW_DOMAIN,
NON_ASCII_URL,
OBFUSCATED_CODE,
PERMIT_NO_EXPIRATION,
PERMIT_UNLIMITED_ALLOWANCE,
POISONED_ADDRESS,
REFERENCED_OFAC_ADDRESS,
SEMI_TRUSTED_BLOCKLIST_DOMAIN,
SET_OWNER_AUTHORITY,
SUSPECTED_MALICIOUS,
TOO_MANY_TRANSACTIONS,
TRADE_FOR_NOTHING,
TRANSFERRING_ERC20_TO_OWN_CONTRACT,
TRUSTED_BLOCKLIST_DOMAIN,
UNLIMITED_ALLOWANCE_TO_NFTS,
UNUSUAL_GAS_CONSUMPTION,
USER_ACCOUNT_OWNER_CHANGE,
WHITELISTED_DOMAIN_CROSS_ORIGIN
};

enum EVMRawInfoKind {
ERC20_TRANSFER,
ERC20_APPROVAL,
NATIVE_ASSET_TRANSFER,
ERC721_TRANSFER,
ERC721_APPROVAL,
ERC721_APPROVAL_FOR_ALL,
ERC1155_TRANSFER,
ERC1155_APPROVAL_FOR_ALL,
ANY_NFT_FROM_COLLECTION_TRANSFER
};

enum SolanaRawInfoKind {
SOL_TRANSFER,
SPL_TRANSFER,
SPL_APPROVAL,
SOL_STAKE_AUTHORITY_CHANGE,
USER_ACCOUNT_OWNER_CHANGE
};

enum MetaplexTokenStandardKind {
non_fungible,
fungible_asset,
fungible,
non_fungible_edition,
unknown
};

enum DiffSign {
PLUS,
MINUS
};

enum EVMAddressKind {
ACCOUNT
};

enum AssetPriceSource {
Simplehash,
Defillama,
Coingecko
};

enum EVMErrorKind {
TRANSACTION_REVERTED,
TRANSACTION_ERROR,
SIMULATION_FAILED,
UNKNOWN_ERROR
};

enum SuggestedColor {
CREDIT,
DEBIT
};

dictionary SolanaPrice {
DOMString source;
AssetPriceSource source;

// FIXME: The fields below should be in camelCase. Needs an upstream fix.
DOMString last_updated_at;
DOMString dollar_value_per_token;
};

dictionary Price {
DOMString source;
AssetPriceSource source;
DOMString updatedAt;
DOMString dollarValuePerToken;
};

dictionary Warning {
DOMString severity;
DOMString kind;
WarningSeverity severity;
WarningKind kind;
DOMString message;
};

dictionary EVMError {
DOMString kind;
EVMErrorKind kind;
DOMString humanReadableError;
};

dictionary EVMContract {
EVMAddressKind kind;
DOMString address;
DOMString kind;
};

dictionary EVMAmount {
Expand Down Expand Up @@ -144,7 +239,7 @@ namespace simulation_responses {
};

dictionary EVMStateChangeRawInfo {
DOMString kind;
EVMRawInfoKind kind;

// data is a non-nullable union of the following types:
// - ERC20TransferData
Expand All @@ -171,13 +266,13 @@ namespace simulation_responses {
};

dictionary EVMSimulationResponse {
DOMString action;
DOMString action; // BLOCK | WARN | NONE
Warning[] warnings;
EVMSimulationResults simulationResults;
};

dictionary SolanaDiff {
DOMString sign;
DiffSign sign;
DOMString digits;
};

Expand All @@ -187,6 +282,71 @@ namespace simulation_responses {
};

dictionary SolanaError {
// ACCOUNT_DOES_NOT_HAVE_ENOUGH_SOL_TO_PERFORM_THE_OPERATION
// ACCOUNT_DOES_NOT_SUPPORT_SPECIFIED_AUTHORITY_TYPE
// ACCOUNT_IN_USE
// ACCOUNT_IS_FROZEN
// ACCOUNT_LOADED_TWICE
// ACCOUNT_NOT_ASSOCIATED_WITH_THIS_MINT
// ADVANCING_STORED_NONCE_REQUIRES_A_POPULATED_RECENTBLOCKHASHES_SYSVAR
// ALREADY_IN_USE
// AN_ACCOUNT_WITH_THE_SAME_ADDRESS_ALREADY_EXISTS
// ATTEMPT_TO_DEBIT_AN_ACCOUNT_BUT_FOUND_NO_RECORD_OF_A_PRIOR_CREDIT
// ATTEMPT_TO_LOAD_A_PROGRAM_THAT_DOES_NOT_EXIST
// BAD_REQUEST
// BLOCKHASH_NOT_FOUND
// CANNOT_ALLOCATE_ACCOUNT_DATA_OF_THIS_LENGTH
// CANNOT_ASSIGN_ACCOUNT_TO_THIS_PROGRAM_ID
// ERROR_PROCESSING_INSTRUCTION_{0}:_{1}
// FIXED_SUPPLY
// INSTRUCTION_DOES_NOT_SUPPORT_NATIVE_TOKENS
// INSTRUCTION_DOES_NOT_SUPPORT_NON-NATIVE_TOKENS
// INSUFFICIENT_FUNDS
// INSUFFICIENT_FUNDS_FOR_FEE
// INVALID_INSTRUCTION
// INVALID_MINT
// INVALID_NUMBER_OF_PROVIDED_SIGNERS
// INVALID_NUMBER_OF_REQUIRED_SIGNERS
// LAMPORT_BALANCE_BELOW_RENT-EXEMPT_THRESHOLD
// LENGTH_OF_REQUESTED_SEED_IS_TOO_LONG
// LOADER_CALL_CHAIN_IS_TOO_DEEP
// NON-NATIVE_ACCOUNT_CAN_ONLY_BE_CLOSED_IF_ITS_BALANCE_IS_ZERO
// OPERATION_OVERFLOWED
// OWNER_DOES_NOT_MATCH
// PROVIDED_ADDRESS_DOES_NOT_MATCH_ADDRESSED_DERIVED_FROM_SEED
// SIMULATION_FAILED
// SIMULATION_TIMED_OUT
// SPECIFIED_NONCE_DOES_NOT_MATCH_STORED_NONCE
// STATE_IS_INVALID_FOR_REQUESTED_OPERATION
// STATE_IS_UNINITIALIZED
// STORED_NONCE_IS_STILL_IN_RECENT_BLOCKHASHES
// THE_PROVIDED_DECIMALS_VALUE_DIFFERENT_FROM_THE_MINT_DECIMALS
// THIS_ACCOUNT_MAY_NOT_BE_USED_TO_PAY_TRANSACTION_FEES
// THIS_PROGRAM_MAY_NOT_BE_USED_FOR_EXECUTING_INSTRUCTIONS
// THIS_TOKEN_MINT_CANNOT_FREEZE_ACCOUNTS
// THIS_TRANSACTION_HAS_ALREADY_BEEN_PROCESSED
// TOO_MANY_TRANSACTIONS
// TRANSACTION_ADDRESS_TABLE_LOOKUP_USES_AN_INVALID_INDEX
// TRANSACTION_CONTAINS_A_DUPLICATE_INSTRUCTION_({0})_THAT_IS_NOT_ALLOWED
// TRANSACTION_CONTAINS_AN_INVALID_ACCOUNT_REFERENCE
// TRANSACTION_DID_NOT_PASS_SIGNATURE_VERIFICATION
// TRANSACTION_FAILED_TO_SANITIZE_ACCOUNTS_OFFSETS_CORRECTLY
// TRANSACTION_LEAVES_AN_ACCOUNT_WITH_A_LOWER_BALANCE_THAN_RENT-EXEMPT_MINIMUM
// TRANSACTION_LOADS_A_WRITABLE_ACCOUNT_THAT_CANNOT_BE_WRITTEN
// TRANSACTION_LOADS_AN_ADDRESS_TABLE_ACCOUNT_THAT_DOESN'T_EXIST
// TRANSACTION_LOADS_AN_ADDRESS_TABLE_ACCOUNT_WITH_AN_INVALID_OWNER
// TRANSACTION_LOADS_AN_ADDRESS_TABLE_ACCOUNT_WITH_INVALID_DATA
// TRANSACTION_LOCKED_TOO_MANY_ACCOUNTS
// TRANSACTION_PROCESSING_LEFT_AN_ACCOUNT_WITH_AN_OUTSTANDING_BORROWED_REFERENCE
// TRANSACTION_REQUIRES_A_FEE_BUT_HAS_NO_SIGNATURE_PRESENT
// TRANSACTION_VERSION_IS_UNSUPPORTED
// TRANSACTION_WOULD_EXCEED_ACCOUNT_DATA_LIMIT_WITHIN_THE_BLOCK
// TRANSACTION_WOULD_EXCEED_MAX_ACCOUNT_LIMIT_WITHIN_THE_BLOCK
// TRANSACTION_WOULD_EXCEED_MAX_BLOCK_COST_LIMIT
// TRANSACTION_WOULD_EXCEED_MAX_VOTE_COST_LIMIT
// TRANSACTION_WOULD_EXCEED_TOTAL_ACCOUNT_DATA_LIMIT
// TRANSACTIONS_ARE_CURRENTLY_DISABLED_DUE_TO_CLUSTER_MAINTENANCE
// UNKNOWN_ERROR
DOMString kind;
DOMString humanReadableError;
};
Expand All @@ -205,7 +365,7 @@ namespace simulation_responses {
DOMString decimals;
SolanaDiff diff;
DOMString supply;
DOMString metaplexTokenStandard;
MetaplexTokenStandardKind metaplexTokenStandard;

// nullable field of type SolanaPrice
any assetPrice;
Expand All @@ -219,7 +379,7 @@ namespace simulation_responses {
DOMString decimals;
SolanaDiff diff;
DOMString supply;
DOMString metaplexTokenStandard;
MetaplexTokenStandardKind metaplexTokenStandard;

// nullable field of type SolanaPrice
any assetPrice;
Expand All @@ -236,7 +396,7 @@ namespace simulation_responses {
};

dictionary SolanaStateChangeRawInfo {
DOMString kind;
SolanaRawInfoKind kind;

// data is a non-nullable union of the following types:
// - SOLTransferData
Expand All @@ -248,7 +408,7 @@ namespace simulation_responses {

dictionary SolanaStateChange {
DOMString humanReadableDiff;
DOMString suggestedColor;
SuggestedColor suggestedColor;
SolanaStateChangeRawInfo rawInfo;
};

Expand All @@ -260,7 +420,7 @@ namespace simulation_responses {
};

dictionary SolanaSimulationResponse {
DOMString action;
DOMString action; // BLOCK | WARN | NONE
Warning[] warnings;
SolanaSimulationResults simulationResults;
};
Expand Down
28 changes: 18 additions & 10 deletions components/brave_wallet/browser/simulation_service_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ TEST_F(SimulationServiceUnitTest, ScanEVMTransactionValidResponse) {
const std::string& error_response,
const std::string& error_string) {
ASSERT_TRUE(response);
EXPECT_EQ(response->action, "NONE");
EXPECT_EQ(response->action, mojom::BlowfishSuggestedAction::kNone);
EXPECT_EQ(response->warnings.size(), 0u);
EXPECT_FALSE(response->simulation_results->error);
ASSERT_EQ(response->simulation_results->expected_state_changes.size(),
Expand All @@ -313,7 +313,8 @@ TEST_F(SimulationServiceUnitTest, ScanEVMTransactionValidResponse) {
const auto& state_change =
response->simulation_results->expected_state_changes.at(0).Clone();
EXPECT_EQ(state_change->human_readable_diff, "Send 1 ETH");
EXPECT_EQ(state_change->raw_info->kind, "NATIVE_ASSET_TRANSFER");
EXPECT_EQ(state_change->raw_info->kind,
mojom::BlowfishEVMRawInfoKind::kNativeAssetTransfer);
ASSERT_TRUE(
state_change->raw_info->data->is_native_asset_transfer_data());
const auto& state_change_raw_info =
Expand All @@ -325,7 +326,8 @@ TEST_F(SimulationServiceUnitTest, ScanEVMTransactionValidResponse) {
"1183957389356504134754");
EXPECT_EQ(state_change_raw_info->contract->address,
"0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
EXPECT_EQ(state_change_raw_info->contract->kind, "ACCOUNT");
EXPECT_EQ(state_change_raw_info->contract->kind,
mojom::BlowfishEVMAddressKind::kAccount);
EXPECT_EQ(state_change_raw_info->asset->address,
"0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
EXPECT_EQ(state_change_raw_info->asset->decimals, 18);
Expand All @@ -334,7 +336,8 @@ TEST_F(SimulationServiceUnitTest, ScanEVMTransactionValidResponse) {
EXPECT_EQ(state_change_raw_info->asset->name, "Ether");
EXPECT_EQ(state_change_raw_info->asset->price->dollar_value_per_token,
"1968.47");
EXPECT_EQ(state_change_raw_info->asset->price->source, "Coingecko");
EXPECT_EQ(state_change_raw_info->asset->price->source,
mojom::BlowfishAssetPriceSource::kCoingecko);
EXPECT_EQ(state_change_raw_info->asset->price->last_updated_at,
"1670324557");
EXPECT_EQ(state_change_raw_info->asset->symbol, "ETH");
Expand Down Expand Up @@ -490,7 +493,7 @@ TEST_F(SimulationServiceUnitTest, ScanSolanaTransactionValid) {
const std::string& error_string) {
ASSERT_TRUE(response);

EXPECT_EQ(response->action, "NONE");
EXPECT_EQ(response->action, mojom::BlowfishSuggestedAction::kNone);
EXPECT_EQ(response->warnings.size(), 0u);
EXPECT_FALSE(response->simulation_results->error);
EXPECT_FALSE(response->simulation_results->is_recent_blockhash_expired);
Expand All @@ -500,8 +503,10 @@ TEST_F(SimulationServiceUnitTest, ScanSolanaTransactionValid) {
const auto& state_change =
response->simulation_results->expected_state_changes.at(0);
EXPECT_EQ(state_change->human_readable_diff, "Send 2 USDT");
EXPECT_EQ(state_change->suggested_color, "DEBIT");
EXPECT_EQ(state_change->raw_info->kind, "SPL_TRANSFER");
EXPECT_EQ(state_change->suggested_color,
mojom::BlowfishSuggestedColor::kDebit);
EXPECT_EQ(state_change->raw_info->kind,
mojom::BlowfishSolanaRawInfoKind::kSplTransfer);
ASSERT_TRUE(state_change->raw_info->data->is_spl_transfer_data());

const auto& state_change_raw_info =
Expand All @@ -512,13 +517,16 @@ TEST_F(SimulationServiceUnitTest, ScanSolanaTransactionValid) {
"Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB");
EXPECT_EQ(state_change_raw_info->decimals, 6);
EXPECT_EQ(state_change_raw_info->supply, 1000000000ULL);
EXPECT_EQ(state_change_raw_info->metaplex_token_standard, "unknown");
EXPECT_EQ(state_change_raw_info->asset_price->source, "Coingecko");
EXPECT_EQ(state_change_raw_info->metaplex_token_standard,
mojom::BlowfishMetaplexTokenStandardKind::kUnknown);
EXPECT_EQ(state_change_raw_info->asset_price->source,
mojom::BlowfishAssetPriceSource::kCoingecko);
EXPECT_EQ(state_change_raw_info->asset_price->last_updated_at,
"1679331222");
EXPECT_EQ(state_change_raw_info->asset_price->dollar_value_per_token,
"0.99");
EXPECT_EQ(state_change_raw_info->diff->sign, "MINUS");
EXPECT_EQ(state_change_raw_info->diff->sign,
mojom::BlowfishDiffSign::kMinus);
EXPECT_EQ(state_change_raw_info->diff->digits, 2000000ULL);

EXPECT_EQ(error_response, "");
Expand Down
Loading

0 comments on commit 2061576

Please sign in to comment.