Skip to content

Commit

Permalink
Uplift of #26593 (squashed) to beta
Browse files Browse the repository at this point in the history
  • Loading branch information
brave-builds committed Nov 18, 2024
1 parent 6613541 commit c8cce12
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 45 deletions.
3 changes: 3 additions & 0 deletions components/brave_wallet/browser/swap_response_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,7 @@ mojom::LiFiStatusPtr ParseStatusResponse(const base::Value& json_value) {
namespace squid {

namespace {
constexpr char kNoTokenData[] = "Unable to fetch token data";

std::optional<std::string> ChainIdToHex(const std::string& value) {
std::optional<uint256_t> out = Base10ValueToUint256(value);
Expand Down Expand Up @@ -1149,6 +1150,8 @@ mojom::SquidErrorPtr ParseErrorResponse(const base::Value& json_value) {
auto result = mojom::SquidError::New();
result->message = value->message;
result->type = ParseErrorType(value->type);
result->is_insufficient_liquidity =
base::Contains(result->message, kNoTokenData);

return result;
}
Expand Down
14 changes: 14 additions & 0 deletions components/brave_wallet/browser/swap_response_parser_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1922,6 +1922,20 @@ TEST(SwapResponseParserUnitTest, ParseSquidErrorResponse) {
ASSERT_TRUE(error);
EXPECT_EQ(error->message, "The request is missing a required parameter.");
EXPECT_EQ(error->type, mojom::SquidErrorType::kUnknownError);

json = R"(
{
"statusCode": "400",
"type": "BAD_REQUEST_ERROR",
"message": "Unable to fetch token data"
}
)";

error = squid::ParseErrorResponse(ParseJson(json));
ASSERT_TRUE(error);
EXPECT_EQ(error->type, mojom::SquidErrorType::kUnknownError);
EXPECT_EQ(error->message, "Unable to fetch token data");
EXPECT_TRUE(error->is_insufficient_liquidity);
}

TEST(SwapResponseParserUnitTest, ParseSquidQuoteResponse) {
Expand Down
32 changes: 17 additions & 15 deletions components/brave_wallet/browser/swap_service_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1915,14 +1915,15 @@ TEST_F(SwapServiceUnitTest, GetSquidQuoteError) {

base::MockCallback<mojom::SwapService::GetQuoteCallback> callback;

EXPECT_CALL(callback,
Run(EqualsMojo(mojom::SwapQuoteUnionPtr()),
EqualsMojo(mojom::SwapFeesPtr()),
EqualsMojo(mojom::SwapErrorUnion::NewSquidError(
mojom::SquidError::New(
"onChainQuoting must be a `boolean` type.",
mojom::SquidErrorType::kSchemaValidationError))),
""));
EXPECT_CALL(
callback,
Run(EqualsMojo(mojom::SwapQuoteUnionPtr()),
EqualsMojo(mojom::SwapFeesPtr()),
EqualsMojo(
mojom::SwapErrorUnion::NewSquidError(mojom::SquidError::New(
"onChainQuoting must be a `boolean` type.",
mojom::SquidErrorType::kSchemaValidationError, false))),
""));

swap_service_->GetQuote(
GetCannedSwapQuoteParams(
Expand Down Expand Up @@ -1950,13 +1951,14 @@ TEST_F(SwapServiceUnitTest, GetSquidTransactionError) {

base::MockCallback<mojom::SwapService::GetTransactionCallback> callback;

EXPECT_CALL(callback,
Run(EqualsMojo(mojom::SwapTransactionUnionPtr()),
EqualsMojo(mojom::SwapErrorUnion::NewSquidError(
mojom::SquidError::New(
"onChainQuoting must be a `boolean` type.",
mojom::SquidErrorType::kSchemaValidationError))),
""));
EXPECT_CALL(
callback,
Run(EqualsMojo(mojom::SwapTransactionUnionPtr()),
EqualsMojo(
mojom::SwapErrorUnion::NewSquidError(mojom::SquidError::New(
"onChainQuoting must be a `boolean` type.",
mojom::SquidErrorType::kSchemaValidationError, false))),
""));

swap_service_->GetTransaction(
mojom::SwapTransactionParamsUnion::NewSquidTransactionParams(
Expand Down
1 change: 1 addition & 0 deletions components/brave_wallet/common/brave_wallet.mojom
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,7 @@ enum SquidErrorType {
struct SquidError {
string message;
SquidErrorType type;
bool is_insufficient_liquidity;
};

union SquidTransactionUnion {
Expand Down
82 changes: 52 additions & 30 deletions components/brave_wallet_ui/page/screens/swap/hooks/useSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -971,41 +971,11 @@ export const useSwap = () => {
return
}

const fromAmountWeiWrapped = new Amount(fromAmount).multiplyByDecimals(
fromToken.decimals
)
if (fromAmountWeiWrapped.gt(fromAssetBalance)) {
return 'insufficientBalance'
}

if (feesWrapped.gt(nativeAssetBalance)) {
return 'insufficientFundsForGas'
}

if (
!fromToken.contractAddress &&
fromAmountWeiWrapped.plus(feesWrapped).gt(fromAssetBalance)
) {
return 'insufficientFundsForGas'
}

// No quote-based validations to perform when backend error is set.
if (backendError) {
return 'unknownError'
}

// EVM specific validations
if (
(quoteUnion?.zeroExQuote ||
quoteUnion?.lifiQuote ||
quoteUnion?.squidQuote) &&
fromToken.coin === BraveWallet.CoinType.ETH &&
fromToken.contractAddress &&
!hasAllowance
) {
return 'insufficientAllowance'
}

// 0x specific validations
if (quoteErrorUnion?.zeroExError) {
if (quoteErrorUnion.zeroExError.isInsufficientLiquidity) {
Expand Down Expand Up @@ -1039,6 +1009,44 @@ export const useSwap = () => {
: 'unknownError'
}

if (quoteErrorUnion?.squidError) {
if (quoteErrorUnion.squidError.isInsufficientLiquidity) {
return 'insufficientLiquidity'
}

return 'unknownError'
}

const fromAmountWeiWrapped = new Amount(fromAmount).multiplyByDecimals(
fromToken.decimals
)
if (fromAmountWeiWrapped.gt(fromAssetBalance)) {
return 'insufficientBalance'
}

if (feesWrapped.gt(nativeAssetBalance)) {
return 'insufficientFundsForGas'
}

if (
!fromToken.contractAddress &&
fromAmountWeiWrapped.plus(feesWrapped).gt(fromAssetBalance)
) {
return 'insufficientFundsForGas'
}

// EVM specific validations
if (
(quoteUnion?.zeroExQuote ||
quoteUnion?.lifiQuote ||
quoteUnion?.squidQuote) &&
fromToken.coin === BraveWallet.CoinType.ETH &&
fromToken.contractAddress &&
!hasAllowance
) {
return 'insufficientAllowance'
}

return undefined
}, [
fromAmount,
Expand Down Expand Up @@ -1183,6 +1191,9 @@ export const useSwap = () => {
const onChangeSwapProvider = useCallback(
async (provider: BraveWallet.SwapProvider) => {
setSelectedProvider(provider)
setQuoteErrorUnion(undefined)
setBackendError(undefined)
setQuoteUnion(undefined)
setSelectedQuoteOptionId(undefined)
await handleQuoteRefreshInternal({
provider
Expand Down Expand Up @@ -1324,6 +1335,17 @@ export const useSwap = () => {
selectedQuoteOptionId
])

useEffect(() => {
// Reset selectedProvider to Auto if no tokens are selected
if (!fromToken && !toToken) {
setSelectingFromOrTo(undefined)
setFromAmount('')
setToAmount('')
reset()
setSelectedProvider(BraveWallet.SwapProvider.kAuto)
}
}, [fromToken, toToken, reset])

return {
fromAccount,
fromToken,
Expand Down

0 comments on commit c8cce12

Please sign in to comment.