BitShares-Core  5.0.0
BitShares blockchain implementation and command-line interface software
transfer_evaluator.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 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  */
27 #include <graphene/chain/hardfork.hpp>
29 
30 namespace graphene { namespace chain {
32 { try {
33 
34  const database& d = db();
35 
36  const account_object& from_account = op.from(d);
37  const account_object& to_account = op.to(d);
38  const asset_object& asset_type = op.amount.asset_id(d);
39 
40  try {
41 
43  is_authorized_asset( d, from_account, asset_type ),
44  transfer_from_account_not_whitelisted,
45  "'from' account ${from} is not whitelisted for asset ${asset}",
46  ("from",op.from)
47  ("asset",op.amount.asset_id)
48  );
50  is_authorized_asset( d, to_account, asset_type ),
51  transfer_to_account_not_whitelisted,
52  "'to' account ${to} is not whitelisted for asset ${asset}",
53  ("to",op.to)
54  ("asset",op.amount.asset_id)
55  );
56 
57  if( asset_type.is_transfer_restricted() )
58  {
60  from_account.id == asset_type.issuer || to_account.id == asset_type.issuer,
61  transfer_restricted_transfer_asset,
62  "Asset ${asset} has transfer_restricted flag enabled",
63  ("asset", op.amount.asset_id)
64  );
65  }
66 
67  bool insufficient_balance = d.get_balance( from_account, asset_type ).amount >= op.amount.amount;
68  FC_ASSERT( insufficient_balance,
69  "Insufficient Balance: ${balance}, unable to transfer '${total_transfer}' from account '${a}' to '${t}'",
70  ("a",from_account.name)("t",to_account.name)("total_transfer",d.to_pretty_string(op.amount))("balance",d.to_pretty_string(d.get_balance(from_account, asset_type))) );
71 
72  return void_result();
73  } FC_RETHROW_EXCEPTIONS( error, "Unable to transfer ${a} from ${f} to ${t}", ("a",d.to_pretty_string(op.amount))("f",op.from(d).name)("t",op.to(d).name) );
74 
75 } FC_CAPTURE_AND_RETHROW( (op) ) }
76 
78 { try {
79  db().adjust_balance( o.from, -o.amount );
80  db().adjust_balance( o.to, o.amount );
81  return void_result();
82 } FC_CAPTURE_AND_RETHROW( (o) ) }
83 
84 
85 
87 { try {
88  const database& d = db();
89 
90  const asset_object& asset_type = op.amount.asset_id(d);
92  asset_type.can_override(),
93  override_transfer_not_permitted,
94  "override_transfer not permitted for asset ${asset}",
95  ("asset", op.amount.asset_id)
96  );
97  FC_ASSERT( asset_type.issuer == op.issuer );
98 
99  const account_object& from_account = op.from(d);
100  const account_object& to_account = op.to(d);
101 
102  FC_ASSERT( is_authorized_asset( d, to_account, asset_type ) );
103  FC_ASSERT( is_authorized_asset( d, from_account, asset_type ) );
104 
105  FC_ASSERT( d.get_balance( from_account, asset_type ).amount >= op.amount.amount,
106  "", ("total_transfer",op.amount)("balance",d.get_balance(from_account, asset_type).amount) );
107 
108  return void_result();
109 } FC_CAPTURE_AND_RETHROW( (op) ) }
110 
112 { try {
113  db().adjust_balance( o.from, -o.amount );
114  db().adjust_balance( o.to, o.amount );
115  return void_result();
116 } FC_CAPTURE_AND_RETHROW( (o) ) }
117 
118 } } // graphene::chain
void_result do_apply(const transfer_operation &o)
void adjust_balance(account_id_type account, asset delta)
Adjust a particular account&#39;s balance in a given asset by a delta.
Definition: db_balance.cpp:54
account_id_type from
Account to transfer asset from.
Definition: transfer.hpp:87
bool is_authorized_asset(const database &d, const account_object &acct, const asset_object &asset_obj)
This class represents an account on the object graphAccounts are the primary unit of authority on the...
string to_pretty_string(const asset &a) const
Definition: db_balance.cpp:49
tracks the blockchain state in an extensible manner
Definition: database.hpp:70
Definition: api.cpp:56
void_result do_evaluate(const transfer_operation &o)
asset get_balance(account_id_type owner, asset_id_type asset_id) const
Retrieve a particular account&#39;s balance in a given asset.
Definition: db_balance.cpp:35
#define FC_RETHROW_EXCEPTIONS(LOG_LEVEL, FORMAT,...)
Catchs all exception&#39;s, std::exceptions, and ... and rethrows them after appending the provided log m...
Definition: exception.hpp:463
account_id_type issuer
ID of the account which issued this asset.
Allows the issuer of an asset to transfer an asset from any account to any account if they have overr...
Definition: transfer.hpp:77
asset amount
The amount of asset to transfer from from to to.
Definition: transfer.hpp:91
Transfers an amount of one asset from one account to another.
Definition: transfer.hpp:45
object_id_type id
Definition: object.hpp:69
#define FC_CAPTURE_AND_RETHROW(...)
Definition: exception.hpp:478
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
Definition: exception.hpp:345
string name
The account&#39;s name. This name must be unique among all account names on the graph. May not be empty.
tracks the parameters of an assetAll assets have a globally unique symbol name that controls how they...
account_id_type to
Account to transfer asset to.
Definition: transfer.hpp:89
asset amount
The amount of asset to transfer from from to to.
Definition: transfer.hpp:58
void_result do_apply(const override_transfer_operation &o)
asset_id_type asset_id
Definition: asset.hpp:39
account_id_type from
Account to transfer asset from.
Definition: transfer.hpp:54
#define GRAPHENE_ASSERT(expr, exc_type, FORMAT,...)
Definition: exceptions.hpp:28
account_id_type to
Account to transfer asset to.
Definition: transfer.hpp:56
void_result do_evaluate(const override_transfer_operation &o)