transaction_builder& transaction_builder::cancel_market_order( const order_id_type& order_id ) { try { const auto order = _wimpl->_blockchain->get_market_order( order_id ); if( !order.valid() ) FC_THROW_EXCEPTION( unknown_market_order, "Cannot find that market order!" ); const auto owner_address = order->get_owner(); const auto owner_key_record = _wimpl->_wallet_db.lookup_key( owner_address ); if( !owner_key_record.valid() || !owner_key_record->has_private_key() ) FC_THROW_EXCEPTION( private_key_not_found, "Cannot find the private key for that market order!" ); const auto account_record = _wimpl->_wallet_db.lookup_account( owner_key_record->account_address ); FC_ASSERT( account_record.valid() ); asset balance = order->get_balance(); if( balance.amount == 0 ) FC_CAPTURE_AND_THROW( zero_amount, (order) ); switch( order_type_enum( order->type ) ) { case ask_order: trx.ask( -balance, order->market_index.order_price, owner_address ); break; case bid_order: trx.bid( -balance, order->market_index.order_price, owner_address ); break; case short_order: trx.short_sell( -balance, order->market_index.order_price, owner_address ); break; default: FC_THROW_EXCEPTION( invalid_cancel, "You cannot cancel this type of order!" ); break; } //Credit this account the cancel proceeds credit_balance(account_record->owner_address(), balance); //Set order key for this account if not already set if( order_keys.find(account_record->owner_address()) == order_keys.end() ) { const oasset_record asset_record = _wimpl->_blockchain->get_asset_record( balance.asset_id ); FC_ASSERT( asset_record.valid() ); if( asset_record->flag_is_active( asset_record::restricted_accounts ) ) order_keys[ account_record->owner_address() ] = account_record->active_key(); else order_keys[ account_record->owner_address() ] = owner_key_record->public_key; } auto entry = ledger_entry(); entry.from_account = owner_key_record->public_key; entry.to_account = account_record->owner_key; entry.amount = balance; entry.memo = "cancel " + order->get_small_id(); transaction_record.is_market = true; transaction_record.ledger_entries.push_back( entry ); required_signatures.insert( owner_address ); return *this; } FC_CAPTURE_AND_RETHROW( (order_id) ) }
transaction_builder& transaction_builder::submit_bid(const wallet_account_record& from_account, const asset& real_quantity, const price& quote_price) { try { validate_market(quote_price.quote_asset_id, quote_price.base_asset_id); asset cost; if( real_quantity.asset_id == quote_price.quote_asset_id ) cost = real_quantity; else { cost = real_quantity * quote_price; FC_ASSERT(cost.asset_id == quote_price.quote_asset_id); } const oasset_record base_asset_record = _wimpl->_blockchain->get_asset_record( quote_price.base_asset_id ); FC_ASSERT( base_asset_record.valid() ); public_key_type order_key; if( base_asset_record->flag_is_active( asset_record::restricted_accounts ) ) order_key = from_account.active_key(); else order_key = order_key_for_account( from_account.owner_address(), from_account.name ); order_keys[ from_account.owner_address() ] = order_key; //Charge this account for the bid deduct_balance(from_account.owner_address(), cost); trx.bid(cost, quote_price, order_key); if( trx.expiration == time_point_sec() ) trx.expiration = blockchain::now() + WALLET_DEFAULT_MARKET_TRANSACTION_EXPIRATION_SEC; auto entry = ledger_entry(); entry.from_account = from_account.owner_key; entry.to_account = order_key; entry.amount = cost; entry.memo = "buy " + _wimpl->_blockchain->get_asset_symbol(quote_price.base_asset_id) + " @ " + _wimpl->_blockchain->to_pretty_price(quote_price); transaction_record.is_market = true; transaction_record.ledger_entries.push_back(entry); required_signatures.insert(order_key); return *this; } FC_CAPTURE_AND_RETHROW( (from_account.name)(real_quantity)(quote_price) ) }