Wallet API : transaction history in progress
This commit is contained in:
parent
663ed04eb8
commit
d500272fa8
8 changed files with 114 additions and 31 deletions
|
@ -35,14 +35,20 @@
|
||||||
|
|
||||||
#include "crypto/hash.h"
|
#include "crypto/hash.h"
|
||||||
#include "wallet/wallet2.h"
|
#include "wallet/wallet2.h"
|
||||||
#include "contrib/epee/include/string_tools.h"
|
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <list>
|
#include <list>
|
||||||
|
|
||||||
|
using namespace epee;
|
||||||
|
|
||||||
namespace Bitmonero {
|
namespace Bitmonero {
|
||||||
|
|
||||||
|
TransactionHistory::~TransactionHistory() {}
|
||||||
|
|
||||||
|
|
||||||
TransactionHistoryImpl::TransactionHistoryImpl(WalletImpl *wallet)
|
TransactionHistoryImpl::TransactionHistoryImpl(WalletImpl *wallet)
|
||||||
|
: m_wallet(wallet)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -54,7 +60,7 @@ TransactionHistoryImpl::~TransactionHistoryImpl()
|
||||||
|
|
||||||
int TransactionHistoryImpl::count() const
|
int TransactionHistoryImpl::count() const
|
||||||
{
|
{
|
||||||
return 0;
|
return m_history.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
TransactionInfo *TransactionHistoryImpl::transaction(const std::string &id) const
|
TransactionInfo *TransactionHistoryImpl::transaction(const std::string &id) const
|
||||||
|
@ -64,7 +70,7 @@ TransactionInfo *TransactionHistoryImpl::transaction(const std::string &id) cons
|
||||||
|
|
||||||
std::vector<TransactionInfo *> TransactionHistoryImpl::getAll() const
|
std::vector<TransactionInfo *> TransactionHistoryImpl::getAll() const
|
||||||
{
|
{
|
||||||
return std::vector<TransactionInfo*>();
|
return m_history;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TransactionHistoryImpl::refresh()
|
void TransactionHistoryImpl::refresh()
|
||||||
|
@ -73,9 +79,18 @@ void TransactionHistoryImpl::refresh()
|
||||||
uint64_t min_height = 0;
|
uint64_t min_height = 0;
|
||||||
uint64_t max_height = (uint64_t)-1;
|
uint64_t max_height = (uint64_t)-1;
|
||||||
|
|
||||||
// TODO: delete old transactions;
|
// delete old transactions;
|
||||||
|
for (auto t : m_history)
|
||||||
|
delete t;
|
||||||
|
|
||||||
std::list<std::pair<crypto::hash, tools::wallet2::payment_details>> payments;
|
std::list<std::pair<crypto::hash, tools::wallet2::payment_details>> payments;
|
||||||
|
|
||||||
|
// transactions are stored in wallet2:
|
||||||
|
// - confirmed_transfer_details - out transfers
|
||||||
|
// - unconfirmed_transfer_details - pending out transfers
|
||||||
|
// - payment_details - input transfers
|
||||||
|
|
||||||
|
// payments are "input transactions";
|
||||||
m_wallet->m_wallet->get_payments(payments, min_height, max_height);
|
m_wallet->m_wallet->get_payments(payments, min_height, max_height);
|
||||||
for (std::list<std::pair<crypto::hash, tools::wallet2::payment_details>>::const_iterator i = payments.begin(); i != payments.end(); ++i) {
|
for (std::list<std::pair<crypto::hash, tools::wallet2::payment_details>>::const_iterator i = payments.begin(); i != payments.end(); ++i) {
|
||||||
const tools::wallet2::payment_details &pd = i->second;
|
const tools::wallet2::payment_details &pd = i->second;
|
||||||
|
@ -83,9 +98,20 @@ void TransactionHistoryImpl::refresh()
|
||||||
if (payment_id.substr(16).find_first_not_of('0') == std::string::npos)
|
if (payment_id.substr(16).find_first_not_of('0') == std::string::npos)
|
||||||
payment_id = payment_id.substr(0,16);
|
payment_id = payment_id.substr(0,16);
|
||||||
// TODO
|
// TODO
|
||||||
TransactionInfo * ti = new TransactionInfo();
|
TransactionInfoImpl * ti = new TransactionInfoImpl();
|
||||||
|
ti->m_paymentid = payment_id;
|
||||||
|
ti->m_amount = pd.m_amount;
|
||||||
|
ti->m_direction = TransactionInfo::Direction_In;
|
||||||
|
ti->m_hash = string_tools::pod_to_hex(pd.m_tx_hash);
|
||||||
|
ti->m_blockheight = pd.m_block_height;
|
||||||
|
// TODO:
|
||||||
|
// ti->m_timestamp = pd.m_timestamp;
|
||||||
|
m_history.push_back(ti);
|
||||||
|
|
||||||
//output.insert(std::make_pair(pd.m_block_height, std::make_pair(true, (boost::format("%20.20s %s %s %s") % print_money(pd.m_amount) % string_tools::pod_to_hex(pd.m_tx_hash) % payment_id % "-").str())));
|
/* output.insert(std::make_pair(pd.m_block_height, std::make_pair(true, (boost::format("%20.20s %s %s %s")
|
||||||
|
% print_money(pd.m_amount)
|
||||||
|
% string_tools::pod_to_hex(pd.m_tx_hash)
|
||||||
|
% payment_id % "-").str())));*/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -47,6 +47,8 @@ public:
|
||||||
virtual void refresh();
|
virtual void refresh();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
// TransactionHistory is responsible of memory management
|
||||||
std::vector<TransactionInfo*> m_history;
|
std::vector<TransactionInfo*> m_history;
|
||||||
WalletImpl *m_wallet;
|
WalletImpl *m_wallet;
|
||||||
};
|
};
|
||||||
|
|
|
@ -35,7 +35,16 @@ using namespace std;
|
||||||
|
|
||||||
namespace Bitmonero {
|
namespace Bitmonero {
|
||||||
|
|
||||||
|
TransactionInfo::~TransactionInfo() {}
|
||||||
|
|
||||||
TransactionInfoImpl::TransactionInfoImpl()
|
TransactionInfoImpl::TransactionInfoImpl()
|
||||||
|
: m_direction(Direction_Out)
|
||||||
|
, m_hold(false)
|
||||||
|
, m_failed(false)
|
||||||
|
, m_amount(0)
|
||||||
|
, m_fee(0)
|
||||||
|
, m_blockheight(0)
|
||||||
|
, m_timestamp(0)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -53,37 +62,42 @@ int TransactionInfoImpl::direction() const
|
||||||
|
|
||||||
bool TransactionInfoImpl::isHold() const
|
bool TransactionInfoImpl::isHold() const
|
||||||
{
|
{
|
||||||
return false;
|
return m_hold;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TransactionInfoImpl::isFailed() const
|
bool TransactionInfoImpl::isFailed() const
|
||||||
{
|
{
|
||||||
return false;
|
return m_failed;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t TransactionInfoImpl::amount() const
|
uint64_t TransactionInfoImpl::amount() const
|
||||||
{
|
{
|
||||||
return 0;
|
return m_amount;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t TransactionInfoImpl::fee() const
|
uint64_t TransactionInfoImpl::fee() const
|
||||||
{
|
{
|
||||||
return 0;
|
return m_fee;
|
||||||
}
|
}
|
||||||
|
|
||||||
string TransactionInfoImpl::address() const
|
uint64_t TransactionInfoImpl::blockHeight() const
|
||||||
{
|
{
|
||||||
return "";
|
return m_blockheight;
|
||||||
|
}
|
||||||
|
|
||||||
|
string TransactionInfoImpl::hash() const
|
||||||
|
{
|
||||||
|
return m_hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::time_t TransactionInfoImpl::timestamp() const
|
std::time_t TransactionInfoImpl::timestamp() const
|
||||||
{
|
{
|
||||||
return std::time_t(0);
|
return m_timestamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
string TransactionInfoImpl::paymentId() const
|
string TransactionInfoImpl::paymentId() const
|
||||||
{
|
{
|
||||||
return "";
|
return m_paymentid;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
|
@ -41,12 +41,17 @@ class TransactionInfoImpl : public TransactionInfo
|
||||||
public:
|
public:
|
||||||
TransactionInfoImpl();
|
TransactionInfoImpl();
|
||||||
~TransactionInfoImpl();
|
~TransactionInfoImpl();
|
||||||
|
//! in/out
|
||||||
virtual int direction() const;
|
virtual int direction() const;
|
||||||
|
//! true if hold
|
||||||
virtual bool isHold() const;
|
virtual bool isHold() const;
|
||||||
virtual bool isFailed() const;
|
virtual bool isFailed() const;
|
||||||
virtual uint64_t amount() const;
|
virtual uint64_t amount() const;
|
||||||
|
//! always 0 for incoming txes
|
||||||
virtual uint64_t fee() const;
|
virtual uint64_t fee() const;
|
||||||
virtual std::string address() const;
|
virtual uint64_t blockHeight() const;
|
||||||
|
|
||||||
|
virtual std::string hash() const;
|
||||||
virtual std::time_t timestamp() const;
|
virtual std::time_t timestamp() const;
|
||||||
virtual std::string paymentId() const;
|
virtual std::string paymentId() const;
|
||||||
|
|
||||||
|
@ -56,8 +61,8 @@ private:
|
||||||
bool m_failed;
|
bool m_failed;
|
||||||
uint64_t m_amount;
|
uint64_t m_amount;
|
||||||
uint64_t m_fee;
|
uint64_t m_fee;
|
||||||
uint64_t b_blockheight;
|
uint64_t m_blockheight;
|
||||||
std::string m_address;
|
std::string m_hash;
|
||||||
std::time_t m_timestamp;
|
std::time_t m_timestamp;
|
||||||
std::string m_paymentid;
|
std::string m_paymentid;
|
||||||
|
|
||||||
|
|
|
@ -31,6 +31,7 @@
|
||||||
|
|
||||||
#include "wallet.h"
|
#include "wallet.h"
|
||||||
#include "pending_transaction.h"
|
#include "pending_transaction.h"
|
||||||
|
#include "transaction_history.h"
|
||||||
#include "common_defines.h"
|
#include "common_defines.h"
|
||||||
|
|
||||||
#include "mnemonics/electrum-words.h"
|
#include "mnemonics/electrum-words.h"
|
||||||
|
@ -58,10 +59,12 @@ WalletImpl::WalletImpl(bool testnet)
|
||||||
:m_wallet(nullptr), m_status(Wallet::Status_Ok)
|
:m_wallet(nullptr), m_status(Wallet::Status_Ok)
|
||||||
{
|
{
|
||||||
m_wallet = new tools::wallet2(testnet);
|
m_wallet = new tools::wallet2(testnet);
|
||||||
|
m_history = new TransactionHistoryImpl(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
WalletImpl::~WalletImpl()
|
WalletImpl::~WalletImpl()
|
||||||
{
|
{
|
||||||
|
delete m_history;
|
||||||
delete m_wallet;
|
delete m_wallet;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -388,7 +391,7 @@ void WalletImpl::disposeTransaction(PendingTransaction *t)
|
||||||
|
|
||||||
TransactionHistory *WalletImpl::history() const
|
TransactionHistory *WalletImpl::history() const
|
||||||
{
|
{
|
||||||
return nullptr;
|
return m_history;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WalletImpl::connectToDaemon()
|
bool WalletImpl::connectToDaemon()
|
||||||
|
|
|
@ -80,6 +80,7 @@ private:
|
||||||
int m_status;
|
int m_status;
|
||||||
std::string m_errorString;
|
std::string m_errorString;
|
||||||
std::string m_password;
|
std::string m_password;
|
||||||
|
TransactionHistoryImpl * m_history;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -56,17 +56,6 @@ struct PendingTransaction
|
||||||
virtual uint64_t fee() const = 0;
|
virtual uint64_t fee() const = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct TransactionInfo;
|
|
||||||
struct TransactionHistory
|
|
||||||
{
|
|
||||||
virtual int count() const;
|
|
||||||
virtual TransactionInfo * transaction(int index) const = 0;
|
|
||||||
virtual TransactionInfo * transaction(const std::string &id) const = 0;
|
|
||||||
virtual std::vector<TransactionInfo*> getAll() const = 0;
|
|
||||||
virtual void refresh() = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief The TransactionInfo - interface for displaying transaction information
|
* @brief The TransactionInfo - interface for displaying transaction information
|
||||||
*/
|
*/
|
||||||
|
@ -76,16 +65,31 @@ struct TransactionInfo
|
||||||
Direction_In,
|
Direction_In,
|
||||||
Direction_Out
|
Direction_Out
|
||||||
};
|
};
|
||||||
|
virtual ~TransactionInfo() = 0;
|
||||||
virtual int direction() const = 0;
|
virtual int direction() const = 0;
|
||||||
virtual bool isHold() const = 0;
|
virtual bool isHold() const = 0;
|
||||||
virtual bool isFailed() const = 0;
|
virtual bool isFailed() const = 0;
|
||||||
virtual uint64_t amount() const = 0;
|
virtual uint64_t amount() const = 0;
|
||||||
virtual uint64_t fee() const = 0;
|
virtual uint64_t fee() const = 0;
|
||||||
virtual std::string address() const = 0;
|
virtual uint64_t blockHeight() const = 0;
|
||||||
|
virtual std::string hash() const = 0;
|
||||||
virtual std::time_t timestamp() const = 0;
|
virtual std::time_t timestamp() const = 0;
|
||||||
virtual std::string paymentId() const = 0;
|
virtual std::string paymentId() const = 0;
|
||||||
};
|
};
|
||||||
|
/**
|
||||||
|
* @brief The TransactionHistory - interface for displaying transaction history
|
||||||
|
*/
|
||||||
|
struct TransactionHistory
|
||||||
|
{
|
||||||
|
virtual ~TransactionHistory() = 0;
|
||||||
|
virtual int count() const = 0;
|
||||||
|
virtual TransactionInfo * transaction(int index) const = 0;
|
||||||
|
virtual TransactionInfo * transaction(const std::string &id) const = 0;
|
||||||
|
virtual std::vector<TransactionInfo*> getAll() const = 0;
|
||||||
|
virtual void refresh() = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Interface for wallet operations.
|
* @brief Interface for wallet operations.
|
||||||
|
|
|
@ -29,7 +29,9 @@
|
||||||
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
|
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
|
||||||
|
|
||||||
#include "gtest/gtest.h"
|
#include "gtest/gtest.h"
|
||||||
|
|
||||||
#include "wallet/wallet2_api.h"
|
#include "wallet/wallet2_api.h"
|
||||||
|
|
||||||
#include <boost/filesystem.hpp>
|
#include <boost/filesystem.hpp>
|
||||||
#include <boost/algorithm/string.hpp>
|
#include <boost/algorithm/string.hpp>
|
||||||
|
|
||||||
|
@ -284,6 +286,32 @@ TEST_F(WalletManagerTest, WalletTransaction)
|
||||||
ASSERT_TRUE(wmgr->closeWallet(wallet1));
|
ASSERT_TRUE(wmgr->closeWallet(wallet1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(WalletManagerTest, WalletHistory)
|
||||||
|
{
|
||||||
|
Bitmonero::Wallet * wallet1 = wmgr->openWallet(TESTNET_WALLET_NAME, TESTNET_WALLET_PASS, true);
|
||||||
|
// make sure testnet daemon is running
|
||||||
|
ASSERT_TRUE(wallet1->init(TESTNET_DAEMON_ADDRESS, 0));
|
||||||
|
ASSERT_TRUE(wallet1->refresh());
|
||||||
|
Bitmonero::TransactionHistory * history = wallet1->history();
|
||||||
|
history->refresh();
|
||||||
|
ASSERT_TRUE(history->count() > 0);
|
||||||
|
auto transaction_print = [=] (Bitmonero::TransactionInfo * t) {
|
||||||
|
std::cout << "d: "
|
||||||
|
<< (t->direction() == Bitmonero::TransactionInfo::Direction_In ? "in" : "out")
|
||||||
|
<< ", bh: " << t->blockHeight()
|
||||||
|
<< ", a: " << Bitmonero::Wallet::displayAmount(t->amount())
|
||||||
|
<< ", f: " << Bitmonero::Wallet::displayAmount(t->fee())
|
||||||
|
<< ", h: " << t->hash()
|
||||||
|
<< ", pid: " << t->paymentId()
|
||||||
|
<< std::endl;
|
||||||
|
};
|
||||||
|
|
||||||
|
for (auto t: history->getAll()) {
|
||||||
|
ASSERT_TRUE(t != nullptr);
|
||||||
|
transaction_print(t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
|
|
Loading…
Reference in a new issue