-
Notifications
You must be signed in to change notification settings - Fork 888
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ability to retrieve transaction history
- Loading branch information
Showing
7 changed files
with
295 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* Copyright (c) 2019 The Brave Authors. All rights reserved. | ||
* 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/. */ | ||
|
||
#include "bat/ledger/transactions_info.h" | ||
|
||
#include "base/json/json_reader.h" | ||
#include "base/json/json_writer.h" | ||
#include "base/values.h" | ||
|
||
namespace confirmations { | ||
|
||
TransactionsInfo::TransactionsInfo() : | ||
transactions({}) {} | ||
|
||
TransactionsInfo::TransactionsInfo(const TransactionsInfo& info) : | ||
transactions(info.transactions) {} | ||
|
||
TransactionsInfo::~TransactionsInfo() = default; | ||
|
||
const std::string TransactionsInfo::ToJson() const { | ||
base::Value dictionary(base::Value::Type::DICTIONARY); | ||
|
||
base::Value list(base::Value::Type::LIST); | ||
for (const auto& transaction : transactions) { | ||
base::Value transaction_dictionary(base::Value::Type::DICTIONARY); | ||
|
||
transaction_dictionary.SetKey("timestamp_in_seconds", | ||
base::Value(std::to_string(transaction.timestamp_in_seconds))); | ||
|
||
transaction_dictionary.SetKey("estimated_redemption_value", | ||
base::Value(transaction.estimated_redemption_value)); | ||
|
||
list.GetList().push_back(std::move(transaction_dictionary)); | ||
} | ||
|
||
dictionary.SetKey("transactions", base::Value(std::move(list))); | ||
|
||
// Write to JSON | ||
std::string json; | ||
base::JSONWriter::Write(dictionary, &json); | ||
|
||
return json; | ||
} | ||
|
||
bool TransactionsInfo::FromJson( | ||
const std::string& json) { | ||
std::unique_ptr<base::DictionaryValue> dictionary = | ||
base::DictionaryValue::From(base::JSONReader::Read(json)); | ||
|
||
auto* transactions_value = dictionary->FindKey("transactions"); | ||
if (!transactions_value) { | ||
return false; | ||
} | ||
|
||
std::vector<TransactionInfo> new_transactions; | ||
|
||
base::ListValue transactions_list_value(transactions_value->GetList()); | ||
for (auto& transaction_value : transactions_list_value) { | ||
base::DictionaryValue* transaction_dictionary; | ||
if (!transaction_value.GetAsDictionary(&transaction_dictionary)) { | ||
return false; | ||
} | ||
|
||
TransactionInfo info; | ||
|
||
// Timestamp | ||
auto* timestamp_in_seconds_value = | ||
transaction_dictionary->FindKey("timestamp_in_seconds"); | ||
if (!timestamp_in_seconds_value) { | ||
return false; | ||
} | ||
info.timestamp_in_seconds = | ||
std::stoull(timestamp_in_seconds_value->GetString()); | ||
|
||
// Estimated redemption value | ||
auto* estimated_redemption_value_value = | ||
transaction_dictionary->FindKey("estimated_redemption_value"); | ||
if (!estimated_redemption_value_value) { | ||
return false; | ||
} | ||
info.estimated_redemption_value = | ||
estimated_redemption_value_value->GetDouble(); | ||
|
||
new_transactions.push_back(info); | ||
} | ||
|
||
transactions = new_transactions; | ||
|
||
return true; | ||
} | ||
|
||
} // namespace confirmations |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.