Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add conditions when accepting a credit offer #2511

Merged
merged 2 commits into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions libraries/chain/credit_offer_evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,12 @@ void_result credit_offer_accept_evaluator::do_evaluate(const credit_offer_accept
FC_ASSERT( _offer->min_deal_amount <= op.borrow_amount.amount,
"Borrowing amount should not be less than minimum deal amount" );

FC_ASSERT( _offer->fee_rate <= op.max_fee_rate,
"The maximum accceptable fee rate is lower than offered" );

FC_ASSERT( _offer->max_duration_seconds >= op.min_duration_seconds,
"The minimum accceptable duration is longer than offered" );

auto coll_itr = _offer->acceptable_collateral.find( op.collateral.asset_id );
FC_ASSERT( coll_itr != _offer->acceptable_collateral.end(),
"Collateral asset type is not acceptable by the credit offer" );
Expand Down
4 changes: 4 additions & 0 deletions libraries/protocol/include/graphene/protocol/credit_offer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ namespace graphene { namespace protocol {
credit_offer_id_type offer_id; ///< ID of the credit offer
asset borrow_amount; ///< The amount to borrow
asset collateral; ///< The collateral
uint32_t max_fee_rate = 0; ///< The maximum acceptable fee rate
uint32_t min_duration_seconds = 0; ///< The minimum acceptable duration

extensions_type extensions; ///< Unused. Reserved for future use.

Expand Down Expand Up @@ -239,6 +241,8 @@ FC_REFLECT( graphene::protocol::credit_offer_accept_operation,
(offer_id)
(borrow_amount)
(collateral)
(max_fee_rate)
(min_duration_seconds)
(extensions)
)

Expand Down
11 changes: 8 additions & 3 deletions tests/common/database_fixture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1807,21 +1807,26 @@ void database_fixture_base::update_credit_offer( account_id_type account, credit

credit_offer_accept_operation database_fixture_base::make_credit_offer_accept_op(
account_id_type account, credit_offer_id_type offer_id,
const asset& borrow_amount, const asset& collateral )const
const asset& borrow_amount, const asset& collateral,
uint32_t max_fee_rate, uint32_t min_duration )const
{
credit_offer_accept_operation op;
op.borrower = account;
op.offer_id = offer_id;
op.borrow_amount = borrow_amount;
op.collateral = collateral;
op.max_fee_rate = max_fee_rate;
op.min_duration_seconds = min_duration;
return op;
}

const credit_deal_object& database_fixture_base::borrow_from_credit_offer(
account_id_type account, credit_offer_id_type offer_id,
const asset& borrow_amount, const asset& collateral )
const asset& borrow_amount, const asset& collateral,
uint32_t max_fee_rate, uint32_t min_duration )
{
credit_offer_accept_operation op = make_credit_offer_accept_op( account, offer_id, borrow_amount, collateral );
credit_offer_accept_operation op = make_credit_offer_accept_op( account, offer_id, borrow_amount, collateral,
max_fee_rate, min_duration );
trx.operations.clear();
trx.operations.push_back( op );

Expand Down
8 changes: 6 additions & 2 deletions tests/common/database_fixture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,10 +517,14 @@ struct database_fixture_base {
const optional<flat_map<account_id_type, share_type>>& acceptable_borrowers );
credit_offer_accept_operation make_credit_offer_accept_op(
account_id_type account, credit_offer_id_type offer_id,
const asset& borrow_amount, const asset& collateral )const;
const asset& borrow_amount, const asset& collateral,
uint32_t max_fee_rate = GRAPHENE_FEE_RATE_DENOM,
uint32_t min_duration = 0 )const;
const credit_deal_object& borrow_from_credit_offer(
account_id_type account, credit_offer_id_type offer_id,
const asset& borrow_amount, const asset& collateral );
const asset& borrow_amount, const asset& collateral,
uint32_t max_fee_rate = GRAPHENE_FEE_RATE_DENOM,
uint32_t min_duration = 0 );
credit_deal_repay_operation make_credit_deal_repay_op(
account_id_type account, credit_deal_id_type deal_id,
const asset& repay_amount, const asset& credit_fee )const;
Expand Down
18 changes: 16 additions & 2 deletions tests/tests/credit_offer_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,7 @@ BOOST_AUTO_TEST_CASE( credit_offer_borrow_repay_test )
BOOST_CHECK_THROW( borrow_from_credit_offer( ray_id, tmp_co_id, asset(100), asset(200, usd_id) ),
fc::exception );

// create a credit offers
// create credit offers
auto disable_time1 = db.head_block_time() + fc::minutes(20); // 20 minutes after init

flat_map<asset_id_type, price> collateral_map1;
Expand Down Expand Up @@ -899,9 +899,23 @@ BOOST_AUTO_TEST_CASE( credit_offer_borrow_repay_test )
BOOST_CHECK_THROW( borrow_from_credit_offer( ray_id, co1_id, asset(100), asset(init_amount, usd_id) ),
fc::exception );

// Unable to borrow : maximum acceptable fee rate too low
auto ok_fee_rate = co1_id(db).fee_rate;
auto low_fee_rate = co1_id(db).fee_rate - 1;
BOOST_CHECK_THROW( borrow_from_credit_offer( ray_id, co1_id, asset(100), asset(200, usd_id), low_fee_rate ),
fc::exception );

// Unable to borrow : minimum acceptable duration too long
auto ok_duration = co1_id(db).max_duration_seconds;
auto long_duration = co1_id(db).max_duration_seconds + 1;
BOOST_CHECK_THROW( borrow_from_credit_offer( ray_id, co1_id, asset(100), asset(200, usd_id), ok_fee_rate,
long_duration ),
fc::exception );

// Able to borrow the same amount with the same collateral
BOOST_TEST_MESSAGE( "Ray borrows more" );
const credit_deal_object& cdo12 = borrow_from_credit_offer( ray_id, co1_id, asset(100), asset(200, usd_id) );
const credit_deal_object& cdo12 = borrow_from_credit_offer( ray_id, co1_id, asset(100), asset(200, usd_id),
ok_fee_rate, ok_duration );
credit_deal_id_type cd12_id = cdo12.id;
time_point_sec expected_repay_time12 = db.head_block_time() + fc::seconds(3600); // 60 minutes after init

Expand Down