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

Database_api code refactory #1891

Merged
merged 6 commits into from
Aug 15, 2019
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
1 change: 1 addition & 0 deletions libraries/app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ file(GLOB EGENESIS_HEADERS "../egenesis/include/graphene/app/*.hpp")

add_library( graphene_app
api.cpp
api_objects.cpp
application.cpp
util.cpp
database_api.cpp
Expand Down
91 changes: 91 additions & 0 deletions libraries/app/api_objects.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright (c) 2017 Cryptonomex, Inc., and contributors.
*
* The MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#include <graphene/app/api_objects.hpp>
#include <graphene/app/util.hpp>

namespace graphene { namespace app {

market_ticker::market_ticker(const market_ticker_object& mto,
const fc::time_point_sec& now,
const asset_object& asset_base,
const asset_object& asset_quote,
const order_book& orders)
{
time = now;
base = asset_base.symbol;
quote = asset_quote.symbol;
percent_change = "0";
lowest_ask = "0";
highest_bid = "0";

fc::uint128_t bv;
fc::uint128_t qv;
price latest_price = asset( mto.latest_base, mto.base ) / asset( mto.latest_quote, mto.quote );
if( mto.base != asset_base.id )
latest_price = ~latest_price;
latest = price_to_string( latest_price, asset_base, asset_quote );
if( mto.last_day_base != 0 && mto.last_day_quote != 0 // has trade data before 24 hours
&& ( mto.last_day_base != mto.latest_base || mto.last_day_quote != mto.latest_quote ) ) // price changed
{
price last_day_price = asset( mto.last_day_base, mto.base ) / asset( mto.last_day_quote, mto.quote );
if( mto.base != asset_base.id )
last_day_price = ~last_day_price;
percent_change = price_diff_percent_string( last_day_price, latest_price );
}
if( asset_base.id == mto.base )
{
bv = mto.base_volume;
qv = mto.quote_volume;
}
else
{
bv = mto.quote_volume;
qv = mto.base_volume;
}
base_volume = uint128_amount_to_string( bv, asset_base.precision );
quote_volume = uint128_amount_to_string( qv, asset_quote.precision );

if(!orders.asks.empty())
lowest_ask = orders.asks[0].price;
if(!orders.bids.empty())
highest_bid = orders.bids[0].price;
}

market_ticker::market_ticker(const fc::time_point_sec& now,
const asset_object& asset_base,
const asset_object& asset_quote)
{
time = now;
base = asset_base.symbol;
quote = asset_quote.symbol;
latest = "0";
lowest_ask = "0";
highest_bid = "0";
percent_change = "0";
base_volume = "0";
quote_volume = "0";
}

} } // graphene::app
943 changes: 340 additions & 603 deletions libraries/app/database_api.cpp

Large diffs are not rendered by default.

366 changes: 366 additions & 0 deletions libraries/app/database_api_impl.hxx

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,21 @@
#pragma once

#include <graphene/chain/account_object.hpp>
#include <graphene/chain/asset_object.hpp>
#include <graphene/chain/vesting_balance_object.hpp>
#include <graphene/chain/market_evaluator.hpp>
#include <graphene/chain/market_object.hpp>
#include <graphene/chain/proposal_object.hpp>
#include <graphene/chain/withdraw_permission_object.hpp>
#include <graphene/chain/htlc_object.hpp>

#include <graphene/api_helper_indexes/api_helper_indexes.hpp>
#include <graphene/market_history/market_history_plugin.hpp>

#include <fc/optional.hpp>

namespace graphene { namespace app {
using namespace graphene::chain;
using namespace graphene::market_history;

struct more_data
{
Expand Down Expand Up @@ -72,6 +78,74 @@ namespace graphene { namespace app {
more_data more_data_available;
};

struct order
{
string price;
string quote;
string base;
};

struct order_book
{
string base;
string quote;
vector< order > bids;
vector< order > asks;
};

struct market_ticker
{
time_point_sec time;
string base;
string quote;
string latest;
string lowest_ask;
string highest_bid;
string percent_change;
string base_volume;
string quote_volume;

market_ticker() {}
market_ticker(const market_ticker_object& mto,
const fc::time_point_sec& now,
const asset_object& asset_base,
const asset_object& asset_quote,
const order_book& orders);
market_ticker(const fc::time_point_sec& now,
const asset_object& asset_base,
const asset_object& asset_quote);
};

struct market_volume
{
time_point_sec time;
string base;
string quote;
string base_volume;
string quote_volume;
};

struct market_trade
{
int64_t sequence = 0;
fc::time_point_sec date;
string price;
string amount;
string value;
account_id_type side1_account_id = GRAPHENE_NULL_ACCOUNT;
account_id_type side2_account_id = GRAPHENE_NULL_ACCOUNT;
};

struct extended_asset_object : asset_object
{
extended_asset_object() {}
explicit extended_asset_object( const asset_object& a ) : asset_object( a ) {}
explicit extended_asset_object( asset_object&& a ) : asset_object( std::move(a) ) {}

optional<share_type> total_in_collateral;
optional<share_type> total_backing_collateral;
};

} }

FC_REFLECT( graphene::app::more_data,
Expand Down Expand Up @@ -100,3 +174,13 @@ FC_REFLECT( graphene::app::full_account,
(htlcs_to)
(more_data_available)
)

FC_REFLECT( graphene::app::order, (price)(quote)(base) );
FC_REFLECT( graphene::app::order_book, (base)(quote)(bids)(asks) );
FC_REFLECT( graphene::app::market_ticker,
(time)(base)(quote)(latest)(lowest_ask)(highest_bid)(percent_change)(base_volume)(quote_volume) );
FC_REFLECT( graphene::app::market_volume, (time)(base)(quote)(base_volume)(quote_volume) );
FC_REFLECT( graphene::app::market_trade, (sequence)(date)(price)(amount)(value)(side1_account_id)(side2_account_id) );

FC_REFLECT_DERIVED( graphene::app::extended_asset_object, (graphene::chain::asset_object),
(total_in_collateral)(total_backing_collateral) );
89 changes: 1 addition & 88 deletions libraries/app/include/graphene/app/database_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,21 @@
*/
#pragma once

#include <graphene/app/full_account.hpp>
#include <graphene/app/api_objects.hpp>

#include <graphene/protocol/types.hpp>

#include <graphene/chain/database.hpp>

#include <graphene/chain/account_object.hpp>
#include <graphene/chain/asset_object.hpp>
#include <graphene/chain/balance_object.hpp>
#include <graphene/chain/chain_property_object.hpp>
#include <graphene/chain/committee_member_object.hpp>
#include <graphene/chain/confidential_object.hpp>
#include <graphene/chain/market_object.hpp>
#include <graphene/chain/operation_history_object.hpp>
#include <graphene/chain/proposal_object.hpp>
#include <graphene/chain/worker_object.hpp>
#include <graphene/chain/witness_object.hpp>
#include <graphene/chain/htlc_object.hpp>

#include <graphene/api_helper_indexes/api_helper_indexes.hpp>
#include <graphene/market_history/market_history_plugin.hpp>

#include <fc/api.hpp>
#include <fc/optional.hpp>
#include <fc/variant_object.hpp>

#include <fc/network/ip.hpp>
Expand All @@ -68,74 +59,6 @@ using std::map;

class database_api_impl;

struct order
{
string price;
string quote;
string base;
};

struct order_book
{
string base;
string quote;
vector< order > bids;
vector< order > asks;
};

struct market_ticker
{
time_point_sec time;
string base;
string quote;
string latest;
string lowest_ask;
string highest_bid;
string percent_change;
string base_volume;
string quote_volume;

market_ticker() {}
market_ticker(const market_ticker_object& mto,
const fc::time_point_sec& now,
const asset_object& asset_base,
const asset_object& asset_quote,
const order_book& orders);
market_ticker(const fc::time_point_sec& now,
const asset_object& asset_base,
const asset_object& asset_quote);
};

struct market_volume
{
time_point_sec time;
string base;
string quote;
string base_volume;
string quote_volume;
};

struct market_trade
{
int64_t sequence = 0;
fc::time_point_sec date;
string price;
string amount;
string value;
account_id_type side1_account_id = GRAPHENE_NULL_ACCOUNT;
account_id_type side2_account_id = GRAPHENE_NULL_ACCOUNT;
};

struct extended_asset_object : asset_object
{
extended_asset_object() {}
explicit extended_asset_object( const asset_object& a ) : asset_object( a ) {}
explicit extended_asset_object( asset_object&& a ) : asset_object( std::move(a) ) {}

optional<share_type> total_in_collateral;
optional<share_type> total_backing_collateral;
};

/**
* @brief The database_api class implements the RPC API for the chain database.
*
Expand Down Expand Up @@ -972,16 +895,6 @@ class database_api

extern template class fc::api<graphene::app::database_api>;

FC_REFLECT( graphene::app::order, (price)(quote)(base) );
FC_REFLECT( graphene::app::order_book, (base)(quote)(bids)(asks) );
FC_REFLECT( graphene::app::market_ticker,
(time)(base)(quote)(latest)(lowest_ask)(highest_bid)(percent_change)(base_volume)(quote_volume) );
FC_REFLECT( graphene::app::market_volume, (time)(base)(quote)(base_volume)(quote_volume) );
FC_REFLECT( graphene::app::market_trade, (sequence)(date)(price)(amount)(value)(side1_account_id)(side2_account_id) );

FC_REFLECT_DERIVED( graphene::app::extended_asset_object, (graphene::chain::asset_object),
(total_in_collateral)(total_backing_collateral) );

FC_API(graphene::app::database_api,
// Objects
(get_objects)
Expand Down
13 changes: 11 additions & 2 deletions libraries/app/include/graphene/app/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,18 @@ namespace graphene {
namespace protocol {
struct price;
}
namespace chain {
struct asset_object;
}

namespace app {
std::string uint128_amount_to_string( const fc::uint128_t& amount, const uint8_t precision );
std::string price_to_string( const graphene::protocol::price& _price, const uint8_t base_precision, const uint8_t quote_precision);
std::string price_diff_percent_string( const graphene::protocol::price& old_price, const graphene::protocol::price& new_price );
std::string price_to_string( const graphene::protocol::price& _price,
const uint8_t base_precision,
const uint8_t quote_precision );
std::string price_to_string( const graphene::protocol::price& _price,
const graphene::chain::asset_object& _base,
const graphene::chain::asset_object& _quote );
std::string price_diff_percent_string( const graphene::protocol::price& old_price,
const graphene::protocol::price& new_price );
} }
17 changes: 16 additions & 1 deletion libraries/app/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include <graphene/app/util.hpp>
#include <graphene/protocol/asset.hpp>
#include <graphene/chain/asset_object.hpp>

namespace graphene { namespace app {

Expand Down Expand Up @@ -64,7 +65,9 @@ std::string uint128_amount_to_string( const fc::uint128_t& amount, const uint8_t
return ss.str();
} FC_CAPTURE_AND_RETHROW( (amount)(precision) ) }

std::string price_to_string( const graphene::protocol::price& _price, const uint8_t base_precision, const uint8_t quote_precision )
std::string price_to_string( const graphene::protocol::price& _price,
const uint8_t base_precision,
const uint8_t quote_precision )
{ try {
if( _price.base.amount == 0 )
return "0";
Expand All @@ -86,6 +89,18 @@ std::string price_to_string( const graphene::protocol::price& _price, const uint
return uint128_amount_to_string( price128, 19 + base_precision - quote_precision );
} FC_CAPTURE_AND_RETHROW( (_price)(base_precision)(quote_precision) ) }

std::string price_to_string( const graphene::protocol::price& _price,
const graphene::chain::asset_object& _base,
const graphene::chain::asset_object& _quote )
{ try {
if( _price.base.asset_id == _base.id && _price.quote.asset_id == _quote.id )
return price_to_string( _price, _base.precision, _quote.precision );
else if( _price.base.asset_id == _quote.id && _price.quote.asset_id == _base.id )
return price_to_string( ~_price, _base.precision, _quote.precision );
else
FC_ASSERT( !"bad parameters" );
} FC_CAPTURE_AND_RETHROW( (_price)(_base)(_quote) ) }

std::string price_diff_percent_string( const graphene::protocol::price& old_price,
const graphene::protocol::price& new_price )
{ try {
Expand Down