31 #include <graphene/chain/hardfork.hpp> 36 namespace graphene {
namespace chain {
43 FC_ASSERT( HARDFORK_CORE_2362_PASSED(block_time),
"Not allowed until the core-2362 hardfork" );
49 "Auto-disable time should not be later than ${d} days in the future",
66 "The account is unauthorized by the asset" );
81 obj.current_balance = op.
balance;
90 return new_credit_offer_object.
id;
99 FC_ASSERT( _offer->owner_account == op.
owner_account,
"The account is not the owner of the credit offer" );
101 FC_ASSERT( _offer->total_balance == _offer->current_balance,
102 "Can only delete a credit offer when the unpaid amount is zero" );
113 asset released( _offer->current_balance, _offer->asset_type );
115 if( _offer->current_balance != 0 )
132 FC_ASSERT( _offer->owner_account == op.
owner_account,
"The account is not the owner of the credit offer" );
142 "The account is unauthorized by the asset" );
147 "Should leave some funds in the credit offer when updating" );
148 FC_ASSERT( _offer->current_balance >= -op.
delta_amount->amount,
"Insufficient balance in the credit offer" );
156 FC_ASSERT( auto_disable_time > block_time,
"Auto-disable time should be in the future" );
158 "Auto-disable time should not be later than ${d} days in the future",
168 FC_ASSERT( _offer->asset_type == collateral.second.base.asset_id,
169 "Asset type mismatch in a price of acceptable collateral" );
214 FC_ASSERT( _offer->total_balance > 0,
"Total balance in the credit offer should be positive" );
215 FC_ASSERT( _offer->current_balance >= 0,
"Current balance in the credit offer should not be negative" );
216 FC_ASSERT( _offer->total_balance >= _offer->current_balance,
217 "Total balance in the credit offer should not be less than current balance" );
218 if( _offer->enabled )
221 "Auto-disable time should be in the future if the credit offer is enabled" );
223 "Auto-disable time should not be too late in the future" );
235 FC_ASSERT( _offer->enabled,
"The credit offer is not enabled" );
240 "Insufficient balance in the credit offer thus unable to borrow" );
243 "Borrowing amount should not be less than minimum deal amount" );
246 "The maximum accceptable fee rate is lower than offered" );
249 "The minimum accceptable duration is longer than offered" );
252 FC_ASSERT( coll_itr != _offer->acceptable_collateral.end(),
253 "Collateral asset type is not acceptable by the credit offer" );
255 const asset_object& debt_asset_obj = _offer->asset_type(d);
259 "The borrower is unauthorized by the borrowing asset" );
261 "The borrower is unauthorized by the collateral asset" );
266 "The owner of the credit offer is unauthorized by the borrowing asset" );
268 "The owner of the credit offer is unauthorized by the collateral asset" );
272 "Insufficient collateral provided, requires ${r}, provided ${p}",
276 if( !_offer->acceptable_borrowers.empty() )
278 auto itr = _offer->acceptable_borrowers.find( op.
borrower );
279 FC_ASSERT( itr != _offer->acceptable_borrowers.end(),
"Account is not in acceptable borrowers" );
280 max_allowed = itr->second;
285 auto summ_itr = deal_summary_idx.find( boost::make_tuple( op.
offer_id, op.
borrower ) );
286 if( summ_itr != deal_summary_idx.end() )
288 _deal_summary = &(*summ_itr);
289 already_borrowed = _deal_summary->total_debt_amount;
292 if( max_allowed.
valid() )
295 "Unable to borrow ${b}, already borrowed ${a}, maximum allowed ${m}",
315 ? ( block_time + _offer->max_duration_seconds )
321 obj.offer_owner = _offer->owner_account;
322 obj.debt_asset = _offer->asset_type;
323 obj.debt_amount = op.borrow_amount.amount;
327 obj.latest_repay_time = repay_time;
330 if( _deal_summary !=
nullptr )
341 obj.offer_owner = _offer->owner_account;
342 obj.debt_asset = _offer->asset_type;
343 obj.total_debt_amount = op.borrow_amount.amount;
348 FC_ASSERT( _offer->total_balance > 0,
"Total balance in the credit offer should be positive" );
349 FC_ASSERT( _offer->current_balance >= 0,
"Current balance in the credit offer should not be negative" );
350 FC_ASSERT( _offer->total_balance >= _offer->current_balance,
351 "Total balance in the credit offer should not be less than current balance" );
352 FC_ASSERT( new_deal.latest_repay_time > block_time,
353 "Latest repayment time should be in the future" );
355 "Latest repayment time should not be too late in the future" );
359 result.
value.new_objects = flat_set<object_id_type>({ new_deal.id });
360 result.
value.impacted_accounts = flat_set<account_id_type>({ _offer->owner_account });
371 FC_ASSERT( _deal->borrower == op.
account,
"A credit deal can only be repaid by the borrower" );
376 "Repay amount should not be greater than unpaid amount" );
383 "Insuffient credit fee, requires ${r}, offered ${p}",
386 const asset_object& debt_asset_obj = _deal->debt_asset(d);
390 "The account is unauthorized by the repaying asset" );
392 "The owner of the credit offer is unauthorized by the repaying asset" );
413 "Total balance in the credit offer should not be less than current balance" );
417 result.
value.updated_objects = flat_set<object_id_type>({ offer.
id });
421 auto summ_itr = deal_summary_idx.find( boost::make_tuple( _deal->offer_id, op.
account ) );
422 FC_ASSERT( summ_itr != deal_summary_idx.end(),
"Internal error" );
437 asset collateral_released( _deal->collateral_amount, _deal->collateral_asset );
440 result.
value.removed_objects = flat_set<object_id_type>({ _deal->id });
446 / _deal->debt_amount.value;
447 FC_ASSERT( amount_to_release < fc::uint128_t( _deal->collateral_amount.value ),
"Internal error" );
448 collateral_released.
amount =
static_cast<int64_t
>( amount_to_release );
455 result.
value.updated_objects->insert( _deal->id );
459 result.
value.received = vector<asset>({ collateral_released });
share_type total_debt_amount
How much funds borrowed.
A credit offer is a fund that can be used by other accounts who provide certain collateral.
account_id_type account
The account who repays to the credit offer.
optional< asset > delta_amount
Delta amount, optional.
void modify(const T &obj, const Lambda &m)
share_type collateral_amount
How much funds in collateral.
void adjust_balance(account_id_type account, asset delta)
Adjust a particular account's balance in a given asset by a delta.
share_type balance
Usable amount in the credit offer.
bool is_authorized_asset(const database &d, const account_object &acct, const asset_object &asset_obj)
time_point_sec head_block_time() const
A credit deal describes the details of a borrower's borrowing of funds from a credit offer...
This class represents an account on the object graphAccounts are the primary unit of authority on the...
tracks the blockchain state in an extensible manner
const IndexType & get_index_type() const
asset repay_amount
The amount to repay.
A credit deal summary describes the summary of a borrower's borrowing of funds from a credit offer...
uint32_t fee_rate
Fee rate, the demominator is GRAPHENE_FEE_RATE_DENOM.
const account_object * fee_paying_account
constexpr int64_t GRAPHENE_MAX_CREDIT_OFFER_DAYS
How long a credit offer will be kept active, in days.
share_type current_balance
Usable amount in the fund.
account_id_type borrower
Borrower.
flat_map< asset_id_type, price > acceptable_collateral
Types and rates of acceptable collateral.
credit_offer_id_type offer_id
ID of the credit offer.
bool enabled
Whether this offer is available.
Accept a creadit offer and create a credit deal.
credit_offer_id_type offer_id
ID of the credit offer.
bool enabled
Whether this offer is available.
credit_deal_id_type deal_id
ID of the credit deal.
flat_map< account_id_type, share_type > acceptable_borrowers
Allowed borrowers and their maximum amounts to borrow. No limitation if empty.
optional< uint32_t > max_duration_seconds
New repayment time limit, optional.
asset multiply_and_round_up(const price &p) const
Multiply and round up.
provides stack-based nullable value similar to boost::optional
optional< time_point_sec > auto_disable_time
New time to disable automatically, optional.
void_result do_evaluate(const credit_offer_create_operation &op) const
optional< uint32_t > fee_rate
New fee rate, optional.
uint32_t fee_rate
Fee rate, the demominator is GRAPHENE_FEE_RATE_DENOM.
share_type total_balance
Total size of the fund.
constexpr int64_t GRAPHENE_MAX_CREDIT_DEAL_DAYS
How long a credit deal will be kept, in days.
microseconds days(int64_t d)
credit_offer_id_type offer_id
ID of the credit offer.
static time_point_sec maximum()
asset do_apply(const credit_offer_delete_operation &op) const
microseconds seconds(int64_t s)
asset collateral
The collateral.
uint32_t max_duration_seconds
The time limit that borrowed funds should be repaid.
const object & get(object_id_type id) const
void_result do_evaluate(const credit_offer_update_operation &op)
account_id_type owner_account
The account who owns the credit offer.
optional< flat_map< asset_id_type, price > > acceptable_collateral
New types and rates of acceptable collateral, optional.
time_point_sec auto_disable_time
The time when this offer will be disabled automatically.
optional< bool > enabled
Whether this offer is available, optional.
void_result do_evaluate(const credit_deal_repay_operation &op)
#define FC_CAPTURE_AND_RETHROW(...)
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
void_result do_evaluate(const credit_offer_accept_operation &op)
optional< share_type > min_deal_amount
Minimum amount to borrow for each new deal, optional.
flat_map< asset_id_type, price > acceptable_collateral
Types and rates of acceptable collateral.
void_result do_evaluate(const credit_offer_delete_operation &op)
asset_id_type asset_type
Asset type in the credit offer.
share_type debt_amount
How much funds borrowed.
flat_map< account_id_type, share_type > acceptable_borrowers
Allowed borrowers and their maximum amounts to borrow. No limitation if empty.
share_type min_deal_amount
Minimum amount to borrow for each new deal.
constexpr uint32_t GRAPHENE_FEE_RATE_DENOM
Denominator for SameT Fund fee calculation.
account_id_type owner_account
Owner of the fund.
optional< flat_map< account_id_type, share_type > > acceptable_borrowers
New allowed borrowers and their maximum amounts to borrow, optional.
void_result do_apply(const credit_offer_update_operation &op) const
tracks the parameters of an assetAll assets have a globally unique symbol name that controls how they...
account_id_type borrower
The account who accepts the offer.
extendable_operation_result do_apply(const credit_deal_repay_operation &op) const
credit_offer_id_type offer_id
ID of the credit offer.
account_id_type borrower
Borrower.
asset_id_type collateral_asset
Asset type of the collateral.
share_type min_deal_amount
Minimum amount to borrow for each new deal.
void remove(const object &obj)
account_id_type owner_account
Owner of the credit offer.
uint32_t fee_rate
Fee rate, the demominator is GRAPHENE_FEE_RATE_DENOM.
object_id_type do_apply(const credit_offer_create_operation &op) const
asset credit_fee
The credit fee relative to the amount to repay.
account_id_type owner_account
Owner of the credit offer.
uint32_t max_fee_rate
The maximum acceptable fee rate.
uint32_t max_duration_seconds
The time limit that borrowed funds should be repaid.
credit_offer_id_type offer_id
ID of the credit offer.
asset borrow_amount
The amount to borrow.
const T & create(F &&constructor)
extendable_operation_result do_apply(const credit_offer_accept_operation &op) const
uint32_t min_duration_seconds
The minimum acceptable duration.
time_point_sec auto_disable_time
The time when this offer will be disabled automatically.
Create a new credit offerA credit offer is a fund that can be used by other accounts who provide cert...