From 8551afa7eaf909cdac49ac4b14590177a0b5e974 Mon Sep 17 00:00:00 2001 From: NejcZdovc Date: Fri, 27 Jul 2018 14:13:42 +0200 Subject: [PATCH] Adds wallet properites --- include/bat/ledger/ledger.h | 1 + include/bat/ledger/ledger_client.h | 3 ++ include/bat/ledger/wallet_info.h | 38 ++++++++++++++++ src/bat/ledger/ledger.cc | 12 +++++ src/bat_client.cc | 72 +++++++++++++++++------------- src/bat_client.h | 5 ++- src/bat_helper.cc | 66 +++++++++++++++++++++++---- src/bat_helper.h | 44 +++++++++++------- src/ledger_impl.cc | 39 +++++++++++----- src/ledger_impl.h | 2 + src/static_values.h | 2 +- 11 files changed, 213 insertions(+), 71 deletions(-) create mode 100644 include/bat/ledger/wallet_info.h diff --git a/include/bat/ledger/ledger.h b/include/bat/ledger/ledger.h index fc94520..7bc48e3 100644 --- a/include/bat/ledger/ledger.h +++ b/include/bat/ledger/ledger.h @@ -72,6 +72,7 @@ class LEDGER_EXPORT Ledger { virtual unsigned int GetPublisherMinVisits() const = 0; virtual bool GetPublisherAllowNonVerified() const = 0; virtual double GetContributionAmount() const = 0; + virtual void GetWalletProperties() const = 0; }; } // namespace ledger diff --git a/include/bat/ledger/ledger_client.h b/include/bat/ledger/ledger_client.h index 010a9e1..b78810f 100644 --- a/include/bat/ledger/ledger_client.h +++ b/include/bat/ledger/ledger_client.h @@ -14,6 +14,7 @@ #include "bat/ledger/ledger_task_runner.h" #include "bat/ledger/ledger_url_loader.h" #include "bat/ledger/publisher_info.h" +#include "bat/ledger/wallet_info.h" namespace ledger { @@ -35,6 +36,8 @@ class LEDGER_EXPORT LedgerClient { // called when the wallet creation has completed virtual std::string GenerateGUID() const = 0; virtual void OnWalletCreated(Result result) = 0; + virtual void GetWalletProperties() = 0; + virtual void OnWalletProperties(ledger::WalletInfo) = 0; virtual void OnReconcileComplete(Result result, const std::string& viewing_id) = 0; diff --git a/include/bat/ledger/wallet_info.h b/include/bat/ledger/wallet_info.h new file mode 100644 index 0000000..22a24ee --- /dev/null +++ b/include/bat/ledger/wallet_info.h @@ -0,0 +1,38 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public +* License, v. 2.0. If a copy of the MPL was not distributed with this +* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef BAT_LEDGER_WALLET_INFO_HANDLER_ +#define BAT_LEDGER_WALLET_INFO_HANDLER_ + +#include +#include +#include + +#include "bat/ledger/export.h" + +namespace ledger { + +struct GRANT { + std::string altcurrency; + std::string probi; + unsigned int expiryTime; +}; + +LEDGER_EXPORT struct WalletInfo { + WalletInfo(); + ~WalletInfo(); + WalletInfo(const WalletInfo& info); + std::string altcurrency_; + std::string probi_; + double balance_; + std::map rates_; + std::vector parameters_choices_; + std::vector parameters_range_; + unsigned int parameters_days_; + std::vector grants_; +}; + +} // namespace ledger + +#endif // BAT_LEDGER_WALLET_INFO_HANDLER_ diff --git a/src/bat/ledger/ledger.cc b/src/bat/ledger/ledger.cc index d578101..cce5b97 100644 --- a/src/bat/ledger/ledger.cc +++ b/src/bat/ledger/ledger.cc @@ -137,4 +137,16 @@ ledger::Ledger* Ledger::CreateInstance(LedgerClient* client) { return new bat_ledger::LedgerImpl(client); } +WalletInfo::WalletInfo () : balance_(0), parameters_days_(0) {} +WalletInfo::~WalletInfo () {} +WalletInfo::WalletInfo (const ledger::WalletInfo &info) { + altcurrency_ = info.altcurrency_; + probi_ = info.probi_; + balance_ = info.balance_; + rates_ = info.rates_; + parameters_choices_ = info.parameters_choices_; + parameters_range_ = info.parameters_range_; + parameters_days_ = info.parameters_days_; + grants_ = info.grants_; +} } diff --git a/src/bat_client.cc b/src/bat_client.cc index d2178fa..f262e19 100644 --- a/src/bat_client.cc +++ b/src/bat_client.cc @@ -19,19 +19,6 @@ using namespace std::placeholders; namespace braveledger_bat_client { -// namespace { -// std::string GetBalanceURL(const std::string& path, const std::string& prefix) { -// std::string url; -// if (braveledger_ledger::g_isProduction) { -// url = BALANCE_PRODUCTION_SERVER; -// } else { -// url = BALANCE_STAGING_SERVER; -// } - -// return url + prefix + path; -// } -// } - BatClient::BatClient(bat_ledger::LedgerImpl* ledger) : ledger_(ledger), state_(new braveledger_bat_helper::CLIENT_STATE_ST()), @@ -44,12 +31,20 @@ BatClient::BatClient(bat_ledger::LedgerImpl* ledger) : BatClient::~BatClient() { } -std::string BatClient::buildURL(const std::string& path, const std::string& prefix) { +std::string BatClient::buildURL(const std::string& path, const std::string& prefix, const bool isBalance = false) { std::string url; - if (braveledger_ledger::g_isProduction) { - url = LEDGER_PRODUCTION_SERVER; + if (isBalance) { + if (braveledger_ledger::g_isProduction) { + url = BALANCE_PRODUCTION_SERVER; + } else { + url = BALANCE_STAGING_SERVER; + } } else { - url = LEDGER_STAGING_SERVER; + if (braveledger_ledger::g_isProduction) { + url = LEDGER_PRODUCTION_SERVER; + } else { + url = LEDGER_STAGING_SERVER; + } } return url + prefix + path; @@ -214,20 +209,35 @@ const std::string& BatClient::getLTCAddress() const { return state_->walletInfo_.addressLTC_; } -// void BatClient::getWalletProperties(const braveledger_bat_helper::FETCH_CALLBACK_EXTRA_DATA_ST& extraData) { -// auto request_id = ledger_->LoadURL( -// GetBalanceURL((std::string)WALLET_PROPERTIES + state_->walletInfo_.paymentId_ + WALLET_PROPERTIES_END, ""), -// std::vector(), -// "", -// "", -// ledger::URL_METHOD::GET, &handler_); -// handler_.AddRequestHandler(std::move(request_id), -// std::bind(&BatClient::publisherTimestampCallback, -// this, -// _1, -// _2, -// extra_data)); -// } +void BatClient::getWalletProperties() { + std::string path = (std::string)WALLET_PROPERTIES + state_->walletInfo_.paymentId_ + WALLET_PROPERTIES_END; + auto request_id = ledger_->LoadURL( + buildURL(path, PREFIX_V2, true), + std::vector(), + "", + "", + ledger::URL_METHOD::GET, + &handler_); + + handler_.AddRequestHandler(std::move(request_id), + std::bind(&BatClient::walletPropertiesCallback, + this, + _1, + _2)); + } + + void BatClient::walletPropertiesCallback(bool success, + const std::string& response) { + if (!success) { + LOG(ERROR) << "walletPropertiesCallback error"; + return; + } + + braveledger_bat_helper::WALLET_PROPERTIES_ST properties; + braveledger_bat_helper::loadFromJson(properties, response); + state_->walletProperties_ = properties; + ledger_->OnWalletProperties(properties); + } bool BatClient::isReadyForReconcile() { // TODO real check of reconcile timestamp diff --git a/src/bat_client.h b/src/bat_client.h index 98b5c16..f1f7541 100644 --- a/src/bat_client.h +++ b/src/bat_client.h @@ -40,11 +40,12 @@ class BatClient : public ledger::LedgerCallbackHandler { void votePublishers(const std::vector& publishers, const std::string& viewingId); void prepareBallots(); std::string getWalletPassphrase(); + void walletPropertiesCallback(bool success, const std::string& response); void recoverWallet(const std::string& passPhrase); void getPromotion(const std::string& lang, const std::string& forPaymentId); void setPromotion(const std::string& promotionId, const std::string& captchaResponse); void getPromotionCaptcha(); - //void getWalletProperties(const braveledger_bat_helper::FETCH_CALLBACK_EXTRA_DATA_ST& extraData); + void getWalletProperties(); private: void saveState(); @@ -76,7 +77,7 @@ class BatClient : public ledger::LedgerCallbackHandler { void viewingCredentials(const std::string& proofStringified, const std::string& anonizeViewingId); void viewingCredentialsCallback(bool result, const std::string& response); std::string getAnonizeProof(const std::string& registrarVK, const std::string& id, std::string& preFlight); - std::string buildURL(const std::string& path, const std::string& prefix); + std::string buildURL(const std::string& path, const std::string& prefix, const bool isBalance); bat_ledger::LedgerImpl* ledger_; // NOT OWNED std::unique_ptr state_; diff --git a/src/bat_helper.cc b/src/bat_helper.cc index fa72c81..e86fad4 100644 --- a/src/bat_helper.cc +++ b/src/bat_helper.cc @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -741,10 +742,21 @@ namespace braveledger_bat_helper { WINNERS_ST::~WINNERS_ST() {} ///////////////////////////////////////////////////////////////////////////// - WALLET_PROPERTIES_ST::WALLET_PROPERTIES_ST() {} + WALLET_PROPERTIES_ST::WALLET_PROPERTIES_ST() : balance_(0), parameters_days_(0) {} WALLET_PROPERTIES_ST::~WALLET_PROPERTIES_ST() {} + WALLET_PROPERTIES_ST::WALLET_PROPERTIES_ST(const WALLET_PROPERTIES_ST &properties) { + altcurrency_ = properties.altcurrency_; + probi_ = properties.probi_; + balance_ = properties.balance_; + rates_ = properties.rates_; + parameters_choices_ = properties.parameters_choices_; + parameters_range_ = properties.parameters_range_; + parameters_days_ = properties.parameters_days_; + grants_ = properties.grants_; + } + bool WALLET_PROPERTIES_ST::loadFromJson(const std::string & json) { rapidjson::Document d; d.Parse(json.c_str()); @@ -752,22 +764,31 @@ namespace braveledger_bat_helper { //has parser errors or wrong types bool error = d.HasParseError(); if (false == error) { - error = !(d.HasMember("altcurrency") && d["altcurrency"].IsString() && - d.HasMember("balance") && d["balance"].IsDouble() && + error = !( + d.HasMember("altcurrency") && d["altcurrency"].IsString() && + d.HasMember("balance") && d["balance"].IsString() && + d.HasMember("probi") && d["probi"].IsString() && d.HasMember("rates") && d["rates"].IsObject() && - d.HasMember("parameters") && d["parameters"].IsObject() ); + d.HasMember("parameters") && d["parameters"].IsObject() + ); } if (false == error) { altcurrency_ = d["altcurrency"].GetString(); - balance_ = d["balance"].GetDouble(); + balance_ = std::stod(d["balance"].GetString()); + probi_ = d["probi"].GetString(); for (auto & i : d["rates"].GetObject()) { - rates_.insert(std::make_pair(i.name.GetString(), i.value.GetDouble())); - } + double value = 0.0; - parameters_currency_ = d["parameters"]["adFree"]["currency"].GetString(); - parameters_fee_ = d["parameters"]["adFree"]["fee"]["BAT"].GetDouble(); + // For some reason BTC is returned as string, where others are double + if (i.value.IsDouble()) { + value = i.value.GetDouble(); + } else if (i.value.IsString()) { + value = std::stod(i.value.GetString()); + } + rates_.insert(std::make_pair(i.name.GetString(), value)); + } for (auto & i : d["parameters"]["adFree"]["choices"]["BAT"].GetArray()) { parameters_choices_.push_back(i.GetDouble()); @@ -778,10 +799,37 @@ namespace braveledger_bat_helper { } parameters_days_ = d["parameters"]["adFree"]["days"].GetUint(); + + if (d.HasMember("grants") && d["grants"].IsObject()) { + for (auto &i : d["grants"].GetArray()) { + GRANT grant; + auto obj = i.GetObject(); + if (obj.HasMember("probi")) { + grant.probi = obj["probi"].GetString(); + } + + if (obj.HasMember("altcurrency")) { + grant.altcurrency = obj["altcurrency"].GetString(); + } + + if (obj.HasMember("expiryTime")) { + grant.expiryTime = obj["expiryTime"].GetUint64(); + } + + grants_.push_back(grant); + } + } else { + grants_.clear(); + } } return !error; } + ///////////////////////////////////////////////////////////////////////////// + GRANT::GRANT() : expiryTime(0) {} + + GRANT::~GRANT() {} + ///////////////////////////////////////////////////////////////////////////// SURVEYOR_INFO_ST::SURVEYOR_INFO_ST() {} diff --git a/src/bat_helper.h b/src/bat_helper.h index 3b0a908..f978dd4 100644 --- a/src/bat_helper.h +++ b/src/bat_helper.h @@ -149,6 +149,32 @@ namespace braveledger_bat_helper { std::vector batchVotesInfo_; }; + struct GRANT { + GRANT(); + ~GRANT(); + std::string altcurrency; + std::string probi; + uint64_t expiryTime; + }; + + struct WALLET_PROPERTIES_ST { + WALLET_PROPERTIES_ST(); + ~WALLET_PROPERTIES_ST(); + WALLET_PROPERTIES_ST(const WALLET_PROPERTIES_ST& properties); + + //load from json string + bool loadFromJson(const std::string & json); + + std::string altcurrency_; + std::string probi_; + double balance_; + std::map rates_; + std::vector parameters_choices_; + std::vector parameters_range_; + unsigned int parameters_days_; + std::vector grants_; + }; + struct CLIENT_STATE_ST { CLIENT_STATE_ST(); CLIENT_STATE_ST(const CLIENT_STATE_ST&); @@ -158,6 +184,7 @@ namespace braveledger_bat_helper { bool loadFromJson(const std::string & json); WALLET_INFO_ST walletInfo_; + WALLET_PROPERTIES_ST walletProperties_; uint64_t bootStamp_ = 0u; uint64_t reconcileStamp_ = 0u; std::string personaId_; @@ -209,23 +236,6 @@ namespace braveledger_bat_helper { unsigned int votes_ = 0; }; - struct WALLET_PROPERTIES_ST { - WALLET_PROPERTIES_ST(); - ~WALLET_PROPERTIES_ST(); - - //load from json string - bool loadFromJson(const std::string & json); - - std::string altcurrency_; - double balance_; - std::map rates_; - std::string parameters_currency_; - double parameters_fee_; - std::vector parameters_choices_; - std::vector parameters_range_; - unsigned int parameters_days_; - }; - struct SURVEYOR_INFO_ST { SURVEYOR_INFO_ST(); ~SURVEYOR_INFO_ST(); diff --git a/src/ledger_impl.cc b/src/ledger_impl.cc index c25053b..2a2c3fc 100644 --- a/src/ledger_impl.cc +++ b/src/ledger_impl.cc @@ -121,17 +121,6 @@ void LedgerImpl::initSynopsis() { bat_publishers_->initSynopsis(); } -// void LedgerImpl::walletPropertiesCallback(bool success, -// const std::string& response) { -// if (!success) { -// // TODO errors handling -// return; -// } -// braveledger_bat_helper::WALLET_PROPERTIES_ST walletProperties; -// braveledger_bat_helper::loadFromJson(walletProperties, response); -// // TODO send the balance to the UI via observer or callback -// } - void LedgerImpl::RunIOTask(LedgerTaskRunnerImpl::Task io_task) { std::unique_ptr task_runner( new LedgerTaskRunnerImpl(io_task)); @@ -298,4 +287,32 @@ void LedgerImpl::OnMediaRequestCallback(uint64_t duration, const braveledger_bat // SaveVisit(mediaPublisherInfo.publisher_, duration, true); } +void LedgerImpl::OnWalletProperties(const braveledger_bat_helper::WALLET_PROPERTIES_ST& properties) { + ledger::WalletInfo info; + + info.altcurrency_ = properties.altcurrency_; + info.probi_ = properties.probi_; + info.balance_ = properties.balance_; + info.rates_ = properties.rates_; + info.parameters_choices_ = properties.parameters_choices_; + info.parameters_range_ = properties.parameters_range_; + info.parameters_days_ = properties.parameters_days_; + + for (size_t i = 0; i < properties.grants_.size(); i ++) { + ledger::GRANT grant; + + grant.altcurrency = properties.grants_[i].altcurrency; + grant.probi = properties.grants_[i].probi; + grant.expiryTime = properties.grants_[i].expiryTime; + + info.grants_.push_back(grant); + } + + ledger_client_->OnWalletProperties(info); +} + +void LedgerImpl::GetWalletProperties() const { + bat_client_->getWalletProperties(); +} + } // namespace bat_ledger diff --git a/src/ledger_impl.h b/src/ledger_impl.h index a1bc7bd..462ef8f 100644 --- a/src/ledger_impl.h +++ b/src/ledger_impl.h @@ -73,6 +73,8 @@ class LedgerImpl : public ledger::Ledger, void LoadPublisherState(ledger::LedgerCallbackHandler* handler); void OnWalletCreated(ledger::Result); + void OnWalletProperties(const braveledger_bat_helper::WALLET_PROPERTIES_ST&); + void GetWalletProperties() const override; std::unique_ptr LoadURL(const std::string& url, const std::vector& headers, diff --git a/src/static_values.h b/src/static_values.h index 9c1d9ed..9d8a876 100644 --- a/src/static_values.h +++ b/src/static_values.h @@ -16,7 +16,7 @@ #define PREFIX_V3 "/v3" #define REGISTER_PERSONA "/registrar/persona" #define REGISTER_VIEWING "/registrar/viewing" -#define WALLET_PROPERTIES "/v2/wallet/" +#define WALLET_PROPERTIES "/wallet/" #define WALLET_PROPERTIES_END "/balance" #define RECONCILE_CONTRIBUTION "/surveyor/contribution/current/" #define SURVEYOR_VOTING "/surveyor/voting/"