BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
credit_offer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021 Abit More, 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  */
25 
26 #include <fc/io/raw.hpp>
27 
28 namespace graphene { namespace protocol {
29 
30 static void validate_acceptable_collateral( const flat_map<asset_id_type, price>& acceptable_collateral,
31  const asset_id_type* p_asset_type = nullptr )
32 {
33  FC_ASSERT( !acceptable_collateral.empty(), "Acceptable collateral list should not be empty" );
34 
35  asset_id_type asset_type = ( p_asset_type != nullptr ) ? *p_asset_type
36  : acceptable_collateral.begin()->second.base.asset_id;
37 
38  for( const auto& collateral : acceptable_collateral )
39  {
40  const auto& collateral_asset_type = collateral.first;
41  const auto& collateral_price = collateral.second;
42  FC_ASSERT( collateral_price.base.asset_id == asset_type,
43  "Base asset ID in price of acceptable collateral should be same as offer asset type" );
44  FC_ASSERT( collateral_price.quote.asset_id == collateral_asset_type,
45  "Quote asset ID in price of acceptable collateral should be same as collateral asset type" );
46  collateral_price.validate( true );
47  }
48 }
49 
50 static void validate_acceptable_borrowers( const flat_map<account_id_type, share_type>& acceptable_borrowers )
51 {
52  for( const auto& borrower : acceptable_borrowers )
53  {
54  const auto& max_borrow_amount = borrower.second.value;
55  FC_ASSERT( max_borrow_amount >= 0,
56  "Maximum amount to borrow for acceptable borrowers should not be negative" );
57  FC_ASSERT( max_borrow_amount <= GRAPHENE_MAX_SHARE_SUPPLY,
58  "Maximum amount to borrow for acceptable borrowers should not be greater than ${max}",
59  ("max", GRAPHENE_MAX_SHARE_SUPPLY) );
60  }
61 }
62 
64 {
65  FC_ASSERT( fee.amount >= 0, "Fee should not be negative" );
66  FC_ASSERT( balance > 0, "Balance should be positive" );
68  "Maximum duration should not be greater than ${d} days",
70  FC_ASSERT( min_deal_amount >= 0, "Minimum deal amount should not be negative" );
72  "Minimum deal amount should not be greater than ${max}",
73  ("max", GRAPHENE_MAX_SHARE_SUPPLY) );
74 
75  validate_acceptable_collateral( acceptable_collateral, &asset_type );
76  validate_acceptable_borrowers( acceptable_borrowers );
77 }
78 
80 {
81  share_type core_fee_required = schedule.fee;
82  core_fee_required += calculate_data_fee( fc::raw::pack_size(*this), schedule.price_per_kbyte );
83  return core_fee_required;
84 }
85 
87 {
88  FC_ASSERT( fee.amount >= 0, "Fee should not be negative" );
89 }
90 
92 {
93  FC_ASSERT( fee.amount >= 0, "Fee should not be negative" );
94  bool updating_something = false;
95 
96  if( delta_amount.valid() )
97  {
98  updating_something = true;
99  FC_ASSERT( delta_amount->amount != 0, "Delta amount should not be zero" );
100  }
101  if( fee_rate.valid() )
102  updating_something = true;
104  {
105  updating_something = true;
107  "Maximum duration should not be greater than ${d} days",
109  }
110  if( min_deal_amount.valid() )
111  {
112  updating_something = true;
113  FC_ASSERT( *min_deal_amount >= 0, "Minimum deal amount should not be negative" );
115  "Minimum deal amount should not be greater than ${max}",
116  ("max", GRAPHENE_MAX_SHARE_SUPPLY) );
117  }
118  if( enabled.valid() )
119  updating_something = true;
120  if( auto_disable_time.valid() )
121  updating_something = true;
122  if( acceptable_collateral.valid() )
123  {
124  updating_something = true;
125  validate_acceptable_collateral( *acceptable_collateral ); // Note: check base asset ID in evaluator
126  }
127  if( acceptable_borrowers.valid() )
128  {
129  updating_something = true;
130  validate_acceptable_borrowers( *acceptable_borrowers );
131  }
132 
133  FC_ASSERT( updating_something,
134  "Should change something - at least one of the optional data fields should be present" );
135 }
136 
138 {
139  share_type core_fee_required = schedule.fee;
140  core_fee_required += calculate_data_fee( fc::raw::pack_size(*this), schedule.price_per_kbyte );
141  return core_fee_required;
142 }
143 
145 {
146  FC_ASSERT( fee.amount >= 0, "Fee should not be negative" );
147  FC_ASSERT( borrow_amount.amount > 0, "Amount to borrow should be positive" );
148  FC_ASSERT( collateral.amount > 0, "Collateral amount should be positive" );
149  if( extensions.value.auto_repay.valid() )
150  {
151  constexpr auto cdar_count = static_cast<uint8_t>( credit_deal_auto_repayment_type::CDAR_TYPE_COUNT );
152  FC_ASSERT( *extensions.value.auto_repay < cdar_count,
153  "auto_repay should be less than ${c}", ("c",cdar_count) );
154  }
155 }
156 
158 {
159  FC_ASSERT( fee.amount >= 0, "Fee should not be negative" );
160  FC_ASSERT( repay_amount.amount > 0, "Amount to repay should be positive" );
161  FC_ASSERT( credit_fee.amount >= 0, "Credit fee should not be negative" );
163  "Asset type of repay amount and credit fee should be the same" );
164 }
165 
167 {
168  FC_ASSERT( fee.amount >= 0, "Fee should not be negative" );
169 
170  constexpr auto cdar_count = static_cast<uint8_t>( credit_deal_auto_repayment_type::CDAR_TYPE_COUNT );
171  FC_ASSERT( auto_repay < cdar_count,
172  "auto_repay should be less than ${c}", ("c",cdar_count) );
173 }
174 
175 } } // graphene::protocol
176 
183 
185 
GRAPHENE_MAX_SHARE_SUPPLY
constexpr int64_t GRAPHENE_MAX_SHARE_SUPPLY(1000000000000000LL)
graphene::protocol::credit_offer_accept_operation::extensions
extension< ext > extensions
Extensions.
Definition: credit_offer.hpp:153
graphene::protocol::credit_deal_update_operation::fee
asset fee
Operation fee.
Definition: credit_offer.hpp:219
graphene::protocol::credit_offer_create_operation::fee
asset fee
Operation fee.
Definition: credit_offer.hpp:43
graphene::protocol::credit_offer_create_operation::balance
share_type balance
Usable amount in the credit offer.
Definition: credit_offer.hpp:46
graphene::protocol::credit_offer_update_operation::validate
void validate() const override
Definition: credit_offer.cpp:91
graphene::protocol::credit_offer_update_operation::acceptable_borrowers
optional< flat_map< account_id_type, share_type > > acceptable_borrowers
New allowed borrowers and their maximum amounts to borrow, optional.
Definition: credit_offer.hpp:109
graphene::protocol::credit_deal_update_operation::auto_repay
uint8_t auto_repay
The specified automatic repayment type.
Definition: credit_offer.hpp:222
graphene::protocol::credit_deal_auto_repayment_type::CDAR_TYPE_COUNT
@ CDAR_TYPE_COUNT
Total number of available automatic repayment types.
graphene::protocol::credit_deal_repay_operation
Repay a credit deal.
Definition: credit_offer.hpp:163
graphene::protocol::credit_offer_create_operation
Create a new credit offer.
Definition: credit_offer.hpp:36
graphene::protocol::credit_offer_create_operation::acceptable_borrowers
flat_map< account_id_type, share_type > acceptable_borrowers
Allowed borrowers and their maximum amounts to borrow. No limitation if empty.
Definition: credit_offer.hpp:57
graphene::protocol::credit_offer_accept_operation::validate
void validate() const override
Definition: credit_offer.cpp:144
graphene::protocol::credit_offer_update_operation::fee_params_t
Definition: credit_offer.hpp:90
graphene::protocol::credit_offer_update_operation::min_deal_amount
optional< share_type > min_deal_amount
Minimum amount to borrow for each new deal, optional.
Definition: credit_offer.hpp:101
graphene::protocol::credit_offer_create_operation::calculate_fee
share_type calculate_fee(const fee_params_t &k) const
Definition: credit_offer.cpp:79
fc::schedule
auto schedule(Functor &&f, const fc::time_point &t, const char *desc FC_TASK_NAME_DEFAULT_ARG, priority prio=priority()) -> fc::future< decltype(f())>
Definition: thread.hpp:231
graphene::protocol::credit_offer_create_operation::validate
void validate() const override
Definition: credit_offer.cpp:63
graphene::protocol::credit_offer_delete_operation
Delete a credit offer.
Definition: credit_offer.hpp:70
graphene::protocol::credit_offer_update_operation::enabled
optional< bool > enabled
Whether this offer is available, optional.
Definition: credit_offer.hpp:102
graphene::protocol::credit_offer_create_operation::min_deal_amount
share_type min_deal_amount
Minimum amount to borrow for each new deal.
Definition: credit_offer.hpp:49
GRAPHENE_IMPLEMENT_EXTERNAL_SERIALIZATION
#define GRAPHENE_IMPLEMENT_EXTERNAL_SERIALIZATION(type)
Definition: types.hpp:86
graphene::protocol::credit_offer_create_operation::fee_params_t
Definition: credit_offer.hpp:38
graphene::protocol::credit_deal_repay_operation::repay_amount
asset repay_amount
The amount to repay.
Definition: credit_offer.hpp:170
graphene::protocol::credit_offer_update_operation::fee_rate
optional< uint32_t > fee_rate
New fee rate, optional.
Definition: credit_offer.hpp:99
graphene::protocol::credit_deal_repay_operation::credit_fee
asset credit_fee
The credit fee relative to the amount to repay.
Definition: credit_offer.hpp:171
graphene::protocol::base_operation::calculate_data_fee
static uint64_t calculate_data_fee(uint64_t bytes, uint64_t price_per_kbyte)
Definition: operations.cpp:33
graphene::protocol::credit_offer_accept_operation::fee_params_t
Definition: credit_offer.hpp:143
fc::optional::valid
bool valid() const
Definition: optional.hpp:186
graphene::protocol::credit_deal_repay_operation::validate
void validate() const override
Definition: credit_offer.cpp:157
graphene::protocol::credit_offer_accept_operation
Accept a credit offer, thereby creating a credit deal.
Definition: credit_offer.hpp:135
graphene::protocol::credit_offer_update_operation::acceptable_collateral
optional< flat_map< asset_id_type, price > > acceptable_collateral
New types and rates of acceptable collateral, optional.
Definition: credit_offer.hpp:106
graphene::protocol::asset::asset_id
asset_id_type asset_id
Definition: asset.hpp:37
graphene::protocol::credit_offer_accept_operation::ext
Definition: credit_offer.hpp:137
graphene::protocol::credit_offer_create_operation::acceptable_collateral
flat_map< asset_id_type, price > acceptable_collateral
Types and rates of acceptable collateral.
Definition: credit_offer.hpp:54
GRAPHENE_MAX_CREDIT_DEAL_DAYS
constexpr int64_t GRAPHENE_MAX_CREDIT_DEAL_DAYS
How long a credit deal will be kept, in days.
Definition: config.hpp:128
fc::raw::pack_size
size_t pack_size(const T &v)
Definition: raw.hpp:757
graphene::protocol::credit_offer_create_operation::asset_type
asset_id_type asset_type
Asset type in the credit offer.
Definition: credit_offer.hpp:45
graphene::protocol::credit_deal_update_operation::fee_params_t
Definition: credit_offer.hpp:217
graphene::protocol::credit_offer_update_operation::fee
asset fee
Operation fee.
Definition: credit_offer.hpp:95
graphene::protocol::credit_offer_accept_operation::borrow_amount
asset borrow_amount
The amount to borrow.
Definition: credit_offer.hpp:148
graphene::protocol::credit_offer_delete_operation::fee_params_t
Definition: credit_offer.hpp:72
FC_ASSERT
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
Definition: exception.hpp:345
graphene::protocol::credit_offer_create_operation::max_duration_seconds
uint32_t max_duration_seconds
The time limit that borrowed funds should be repaid.
Definition: credit_offer.hpp:48
graphene::protocol::credit_offer_update_operation::calculate_fee
share_type calculate_fee(const fee_params_t &k) const
Definition: credit_offer.cpp:137
credit_offer.hpp
graphene::protocol::credit_deal_update_operation
Update a credit deal.
Definition: credit_offer.hpp:215
graphene::protocol::asset::amount
share_type amount
Definition: asset.hpp:36
graphene::protocol::credit_deal_repay_operation::fee_params_t
Definition: credit_offer.hpp:165
graphene::protocol::credit_deal_expired_operation
A credit deal expired without being fully repaid.
Definition: credit_offer.hpp:184
graphene::protocol::credit_offer_accept_operation::fee
asset fee
Operation fee.
Definition: credit_offer.hpp:145
graphene::protocol::credit_offer_delete_operation::fee
asset fee
Operation fee.
Definition: credit_offer.hpp:74
graphene::protocol::credit_deal_update_operation::validate
void validate() const override
Definition: credit_offer.cpp:166
graphene::protocol::credit_offer_update_operation::delta_amount
optional< asset > delta_amount
Delta amount, optional.
Definition: credit_offer.hpp:98
graphene::protocol::credit_deal_repay_operation::fee
asset fee
Operation fee.
Definition: credit_offer.hpp:167
graphene::protocol::credit_offer_update_operation::auto_disable_time
optional< time_point_sec > auto_disable_time
New time to disable automatically, optional.
Definition: credit_offer.hpp:103
graphene::protocol::credit_offer_accept_operation::collateral
asset collateral
The collateral.
Definition: credit_offer.hpp:149
graphene::protocol::credit_offer_delete_operation::validate
void validate() const override
Definition: credit_offer.cpp:86
graphene::protocol::credit_offer_update_operation
Update a credit offer.
Definition: credit_offer.hpp:88
graphene::protocol::credit_offer_update_operation::max_duration_seconds
optional< uint32_t > max_duration_seconds
New repayment time limit, optional.
Definition: credit_offer.hpp:100
GRAPHENE_MAX_CREDIT_DEAL_SECS
constexpr int64_t GRAPHENE_MAX_CREDIT_DEAL_SECS
How long a credit deal will be kept, in seconds.
Definition: config.hpp:130
graphene
Definition: api.cpp:48
raw.hpp
fc::safe
Definition: safe.hpp:26