BitShares-Core  6.1.0
BitShares blockchain implementation and command-line interface 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  auto b = _remote_db->get_block_header(i.block_num);
54  FC_ASSERT(b);
55  ss << i.block_num << " ";
56  ss << b->timestamp.to_iso_string() << " ";
57  ss << string(i.id) << " ";
58  i.op.visit(operation_printer(ss, *this, i));
59  ss << " \n";
60  }
61 
62  return ss.str();
63  };
64 
65  m["get_account_history"] = format_account_history;
66  m["get_relative_account_history"] = format_account_history;
67 
68  m["get_account_history_by_operations"] = [this](const variant& result, const fc::variants&) {
70  std::stringstream ss;
71  ss << "total_count : " << r.total_count << " \n";
72  ss << "result_count : " << r.result_count << " \n";
73  for (operation_detail_ex& d : r.details) {
75  auto b = _remote_db->get_block_header(i.block_num);
76  FC_ASSERT(b);
77  ss << i.block_num << " ";
78  ss << b->timestamp.to_iso_string() << " ";
79  ss << string(i.id) << " ";
80  i.op.visit(operation_printer(ss, *this, i));
81  ss << " transaction_id : ";
82  ss << d.transaction_id.str();
83  ss << " \n";
84  }
85 
86  return ss.str();
87  };
88 
89  auto format_balances = [this](const variant& result, const fc::variants&)
90  {
91  auto r = result.as<vector<asset>>( GRAPHENE_MAX_NESTED_OBJECTS );
92  vector<asset_object> asset_recs;
93  std::transform(r.begin(), r.end(), std::back_inserter(asset_recs), [this](const asset& a) {
94  return get_asset(a.asset_id);
95  });
96 
97  std::stringstream ss;
98  for( unsigned i = 0; i < asset_recs.size(); ++i )
99  ss << asset_recs[i].amount_to_pretty_string(r[i]) << "\n";
100 
101  return ss.str();
102  };
103 
104  m["list_account_balances"] = format_balances;
105  m["get_blind_balances"] = format_balances;
106 
107  auto format_blind_transfers = [this](const variant& result, const fc::variants&)
108  {
110  std::stringstream ss;
111  r.trx.operations[0].visit( operation_printer( ss, *this, operation_history_object() ) );
112  ss << "\n";
113  for( const auto& out : r.outputs )
114  {
115  auto a = get_asset( out.decrypted_memo.amount.asset_id );
116  ss << a.amount_to_pretty_string( out.decrypted_memo.amount ) << " to " << out.label
117  << "\n\t receipt: " << out.confirmation_receipt << "\n\n";
118  }
119  return ss.str();
120  };
121 
122  m["transfer_to_blind"] = format_blind_transfers;
123  m["blind_transfer"] = format_blind_transfers;
124 
125  m["receive_blind_transfer"] = [this](const variant& result, const fc::variants&)
126  {
127  auto r = result.as<blind_receipt>( GRAPHENE_MAX_NESTED_OBJECTS );
128  std::stringstream ss;
129  auto as = get_asset( r.amount.asset_id );
130  ss << as.amount_to_pretty_string( r.amount ) << " " << r.from_label << " => "
131  << r.to_label << " " << r.memo <<"\n";
132  return ss.str();
133  };
134 
135  m["blind_history"] = [this](const variant& result, const fc::variants&)
136  {
137  auto records = result.as<vector<blind_receipt>>( GRAPHENE_MAX_NESTED_OBJECTS );
138  std::stringstream ss;
139  ss << "WHEN "
140  << " " << "AMOUNT" << " " << "FROM" << " => " << "TO" << " " << "MEMO" <<"\n";
141  ss << "====================================================================================\n";
142  for( auto& r : records )
143  {
144  auto as = get_asset( r.amount.asset_id );
146  << " " << as.amount_to_pretty_string( r.amount ) << " " << r.from_label << " => " << r.to_label
147  << " " << r.memo <<"\n";
148  }
149  return ss.str();
150  };
151 
152  m["get_order_book"] = [](const variant& result, const fc::variants&)
153  {
154  auto orders = result.as<order_book>( GRAPHENE_MAX_NESTED_OBJECTS );
155  auto bids = orders.bids;
156  auto asks = orders.asks;
157  std::stringstream ss;
158  std::stringstream sum_stream;
159  sum_stream << "Sum(" << orders.base << ')';
160  double bid_sum = 0;
161  double ask_sum = 0;
162  const int spacing = 20;
163 
164  auto prettify_num = [&ss]( double n )
165  {
166  if (abs( round( n ) - n ) < 0.00000000001 )
167  {
168  ss << (int) n;
169  }
170  else if (n - floor(n) < 0.000001)
171  {
172  ss << setiosflags( ios::fixed ) << setprecision(10) << n;
173  }
174  else
175  {
176  ss << setiosflags( ios::fixed ) << setprecision(6) << n;
177  }
178  };
179  auto prettify_num_string = [&]( string& num_string )
180  {
181  double n = fc::to_double( num_string );
182  prettify_num( n );
183  };
184 
185  ss << setprecision( 8 ) << setiosflags( ios::fixed ) << setiosflags( ios::left );
186 
187  ss << ' ' << setw( (spacing * 4) + 6 ) << "BUY ORDERS" << "SELL ORDERS\n"
188  << ' ' << setw( spacing + 1 ) << "Price" << setw( spacing ) << orders.quote << ' ' << setw( spacing )
189  << orders.base << ' ' << setw( spacing ) << sum_stream.str()
190  << " " << setw( spacing + 1 ) << "Price" << setw( spacing ) << orders.quote << ' ' << setw( spacing )
191  << orders.base << ' ' << setw( spacing ) << sum_stream.str()
192  << "\n====================================================================================="
193  << "|=====================================================================================\n";
194 
195  for (unsigned int i = 0; i < bids.size() || i < asks.size() ; i++)
196  {
197  if ( i < bids.size() )
198  {
199  bid_sum += fc::to_double( bids[i].base );
200  ss << ' ' << setw( spacing );
201  prettify_num_string( bids[i].price );
202  ss << ' ' << setw( spacing );
203  prettify_num_string( bids[i].quote );
204  ss << ' ' << setw( spacing );
205  prettify_num_string( bids[i].base );
206  ss << ' ' << setw( spacing );
207  prettify_num( bid_sum );
208  ss << ' ';
209  }
210  else
211  {
212  ss << setw( (spacing * 4) + 5 ) << ' ';
213  }
214 
215  ss << '|';
216 
217  if ( i < asks.size() )
218  {
219  ask_sum += fc::to_double( asks[i].base );
220  ss << ' ' << setw( spacing );
221  prettify_num_string( asks[i].price );
222  ss << ' ' << setw( spacing );
223  prettify_num_string( asks[i].quote );
224  ss << ' ' << setw( spacing );
225  prettify_num_string( asks[i].base );
226  ss << ' ' << setw( spacing );
227  prettify_num( ask_sum );
228  }
229 
230  ss << '\n';
231  }
232 
233  ss << endl
234  << "Buy Total: " << bid_sum << ' ' << orders.base << endl
235  << "Sell Total: " << ask_sum << ' ' << orders.base << endl;
236 
237  return ss.str();
238  };
239 
240  m["sign_message"] = [](const variant& result, const fc::variants&)
241  {
242  auto r = result.as<signed_message>( GRAPHENE_MAX_NESTED_OBJECTS );
243 
244  fc::stringstream encapsulated;
245  encapsulated << ENC_HEADER;
246  encapsulated << r.message << '\n';
247  encapsulated << ENC_META;
248  encapsulated << "account=" << r.meta.account << '\n';
249  encapsulated << "memokey=" << std::string( r.meta.memo_key ) << '\n';
250  encapsulated << "block=" << r.meta.block << '\n';
251  encapsulated << "timestamp=" << r.meta.time << '\n';
252  encapsulated << ENC_SIG;
253  encapsulated << fc::to_hex( (const char*)r.signature->data(), r.signature->size() ) << '\n';
254  encapsulated << ENC_FOOTER;
255 
256  return encapsulated.str();
257  };
258 
259  return m;
260  }
261 
262 }}} // namespace graphene::wallet::detail
extended_asset_object get_asset(asset_id_type id) const
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
std::map< string, std::function< string(const fc::variant &, const fc::variants &) >, std::less<> > get_result_formatters() const
vector< operation > operations
Definition: transaction.hpp:89
#define GRAPHENE_MAX_NESTED_OBJECTS
Definition: config.hpp:33
tracks the history of all logical operations on blockchain stateAll operations and virtual operations...
Definition: api.cpp:48
std::vector< variant > variants
Definition: variant.hpp:170
visitor::result_type visit(visitor &v)
object_id_type id
Definition: object.hpp:69
The price struct stores asset prices in the BitShares system.
Definition: asset.hpp:108
double to_double(const std::string &)
Definition: string.cpp:60
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
Definition: exception.hpp:345
T as(uint32_t max_depth) const
Definition: variant.hpp:337
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object&#39;s.
Definition: variant.hpp:198
string str() const
Definition: ripemd160.cpp:21
typename impl::transform< List, Transformer >::type transform
Transform elements of a typelist.
Definition: typelist.hpp:170
const std::string & get_string() const
Definition: variant.cpp:575
std::string to_hex(const char *d, uint32_t s)
Definition: hex.cpp:17