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