Skip to content

Commit

Permalink
Merge pull request #4 from bitshares/oxarbitrage-issue1051
Browse files Browse the repository at this point in the history
Implement API changes
  • Loading branch information
oxarbitrage authored Feb 1, 2019
2 parents db640be + b579a54 commit 2efac1f
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 24 deletions.
31 changes: 22 additions & 9 deletions libraries/app/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ namespace graphene { namespace app {
}
else if( api_name == "asset_api" )
{
_asset_api = std::make_shared< asset_api >( std::ref( *_app.chain_database() ) );
_asset_api = std::make_shared< asset_api >( _app );
}
else if( api_name == "orders_api" )
{
Expand Down Expand Up @@ -284,10 +284,12 @@ namespace graphene { namespace app {
return *_debug_api;
}

vector<order_history_object> history_api::get_fill_order_history( asset_id_type a, asset_id_type b, uint32_t limit )const
vector<order_history_object> history_api::get_fill_order_history( std::string asset_a, std::string asset_b, uint32_t limit )const
{
FC_ASSERT(_app.chain_database());
const auto& db = *_app.chain_database();
asset_id_type a = database_api.get_asset_id_from_string( asset_a );
asset_id_type b = database_api.get_asset_id_from_string( asset_b );
if( a > b ) std::swap(a,b);
const auto& history_idx = db.get_index_type<graphene::market_history::history_index>().indices().get<by_key>();
history_key hkey;
Expand Down Expand Up @@ -443,11 +445,13 @@ namespace graphene { namespace app {
return result;
}

vector<bucket_object> history_api::get_market_history( asset_id_type a, asset_id_type b,
vector<bucket_object> history_api::get_market_history( std::string asset_a, std::string asset_b,
uint32_t bucket_seconds, fc::time_point_sec start, fc::time_point_sec end )const
{ try {
FC_ASSERT(_app.chain_database());
const auto& db = *_app.chain_database();
asset_id_type a = database_api.get_asset_id_from_string( asset_a );
asset_id_type b = database_api.get_asset_id_from_string( asset_b );
vector<bucket_object> result;
result.reserve(200);

Expand All @@ -467,7 +471,7 @@ namespace graphene { namespace app {
++itr;
}
return result;
} FC_CAPTURE_AND_RETHROW( (a)(b)(bucket_seconds)(start)(end) ) }
} FC_CAPTURE_AND_RETHROW( (asset_a)(asset_b)(bucket_seconds)(start)(end) ) }

crypto_api::crypto_api(){};

Expand Down Expand Up @@ -526,12 +530,17 @@ namespace graphene { namespace app {
}

// asset_api
asset_api::asset_api(graphene::chain::database& db) : _db(db) { }
asset_api::asset_api(graphene::app::application& app) :
_db( *app.chain_database()),
database_api( std::ref(*app.chain_database()), &(app.get_options())
) { }
asset_api::~asset_api() { }

vector<account_asset_balance> asset_api::get_asset_holders( asset_id_type asset_id, uint32_t start, uint32_t limit ) const {
vector<account_asset_balance> asset_api::get_asset_holders( std::string asset, uint32_t start, uint32_t limit ) const {
FC_ASSERT(limit <= 100);

asset_id_type asset_id = database_api.get_asset_id_from_string( asset );

const auto& bal_idx = _db.get_index_type< account_balance_index >().indices().get< by_asset_balance >();
auto range = bal_idx.equal_range( boost::make_tuple( asset_id ) );

Expand Down Expand Up @@ -562,9 +571,10 @@ namespace graphene { namespace app {
return result;
}
// get number of asset holders.
int asset_api::get_asset_holders_count( asset_id_type asset_id ) const {
int asset_api::get_asset_holders_count( std::string asset ) const {

const auto& bal_idx = _db.get_index_type< account_balance_index >().indices().get< by_asset_balance >();
asset_id_type asset_id = database_api.get_asset_id_from_string( asset );
auto range = bal_idx.equal_range( boost::make_tuple( asset_id ) );

int count = boost::distance(range) - 1;
Expand Down Expand Up @@ -607,8 +617,8 @@ namespace graphene { namespace app {
return plugin->tracked_groups();
}

vector< limit_order_group > orders_api::get_grouped_limit_orders( asset_id_type base_asset_id,
asset_id_type quote_asset_id,
vector< limit_order_group > orders_api::get_grouped_limit_orders( std::string base_asset,
std::string quote_asset,
uint16_t group,
optional<price> start,
uint32_t limit )const
Expand All @@ -619,6 +629,9 @@ namespace graphene { namespace app {
const auto& limit_groups = plugin->limit_order_groups();
vector< limit_order_group > result;

asset_id_type base_asset_id = database_api.get_asset_id_from_string( base_asset );
asset_id_type quote_asset_id = database_api.get_asset_id_from_string( quote_asset );

price max_price = price::max( base_asset_id, quote_asset_id );
price min_price = price::min( base_asset_id, quote_asset_id );
if( start.valid() && !start->is_null() )
Expand Down
5 changes: 5 additions & 0 deletions libraries/app/database_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,11 @@ vector<vesting_balance_object> database_api_impl::get_vesting_balances( const st
// //
//////////////////////////////////////////////////////////////////////

asset_id_type database_api::get_asset_id_from_string(const std::string& name_or_id)const
{
return my->get_asset_from_string( name_or_id )->id; // safe?
}

vector<optional<asset_object>> database_api::get_assets(const vector<std::string>& asset_symbols_or_ids)const
{
return my->get_assets( asset_symbols_or_ids );
Expand Down
26 changes: 14 additions & 12 deletions libraries/app/include/graphene/app/api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ namespace graphene { namespace app {
* @param limit Maximum records to return
* @return a list of order_history objects, in "most recent first" order
*/
vector<order_history_object> get_fill_order_history( asset_id_type a, asset_id_type b, uint32_t limit )const;
vector<order_history_object> get_fill_order_history( std::string a, std::string b, uint32_t limit )const;

/**
* @brief Get OHLCV data of a trading pair in a time range
Expand All @@ -205,7 +205,7 @@ namespace graphene { namespace app {
* @return A list of OHLCV data, in "least recent first" order.
* If there are more than 200 records in the specified time range, the first 200 records will be returned.
*/
vector<bucket_object> get_market_history( asset_id_type a, asset_id_type b, uint32_t bucket_seconds,
vector<bucket_object> get_market_history( std::string a, std::string b, uint32_t bucket_seconds,
fc::time_point_sec start, fc::time_point_sec end )const;

/**
Expand Down Expand Up @@ -438,24 +438,24 @@ namespace graphene { namespace app {
class asset_api
{
public:
asset_api(graphene::chain::database& db);
asset_api(graphene::app::application& app);
~asset_api();

/**
* @brief Get asset holders for a specific asset
* @param asset_id The specific asset
* @param asset The specific asset id or name
* @param start The start index
* @param limit Maximum limit must not exceed 100
* @return A list of asset holders for the specified asset
*/
vector<account_asset_balance> get_asset_holders( asset_id_type asset_id, uint32_t start, uint32_t limit )const;
vector<account_asset_balance> get_asset_holders( std::string asset, uint32_t start, uint32_t limit )const;

/**
* @brief Get asset holders count for a specific asset
* @param asset_id The specific asset
* @param asset The specific asset id or name
* @return Holders count for the specified asset
*/
int get_asset_holders_count( asset_id_type asset_id )const;
int get_asset_holders_count( std::string asset )const;

/**
* @brief Get all asset holders
Expand All @@ -465,6 +465,7 @@ namespace graphene { namespace app {

private:
graphene::chain::database& _db;
graphene::app::database_api database_api;
};

/**
Expand All @@ -473,7 +474,7 @@ namespace graphene { namespace app {
class orders_api
{
public:
orders_api(application& app):_app(app){}
orders_api(application& app):_app(app), database_api( std::ref(*app.chain_database()), &(app.get_options()) ){}
//virtual ~orders_api() {}

/**
Expand All @@ -485,21 +486,22 @@ namespace graphene { namespace app {
/**
* @breif Get grouped limit orders in given market.
*
* @param base_asset_id ID of asset being sold
* @param quote_asset_id ID of asset being purchased
* @param base_asset ID or name of asset being sold
* @param quote_asset ID or name of asset being purchased
* @param group Maximum price diff within each order group, have to be one of configured values
* @param start Optional price to indicate the first order group to retrieve
* @param limit Maximum number of order groups to retrieve (must not exceed 101)
* @return The grouped limit orders, ordered from best offered price to worst
*/
vector< limit_order_group > get_grouped_limit_orders( asset_id_type base_asset_id,
asset_id_type quote_asset_id,
vector< limit_order_group > get_grouped_limit_orders( std::string base_asset,
std::string quote_asset,
uint16_t group,
optional<price> start,
uint32_t limit )const;

private:
application& _app;
graphene::app::database_api database_api;
};

/**
Expand Down
7 changes: 7 additions & 0 deletions libraries/app/include/graphene/app/database_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,13 @@ class database_api
// Assets //
////////////

/**
* @brief Get asset id from a name or ID
* @param name_or_id ID or name of the asset
* @return asset id
*/
asset_id_type get_asset_id_from_string(const std::string& name_or_id) const;

/**
* @brief Get a list of assets by ID
* @param asset_symbols_or_ids Symbol names or IDs of the assets to retrieve
Expand Down
2 changes: 1 addition & 1 deletion libraries/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3190,7 +3190,7 @@ vector<bucket_object> wallet_api::get_market_history(
fc::time_point_sec start,
fc::time_point_sec end )const
{
return my->_remote_hist->get_market_history( get_asset_id(symbol1), get_asset_id(symbol2), bucket, start, end );
return my->_remote_hist->get_market_history( symbol1, symbol2, bucket, start, end );
}

vector<limit_order_object> wallet_api::get_account_limit_orders(
Expand Down
4 changes: 2 additions & 2 deletions tests/tests/asset_api_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ BOOST_FIXTURE_TEST_SUITE(asset_api_tests, database_fixture)

BOOST_AUTO_TEST_CASE( asset_holders )
{
graphene::app::asset_api asset_api(db);
graphene::app::asset_api asset_api(app);

// create an asset and some accounts
create_bitasset("USD", account_id_type());
Expand All @@ -51,7 +51,7 @@ BOOST_AUTO_TEST_CASE( asset_holders )
transfer(account_id_type()(db), bob, asset(300));

// make call
vector<account_asset_balance> holders = asset_api.get_asset_holders(asset_id_type(), 0, 100);
vector<account_asset_balance> holders = asset_api.get_asset_holders( std::string( static_cast<object_id_type>(asset_id_type())), 0, 100);
BOOST_CHECK_EQUAL(holders.size(), 4u);

// by now we can guarantee the order
Expand Down

0 comments on commit 2efac1f

Please sign in to comment.