Skip to content
This repository has been archived by the owner on Feb 21, 2019. It is now read-only.

Commit

Permalink
Enable voting with cover collateral
Browse files Browse the repository at this point in the history
  - changed the update_call_price to update_cover and allowed it to
  change the slate and owner (transferring short positions is now
                              possible)
  - market engine now updates votes when using collateral
  - remove comments
  • Loading branch information
bytemaster committed Feb 6, 2015
1 parent edd8c15 commit c48646f
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 24 deletions.
2 changes: 2 additions & 0 deletions libraries/blockchain/include/bts/blockchain/market_engine.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <bts/blockchain/chain_database_impl.hpp>
#include <bts/blockchain/transaction_evaluation_state.hpp>

namespace bts { namespace blockchain { namespace detail {

Expand Down Expand Up @@ -62,6 +63,7 @@ namespace bts { namespace blockchain { namespace detail {
void cancel_current_relative_bid( market_transaction& mtrx );
void cancel_current_relative_ask( market_transaction& mtrx );

transaction_evaluation_state _eval_state;
pending_chain_state_ptr _pending_state;
pending_chain_state_ptr _prior_state;
const chain_database_impl& _db_impl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,14 @@ namespace bts { namespace blockchain {
* allow the short to exit their position without having to keep
* extra BTS on the side to buy USD to cover the order.
*/
struct update_call_price_operation
struct update_cover_operation
{
static const operation_type_enum type;

market_index_key cover_index;
price new_call_price;
market_index_key cover_index;
price new_call_price;
optional<address> new_owner;
optional<slate_id_type> new_slate;

void evaluate( transaction_evaluation_state& eval_state )const;
};
Expand All @@ -131,4 +133,4 @@ FC_REFLECT( bts::blockchain::relative_ask_operation, (amount)(ask_index)(li
FC_REFLECT( bts::blockchain::short_operation, (amount)(short_index) )
FC_REFLECT( bts::blockchain::cover_operation, (amount)(cover_index)(new_cover_price) )
FC_REFLECT( bts::blockchain::add_collateral_operation, (amount)(cover_index))
FC_REFLECT( bts::blockchain::update_call_price_operation, (cover_index)(new_call_price))
FC_REFLECT( bts::blockchain::update_cover_operation, (cover_index)(new_call_price)(new_owner)(new_slate) )
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,11 @@ namespace bts { namespace blockchain {
:collateral_balance(c),payoff_balance(p),interest_rate(apr),expiration(exp){}
bool is_null() const { return 0 == payoff_balance && 0 == collateral_balance; }

share_type collateral_balance;
share_type payoff_balance;
share_type collateral_balance = 0;
share_type payoff_balance = 0;
price interest_rate;
time_point_sec expiration; // after expiration the collateral is forced to be called.
slate_id_type slate_id = 0;
};
typedef fc::optional<collateral_record> ocollateral_record;

Expand Down Expand Up @@ -324,7 +325,7 @@ FC_REFLECT( bts::blockchain::market_history_record, (highest_bid)(lowest_ask)(op
FC_REFLECT( bts::blockchain::market_history_key, (quote_id)(base_id)(granularity)(timestamp) )
FC_REFLECT( bts::blockchain::market_history_point, (timestamp)(highest_bid)(lowest_ask)(opening_price)(closing_price)(volume) )
FC_REFLECT( bts::blockchain::order_record, (balance)(limit_price)(last_update) )
FC_REFLECT( bts::blockchain::collateral_record, (collateral_balance)(payoff_balance)(interest_rate)(expiration) )
FC_REFLECT( bts::blockchain::collateral_record, (collateral_balance)(payoff_balance)(interest_rate)(expiration)(slate_id) )
FC_REFLECT( bts::blockchain::market_order, (type)(market_index)(state)(collateral)(interest_rate)(expiration) )
FC_REFLECT_TYPENAME( std::vector<bts::blockchain::market_transaction> )
FC_REFLECT_TYPENAME( bts::blockchain::market_history_key::time_granularity_enum ) // http://en.wikipedia.org/wiki/Voodoo_programminqg
Expand Down
15 changes: 2 additions & 13 deletions libraries/blockchain/include/bts/blockchain/operations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,8 @@ namespace bts { namespace blockchain {
make_sale_op_type = 37,
buy_sale_op_type = 38, // makes a buy or an offer

/* devshares
// events
make_event_op_type = 39,
resolve_event_op_type = 40,
// prediction markets
make_lsmr_market = 41,
buy_lsmr_share = 42,
redeem_lsmr_share = 43
*/

pay_fee_op_type = 44,
update_call_price_op_type = 45
update_cover_op_type = 45

};

Expand Down Expand Up @@ -198,7 +187,7 @@ FC_REFLECT_ENUM( bts::blockchain::operation_type_enum,
(make_sale_op_type)
(buy_sale_op_type)
(pay_fee_op_type)
(update_call_price_op_type)
(update_cover_op_type)
)

FC_REFLECT( bts::blockchain::operation, (type)(data) )
Expand Down
4 changes: 4 additions & 0 deletions libraries/blockchain/market_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace bts { namespace blockchain { namespace detail {
{
_pending_state = std::make_shared<pending_chain_state>( ps );
_prior_state = ps;
_eval_state._current_state = _pending_state.get();
}

bool market_engine::execute( asset_id_type quote_id, asset_id_type base_id, const fc::time_point_sec timestamp )
Expand Down Expand Up @@ -377,6 +378,7 @@ namespace bts { namespace blockchain { namespace detail {
wlog( "done matching orders" );
idump( (_current_bid)(_current_ask) );

_eval_state.update_delegate_votes();
_pending_state->apply_changes();
return true;
}
Expand Down Expand Up @@ -624,6 +626,7 @@ namespace bts { namespace blockchain { namespace detail {

*(_current_ask->collateral) -= mtrx.ask_paid.amount;


FC_ASSERT( *_current_ask->collateral >= 0, "",
("mtrx",mtrx)("_current_ask", _current_ask)("interest_paid",interest_paid) );

Expand Down Expand Up @@ -657,6 +660,7 @@ namespace bts { namespace blockchain { namespace detail {
_pending_state->store_balance_record( *ask_payout );
_current_ask->collateral = 0;
}
_eval_state.adjust_vote( _current_collat_record.slate_id, -mtrx.ask_paid.amount );

_current_collat_record.collateral_balance = *_current_ask->collateral;
_current_collat_record.payoff_balance = _current_ask->state.balance;
Expand Down
14 changes: 12 additions & 2 deletions libraries/blockchain/market_operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,10 @@ namespace bts { namespace blockchain {
}
else // withdraw the collateral to the transaction to be deposited at owners discretion / cover fees
{
if( current_cover->slate_id && cover_index.order_price.base_asset_id == 0 )
{
eval_state.adjust_vote( current_cover->slate_id, -current_cover->collateral_balance );
}
eval_state.add_balance( asset( current_cover->collateral_balance, cover_index.order_price.base_asset_id ) );
}
}
Expand Down Expand Up @@ -415,7 +419,7 @@ namespace bts { namespace blockchain {
*current_cover );
}

void update_call_price_operation::evaluate( transaction_evaluation_state& eval_state )const
void update_cover_operation::evaluate( transaction_evaluation_state& eval_state )const
{
if( this->cover_index.order_price == price() )
FC_CAPTURE_AND_THROW( zero_price, (cover_index.order_price) );
Expand All @@ -441,7 +445,13 @@ namespace bts { namespace blockchain {
// changing the payoff balance changes the call price... so we need to remove the old record
// and insert a new one.
eval_state._current_state->store_collateral_record( this->cover_index, collateral_record() );
eval_state._current_state->store_collateral_record( market_index_key( new_call_price, this->cover_index.owner),
if( this->new_slate && *this->new_slate != current_cover->slate_id )
{
eval_state.adjust_vote( current_cover->slate_id, -current_cover->collateral_balance );
current_cover->slate_id = *this->new_slate;
eval_state.adjust_vote( current_cover->slate_id, current_cover->collateral_balance );
}
eval_state._current_state->store_collateral_record( market_index_key( new_call_price, this->new_owner ? *this->new_owner : this->cover_index.owner),
*current_cover );
}

Expand Down
4 changes: 2 additions & 2 deletions libraries/blockchain/operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace bts { namespace blockchain {
const operation_type_enum short_operation::type = short_op_type;
const operation_type_enum cover_operation::type = cover_op_type;
const operation_type_enum add_collateral_operation::type = add_collateral_op_type;
const operation_type_enum update_call_price_operation::type = update_call_price_op_type;
const operation_type_enum update_cover_operation::type = update_cover_op_type;

const operation_type_enum define_slate_operation::type = define_slate_op_type;

Expand Down Expand Up @@ -72,7 +72,7 @@ namespace bts { namespace blockchain {
bts::blockchain::operation_factory::instance().register_operation<short_operation>();
bts::blockchain::operation_factory::instance().register_operation<cover_operation>();
bts::blockchain::operation_factory::instance().register_operation<add_collateral_operation>();
bts::blockchain::operation_factory::instance().register_operation<update_call_price_operation>();
bts::blockchain::operation_factory::instance().register_operation<update_cover_operation>();

bts::blockchain::operation_factory::instance().register_operation<define_slate_operation>();

Expand Down

0 comments on commit c48646f

Please sign in to comment.