89 lines
No EOL
2.9 KiB
C++
89 lines
No EOL
2.9 KiB
C++
|
|
#include <graphene/peerplays_sidechain/bitcoin/libbitcoin_client.hpp>
|
|
|
|
#include <system_error>
|
|
|
|
#include <bitcoin/system/config/hash256.hpp>
|
|
#include <bitcoin/explorer/config/transaction.hpp>
|
|
|
|
#include <fc/log/logger.hpp>
|
|
#include <fc/crypto/hex.hpp>
|
|
|
|
namespace graphene { namespace peerplays_sidechain {
|
|
|
|
libbitcoin_client::libbitcoin_client() :
|
|
obelisk_client(10, 100) {
|
|
libbitcoin::client::connection_type connection;
|
|
connection.retries = 100;
|
|
connection.server = libbitcoin::config::endpoint("tcp://localhost", 9091);
|
|
|
|
if (!obelisk_client.connect(connection)) {
|
|
//display_connection_failure(error, connection.server);
|
|
//return console_result::failure;
|
|
std::cout << "Can't connect" << std::endl;
|
|
}
|
|
}
|
|
|
|
void libbitcoin_client::send_transaction(std::string tx) {
|
|
|
|
auto error_handler = [&](const std::error_code &ec) {
|
|
elog("error on sending bitcoin transaction ${error_code}", ("error_code", ec.message()));
|
|
};
|
|
|
|
auto result_handler = [&](libbitcoin::code result_code) {
|
|
ilog("result code on sending transaction ${result_code}", ("result_code", result_code.message()));
|
|
};
|
|
|
|
libbitcoin::explorer::config::transaction transaction(tx);
|
|
|
|
libbitcoin::chain::transaction trx;
|
|
|
|
// This validates the tx, submits it to local tx pool, and notifies peers.
|
|
obelisk_client.transaction_pool_broadcast(error_handler, result_handler, transaction);
|
|
obelisk_client.wait();
|
|
}
|
|
|
|
void libbitcoin_client::get_transaction(std::string tx_id) {
|
|
|
|
auto error_handler = [&](const std::error_code &ec) {
|
|
elog("error on fetch_trx_by_address ${error_code}", ("error_code", ec.message()));
|
|
};
|
|
|
|
auto transaction_handler = [&] (const libbitcoin::chain::transaction& tx_handler) {
|
|
|
|
};
|
|
|
|
libbitcoin::hash_digest tx_hash = libbitcoin::config::hash256(tx_id);
|
|
|
|
obelisk_client.blockchain_fetch_transaction (error_handler, transaction_handler,tx_hash);
|
|
|
|
obelisk_client.wait();
|
|
}
|
|
|
|
std::vector<list_unspent_replay> libbitcoin_client::list_unspent(std::string address, double amount) {
|
|
std::vector<list_unspent_replay> result;
|
|
|
|
auto error_handler = [&](const std::error_code &ec) {
|
|
elog("error on list_unspent ${error_code}", ("error_code", ec.message()));
|
|
};
|
|
|
|
auto replay_handler = [&](const libbitcoin::chain::points_value &points) {
|
|
for (auto &point : points.points) {
|
|
list_unspent_replay output;
|
|
output.hash = libbitcoin::config::hash256(point.hash()).to_string();
|
|
output.value = point.value();
|
|
output.index = point.index();
|
|
result.emplace_back(output);
|
|
}
|
|
};
|
|
|
|
libbitcoin::wallet::payment_address payment_address(address);
|
|
|
|
obelisk_client.blockchain_fetch_unspent_outputs(error_handler,
|
|
replay_handler, payment_address, 100000000, libbitcoin::wallet::select_outputs::algorithm::individual);
|
|
|
|
obelisk_client.wait();
|
|
|
|
return result;
|
|
}
|
|
}} // namespace graphene::peerplays_sidechain
|