BitShares-Core  6.0.1
BitShares blockchain implementation and command-line interface 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;
103  if( max_duration_seconds.valid() )
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 }
150 
152 {
153  FC_ASSERT( fee.amount >= 0, "Fee should not be negative" );
154  FC_ASSERT( repay_amount.amount > 0, "Amount to repay should be positive" );
155  FC_ASSERT( credit_fee.amount >= 0, "Credit fee should not be negative" );
156  FC_ASSERT( repay_amount.asset_id == credit_fee.asset_id,
157  "Asset type of repay amount and credit fee should be the same" );
158 }
159 
160 } } // graphene::protocol
161 
167 
share_type balance
Usable amount in the credit offer.
Definition: api.cpp:56
constexpr int64_t GRAPHENE_MAX_CREDIT_DEAL_SECS
How long a credit deal will be kept, in seconds.
Definition: config.hpp:130
share_type calculate_fee(const fee_parameters_type &k) const
uint32_t fee_rate
Fee rate, the demominator is GRAPHENE_FEE_RATE_DENOM.
Accept a creadit offer and create a credit deal.
bool enabled
Whether this offer is available.
size_t pack_size(const T &v)
Definition: raw.hpp:757
constexpr int64_t GRAPHENE_MAX_CREDIT_DEAL_DAYS
How long a credit deal will be kept, in days.
Definition: config.hpp:128
time_point_sec auto_disable_time
The time when this offer will be disabled automatically.
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
Definition: exception.hpp:345
A credit deal expired without being fully repaid.
#define GRAPHENE_IMPLEMENT_EXTERNAL_SERIALIZATION(type)
Definition: types.hpp:86
share_type calculate_fee(const fee_parameters_type &k) const
share_type min_deal_amount
Minimum amount to borrow for each new deal.
static uint64_t calculate_data_fee(uint64_t bytes, uint64_t price_per_kbyte)
Definition: operations.cpp:33
constexpr int64_t GRAPHENE_MAX_SHARE_SUPPLY(1000000000000000LL)
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
uint32_t max_duration_seconds
The time limit that borrowed funds should be repaid.
Create a new credit offerA credit offer is a fund that can be used by other accounts who provide cert...