BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
wallet_results.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Cryptonomex, Inc., and contributors.
3  *
4  * The MIT License
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #include <fc/io/sstream.hpp>
25 #include "wallet_api_impl.hpp"
26 #include "operation_printer.hpp"
27 
28 namespace graphene { namespace wallet { namespace detail {
29 
30  std::map< string, std::function< string( const fc::variant&, const fc::variants& ) >, std::less<> >
32  {
33  std::map< string, std::function< string( const fc::variant&, const fc::variants& ) >, std::less<> > m;
34 
35  m["help"] = [](const variant& result, const fc::variants&)
36  {
37  return result.get_string();
38  };
39 
40  m["gethelp"] = [](const variant& result, const fc::variants&)
41  {
42  return result.get_string();
43  };
44 
45  auto format_account_history = [this](const variant& result, const fc::variants&)
46  {
47  auto r = result.as<vector<operation_detail>>( GRAPHENE_MAX_NESTED_OBJECTS );
48  std::stringstream ss;
49 
50  for( operation_detail& d : r )
51  {
53  ss << i.block_num << " ";
54  ss << i.block_time.to_iso_string() << " ";
55  ss << string(i.id) << " ";
56  i.op.visit(operation_printer(ss, *this, i));
57  ss << " \n";
58  }
59 
60  return ss.str();
61  };
62 
63  m["get_account_history"] = format_account_history;
64  m["get_relative_account_history"] = format_account_history;
65 
66  m["get_account_history_by_operations"] = [this](const variant& result, const fc::variants&) {
68  std::stringstream ss;
69  ss << "total_count : " << r.total_count << " \n";
70  ss << "result_count : " << r.result_count << " \n";
71  for (operation_detail_ex& d : r.details) {
73  ss << i.block_num << " ";
74  ss << i.block_time.to_iso_string() << " ";
75  ss << string(i.id) << " ";
76  i.op.visit(operation_printer(ss, *this, i));
77  ss << " transaction_id : ";
78  ss << d.transaction_id.str();
79  ss << " \n";
80  }
81 
82  return ss.str();
83  };
84 
85  auto format_balances = [this](const variant& result, const fc::variants&)
86  {
87  auto r = result.as<vector<asset>>( GRAPHENE_MAX_NESTED_OBJECTS );
88  vector<asset_object> asset_recs;
89  std::transform(r.begin(), r.end(), std::back_inserter(asset_recs), [this](const asset& a) {
90  return get_asset(a.asset_id);
91  });
92 
93  std::stringstream ss;
94  for( unsigned i = 0; i < asset_recs.size(); ++i )
95  ss << asset_recs[i].amount_to_pretty_string(r[i]) << "\n";
96 
97  return ss.str();
98  };
99 
100  m["list_account_balances"] = format_balances;
101  m["get_blind_balances"] = format_balances;
102 
103  auto format_blind_transfers = [this](const variant& result, const fc::variants&)
104  {
106  std::stringstream ss;
107  r.trx.operations[0].visit( operation_printer( ss, *this, operation_history_object() ) );
108  ss << "\n";
109  for( const auto& out : r.outputs )
110  {
111  auto a = get_asset( out.decrypted_memo.amount.asset_id );
112  ss << a.amount_to_pretty_string( out.decrypted_memo.amount ) << " to " << out.label
113  << "\n\t receipt: " << out.confirmation_receipt << "\n\n";
114  }
115  return ss.str();
116  };
117 
118  m["transfer_to_blind"] = format_blind_transfers;
119  m["blind_transfer"] = format_blind_transfers;
120 
121  m["receive_blind_transfer"] = [this](const variant& result, const fc::variants&)
122  {
123  auto r = result.as<blind_receipt>( GRAPHENE_MAX_NESTED_OBJECTS );
124  std::stringstream ss;
125  auto as = get_asset( r.amount.asset_id );
126  ss << as.amount_to_pretty_string( r.amount ) << " " << r.from_label << " => "
127  << r.to_label << " " << r.memo <<"\n";
128  return ss.str();
129  };
130 
131  m["blind_history"] = [this](const variant& result, const fc::variants&)
132  {
133  auto records = result.as<vector<blind_receipt>>( GRAPHENE_MAX_NESTED_OBJECTS );
134  std::stringstream ss;
135  ss << "WHEN "
136  << " " << "AMOUNT" << " " << "FROM" << " => " << "TO" << " " << "MEMO" <<"\n";
137  ss << "====================================================================================\n";
138  for( auto& r : records )
139  {
140  auto as = get_asset( r.amount.asset_id );
142  << " " << as.amount_to_pretty_string( r.amount ) << " " << r.from_label << " => " << r.to_label
143  << " " << r.memo <<"\n";
144  }
145  return ss.str();
146  };
147 
148  m["get_order_book"] = [](const variant& result, const fc::variants&)
149  {
150  auto orders = result.as<order_book>( GRAPHENE_MAX_NESTED_OBJECTS );
151  auto bids = orders.bids;
152  auto asks = orders.asks;
153  std::stringstream ss;
154  std::stringstream sum_stream;
155  sum_stream << "Sum(" << orders.base << ')';
156  double bid_sum = 0;
157  double ask_sum = 0;
158  const int spacing = 20;
159 
160  auto prettify_num = [&ss]( double n )
161  {
162  if (abs( round( n ) - n ) < 0.00000000001 )
163  {
164  ss << (int) n;
165  }
166  else if (n - floor(n) < 0.000001)
167  {
168  ss << setiosflags( ios::fixed ) << setprecision(10) << n;
169  }
170  else
171  {
172  ss << setiosflags( ios::fixed ) << setprecision(6) << n;
173  }
174  };
175  auto prettify_num_string = [&]( string& num_string )
176  {
177  double n = fc::to_double( num_string );
178  prettify_num( n );
179  };
180 
181  ss << setprecision( 8 ) << setiosflags( ios::fixed ) << setiosflags( ios::left );
182 
183  ss << ' ' << setw( (spacing * 4) + 6 ) << "BUY ORDERS" << "SELL ORDERS\n"
184  << ' ' << setw( spacing + 1 ) << "Price" << setw( spacing ) << orders.quote << ' ' << setw( spacing )
185  << orders.base << ' ' << setw( spacing ) << sum_stream.str()
186  << " " << setw( spacing + 1 ) << "Price" << setw( spacing ) << orders.quote << ' ' << setw( spacing )
187  << orders.base << ' ' << setw( spacing ) << sum_stream.str()
188  << "\n====================================================================================="
189  << "|=====================================================================================\n";
190 
191  for (unsigned int i = 0; i < bids.size() || i < asks.size() ; i++)
192  {
193  if ( i < bids.size() )
194  {
195  bid_sum += fc::to_double( bids[i].base );
196  ss << ' ' << setw( spacing );
197  prettify_num_string( bids[i].price );
198  ss << ' ' << setw( spacing );
199  prettify_num_string( bids[i].quote );
200  ss << ' ' << setw( spacing );
201  prettify_num_string( bids[i].base );
202  ss << ' ' << setw( spacing );
203  prettify_num( bid_sum );
204  ss << ' ';
205  }
206  else
207  {
208  ss << setw( (spacing * 4) + 5 ) << ' ';
209  }
210 
211  ss << '|';
212 
213  if ( i < asks.size() )
214  {
215  ask_sum += fc::to_double( asks[i].base );
216  ss << ' ' << setw( spacing );
217  prettify_num_string( asks[i].price );
218  ss << ' ' << setw( spacing );
219  prettify_num_string( asks[i].quote );
220  ss << ' ' << setw( spacing );
221  prettify_num_string( asks[i].base );
222  ss << ' ' << setw( spacing );
223  prettify_num( ask_sum );
224  }
225 
226  ss << '\n';
227  }
228 
229  ss << endl
230  << "Buy Total: " << bid_sum << ' ' << orders.base << endl
231  << "Sell Total: " << ask_sum << ' ' << orders.base << endl;
232 
233  return ss.str();
234  };
235 
236  m["sign_message"] = [](const variant& result, const fc::variants&)
237  {
238  auto r = result.as<signed_message>( GRAPHENE_MAX_NESTED_OBJECTS );
239 
240  fc::stringstream encapsulated;
241  encapsulated << ENC_HEADER;
242  encapsulated << r.message << '\n';
243  encapsulated << ENC_META;
244  encapsulated << "account=" << r.meta.account << '\n';
245  encapsulated << "memokey=" << std::string( r.meta.memo_key ) << '\n';
246  encapsulated << "block=" << r.meta.block << '\n';
247  encapsulated << "timestamp=" << r.meta.time << '\n';
248  encapsulated << ENC_SIG;
249  encapsulated << fc::to_hex( (const char*)r.signature->data(), r.signature->size() ) << '\n';
250  encapsulated << ENC_FOOTER;
251 
252  return encapsulated.str();
253  };
254 
255  return m;
256  }
257 
258 }}} // namespace graphene::wallet::detail
graphene::protocol::transaction::operations
vector< operation > operations
Definition: transaction.hpp:89
graphene::wallet::account_history_operation_detail
Definition: wallet_structs.hpp:316
graphene::db::object::id
object_id_type id
Definition: object.hpp:69
graphene::wallet::detail::wallet_api_impl::get_asset
extended_asset_object get_asset(asset_id_type id) const
Definition: wallet_asset.cpp:63
graphene::protocol::price
The price struct stores asset prices in the BitShares system.
Definition: asset.hpp:108
graphene::chain::operation_history_object::op
operation op
Definition: operation_history_object.hpp:58
fc::variant::get_string
const std::string & get_string() const
Definition: variant.cpp:575
graphene::wallet::operation_detail_ex::transaction_id
transaction_id_type transaction_id
Definition: wallet_structs.hpp:313
wallet_api_impl.hpp
graphene::wallet::operation_detail_ex::op
operation_history_object op
Definition: wallet_structs.hpp:312
fc::to_hex
std::string to_hex(const char *d, uint32_t s)
Definition: hex.cpp:17
operation_printer.hpp
graphene::chain::operation_history_object::block_num
uint32_t block_num
Definition: operation_history_object.hpp:61
fc::to_double
double to_double(const std::string &)
Definition: string.cpp:60
fc::variants
std::vector< variant > variants
Definition: variant.hpp:170
sstream.hpp
fc::variant::as
T as(uint32_t max_depth) const
Definition: variant.hpp:337
fc::ripemd160::str
string str() const
Definition: ripemd160.cpp:21
graphene::wallet::operation_detail
Definition: wallet_structs.hpp:303
graphene::app::order_book
Definition: api_objects.hpp:102
fc::typelist::transform
typename impl::transform< List, Transformer >::type transform
Transform elements of a typelist.
Definition: typelist.hpp:170
fc::get_approximate_relative_time_string
std::string get_approximate_relative_time_string(const time_point_sec &event_time, const time_point_sec &relative_to_time=fc::time_point::now(), const std::string &ago=" ago")
Definition: time.cpp:70
fc::variant
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition: variant.hpp:198
graphene::wallet::blind_receipt
Definition: wallet_structs.hpp:85
graphene::wallet::detail::wallet_api_impl::get_result_formatters
std::map< string, std::function< string(const fc::variant &, const fc::variants &) >, std::less<> > get_result_formatters() const
Definition: wallet_results.cpp:31
GRAPHENE_MAX_NESTED_OBJECTS
#define GRAPHENE_MAX_NESTED_OBJECTS
Definition: config.hpp:33
fc::static_variant::visit
visitor::result_type visit(visitor &v)
Definition: static_variant.hpp:256
graphene::chain::operation_history_object::block_time
time_point_sec block_time
Definition: operation_history_object.hpp:71
graphene::app::order_book::bids
vector< order > bids
Definition: api_objects.hpp:106
fc::stringstream
Definition: sstream.hpp:7
graphene::protocol::asset
Definition: asset.hpp:31
graphene::wallet::signed_message
Definition: wallet_structs.hpp:260
graphene::wallet::account_history_operation_detail::total_count
uint32_t total_count
Definition: wallet_structs.hpp:317
graphene::wallet::detail::operation_printer
Definition: operation_printer.hpp:56
graphene::wallet::operation_detail_ex
Definition: wallet_structs.hpp:309
graphene
Definition: api.cpp:48
fc::time_point_sec::to_iso_string
std::string to_iso_string() const
Definition: time.cpp:24
graphene::wallet::blind_confirmation
Definition: wallet_structs.hpp:58
graphene::wallet::blind_confirmation::trx
signed_transaction trx
Definition: wallet_structs.hpp:70
graphene::chain::operation_history_object
tracks the history of all logical operations on blockchain state
Definition: operation_history_object.hpp:48