Kagome
Polkadot Runtime Engine in C++17
call.cpp
Go to the documentation of this file.
1 
7 
8 #include "scale/scale.hpp"
9 
11 
12  Call::Call(std::shared_ptr<StateApi> api) : api_(std::move(api)) {
13  BOOST_ASSERT(api_);
14  };
15 
16  outcome::result<void> Call::init(const jsonrpc::Request::Parameters &params) {
17  if (params.size() > 3 or params.size() < 2) {
18  throw jsonrpc::InvalidParametersFault("Incorrect number of params");
19  }
20  auto &param0 = params[0];
21 
22  if (param0.IsString() and not param0.IsNil()) {
23  method_ = param0.AsString();
24  } else {
25  throw jsonrpc::InvalidParametersFault(
26  "Parameter '[method]' must be a string");
27  }
28 
29  auto &param1 = params[1];
30  if (param1.IsString() and not param1.IsNil()) {
31  auto encoded_args = common::unhexWith0x(param1.AsString());
32  if (encoded_args.has_value()) {
33  data_ = common::Buffer(encoded_args.value());
34  } else {
35  throw jsonrpc::InvalidParametersFault(
36  "Parameter '[data]' must be a hex-encoded string");
37  }
38  } else {
39  throw jsonrpc::InvalidParametersFault(
40  "Parameter '[data]' must be a hex-encoded string");
41  }
42 
43  if (params.size() == 2) {
44  return outcome::success();
45  }
46 
47  auto &param2 = params[2];
48  // process at param
49  if (not param2.IsString()) {
50  throw jsonrpc::InvalidParametersFault(
51  "Parameter '[at]' must be a hex string representation of an encoded "
52  "optional byte sequence");
53  }
54  OUTCOME_TRY(at_span, common::unhexWith0x(param2.AsString()));
55  OUTCOME_TRY(at, primitives::BlockHash::fromSpan(at_span));
56  at_ = at;
57 
58  return outcome::success();
59  }
60 
61  outcome::result<common::Buffer> Call::execute() {
62  return api_->call(method_, data_, at_);
63  }
64 } // namespace kagome::api::state::request
std::shared_ptr< StateApi > api_
Definition: call.hpp:39
outcome::result< void > init(const jsonrpc::Request::Parameters &params)
Definition: call.cpp:16
outcome::result< std::vector< uint8_t > > unhexWith0x(std::string_view hex_with_prefix)
Unhex hex-string with 0x in the begining.
Definition: hexutil.cpp:89
STL namespace.
SLBuffer< std::numeric_limits< size_t >::max()> Buffer
Definition: buffer.hpp:244
std::optional< primitives::BlockHash > at_
Definition: call.hpp:42
static outcome::result< Blob< size_ > > fromSpan(const gsl::span< const uint8_t > &span)
Definition: blob.hpp:208
outcome::result< common::Buffer > execute()
Definition: call.cpp:61