BitShares-Core  5.0.0
BitShares blockchain implementation and command-line interface software
wallet_asset.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 "wallet_api_impl.hpp"
26 
27 namespace graphene { namespace wallet { namespace detail {
28 
29  std::string wallet_api_impl::asset_id_to_string(asset_id_type id) const
30  {
31  std::string asset_id = fc::to_string(id.space_id) +
32  "." + fc::to_string(id.type_id) +
33  "." + fc::to_string(id.instance.value);
34  return asset_id;
35  }
36 
38  {
39  auto rec = _remote_db->get_assets({asset_id_to_string(id)}, {}).front();
40  return rec;
41  }
42 
44  {
45  FC_ASSERT( asset_symbol_or_id.size() > 0 );
46 
47  if( auto id = maybe_id<asset_id_type>(asset_symbol_or_id) )
48  {
49  // It's an ID
50  return find_asset(*id);
51  } else {
52  // It's a symbol
53  auto rec = _remote_db->lookup_asset_symbols({asset_symbol_or_id}).front();
54  if( rec )
55  {
56  if( rec->symbol != asset_symbol_or_id )
57  return optional<asset_object>();
58  }
59  return rec;
60  }
61  }
62 
64  {
65  auto opt = find_asset(id);
66  FC_ASSERT(opt);
67  return *opt;
68  }
69 
70  extended_asset_object wallet_api_impl::get_asset(string asset_symbol_or_id)const
71  {
72  auto opt = find_asset(asset_symbol_or_id);
73  FC_ASSERT(opt);
74  return *opt;
75  }
76 
77  asset_id_type wallet_api_impl::get_asset_id(string asset_symbol_or_id) const
78  {
79  FC_ASSERT( asset_symbol_or_id.size() > 0 );
80  vector<optional<extended_asset_object>> opt_asset;
81  if( std::isdigit( asset_symbol_or_id.front() ) )
82  return fc::variant(asset_symbol_or_id, 1).as<asset_id_type>( 1 );
83  opt_asset = _remote_db->lookup_asset_symbols( {asset_symbol_or_id} );
84  FC_ASSERT( (opt_asset.size() > 0) && (opt_asset[0].valid()) );
85  return opt_asset[0]->id;
86  }
87 
88  signed_transaction wallet_api_impl::create_asset(string issuer, string symbol,
89  uint8_t precision, asset_options common, fc::optional<bitasset_options> bitasset_opts,
90  bool broadcast )
91  { try {
92  account_object issuer_account = get_account( issuer );
93  FC_ASSERT(!find_asset(symbol).valid(), "Asset with that symbol already exists!");
94 
95  asset_create_operation create_op;
96  create_op.issuer = issuer_account.id;
97  create_op.symbol = symbol;
98  create_op.precision = precision;
99  create_op.common_options = common;
100  create_op.bitasset_opts = bitasset_opts;
101 
103  tx.operations.push_back( create_op );
104  set_operation_fees( tx, _remote_db->get_global_properties().parameters.get_current_fees());
105  tx.validate();
106 
107  return sign_transaction( tx, broadcast );
108  } FC_CAPTURE_AND_RETHROW( (issuer)(symbol)(precision)(common)(bitasset_opts)(broadcast) ) }
109 
111  asset_options new_options, bool broadcast /* = false */)
112  { try {
113  optional<asset_object> asset_to_update = find_asset(symbol);
114  if (!asset_to_update)
115  FC_THROW("No asset with that symbol exists!");
116  optional<account_id_type> new_issuer_account_id;
117  if (new_issuer)
118  {
119  FC_THROW( "The use of 'new_issuer' is no longer supported. Please use `update_asset_issuer' instead!" );
120  }
121 
122  asset_update_operation update_op;
123  update_op.issuer = asset_to_update->issuer;
124  update_op.asset_to_update = asset_to_update->id;
125  update_op.new_issuer = new_issuer_account_id;
126  update_op.new_options = new_options;
127 
129  tx.operations.push_back( update_op );
130  set_operation_fees( tx, _remote_db->get_global_properties().parameters.get_current_fees());
131  tx.validate();
132 
133  return sign_transaction( tx, broadcast );
134  } FC_CAPTURE_AND_RETHROW( (symbol)(new_issuer)(new_options)(broadcast) ) }
135 
137  bool broadcast /* = false */)
138  { try {
139  optional<asset_object> asset_to_update = find_asset(symbol);
140  if (!asset_to_update)
141  FC_THROW("No asset with that symbol exists!");
142 
143  account_object new_issuer_account = get_account(new_issuer);
144 
145  asset_update_issuer_operation update_issuer;
146  update_issuer.issuer = asset_to_update->issuer;
147  update_issuer.asset_to_update = asset_to_update->id;
148  update_issuer.new_issuer = new_issuer_account.id;
149 
151  tx.operations.push_back( update_issuer );
152  set_operation_fees( tx, _remote_db->get_global_properties().parameters.get_current_fees());
153  tx.validate();
154 
155  return sign_transaction( tx, broadcast );
156  } FC_CAPTURE_AND_RETHROW( (symbol)(new_issuer)(broadcast) ) }
157 
159  bool broadcast /* = false */)
160  { try {
161  optional<asset_object> asset_to_update = find_asset(symbol);
162  if (!asset_to_update)
163  FC_THROW("No asset with that symbol exists!");
164 
166  update_op.issuer = asset_to_update->issuer;
167  update_op.asset_to_update = asset_to_update->id;
168  update_op.new_options = new_options;
169 
171  tx.operations.push_back( update_op );
172  set_operation_fees( tx, _remote_db->get_global_properties().parameters.get_current_fees());
173  tx.validate();
174 
175  return sign_transaction( tx, broadcast );
176  } FC_CAPTURE_AND_RETHROW( (symbol)(new_options)(broadcast) ) }
177 
179  flat_set<string> new_feed_producers, bool broadcast /* = false */)
180  { try {
181  optional<asset_object> asset_to_update = find_asset(symbol);
182  if (!asset_to_update)
183  FC_THROW("No asset with that symbol exists!");
184 
186  update_op.issuer = asset_to_update->issuer;
187  update_op.asset_to_update = asset_to_update->id;
188  update_op.new_feed_producers.reserve(new_feed_producers.size());
189  std::transform(new_feed_producers.begin(), new_feed_producers.end(),
190  std::inserter(update_op.new_feed_producers, update_op.new_feed_producers.end()),
191  [this](const std::string& account_name_or_id){ return get_account_id(account_name_or_id); });
192 
194  tx.operations.push_back( update_op );
195  set_operation_fees( tx, _remote_db->get_global_properties().parameters.get_current_fees());
196  tx.validate();
197 
198  return sign_transaction( tx, broadcast );
199  } FC_CAPTURE_AND_RETHROW( (symbol)(new_feed_producers)(broadcast) ) }
200 
201  signed_transaction wallet_api_impl::publish_asset_feed(string publishing_account, string symbol,
202  price_feed feed, bool broadcast /* = false */)
203  { try {
204  optional<asset_object> asset_to_update = find_asset(symbol);
205  if (!asset_to_update)
206  FC_THROW("No asset with that symbol exists!");
207 
208  asset_publish_feed_operation publish_op;
209  publish_op.publisher = get_account_id(publishing_account);
210  publish_op.asset_id = asset_to_update->id;
211  publish_op.feed = feed;
212 
214  tx.operations.push_back( publish_op );
215  set_operation_fees( tx, _remote_db->get_global_properties().parameters.get_current_fees());
216  tx.validate();
217 
218  return sign_transaction( tx, broadcast );
219  } FC_CAPTURE_AND_RETHROW( (publishing_account)(symbol)(feed)(broadcast) ) }
220 
221  signed_transaction wallet_api_impl::fund_asset_fee_pool(string from, string symbol, string amount,
222  bool broadcast /* = false */)
223  { try {
224  account_object from_account = get_account(from);
225  optional<asset_object> asset_to_fund = find_asset(symbol);
226  if (!asset_to_fund)
227  FC_THROW("No asset with that symbol exists!");
228  asset_object core_asset = get_asset(asset_id_type());
229 
231  fund_op.from_account = from_account.id;
232  fund_op.asset_id = asset_to_fund->id;
233  fund_op.amount = core_asset.amount_from_string(amount).amount;
234 
236  tx.operations.push_back( fund_op );
237  set_operation_fees( tx, _remote_db->get_global_properties().parameters.get_current_fees());
238  tx.validate();
239 
240  return sign_transaction( tx, broadcast );
241  } FC_CAPTURE_AND_RETHROW( (from)(symbol)(amount)(broadcast) ) }
242 
244  bool broadcast /* = false */)
245  { try {
246  optional<asset_object> asset_pool_to_claim = find_asset(symbol);
247  if (!asset_pool_to_claim)
248  FC_THROW("No asset with that symbol exists!");
249  asset_object core_asset = get_asset(asset_id_type());
250 
252  claim_op.issuer = asset_pool_to_claim->issuer;
253  claim_op.asset_id = asset_pool_to_claim->id;
254  claim_op.amount_to_claim = core_asset.amount_from_string(amount).amount;
255 
257  tx.operations.push_back( claim_op );
258  set_operation_fees( tx, _remote_db->get_global_properties().parameters.get_current_fees());
259  tx.validate();
260 
261  return sign_transaction( tx, broadcast );
262  } FC_CAPTURE_AND_RETHROW( (symbol)(amount)(broadcast) ) }
263 
264 
265  signed_transaction wallet_api_impl::reserve_asset(string from, string amount, string symbol,
266  bool broadcast /* = false */)
267  { try {
268  account_object from_account = get_account(from);
269  optional<asset_object> asset_to_reserve = find_asset(symbol);
270  if (!asset_to_reserve)
271  FC_THROW("No asset with that symbol exists!");
272 
273  asset_reserve_operation reserve_op;
274  reserve_op.payer = from_account.id;
275  reserve_op.amount_to_reserve = asset_to_reserve->amount_from_string(amount);
276 
278  tx.operations.push_back( reserve_op );
279  set_operation_fees( tx, _remote_db->get_global_properties().parameters.get_current_fees());
280  tx.validate();
281 
282  return sign_transaction( tx, broadcast );
283  } FC_CAPTURE_AND_RETHROW( (from)(amount)(symbol)(broadcast) ) }
284 
286  bool broadcast /* = false */)
287  { try {
288  optional<asset_object> asset_to_settle = find_asset(symbol);
289  if (!asset_to_settle)
290  FC_THROW("No asset with that symbol exists!");
291 
293  settle_op.issuer = asset_to_settle->issuer;
294  settle_op.asset_to_settle = asset_to_settle->id;
295  settle_op.settle_price = settle_price;
296 
298  tx.operations.push_back( settle_op );
299  set_operation_fees( tx, _remote_db->get_global_properties().parameters.get_current_fees());
300  tx.validate();
301 
302  return sign_transaction( tx, broadcast );
303  } FC_CAPTURE_AND_RETHROW( (symbol)(settle_price)(broadcast) ) }
304 
305  signed_transaction wallet_api_impl::settle_asset(string account_to_settle, string amount_to_settle,
306  string symbol, bool broadcast /* = false */)
307  { try {
308  optional<asset_object> asset_to_settle = find_asset(symbol);
309  if (!asset_to_settle)
310  FC_THROW("No asset with that symbol exists!");
311 
312  asset_settle_operation settle_op;
313  settle_op.account = get_account_id(account_to_settle);
314  settle_op.amount = asset_to_settle->amount_from_string(amount_to_settle);
315 
317  tx.operations.push_back( settle_op );
318  set_operation_fees( tx, _remote_db->get_global_properties().parameters.get_current_fees());
319  tx.validate();
320 
321  return sign_transaction( tx, broadcast );
322  } FC_CAPTURE_AND_RETHROW( (account_to_settle)(amount_to_settle)(symbol)(broadcast) ) }
323 
324  signed_transaction wallet_api_impl::issue_asset(string to_account, string amount, string symbol,
325  string memo, bool broadcast )
326  {
327  auto asset_obj = get_asset(symbol);
328 
329  account_object to = get_account(to_account);
330  account_object issuer = get_account(asset_obj.issuer);
331 
332  asset_issue_operation issue_op;
333  issue_op.issuer = asset_obj.issuer;
334  issue_op.asset_to_issue = asset_obj.amount_from_string(amount);
335  issue_op.issue_to_account = to.id;
336 
337  if( memo.size() )
338  {
339  issue_op.memo = memo_data();
340  issue_op.memo->from = issuer.options.memo_key;
341  issue_op.memo->to = to.options.memo_key;
342  issue_op.memo->set_message(get_private_key(issuer.options.memo_key),
343  to.options.memo_key, memo);
344  }
345 
347  tx.operations.push_back(issue_op);
348  set_operation_fees(tx,_remote_db->get_global_properties().parameters.get_current_fees());
349  tx.validate();
350 
351  return sign_transaction(tx, broadcast);
352  }
353 
354  signed_transaction wallet_api_impl::bid_collateral(string bidder_name, string debt_amount, string debt_symbol,
355  string additional_collateral, bool broadcast )
356  { try {
357  optional<asset_object> debt_asset = find_asset(debt_symbol);
358  if (!debt_asset)
359  FC_THROW("No asset with that symbol exists!");
360 
361  FC_ASSERT(debt_asset->bitasset_data_id.valid(), "Not a bitasset, bidding not possible.");
362  const asset_object& collateral =
363  get_asset(get_object(*debt_asset->bitasset_data_id).options.short_backing_asset);
364 
366  op.bidder = get_account_id(bidder_name);
367  op.debt_covered = debt_asset->amount_from_string(debt_amount);
368  op.additional_collateral = collateral.amount_from_string(additional_collateral);
369 
371  tx.operations.push_back( op );
372  set_operation_fees( tx, _remote_db->get_global_properties().parameters.get_current_fees());
373  tx.validate();
374 
375  return sign_transaction( tx, broadcast );
376  } FC_CAPTURE_AND_RETHROW( (bidder_name)(debt_amount)(debt_symbol)(additional_collateral)(broadcast) ) }
377 
378 
379 }}} // namespace graphene::wallet::detail
signed_transaction bid_collateral(string bidder_name, string debt_amount, string debt_symbol, string additional_collateral, bool broadcast)
asset_id_type asset_id
asset for which the feed is published
Definition: asset_ops.hpp:427
asset additional_collateral
the amount of collateral to bid for the debt
Definition: market.hpp:185
Update the set of feed-producing accounts for a BitAssetBitAssets have price feeds selected by taking...
Definition: asset_ops.hpp:384
account_id_type get_account_id(string account_name_or_id) const
T as(uint32_t max_depth) const
Definition: variant.hpp:336
vector< operation > operations
Definition: transaction.hpp:89
virtual void validate() const
Definition: transaction.cpp:58
This class represents an account on the object graphAccounts are the primary unit of authority on the...
asset amount
Amount of asset to force settle. This must be a market-issued asset.
Definition: asset_ops.hpp:242
Definition: api.cpp:56
The asset_options struct contains options available on all assets in the network. ...
Definition: asset_ops.hpp:47
account_id_type bidder
pays fee and additional collateral
Definition: market.hpp:184
Schedules a market-issued asset for automatic settlementHolders of market-issued assests may request ...
Definition: asset_ops.hpp:226
signed_transaction update_asset_feed_producers(string symbol, flat_set< string > new_feed_producers, bool broadcast)
bool valid() const
Definition: optional.hpp:186
#define FC_THROW(...)
Definition: exception.hpp:366
account_object get_account(account_id_type id) const
asset amount_from_string(string amount_string) const
signed_transaction update_asset(string symbol, optional< string > new_issuer, asset_options new_options, bool broadcast)
account_id_type issuer
ID of the account which issued this asset.
Publish price feeds for market-issued assetsPrice feed providers use this operation to publish their ...
Definition: asset_ops.hpp:415
uint8_t precision
Number of digits to the right of decimal point, must be less than or equal to 12. ...
Definition: asset_ops.hpp:169
provides stack-based nullable value similar to boost::optional
Definition: optional.hpp:20
signed_transaction settle_asset(string account_to_settle, string amount_to_settle, string symbol, bool broadcast)
signed_transaction update_bitasset(string symbol, bitasset_options new_options, bool broadcast)
string symbol
The ticker symbol of this asset.
Definition: asset_ops.hpp:167
used to take an asset out of circulation, returning to the issuer
Definition: asset_ops.hpp:466
object_id_type id
Definition: object.hpp:69
signed_transaction reserve_asset(string from, string amount, string symbol, bool broadcast)
The price struct stores asset prices in the BitShares system.
Definition: asset.hpp:114
signed_transaction sign_transaction(signed_transaction tx, bool broadcast=false)
signed_transaction publish_asset_feed(string publishing_account, string symbol, price_feed feed, bool broadcast)
#define FC_CAPTURE_AND_RETHROW(...)
Definition: exception.hpp:478
Update options specific to BitAssetsBitAssets have some options which are not relevant to other asset...
Definition: asset_ops.hpp:353
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
Definition: exception.hpp:345
adds a signature to a transaction
signed_transaction issue_asset(string to_account, string amount, string symbol, string memo, bool broadcast=false)
account_id_type account
Account requesting the force settlement. This account pays the fee.
Definition: asset_ops.hpp:240
signed_transaction claim_asset_fee_pool(string symbol, string amount, bool broadcast)
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object&#39;s.
Definition: variant.hpp:198
optional< extended_asset_object > find_asset(asset_id_type id) const
fc::ecc::private_key get_private_key(const public_key_type &id) const
account_id_type issuer
Must be asset_to_issue->asset_id->issuer.
Definition: asset_ops.hpp:446
Update options common to all assetsThere are a number of options which all assets in the network use...
Definition: asset_ops.hpp:307
signed_transaction create_asset(string issuer, string symbol, uint8_t precision, asset_options common, fc::optional< bitasset_options > bitasset_opts, bool broadcast=false)
account_id_type issuer
This account must sign and pay the fee for this operation. Later, this account may update the asset...
Definition: asset_ops.hpp:165
Transfers BTS from the fee pool of a specified asset back to the issuer&#39;s balance.
Definition: asset_ops.hpp:554
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
defines market parameters for margin positions
Definition: asset.hpp:164
std::string to_string(double)
Definition: string.cpp:73
graphene::db::object_downcast_t< ID > get_object(ID id) const
std::string asset_id_to_string(asset_id_type id) const
optional< bitasset_options > bitasset_opts
Definition: asset_ops.hpp:179
signed_transaction fund_asset_fee_pool(string from, string symbol, string amount, bool broadcast)
extended_asset_object get_asset(asset_id_type id) const
asset amount_to_claim
fee.asset_id must != asset_id
Definition: asset_ops.hpp:563
asset_id_type get_asset_id(string asset_symbol_or_id) const
Update issuer of an assetAn issuer has general administrative power of an asset and in some cases als...
Definition: asset_ops.hpp:518
optional< asset_bitasset_data_id_type > bitasset_data_id
Extra data associated with BitAssets. This field is non-null if and only if is_market_issued() return...
signed_transaction update_asset_issuer(string symbol, string new_issuer, bool broadcast)
signed_transaction global_settle_asset(string symbol, price settle_price, bool broadcast)
void set_operation_fees(signed_transaction &tx, const fee_schedule &s)
account_id_type issuer
must equal asset_to_settle->issuer
Definition: asset_ops.hpp:204
The bitasset_options struct contains configurable options available only to BitAssets.
Definition: asset_ops.hpp:109
asset debt_covered
the amount of debt to take over
Definition: market.hpp:186
defines the keys used to derive the shared secret
Definition: memo.hpp:40
allows global settling of bitassets (black swan or prediction markets)
Definition: asset_ops.hpp:199
optional< account_id_type > new_issuer
If the asset is to be given a new issuer, specify his ID here.
Definition: asset_ops.hpp:331