Class: Radiator::Api
Overview
Radiator::Api allows you to call remote methods to interact with the STEEM blockchain. The ‘Api` class is a shortened name for `Radiator::DatabaseApi`.
Examples:
api = Radiator::Api.new
response = api.get_dynamic_global_properties
virtual_supply = response.result.virtual_supply
… or …
api = Radiator::Api.new
virtual_supply = api.get_dynamic_global_properties do |prop|
prop.virtual_supply
end
If you need access to the ‘error` property, they can be accessed as follows:
api = Radiator::Api.new
response = api.get_dynamic_global_properties
if response.result.nil?
puts response.error
exit
end
virtual_supply = response.result.virtual_supply
… or …
api = Radiator::Api.new
virtual_supply = api.get_dynamic_global_properties do |prop, error|
if prop.nil?
puts error
exis
end
prop.virtual_supply
end
List of remote methods:
set_subscribe_callback
set_pending_transaction_callback
set_block_applied_callback
cancel_all_subscriptions
get_post_discussions_by_payout
get_comment_discussions_by_payout
get_discussions_by_trending
get_discussions_by_trending30
get_discussions_by_created
get_discussions_by_active
get_discussions_by_cashout
get_discussions_by_payout
get_discussions_by_votes
get_discussions_by_children
get_discussions_by_hot
get_discussions_by_feed
get_discussions_by_blog
get_discussions_by_comments
get_discussions_by_promoted
get_block_header
get_block
get_ops_in_block
get_state
get_trending_categories
get_best_categories
get_active_categories
get_recent_categories
get_config
get_dynamic_global_properties
get_chain_properties
get_feed_history
get_current_median_history_price
get_witness_schedule
get_hardfork_version
get_next_scheduled_hardfork
get_accounts
get_account_references
lookup_account_names
lookup_accounts
get_account_count
get_conversion_requests
get_account_history
get_owner_history
get_recovery_request
get_escrow
get_withdraw_routes
get_account_bandwidth
get_savings_withdraw_from
get_savings_withdraw_to
get_order_book
get_open_orders
get_liquidity_queue
get_transaction_hex
get_transaction
get_required_signatures
get_potential_signatures
get_active_votes
get_account_votes
get_content
get_content_replies
get_replies_by_last_update
get_witnesses
get_witness_by_account
get_witnesses_by_vote
lookup_witness_accounts
get_witness_count
get_active_witnesses
get_miner_queue
get_reward_fund
These methods and their characteristics are copied directly from methods marked as ‘database_api` in `steem-js`:
raw.githubusercontent.com/steemit/steem-js/master/src/api/methods.js
Direct Known Subclasses
AccountByKeyApi, BlockApi, ChainStatsApi, CondenserApi, DatabaseApi, FollowApi, MarketHistoryApi, NetworkBroadcastApi, Stream, TagApi
Constant Summary collapse
- DEFAULT_STEEM_URL =
'https://api.steemit.com'- DEFAULT_GOLOS_URL =
'https://ws.golos.io'- DEFAULT_STEEM_FAILOVER_URLS =
[ DEFAULT_STEEM_URL, 'https://api.steemitstage.com', 'https://gtg.steem.house:8090', 'https://seed.bitcoiner.me', 'https://steemd.minnowsupportproject.org', 'https://steemd.privex.io', 'https://rpc.steemliberator.com' ]
- DEFAULT_GOLOS_FAILOVER_URLS =
[ DEFAULT_GOLOS_URL, 'https://api.golos.cf' ]
- POST_HEADERS =
{ 'Content-Type' => 'application/json' }
- HEALTH_URI =
'/health'
Class Method Summary collapse
Instance Method Summary collapse
- #api_name ⇒ Object
-
#base_per_debt ⇒ Object
(also: #steem_per_usd)
Returns the current base (STEEM) price in the debt asset (SBD).
-
#base_per_mvest ⇒ Object
(also: #steem_per_mvest)
Returns the current base (STEEM) price in the vest asset (VESTS).
-
#find_account(id, &block) ⇒ Hash
Find a specific account.
-
#find_block(block_number, &block) ⇒ Hash
Find a specific block.
-
#get_blocks(block_number, &block) ⇒ Array
Get a specific block or range of blocks.
-
#initialize(options = {}) ⇒ Api
constructor
Cretes a new instance of Radiator::Api.
- #method_missing(m, *args, &block) ⇒ Object
- #method_names ⇒ Object
- #respond_to_missing?(m, include_private = false) ⇒ Boolean
-
#shutdown ⇒ Object
Stops the persistant http connections.
Methods included from Utils
#debug, #error, #extract_signatures, #hexlify, #pakArr, #pakC, #pakHash, #pakI, #pakL!, #pakS, #pakStr, #pakc, #paks, #send_log, #unhexlify, #varint, #warning
Constructor Details
#initialize(options = {}) ⇒ Api
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/radiator/api.rb', line 194 def initialize( = {}) @user = [:user] @password = [:password] @chain = [:chain] || :steem @url = [:url] || Api::default_url(@chain) @preferred_url = @url.dup @failover_urls = [:failover_urls] @debug = !![:debug] @logger = [:logger] || Radiator.logger @hashie_logger = [:hashie_logger] || Logger.new(nil) @max_requests = [:max_requests] || 30 @pool_size = [:pool_size] || Net::HTTP::Persistent::DEFAULT_POOL_SIZE @ssl_verify_mode = [:ssl_verify_mode] || OpenSSL::SSL::VERIFY_PEER @reuse_ssl_sessions = !![:reuse_ssl_sessions] @ssl_version = [:ssl_version] if @failover_urls.nil? @failover_urls = Api::default_failover_urls(@chain) - [@url] end @failover_urls = [@failover_urls].flatten.compact @preferred_failover_urls = @failover_urls.dup unless @hashie_logger.respond_to? :warn @hashie_logger = Logger.new(@hashie_logger) end @recover_transactions_on_error = if .keys.include? :recover_transactions_on_error [:recover_transactions_on_error] else true end Hashie.logger = @hashie_logger @method_names = nil @http = nil @api_options = .dup.merge(chain: @chain) @api = nil @block_api = nil @backoff_at = nil end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 |
# File 'lib/radiator/api.rb', line 383 def method_missing(m, *args, &block) super unless respond_to_missing?(m) current_rpc_id = rpc_id method_name = [api_name, m].join('.') response = nil = { jsonrpc: "2.0", params: [api_name, m, args], id: current_rpc_id, method: "call" } tries = 0 = Time.now.utc loop do tries += 1 begin if @recover_transactions_on_error && api_name == :network_broadcast_api signatures, exp = extract_signatures() if tries > 1 && !!signatures && signatures.any? offset = [(exp - ).abs, 300].min if !!(response = recover_transaction(signatures, current_rpc_id, - offset)) warning 'Found recovered transaction after retry.', method_name, true response = Hashie::Mash.new(response) end end end if response.nil? response = request() response = if response.nil? error "No response, retrying ...", method_name elsif !response.kind_of? Net::HTTPSuccess warning "Unexpected response (code: #{response.code}): #{response.inspect}, retrying ...", method_name, true else case response.code when '200' body = response.body response = JSON[body] if response['id'] != [:id] warning "Unexpected rpc_id (expected: #{[:id]}, got: #{response['id']}), retrying ...", method_name, true elsif response.keys.include?('error') handle_error(response, , method_name, tries) else Hashie::Mash.new(response) end when '400' then warning 'Code 400: Bad Request, retrying ...', method_name, true when '429' then warning 'Code 429: Too Many Requests, retrying ...', method_name, true when '502' then warning 'Code 502: Bad Gateway, retrying ...', method_name, true when '503' then warning 'Code 503: Service Unavailable, retrying ...', method_name, true when '504' then warning 'Code 504: Gateway Timeout, retrying ...', method_name, true else warning "Unknown code #{response.code}, retrying ...", method_name, true ap response end end end rescue Net::HTTP::Persistent::Error => e warning "Unable to perform request: #{e} :: #{!!e.cause ? "cause: #{e.cause.}" : ''}, retrying ...", method_name, true rescue Errno::ECONNREFUSED => e warning 'Connection refused, retrying ...', method_name, true rescue Errno::EADDRNOTAVAIL => e warning 'Node not available, retrying ...', method_name, true rescue Errno::ECONNRESET => e warning "Connection Reset (#{e.}), retrying ...", method_name, true rescue Net::ReadTimeout => e warning 'Node read timeout, retrying ...', method_name, true rescue Net::OpenTimeout => e warning 'Node timeout, retrying ...', method_name, true rescue RangeError => e warning 'Range Error, retrying ...', method_name, true rescue OpenSSL::SSL::SSLError => e warning "SSL Error (#{e.}), retrying ...", method_name, true rescue SocketError => e warning "Socket Error (#{e.}), retrying ...", method_name, true rescue JSON::ParserError => e warning "JSON Parse Error (#{e.}), retrying ...", method_name, true response = nil rescue ApiError => e warning "ApiError (#{e.}), retrying ...", method_name, true # rescue => e # warning "Unknown exception from request, retrying ...", method_name, true # ap e if defined? ap end if !!response if !!block return yield(response.result, response.error, response.id) else return response end end backoff end # loop end |
Class Method Details
.default_failover_urls(chain) ⇒ Object
172 173 174 175 176 177 178 |
# File 'lib/radiator/api.rb', line 172 def self.default_failover_urls(chain) case chain.to_sym when :steem then DEFAULT_STEEM_FAILOVER_URLS when :golos then DEFAULT_GOLOS_FAILOVER_URLS else; raise ApiError, "Unsupported chain: #{chain}" end end |
.default_url(chain) ⇒ Object
164 165 166 167 168 169 170 |
# File 'lib/radiator/api.rb', line 164 def self.default_url(chain) case chain.to_sym when :steem then DEFAULT_STEEM_URL when :golos then DEFAULT_GOLOS_URL else; raise ApiError, "Unsupported chain: #{chain}" end end |
Instance Method Details
#api_name ⇒ Object
373 374 375 |
# File 'lib/radiator/api.rb', line 373 def api_name :database_api end |
#base_per_debt ⇒ Object Also known as: steem_per_usd
Returns the current base (STEEM) price in the debt asset (SBD).
336 337 338 339 340 341 342 343 344 345 346 |
# File 'lib/radiator/api.rb', line 336 def base_per_debt get_feed_history do |feed_history| current_median_history = feed_history.current_median_history base = current_median_history.base base = base.split(' ').first.to_f quote = current_median_history.quote quote = quote.split(' ').first.to_f (base / quote) * steem_per_mvest end end |
#base_per_mvest ⇒ Object Also known as: steem_per_mvest
Returns the current base (STEEM) price in the vest asset (VESTS).
323 324 325 326 327 328 329 330 |
# File 'lib/radiator/api.rb', line 323 def base_per_mvest api.get_dynamic_global_properties do |properties| total_vesting_fund_steem = properties.total_vesting_fund_steem.to_f total_vesting_shares_mvest = properties.total_vesting_shares.to_f / 1e6 total_vesting_fund_steem / total_vesting_shares_mvest end end |
#find_account(id, &block) ⇒ Hash
313 314 315 316 317 318 319 |
# File 'lib/radiator/api.rb', line 313 def find_account(id, &block) if !!block yield api.get_accounts([id]).result.first else api.get_accounts([id]).result.first end end |
#find_block(block_number, &block) ⇒ Hash
287 288 289 290 291 292 293 |
# File 'lib/radiator/api.rb', line 287 def find_block(block_number, &block) if !!block yield api.get_blocks(block_number).first else api.get_blocks(block_number).first end end |
#get_blocks(block_number, &block) ⇒ Array
255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/radiator/api.rb', line 255 def get_blocks(block_number, &block) block_number = [*(block_number)].flatten if !!block block_number.each do |i| yield block_api.get_block(i).result, i end else block_number.map do |i| block_api.get_block(i).result end end end |
#method_names ⇒ Object
364 365 366 367 368 369 370 |
# File 'lib/radiator/api.rb', line 364 def method_names return @method_names if !!@method_names @method_names = Radiator::Api.methods(api_name).map do |e| e['method'].to_sym end end |
#respond_to_missing?(m, include_private = false) ⇒ Boolean
378 379 380 |
# File 'lib/radiator/api.rb', line 378 def respond_to_missing?(m, include_private = false) method_names.include?(m.to_sym) end |
#shutdown ⇒ Object
Stops the persistant http connections.
352 353 354 355 356 357 358 359 360 361 |
# File 'lib/radiator/api.rb', line 352 def shutdown @uri = nil @http_id = nil @http.shutdown if !!@http && defined?(@http.shutdown) @http = nil @api.shutdown if !!@api && @api != self @api = nil @block_api.shutdown if !!@block_api && @block_api != self @block_api = nil end |