/* * Copyright (c) 2015 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. */ #pragma once #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace graphene { namespace app { using namespace graphene::chain; using namespace graphene::market_history; using namespace std; class database_api_impl; struct order { double price; double quote; double base; }; struct order_book { string base; string quote; vector< order > bids; vector< order > asks; }; struct market_ticker { string base; string quote; double latest; double lowest_ask; double highest_bid; double percent_change; double base_volume; double quote_volume; }; struct market_volume { string base; string quote; double base_volume; double quote_volume; }; struct market_trade { fc::time_point_sec date; double price; double amount; double value; }; struct gpos_info { double vesting_factor; asset award; share_type total_amount; uint32_t current_subperiod; fc::time_point_sec last_voted_time; share_type allowed_withdraw_amount; share_type account_vested_balance; }; /** * @brief The database_api class implements the RPC API for the chain database. * * This API exposes accessors on the database which query state tracked by a blockchain validating node. This API is * read-only; all modifications to the database must be performed via transactions. Transactions are broadcast via * the @ref network_broadcast_api. */ class database_api { public: database_api(graphene::chain::database& db); ~database_api(); ///////////// // Objects // ///////////// /** * @brief Get the objects corresponding to the provided IDs * @param ids IDs of the objects to retrieve * @return The objects retrieved, in the order they are mentioned in ids * * If any of the provided IDs does not map to an object, a null variant is returned in its position. */ fc::variants get_objects(const vector& ids)const; /////////////////// // Subscriptions // /////////////////// void set_subscribe_callback( std::function cb, bool clear_filter ); void set_pending_transaction_callback( std::function cb ); void set_block_applied_callback( std::function cb ); /** * @brief Stop receiving any notifications * * This unsubscribes from all subscribed markets and objects. */ void cancel_all_subscriptions(); ///////////////////////////// // Blocks and transactions // ///////////////////////////// /** * @brief Retrieve a block header * @param block_num Height of the block whose header should be returned * @return header of the referenced block, or null if no matching block was found */ optional get_block_header(uint32_t block_num)const; /** * @brief Retrieve multiple block header by block numbers * @param block_num vector containing heights of the block whose header should be returned * @return array of headers of the referenced blocks, or null if no matching block was found */ map> get_block_header_batch(const vector block_nums)const; /** * @brief Retrieve a full, signed block * @param block_num Height of the block to be returned * @return the referenced block, or null if no matching block was found */ optional get_block(uint32_t block_num)const; /** * @brief used to fetch an individual transaction. */ processed_transaction get_transaction( uint32_t block_num, uint32_t trx_in_block )const; /** * If the transaction has not expired, this method will return the transaction for the given ID or * it will return NULL if it is not known. Just because it is not known does not mean it wasn't * included in the blockchain. */ optional get_recent_transaction_by_id( const transaction_id_type& id )const; ///////////// // Globals // ///////////// /** * @brief Retrieve the @ref chain_property_object associated with the chain */ chain_property_object get_chain_properties()const; /** * @brief Retrieve the current @ref global_property_object */ global_property_object get_global_properties()const; /** * @brief Retrieve compile-time constants */ fc::variant_object get_config()const; /** * @brief Get the chain ID */ chain_id_type get_chain_id()const; /** * @brief Retrieve the current @ref dynamic_global_property_object */ dynamic_global_property_object get_dynamic_global_properties()const; ////////// // Keys // ////////// vector> get_key_references( vector key )const; /** * Determine whether a textual representation of a public key * (in Base-58 format) is *currently* linked * to any *registered* (i.e. non-stealth) account on the blockchain * @param public_key Public key * @return Whether a public key is known */ bool is_public_key_registered(string public_key) const; ////////////// // Accounts // ////////////// /** * @brief Get account object from a name or ID * @param name_or_id name or ID of the account * @return Account ID * */ account_id_type get_account_id_from_string(const std::string& name_or_id)const; /** * @brief Get a list of accounts by ID or Name * @param account_ids IDs of the accounts to retrieve * @return The accounts corresponding to the provided IDs * * This function has semantics identical to @ref get_objects */ vector> get_accounts(const vector& account_names_or_ids)const; /** * @brief Fetch all objects relevant to the specified accounts and subscribe to updates * @param callback Function to call with updates * @param names_or_ids Each item must be the name or ID of an account to retrieve * @return Map of string from @ref names_or_ids to the corresponding account * * This function fetches all relevant objects for the given accounts, and subscribes to updates to the given * accounts. If any of the strings in @ref names_or_ids cannot be tied to an account, that input will be * ignored. All other accounts will be retrieved and subscribed. * */ std::map get_full_accounts( const vector& names_or_ids, bool subscribe ); optional get_account_by_name( string name )const; /** * @return all accounts that referr to the key or account id in their owner or active authorities. */ vector get_account_references( const std::string account_name_or_id )const; /** * @brief Get a list of accounts by name * @param account_names Names of the accounts to retrieve * @return The accounts holding the provided names * * This function has semantics identical to @ref get_objects */ vector> lookup_account_names(const vector& account_names)const; /** * @brief Get names and IDs for registered accounts * @param lower_bound_name Lower bound of the first name to return * @param limit Maximum number of results to return -- must not exceed 1000 * @return Map of account names to corresponding IDs */ map lookup_accounts(const string& lower_bound_name, uint32_t limit)const; ////////////// // Balances // ////////////// /** * @brief Get an account's balances in various assets * @param id ID of the account to get balances for * @param assets IDs of the assets to get balances of; if empty, get all assets account has a balance in * @return Balances of the account */ vector get_account_balances( const std::string& account_name_or_id, const flat_set& assets )const; /// Semantically equivalent to @ref get_account_balances, but takes a name instead of an ID. vector get_named_account_balances(const std::string& name, const flat_set& assets)const; /** @return all unclaimed balance objects for a set of addresses */ vector get_balance_objects( const vector
& addrs )const; vector get_vested_balances( const vector& objs )const; vector get_vesting_balances( const std::string account_id_or_name )const; /** * @brief Get the total number of accounts registered with the blockchain */ uint64_t get_account_count()const; //////////// // Assets // //////////// /** * @brief Get asset ID from an asset symbol or ID * @param symbol_or_id symbol name or ID of the asset * @return asset ID */ asset_id_type get_asset_id_from_string(const std::string& symbol_or_id) const; /** * @brief Get a list of assets by ID * @param asset_symbols_or_ids IDs or names of the assets to retrieve * @return The assets corresponding to the provided IDs * * This function has semantics identical to @ref get_objects */ vector> get_assets(const vector& asset_symbols_or_ids)const; /** * @brief Get assets alphabetically by symbol name * @param lower_bound_symbol Lower bound of symbol names to retrieve * @param limit Maximum number of assets to fetch (must not exceed 100) * @return The assets found */ vector list_assets(const string& lower_bound_symbol, uint32_t limit)const; /** * @brief Get a list of assets by symbol * @param asset_symbols Symbols or stringified IDs of the assets to retrieve * @return The assets corresponding to the provided symbols or IDs * * This function has semantics identical to @ref get_objects */ vector> lookup_asset_symbols(const vector& symbols_or_ids)const; /** * @brief Get assets count * @return The assets count */ uint64_t get_asset_count()const; //////////////////// // Lottery Assets // //////////////////// /** * @brief Get a list of lottery assets * @return The lottery assets between start and stop ids */ vector get_lotteries( asset_id_type stop = asset_id_type(), unsigned limit = 100, asset_id_type start = asset_id_type() )const; vector get_account_lotteries( account_id_type issuer, asset_id_type stop, unsigned limit, asset_id_type start )const; sweeps_vesting_balance_object get_sweeps_vesting_balance_object( account_id_type account )const; asset get_sweeps_vesting_balance_available_for_claim( account_id_type account )const; /** * @brief Get balance of lottery assets */ asset get_lottery_balance( asset_id_type lottery_id ) const; ///////////////////// // Peerplays // ///////////////////// /** * @brief Get global betting statistics */ global_betting_statistics_object get_global_betting_statistics() const; /** * @brief Get a list of all sports */ vector list_sports() const; /** * @brief Return a list of all event groups for a sport (e.g. all soccer leagues in soccer) */ vector list_event_groups(sport_id_type sport_id) const; /** * @brief Return a list of all events in an event group */ vector list_events_in_group(event_group_id_type event_group_id) const; /** * @brief Return a list of all betting market groups for an event */ vector list_betting_market_groups(event_id_type) const; /** * @brief Return a list of all betting markets for a betting market group */ vector list_betting_markets(betting_market_group_id_type) const; /** * @brief Return a list of all unmatched bets for a given account on a specific betting market */ vector get_unmatched_bets_for_bettor(betting_market_id_type, account_id_type) const; /** * @brief Return a list of all unmatched bets for a given account (includes bets on all markets) */ vector get_all_unmatched_bets_for_bettor(account_id_type) const; ///////////////////// // Markets / feeds // ///////////////////// /** * @brief Get limit orders in a given market * @param a ID of asset being sold * @param b ID of asset being purchased * @param limit Maximum number of orders to retrieve * @return The limit orders, ordered from least price to greatest */ vector get_limit_orders(const std::string& a, const std::string& b, uint32_t limit)const; /** * @brief Get call orders in a given asset * @param a ID or name of asset being called * @param limit Maximum number of orders to retrieve * @return The call orders, ordered from earliest to be called to latest */ vector get_call_orders(const std::string& a, uint32_t limit)const; /** * @brief Get forced settlement orders in a given asset * @param a ID or name of asset being settled * @param limit Maximum number of orders to retrieve * @return The settle orders, ordered from earliest settlement date to latest */ vector get_settle_orders(const std::string& a, uint32_t limit)const; /** * @return all open margin positions for a given account id. */ vector get_margin_positions( const std::string account_id_or_name )const; /** * @brief Request notification when the active orders in the market between two assets changes * @param callback Callback method which is called when the market changes * @param a First asset ID or name * @param b Second asset ID or name * * Callback will be passed a variant containing a vector>. The vector will * contain, in order, the operations which changed the market, and their results. */ void subscribe_to_market(std::function callback, const std::string& a, const std::string& b); /** * @brief Unsubscribe from updates to a given market * @param a First asset ID or name * @param b Second asset ID or name */ void unsubscribe_from_market( const std::string& a, const std::string& b ); /** * @brief Returns the ticker for the market assetA:assetB * @param a String name of the first asset * @param b String name of the second asset * @return The market ticker for the past 24 hours. */ market_ticker get_ticker( const string& base, const string& quote )const; /** * @brief Returns the 24 hour volume for the market assetA:assetB * @param a String name of the first asset * @param b String name of the second asset * @return The market volume over the past 24 hours */ market_volume get_24_volume( const string& base, const string& quote )const; /** * @brief Returns the order book for the market base:quote * @param base String name of the first asset * @param quote String name of the second asset * @param depth of the order book. Up to depth of each asks and bids, capped at 50. Prioritizes most moderate of each * @return Order book of the market */ order_book get_order_book( const string& base, const string& quote, unsigned limit = 50 )const; /** * @brief Returns recent trades for the market assetA:assetB * Note: Currentlt, timezone offsets are not supported. The time must be UTC. * @param a String name of the first asset * @param b String name of the second asset * @param stop Stop time as a UNIX timestamp * @param limit Number of trasactions to retrieve, capped at 100 * @param start Start time as a UNIX timestamp * @return Recent transactions in the market */ vector get_trade_history( const string& base, const string& quote, fc::time_point_sec start, fc::time_point_sec stop, unsigned limit = 100 )const; /////////////// // Witnesses // /////////////// /** * @brief Get a list of witnesses by ID * @param witness_ids IDs of the witnesses to retrieve * @return The witnesses corresponding to the provided IDs * * This function has semantics identical to @ref get_objects */ vector> get_witnesses(const vector& witness_ids)const; /** * @brief Get the witness owned by a given account * @param account The ID of the account whose witness should be retrieved * @return The witness object, or null if the account does not have a witness */ fc::optional get_witness_by_account(const std::string account_name_or_id)const; /** * @brief Get names and IDs for registered witnesses * @param lower_bound_name Lower bound of the first name to return * @param limit Maximum number of results to return -- must not exceed 1000 * @return Map of witness names to corresponding IDs */ map lookup_witness_accounts(const string& lower_bound_name, uint32_t limit)const; /** * @brief Get the total number of witnesses registered with the blockchain */ uint64_t get_witness_count()const; /////////////////////// // Committee members // /////////////////////// /** * @brief Get a list of committee_members by ID * @param committee_member_ids IDs of the committee_members to retrieve * @return The committee_members corresponding to the provided IDs * * This function has semantics identical to @ref get_objects */ vector> get_committee_members(const vector& committee_member_ids)const; /** * @brief Get the committee_member owned by a given account * @param account_id_or_name The ID or name of the account whose committee_member should be retrieved * @return The committee_member object, or null if the account does not have a committee_member */ fc::optional get_committee_member_by_account(const std::string account_id_or_name)const; /** * @brief Get names and IDs for registered committee_members * @param lower_bound_name Lower bound of the first name to return * @param limit Maximum number of results to return -- must not exceed 1000 * @return Map of committee_member names to corresponding IDs */ map lookup_committee_member_accounts(const string& lower_bound_name, uint32_t limit)const; /// WORKERS /** * @brief Return the worker objects associated with this account. * @param account_id_or_name The ID or name of the account whose worker should be retrieved * @return The worker object or null if the account does not have a worker */ vector get_workers_by_account(const std::string account_id_or_name)const; /////////// // Votes // /////////// /** * @brief Given a set of votes, return the objects they are voting for. * * This will be a mixture of committee_member_object, witness_objects, and worker_objects * * The results will be in the same order as the votes. Null will be returned for * any vote ids that are not found. */ vector lookup_vote_ids( const vector& votes )const; //////////////////////////// // Authority / validation // //////////////////////////// /// @brief Get a hexdump of the serialized binary form of a transaction std::string get_transaction_hex(const signed_transaction& trx)const; /** * This API will take a partially signed transaction and a set of public keys that the owner has the ability to sign for * and return the minimal subset of public keys that should add signatures to the transaction. */ set get_required_signatures( const signed_transaction& trx, const flat_set& available_keys )const; /** * This method will return the set of all public keys that could possibly sign for a given transaction. This call can * be used by wallets to filter their set of public keys to just the relevant subset prior to calling @ref get_required_signatures * to get the minimum subset. */ set get_potential_signatures( const signed_transaction& trx )const; set
get_potential_address_signatures( const signed_transaction& trx )const; /** * @return true of the @ref trx has all of the required signatures, otherwise throws an exception */ bool verify_authority( const signed_transaction& trx )const; /** * @return true if the signers have enough authority to authorize an account */ bool verify_account_authority( const string& name_or_id, const flat_set& signers )const; /** * Validates a transaction against the current state without broadcasting it on the network. */ processed_transaction validate_transaction( const signed_transaction& trx )const; /** * For each operation calculate the required fee in the specified asset type. If the asset type does * not have a valid core_exchange_rate */ vector< fc::variant > get_required_fees( const vector& ops, const std::string& asset_id_or_symbol )const; /////////////////////////// // Proposed transactions // /////////////////////////// /** * @return the set of proposed transactions relevant to the specified account id. */ vector get_proposed_transactions( const std::string account_id_or_name )const; ////////////////////// // Blinded balances // ////////////////////// /** * @return the set of blinded balance objects by commitment ID */ vector get_blinded_balances( const flat_set& commitments )const; ///////////////// // Tournaments // ///////////////// /** * @return the list of tournaments in the given state */ vector get_tournaments_in_state(tournament_state state, uint32_t limit) const; vector get_tournaments(tournament_id_type stop = tournament_id_type(), unsigned limit = 100, tournament_id_type start = tournament_id_type()); vector get_tournaments_by_state(tournament_id_type stop = tournament_id_type(), unsigned limit = 100, tournament_id_type start = tournament_id_type(), tournament_state state = tournament_state::accepting_registrations); /** * @return the list of tournaments that a given account is registered to play in */ vector get_registered_tournaments(account_id_type account_filter, uint32_t limit) const; ////////// // GPOS // ////////// /** * @return account and network GPOS information */ gpos_info get_gpos_info(const account_id_type account) const; private: std::shared_ptr< database_api_impl > my; }; } } extern template class fc::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, (base)(quote)(latest)(lowest_ask)(highest_bid)(percent_change)(base_volume)(quote_volume) ); FC_REFLECT( graphene::app::market_volume, (base)(quote)(base_volume)(quote_volume) ); FC_REFLECT( graphene::app::market_trade, (date)(price)(amount)(value) ); FC_REFLECT( graphene::app::gpos_info, (vesting_factor)(award)(total_amount)(current_subperiod)(last_voted_time)(allowed_withdraw_amount)(account_vested_balance) ); FC_API(graphene::app::database_api, // Objects (get_objects) // Subscriptions (set_subscribe_callback) (set_pending_transaction_callback) (set_block_applied_callback) (cancel_all_subscriptions) // Blocks and transactions (get_block_header) (get_block_header_batch) (get_block) (get_transaction) (get_recent_transaction_by_id) // Globals (get_chain_properties) (get_global_properties) (get_config) (get_chain_id) (get_dynamic_global_properties) // Keys (get_key_references) (is_public_key_registered) // Accounts (get_account_id_from_string) (get_accounts) (get_full_accounts) (get_account_by_name) (get_account_references) (lookup_account_names) (lookup_accounts) (get_account_count) // Balances (get_account_balances) (get_named_account_balances) (get_balance_objects) (get_vested_balances) (get_vesting_balances) // Assets (get_assets) (list_assets) (lookup_asset_symbols) (get_asset_count) (get_asset_id_from_string) // Peerplays (list_sports) (get_global_betting_statistics) (list_event_groups) (list_events_in_group) (list_betting_market_groups) (list_betting_markets) (get_unmatched_bets_for_bettor) (get_all_unmatched_bets_for_bettor) // Sweeps (get_lotteries) (get_account_lotteries) (get_lottery_balance) (get_sweeps_vesting_balance_object) (get_sweeps_vesting_balance_available_for_claim) // Markets / feeds (get_order_book) (get_limit_orders) (get_call_orders) (get_settle_orders) (get_margin_positions) (subscribe_to_market) (unsubscribe_from_market) (get_ticker) (get_24_volume) (get_trade_history) // Witnesses (get_witnesses) (get_witness_by_account) (lookup_witness_accounts) (get_witness_count) // Committee members (get_committee_members) (get_committee_member_by_account) (lookup_committee_member_accounts) // workers (get_workers_by_account) // Votes (lookup_vote_ids) // Authority / validation (get_transaction_hex) (get_required_signatures) (get_potential_signatures) (get_potential_address_signatures) (verify_authority) (verify_account_authority) (validate_transaction) (get_required_fees) // Proposed transactions (get_proposed_transactions) // Blinded balances (get_blinded_balances) // Tournaments (get_tournaments_in_state) (get_tournaments_by_state) (get_tournaments ) (get_registered_tournaments) // gpos (get_gpos_info) )