Skip to content

Commit

Permalink
Etherscan proxy migration (#18972)
Browse files Browse the repository at this point in the history
* Update kAssetRatioBaseURL to new URL

Previously it was ratios.rewards.brave.com/, going forward it will be
ratios.wallet.brave.com. Note: the trailing slash was removed and added
to the paths.

* Update ParseTokenInfo to match new v3 schemas
  • Loading branch information
nvonpentz committed Jun 21, 2023
1 parent c5020b7 commit 2ed146e
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 161 deletions.
43 changes: 1 addition & 42 deletions components/brave_wallet/api/asset_ratio.idl
Original file line number Diff line number Diff line change
Expand Up @@ -64,50 +64,9 @@ namespace asset_ratio {
// "message": "OK",
// "result": []
//
dictionary TokenInfoPayload {
// A list with result records.
TokenInfoResult[] result;
};


// A token info dictionary. Looks like this:
// {
// "payload": {
// "status": "1",
// "message": "OK",
// "result": [
// {
// "contractAddress": "0xdac17f958d2ee523a2206206994597c13d831ec7",
// "tokenName": "Tether USD",
// "symbol": "USDT",
// "divisor": "6",
// "tokenType": "ERC20",
// "totalSupply": "39828710009874796",
// "blueCheckmark": "true",
// "description": "Tether gives you the joint benefits of open...",
// "website": "https://tether.to/",
// "email": "support@tether.to",
// "blog": "https://tether.to/category/announcements/",
// "reddit": "",
// "slack": "",
// "facebook": "",
// "twitter": "https://twitter.com/Tether_to",
// "bitcointalk": "",
// "github": "",
// "telegram": "",
// "wechat": "",
// "linkedin": "",
// "discord": "",
// "whitepaper": "https://path/to/TetherWhitePaper.pdf",
// "tokenPriceUSD": "1.000000000000000000"
// }
// ]
// },
// "lastUpdated": "2021-12-09T22:02:23.187Z"
// }
dictionary TokenInfo {
// A list with result records.
TokenInfoPayload payload;
TokenInfoResult[] result;
};

// A CoinMarket payload record. Sample looks like:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,10 @@ mojom::BlockchainTokenPtr ParseTokenInfo(const base::Value& json_value,
return nullptr;
}

if (token_info->payload.result.size() != 1) {
if (token_info->result.size() != 1) {
return nullptr;
}
const api::asset_ratio::TokenInfoResult& result =
token_info->payload.result.front();
const api::asset_ratio::TokenInfoResult& result = token_info->result.front();

int decimals = 0;
const auto eth_addr = EthAddress::FromHex(result.contract_address);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,36 +145,33 @@ TEST(AssetRatioResponseParserUnitTest, ParseGetTokenInfo) {
// ERC20
std::string json(R"(
{
"payload": {
"status": "1",
"message": "OK",
"result": [{
"contractAddress": "0xdac17f958d2ee523a2206206994597c13d831ec7",
"tokenName": "Tether USD",
"symbol": "USDT",
"divisor": "6",
"tokenType": "ERC20",
"totalSupply": "39828710009874796",
"blueCheckmark": "true",
"description": "Tether gives you the joint benefits of open...",
"website": "https://tether.to/",
"email": "support@tether.to",
"blog": "https://tether.to/category/announcements/",
"reddit": "",
"slack": "",
"facebook": "",
"twitter": "https://twitter.com/Tether_to",
"bitcointalk": "",
"github": "",
"telegram": "",
"wechat": "",
"linkedin": "",
"discord": "",
"whitepaper": "https://path/to/TetherWhitePaper.pdf",
"tokenPriceUSD": "1.000000000000000000"
}]
},
"lastUpdated": "2021-12-09T22:02:23.187Z"
"status": "1",
"message": "OK",
"result": [{
"contractAddress": "0xdac17f958d2ee523a2206206994597c13d831ec7",
"tokenName": "Tether USD",
"symbol": "USDT",
"divisor": "6",
"tokenType": "ERC20",
"totalSupply": "39828710009874796",
"blueCheckmark": "true",
"description": "Tether gives you the joint benefits of open...",
"website": "https://tether.to/",
"email": "support@tether.to",
"blog": "https://tether.to/category/announcements/",
"reddit": "",
"slack": "",
"facebook": "",
"twitter": "https://twitter.com/Tether_to",
"bitcointalk": "",
"github": "",
"telegram": "",
"wechat": "",
"linkedin": "",
"discord": "",
"whitepaper": "https://path/to/TetherWhitePaper.pdf",
"tokenPriceUSD": "1.000000000000000000"
}]
}
)");

Expand All @@ -190,18 +187,15 @@ TEST(AssetRatioResponseParserUnitTest, ParseGetTokenInfo) {
// ERC721
json = (R"(
{
"payload": {
"status": "1",
"message": "OK",
"result": [{
"contractAddress": "0x0e3a2a1f2146d86a604adc220b4967a898d7fe07",
"tokenName": "Gods Unchained Cards",
"symbol": "CARD",
"divisor": "0",
"tokenType": "ERC721"
}]
},
"lastUpdated": "2021-12-09T22:02:23.187Z"
"status": "1",
"message": "OK",
"result": [{
"contractAddress": "0x0e3a2a1f2146d86a604adc220b4967a898d7fe07",
"tokenName": "Gods Unchained Cards",
"symbol": "CARD",
"divisor": "0",
"tokenType": "ERC721"
}]
}
)");
expected_token = mojom::BlockchainToken::New(
Expand All @@ -214,18 +208,15 @@ TEST(AssetRatioResponseParserUnitTest, ParseGetTokenInfo) {

const std::string valid_json = (R"(
{
"payload": {
"status": "1",
"message": "OK",
"result": [{
"contractAddress": "0xdac17f958d2ee523a2206206994597c13d831ec7",
"tokenName": "Tether USD",
"symbol": "USDT",
"divisor": "6",
"tokenType": "ERC20"
}]
},
"lastUpdated": "2021-12-09T22:02:23.187Z"
"status": "1",
"message": "OK",
"result": [{
"contractAddress": "0xdac17f958d2ee523a2206206994597c13d831ec7",
"tokenName": "Tether USD",
"symbol": "USDT",
"divisor": "6",
"tokenType": "ERC20"
}]
}
)");
ASSERT_TRUE(
Expand All @@ -244,18 +235,15 @@ TEST(AssetRatioResponseParserUnitTest, ParseGetTokenInfo) {
// Invalid decimals.
json = (R"(
{
"payload": {
"status": "1",
"message": "OK",
"result": [{
"contractAddress": "0xdac17f958d2ee523a2206206994597c13d831ec7",
"tokenName": "Tether USD",
"symbol": "USDT",
"divisor": "NOT A NUMBER",
"tokenType": "ERC20"
}]
},
"lastUpdated": "2021-12-09T22:02:23.187Z"
"status": "1",
"message": "OK",
"result": [{
"contractAddress": "0xdac17f958d2ee523a2206206994597c13d831ec7",
"tokenName": "Tether USD",
"symbol": "USDT",
"divisor": "NOT A NUMBER",
"tokenType": "ERC20"
}]
}
)");
EXPECT_FALSE(ParseTokenInfo(ParseJson(json), "0x1", mojom::CoinType::ETH))
Expand Down
40 changes: 22 additions & 18 deletions components/brave_wallet/browser/asset_ratio_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@ absl::optional<std::string> ChainIdToStripeChainId(
return chain_id_lookup->at(chain_id);
}

base::flat_map<std::string, std::string> MakeBraveServicesKeyHeader() {
base::flat_map<std::string, std::string> request_headers;
std::unique_ptr<base::Environment> env(base::Environment::Create());
std::string brave_key(BUILDFLAG(BRAVE_SERVICES_KEY));
if (env->HasVar("BRAVE_SERVICES_KEY")) {
env->GetVar("BRAVE_SERVICES_KEY", &brave_key);
}
request_headers["x-brave-key"] = std::move(brave_key);

return request_headers;
}

} // namespace

namespace brave_wallet {
Expand Down Expand Up @@ -172,7 +184,7 @@ GURL AssetRatioService::GetPriceURL(
std::string from = VectorToCommaSeparatedList(from_assets);
std::string to = VectorToCommaSeparatedList(to_assets);
std::string spec = base::StringPrintf(
"%sv2/relative/provider/coingecko/%s/%s/%s",
"%s/v2/relative/provider/coingecko/%s/%s/%s",
base_url_for_test_.is_empty() ? kAssetRatioBaseURL
: base_url_for_test_.spec().c_str(),
from.c_str(), to.c_str(), TimeFrameKeyToString(timeframe).c_str());
Expand All @@ -185,7 +197,7 @@ GURL AssetRatioService::GetPriceHistoryURL(
const std::string& vs_asset,
brave_wallet::mojom::AssetPriceTimeframe timeframe) {
std::string spec = base::StringPrintf(
"%sv2/history/coingecko/%s/%s/%s",
"%s/v2/history/coingecko/%s/%s/%s",
base_url_for_test_.is_empty() ? kAssetRatioBaseURL
: base_url_for_test_.spec().c_str(),
asset.c_str(), vs_asset.c_str(), TimeFrameKeyToString(timeframe).c_str());
Expand Down Expand Up @@ -304,17 +316,9 @@ void AssetRatioService::GetPrice(
&AssetRatioService::OnGetPrice, weak_ptr_factory_.GetWeakPtr(),
from_assets_lower, to_assets_lower, std::move(callback));

base::flat_map<std::string, std::string> request_headers;
std::unique_ptr<base::Environment> env(base::Environment::Create());
std::string brave_key(BUILDFLAG(BRAVE_SERVICES_KEY));
if (env->HasVar("BRAVE_SERVICES_KEY")) {
env->GetVar("BRAVE_SERVICES_KEY", &brave_key);
}
request_headers[kBraveServicesKeyHeader] = std::move(brave_key);

api_request_helper_->Request(
"GET", GetPriceURL(from_assets_lower, to_assets_lower, timeframe), "", "",
std::move(internal_callback), request_headers,
std::move(internal_callback), MakeBraveServicesKeyHeader(),
{.auto_retry_on_network_change = true, .enable_cache = true});
}

Expand Down Expand Up @@ -366,7 +370,7 @@ void AssetRatioService::GetStripeBuyURL(

const std::string json_payload = GetJSON(payload);

GURL url = GURL(base::StringPrintf("%sv2/stripe/onramp_sessions",
GURL url = GURL(base::StringPrintf("%s/v2/stripe/onramp_sessions",
base_url_for_test_.is_empty()
? kAssetRatioBaseURL
: base_url_for_test_.spec().c_str()));
Expand All @@ -377,7 +381,7 @@ void AssetRatioService::GetStripeBuyURL(

api_request_helper_->Request(
"POST", url, json_payload, "application/json",
std::move(internal_callback), {},
std::move(internal_callback), MakeBraveServicesKeyHeader(),
{.auto_retry_on_network_change = true, .enable_cache = false});
}

Expand Down Expand Up @@ -427,7 +431,7 @@ void AssetRatioService::GetPriceHistory(
weak_ptr_factory_.GetWeakPtr(), std::move(callback));
api_request_helper_->Request(
"GET", GetPriceHistoryURL(asset_lower, vs_asset_lower, timeframe), "", "",
std::move(internal_callback), {},
std::move(internal_callback), MakeBraveServicesKeyHeader(),
{.auto_retry_on_network_change = true, .enable_cache = true});
}

Expand All @@ -449,7 +453,7 @@ void AssetRatioService::OnGetPriceHistory(GetPriceHistoryCallback callback,
// static
GURL AssetRatioService::GetTokenInfoURL(const std::string& contract_address) {
std::string spec = base::StringPrintf(
"%sv2/etherscan/"
"%s/v3/etherscan/"
"passthrough?module=token&action=tokeninfo&contractaddress=%s",
base_url_for_test_.is_empty() ? kAssetRatioBaseURL
: base_url_for_test_.spec().c_str(),
Expand All @@ -464,7 +468,7 @@ void AssetRatioService::GetTokenInfo(const std::string& contract_address,
weak_ptr_factory_.GetWeakPtr(), std::move(callback));
api_request_helper_->Request(
"GET", GetTokenInfoURL(contract_address), "", "",
std::move(internal_callback), {},
std::move(internal_callback), MakeBraveServicesKeyHeader(),
{.auto_retry_on_network_change = true, .enable_cache = true});
}

Expand All @@ -483,7 +487,7 @@ void AssetRatioService::OnGetTokenInfo(GetTokenInfoCallback callback,
// static
GURL AssetRatioService::GetCoinMarketsURL(const std::string& vs_asset,
const uint8_t limit) {
GURL url = GURL(base::StringPrintf("%sv2/market/provider/coingecko",
GURL url = GURL(base::StringPrintf("%s/v2/market/provider/coingecko",
base_url_for_test_.is_empty()
? kAssetRatioBaseURL
: base_url_for_test_.spec().c_str()));
Expand All @@ -501,7 +505,7 @@ void AssetRatioService::GetCoinMarkets(const std::string& vs_asset,
weak_ptr_factory_.GetWeakPtr(), std::move(callback));
api_request_helper_->Request(
"GET", GetCoinMarketsURL(vs_asset_lower, limit), "", "",
std::move(internal_callback), {},
std::move(internal_callback), MakeBraveServicesKeyHeader(),
{.auto_retry_on_network_change = true, .enable_cache = true});
}

Expand Down
Loading

0 comments on commit 2ed146e

Please sign in to comment.