account_history_api_plugin.cpp 2.4 KB
Newer Older
1 2 3 4
/**
 *  @file
 *  @copyright defined in eos/LICENSE.txt
 */
5
#include <eosio/account_history_api_plugin/account_history_api_plugin.hpp>
6 7
#include <eosio/chain/chain_controller.hpp>
#include <eosio/chain/exceptions.hpp>
B
Brian Johnson 已提交
8 9 10

#include <fc/io/json.hpp>

P
Pravin 已提交
11
namespace eosio {
B
Brian Johnson 已提交
12

P
Pravin 已提交
13
using namespace eosio;
B
Brian Johnson 已提交
14

15 16
static appbase::abstract_plugin& _account_history_api_plugin = app().register_plugin<account_history_api_plugin>();

B
Brian Johnson 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29
account_history_api_plugin::account_history_api_plugin(){}
account_history_api_plugin::~account_history_api_plugin(){}

void account_history_api_plugin::set_program_options(options_description&, options_description&) {}
void account_history_api_plugin::plugin_initialize(const variables_map&) {}

#define CALL(api_name, api_handle, api_namespace, call_name) \
{std::string("/v1/" #api_name "/" #call_name), \
   [this, api_handle](string, string body, url_response_callback cb) mutable { \
          try { \
             if (body.empty()) body = "{}"; \
             auto result = api_handle.call_name(fc::json::from_string(body).as<api_namespace::call_name ## _params>()); \
             cb(200, fc::json::to_string(result)); \
30 31 32
          } catch (fc::eof_exception& e) { \
             error_results results{400, "Bad Request", e.to_string()}; \
             cb(400, fc::json::to_string(results)); \
B
Brian Johnson 已提交
33 34
             elog("Unable to parse arguments: ${args}", ("args", body)); \
          } catch (fc::exception& e) { \
35 36
             error_results results{500, "Internal Service Error", e.to_detail_string()}; \
             cb(500, fc::json::to_string(results)); \
B
Brian Johnson 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49
             elog("Exception encountered while processing ${call}: ${e}", ("call", #api_name "." #call_name)("e", e)); \
          } \
       }}

#define CHAIN_RO_CALL(call_name) CALL(account_history, ro_api, account_history_apis::read_only, call_name)
#define CHAIN_RW_CALL(call_name) CALL(account_history, rw_api, account_history_apis::read_write, call_name)

void account_history_api_plugin::plugin_startup() {
   ilog( "starting account_history_api_plugin" );
   auto ro_api = app().get_plugin<account_history_plugin>().get_read_only_api();
   auto rw_api = app().get_plugin<account_history_plugin>().get_read_write_api();

   app().get_plugin<http_plugin>().add_api({
50
      CHAIN_RO_CALL(get_transaction),
51
      CHAIN_RO_CALL(get_transactions),
52 53
      CHAIN_RO_CALL(get_key_accounts),
      CHAIN_RO_CALL(get_controlled_accounts)
B
Brian Johnson 已提交
54 55 56 57 58 59
   });
}

void account_history_api_plugin::plugin_shutdown() {}

}