Skip to content

Commit 81bc1f6

Browse files
committed
Fixes brave/brave-browser#9559 - Uses updated balances endpoint
Depcrecates v1 API methods for getting symbol prices Removes volume endpoints (Dead Code
1 parent 8e1f405 commit 81bc1f6

File tree

16 files changed

+127
-601
lines changed

16 files changed

+127
-601
lines changed

browser/extensions/api/binance_api.cc

+9-61
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,19 @@ BinanceGetAccountBalancesFunction::Run() {
114114
}
115115

116116
void BinanceGetAccountBalancesFunction::OnGetAccountBalances(
117-
const std::map<std::string, std::string>& balances, bool success) {
118-
auto balance_dict = std::make_unique<base::Value>(
119-
base::Value::Type::DICTIONARY);
117+
const std::map<std::string, std::vector<std::string>>& balances,
118+
bool success) {
119+
std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue());
120120

121121
for (const auto& balance : balances) {
122-
balance_dict->SetStringKey(balance.first, balance.second);
122+
auto info = std::make_unique<base::DictionaryValue>();
123+
info->SetString("balance", balance.second[0]);
124+
info->SetString("btcValue", balance.second[1]);
125+
info->SetString("fiatValue", balance.second[2]);
126+
result->SetDictionary(balance.first, std::move(info));
123127
}
124128

125-
Respond(TwoArguments(std::move(balance_dict),
129+
Respond(TwoArguments(std::move(result),
126130
std::make_unique<base::Value>(success)));
127131
}
128132

@@ -161,62 +165,6 @@ void BinanceGetConvertQuoteFunction::OnQuoteResult(
161165
Respond(OneArgument(std::move(quote)));
162166
}
163167

164-
ExtensionFunction::ResponseAction
165-
BinanceGetTickerPriceFunction::Run() {
166-
std::unique_ptr<binance::GetTickerPrice::Params> params(
167-
binance::GetTickerPrice::Params::Create(*args_));
168-
EXTENSION_FUNCTION_VALIDATE(params.get());
169-
170-
if (!IsBinanceAPIAvailable(browser_context())) {
171-
return RespondNow(Error("Not available in Tor/incognito/guest profile"));
172-
}
173-
174-
auto* service = GetBinanceService(browser_context());
175-
bool value_request = service->GetTickerPrice(params->symbol_pair,
176-
base::BindOnce(
177-
&BinanceGetTickerPriceFunction::OnGetTickerPrice, this));
178-
179-
if (!value_request) {
180-
return RespondNow(
181-
Error("Could not make request for BTC price"));
182-
}
183-
184-
return RespondLater();
185-
}
186-
187-
void BinanceGetTickerPriceFunction::OnGetTickerPrice(
188-
const std::string& symbol_pair_price) {
189-
Respond(OneArgument(std::make_unique<base::Value>(symbol_pair_price)));
190-
}
191-
192-
ExtensionFunction::ResponseAction
193-
BinanceGetTickerVolumeFunction::Run() {
194-
std::unique_ptr<binance::GetTickerVolume::Params> params(
195-
binance::GetTickerVolume::Params::Create(*args_));
196-
EXTENSION_FUNCTION_VALIDATE(params.get());
197-
198-
if (!IsBinanceAPIAvailable(browser_context())) {
199-
return RespondNow(Error("Not available in Tor/incognito/guest profile"));
200-
}
201-
202-
auto* service = GetBinanceService(browser_context());
203-
bool value_request = service->GetTickerVolume(params->symbol_pair,
204-
base::BindOnce(
205-
&BinanceGetTickerVolumeFunction::OnGetTickerVolume, this));
206-
207-
if (!value_request) {
208-
return RespondNow(
209-
Error("Could not make request for Volume"));
210-
}
211-
212-
return RespondLater();
213-
}
214-
215-
void BinanceGetTickerVolumeFunction::OnGetTickerVolume(
216-
const std::string& symbol_pair_volume) {
217-
Respond(OneArgument(std::make_unique<base::Value>(symbol_pair_volume)));
218-
}
219-
220168
ExtensionFunction::ResponseAction
221169
BinanceIsSupportedRegionFunction::Run() {
222170
if (!IsBinanceAPIAvailable(browser_context())) {

browser/extensions/api/binance_api.h

+2-25
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ class BinanceGetAccountBalancesFunction :
6666

6767
protected:
6868
~BinanceGetAccountBalancesFunction() override {}
69-
void OnGetAccountBalances(const std::map<std::string, std::string>& balances,
69+
void OnGetAccountBalances(const std::map<std::string,
70+
std::vector<std::string>>& balances,
7071
bool success);
7172

7273
ResponseAction Run() override;
@@ -87,30 +88,6 @@ class BinanceGetConvertQuoteFunction :
8788
ResponseAction Run() override;
8889
};
8990

90-
class BinanceGetTickerPriceFunction :
91-
public ExtensionFunction {
92-
public:
93-
DECLARE_EXTENSION_FUNCTION("binance.getTickerPrice", UNKNOWN)
94-
95-
protected:
96-
~BinanceGetTickerPriceFunction() override {}
97-
void OnGetTickerPrice(const std::string& symbol_pair_price);
98-
99-
ResponseAction Run() override;
100-
};
101-
102-
class BinanceGetTickerVolumeFunction :
103-
public ExtensionFunction {
104-
public:
105-
DECLARE_EXTENSION_FUNCTION("binance.getTickerVolume", UNKNOWN)
106-
107-
protected:
108-
~BinanceGetTickerVolumeFunction() override {}
109-
void OnGetTickerVolume(const std::string& symbol_pair_volume);
110-
111-
ResponseAction Run() override;
112-
};
113-
11491
class BinanceGetDepositInfoFunction :
11592
public ExtensionFunction {
11693
public:

common/extensions/api/binance.json

+1-42
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@
9292
"parameters": [
9393
{
9494
"name": "balances",
95-
"type": "object",
96-
"additionalProperties": { "type": "string" }
95+
"type": "any"
9796
}, {
9897
"name": "success",
9998
"type": "boolean",
@@ -150,46 +149,6 @@
150149
}
151150
]
152151
},
153-
{
154-
"name": "getTickerPrice",
155-
"type": "function",
156-
"description": "Fetches latest symbol pair trading value",
157-
"parameters": [
158-
{
159-
"type": "string",
160-
"name": "symbolPair"
161-
}, {
162-
"type": "function",
163-
"name": "callback",
164-
"parameters": [
165-
{
166-
"name": "symbolPairValue",
167-
"type": "string"
168-
}
169-
]
170-
}
171-
]
172-
},
173-
{
174-
"name": "getTickerVolume",
175-
"type": "function",
176-
"description": "Fetches latest symbol pair trading volume",
177-
"parameters": [
178-
{
179-
"type": "string",
180-
"name": "symbolPair"
181-
}, {
182-
"type": "function",
183-
"name": "callback",
184-
"parameters": [
185-
{
186-
"name": "symbolPairVolume",
187-
"type": "string"
188-
}
189-
]
190-
}
191-
]
192-
},
193152
{
194153
"name": "getDepositInfo",
195154
"type": "function",

components/binance/browser/binance_json_parser.cc

+29-69
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,16 @@ bool BinanceJSONParser::GetTokensFromJSON(
5454
// "free": "0.00000000",
5555
// "locked": "0.00000000",
5656
// "freeze": "1.00000000",
57-
// "withdrawing": "0.00000000"
57+
// "withdrawing": "0.00000000",
58+
// "btcValuation": "0.00000000",
59+
// "fiatValuation": "0.00000000"
5860
// }
5961
// ]
6062
// }
6163
//
6264
bool BinanceJSONParser::GetAccountBalancesFromJSON(
63-
const std::string& json, std::map<std::string, std::string>* balances) {
65+
const std::string& json,
66+
std::map<std::string, std::vector<std::string>>* balances) {
6467
if (!balances) {
6568
return false;
6669
}
@@ -76,18 +79,31 @@ bool BinanceJSONParser::GetAccountBalancesFromJSON(
7679
}
7780

7881
const base::Value* pv_arr = records_v->FindKey("data");
79-
if (pv_arr && pv_arr->is_list()) {
80-
for (const base::Value &val : pv_arr->GetList()) {
81-
const base::Value* asset = val.FindKey("asset");
82-
const base::Value* free_amount = val.FindKey("free");
83-
const base::Value* locked_amount = val.FindKey("locked");
84-
if (asset && asset->is_string() &&
85-
free_amount && free_amount->is_string() &&
86-
locked_amount && locked_amount->is_string()) {
87-
const std::string asset_symbol = asset->GetString();
88-
balances->insert({asset_symbol, free_amount->GetString()});
89-
}
82+
if (!pv_arr || !pv_arr->is_list()) {
83+
return false;
84+
}
85+
86+
for (const base::Value &val : pv_arr->GetList()) {
87+
const base::Value* asset = val.FindKey("asset");
88+
const base::Value* free_amount = val.FindKey("free");
89+
const base::Value* btc_val = val.FindKey("btcValuation");
90+
const base::Value* fiat_val = val.FindKey("fiatValuation");
91+
92+
bool has_asset = asset && asset->is_string();
93+
bool has_free = free_amount && free_amount->is_string();
94+
bool has_btc_val = btc_val && btc_val->is_string();
95+
bool has_fiat_val = fiat_val && fiat_val->is_string();
96+
97+
if (!has_asset || !has_free || !has_btc_val || !has_fiat_val) {
98+
continue;
9099
}
100+
101+
std::vector<std::string> balance_data;
102+
balance_data.push_back(free_amount->GetString());
103+
balance_data.push_back(btc_val->GetString());
104+
balance_data.push_back(fiat_val->GetString());
105+
106+
balances->insert({asset->GetString(), balance_data});
91107
}
92108

93109
return true;
@@ -146,62 +162,6 @@ bool BinanceJSONParser::GetQuoteInfoFromJSON(
146162
return true;
147163
}
148164

149-
// static
150-
// Response format:
151-
// {
152-
// "symbol": "BTCUSDT",
153-
// "price":"7265.82000000"
154-
// }
155-
//
156-
bool BinanceJSONParser::GetTickerPriceFromJSON(
157-
const std::string& json, std::string* symbol_pair_price) {
158-
DCHECK(symbol_pair_price);
159-
base::JSONReader::ValueWithError value_with_error =
160-
base::JSONReader::ReadAndReturnValueWithError(
161-
json, base::JSONParserOptions::JSON_PARSE_RFC);
162-
base::Optional<base::Value>& parsed_response = value_with_error.value;
163-
if (!parsed_response) {
164-
LOG(ERROR) << "Invalid response, could not parse JSON, JSON is: " << json;
165-
return false;
166-
}
167-
168-
const base::Value* price = parsed_response->FindKey("price");
169-
if (!price || !price->is_string()) {
170-
return false;
171-
}
172-
173-
*symbol_pair_price = price->GetString();
174-
return true;
175-
}
176-
177-
// static
178-
// Response Format:
179-
// {
180-
// "symbol":"BTCUSDT",
181-
// "volume":"1337"
182-
// }
183-
bool BinanceJSONParser::GetTickerVolumeFromJSON(
184-
const std::string& json, std::string* symbol_pair_volume) {
185-
DCHECK(symbol_pair_volume);
186-
187-
base::JSONReader::ValueWithError value_with_error =
188-
base::JSONReader::ReadAndReturnValueWithError(
189-
json, base::JSONParserOptions::JSON_PARSE_RFC);
190-
base::Optional<base::Value>& parsed_response = value_with_error.value;
191-
if (!parsed_response) {
192-
LOG(ERROR) << "Invalid response, could not parse JSON, JSON is: " << json;
193-
return false;
194-
}
195-
196-
const base::Value* volume = parsed_response->FindKey("volume");
197-
if (!volume || !volume->is_string()) {
198-
return false;
199-
}
200-
201-
*symbol_pair_volume = volume->GetString();
202-
return true;
203-
}
204-
205165
// static
206166
// Response Format:
207167
// {

components/binance/browser/binance_json_parser.h

+1-5
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,9 @@ class BinanceJSONParser {
1515
static bool GetTokensFromJSON(const std::string& json,
1616
std::string *value, std::string type);
1717
static bool GetAccountBalancesFromJSON(const std::string& json,
18-
std::map<std::string, std::string>*);
18+
std::map<std::string, std::vector<std::string>>* balances);
1919
static bool GetQuoteIDFromJSON(const std::string& json,
2020
std::string *quote_id);
21-
static bool GetTickerPriceFromJSON(const std::string& json,
22-
std::string* symbol_pair_price);
23-
static bool GetTickerVolumeFromJSON(const std::string& json,
24-
std::string* symbol_pair_volume);
2521
static bool GetDepositInfoFromJSON(const std::string& json,
2622
std::string* address,
2723
std::string* tag);

0 commit comments

Comments
 (0)