Skip to content

Commit

Permalink
Uplift of #12790 (squashed) to beta
Browse files Browse the repository at this point in the history
  • Loading branch information
brave-browser-releases committed Mar 29, 2022
1 parent 584fad2 commit ab2c3bd
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
13 changes: 7 additions & 6 deletions components/brave_wallet/browser/json_rpc_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1297,8 +1297,10 @@ bool JsonRpcService::IsValidDomain(const std::string& domain) {

void JsonRpcService::GetERC721OwnerOf(const std::string& contract,
const std::string& token_id,
const std::string& chain_id,
GetERC721OwnerOfCallback callback) {
if (!EthAddress::IsValidAddress(contract)) {
auto network_url = GetNetworkURL(prefs_, chain_id, mojom::CoinType::ETH);
if (!EthAddress::IsValidAddress(contract) || !network_url.is_valid()) {
std::move(callback).Run(
"", mojom::ProviderError::kInvalidParams,
l10n_util::GetStringUTF8(IDS_WALLET_INVALID_PARAMETERS));
Expand All @@ -1325,8 +1327,7 @@ void JsonRpcService::GetERC721OwnerOf(const std::string& contract,
base::BindOnce(&JsonRpcService::OnGetERC721OwnerOf,
weak_ptr_factory_.GetWeakPtr(), std::move(callback));
RequestInternal(eth::eth_call("", contract, "", "", "", data, "latest"), true,
network_urls_[mojom::CoinType::ETH],
std::move(internal_callback));
network_url, std::move(internal_callback));
}

void JsonRpcService::OnGetERC721OwnerOf(
Expand Down Expand Up @@ -1360,8 +1361,7 @@ void JsonRpcService::GetERC721TokenBalance(
const std::string& chain_id,
GetERC721TokenBalanceCallback callback) {
const auto eth_account_address = EthAddress::FromHex(account_address);
auto network_url = GetNetworkURL(prefs_, chain_id, mojom::CoinType::ETH);
if (eth_account_address.IsEmpty() || !network_url.is_valid()) {
if (eth_account_address.IsEmpty()) {
std::move(callback).Run(
"", mojom::ProviderError::kInvalidParams,
l10n_util::GetStringUTF8(IDS_WALLET_INVALID_PARAMETERS));
Expand All @@ -1372,7 +1372,8 @@ void JsonRpcService::GetERC721TokenBalance(
&JsonRpcService::ContinueGetERC721TokenBalance,
weak_ptr_factory_.GetWeakPtr(), eth_account_address.ToChecksumAddress(),
std::move(callback));
GetERC721OwnerOf(contract_address, token_id, std::move(internal_callback));
GetERC721OwnerOf(contract_address, token_id, chain_id,
std::move(internal_callback));
}

void JsonRpcService::ContinueGetERC721TokenBalance(
Expand Down
1 change: 1 addition & 0 deletions components/brave_wallet/browser/json_rpc_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ class JsonRpcService : public KeyedService, public mojom::JsonRpcService {

void GetERC721OwnerOf(const std::string& contract,
const std::string& token_id,
const std::string& chain_id,
GetERC721OwnerOfCallback callback) override;

void GetERC721TokenBalance(const std::string& contract_address,
Expand Down
18 changes: 16 additions & 2 deletions components/brave_wallet/browser/json_rpc_service_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1814,7 +1814,7 @@ TEST_F(JsonRpcServiceUnitTest, IsValidDomain) {
TEST_F(JsonRpcServiceUnitTest, GetERC721OwnerOf) {
bool callback_called = false;
json_rpc_service_->GetERC721OwnerOf(
"", "0x1",
"", "0x1", mojom::kMainnetChainId,
base::BindOnce(&OnStringResponse, &callback_called,
mojom::ProviderError::kInvalidParams,
l10n_util::GetStringUTF8(IDS_WALLET_INVALID_PARAMETERS),
Expand All @@ -1824,7 +1824,17 @@ TEST_F(JsonRpcServiceUnitTest, GetERC721OwnerOf) {

callback_called = false;
json_rpc_service_->GetERC721OwnerOf(
"0x06012c8cf97BEaD5deAe237070F9587f8E7A266d", "",
"0x06012c8cf97BEaD5deAe237070F9587f8E7A266d", "", mojom::kMainnetChainId,
base::BindOnce(&OnStringResponse, &callback_called,
mojom::ProviderError::kInvalidParams,
l10n_util::GetStringUTF8(IDS_WALLET_INVALID_PARAMETERS),
""));
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(callback_called);

callback_called = false;
json_rpc_service_->GetERC721OwnerOf(
"0x06012c8cf97BEaD5deAe237070F9587f8E7A266d", "0x1", "",
base::BindOnce(&OnStringResponse, &callback_called,
mojom::ProviderError::kInvalidParams,
l10n_util::GetStringUTF8(IDS_WALLET_INVALID_PARAMETERS),
Expand All @@ -1841,6 +1851,7 @@ TEST_F(JsonRpcServiceUnitTest, GetERC721OwnerOf) {
callback_called = false;
json_rpc_service_->GetERC721OwnerOf(
"0x06012c8cf97BEaD5deAe237070F9587f8E7A266d", "0x1",
mojom::kMainnetChainId,
base::BindOnce(
&OnStringResponse, &callback_called, mojom::ProviderError::kSuccess,
"",
Expand All @@ -1851,6 +1862,7 @@ TEST_F(JsonRpcServiceUnitTest, GetERC721OwnerOf) {
SetHTTPRequestTimeoutInterceptor();
json_rpc_service_->GetERC721OwnerOf(
"0x06012c8cf97BEaD5deAe237070F9587f8E7A266d", "0x1",
mojom::kMainnetChainId,
base::BindOnce(&OnStringResponse, &callback_called,
mojom::ProviderError::kInternalError,
l10n_util::GetStringUTF8(IDS_WALLET_INTERNAL_ERROR), ""));
Expand All @@ -1860,6 +1872,7 @@ TEST_F(JsonRpcServiceUnitTest, GetERC721OwnerOf) {
SetInvalidJsonInterceptor();
json_rpc_service_->GetERC721OwnerOf(
"0x06012c8cf97BEaD5deAe237070F9587f8E7A266d", "0x1",
mojom::kMainnetChainId,
base::BindOnce(&OnStringResponse, &callback_called,
mojom::ProviderError::kParsingError,
l10n_util::GetStringUTF8(IDS_WALLET_PARSING_ERROR), ""));
Expand All @@ -1869,6 +1882,7 @@ TEST_F(JsonRpcServiceUnitTest, GetERC721OwnerOf) {
SetLimitExceededJsonErrorResponse();
json_rpc_service_->GetERC721OwnerOf(
"0x06012c8cf97BEaD5deAe237070F9587f8E7A266d", "0x1",
mojom::kMainnetChainId,
base::BindOnce(&OnStringResponse, &callback_called,
mojom::ProviderError::kLimitExceeded,
"Request exceeds defined limit", ""));
Expand Down
2 changes: 1 addition & 1 deletion components/brave_wallet/common/brave_wallet.mojom
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ interface JsonRpcService {
SetCustomNetworkForTesting(string chain_id, CoinType coin, url.mojom.Url provider_url);

// Obtains the owner of a contract's ERC721 token ID
GetERC721OwnerOf(string contract, string token_id) => (string owner_address, ProviderError error, string error_message);
GetERC721OwnerOf(string contract, string token_id, string chain_id) => (string owner_address, ProviderError error, string error_message);

// Obtains the balance of a contract's ERC721 token ID for an address
// Balance is 0x1 if current selected account is the same as token owner's
Expand Down

0 comments on commit ab2c3bd

Please sign in to comment.