class Peatio::Ndex::Blockchain
TODO: Processing of unconfirmed transactions from mempool isn't supported now.
Constants
- DEFAULT_FEATURES
Public Class Methods
new(*)
click to toggle source
Calls superclass method
# File lib/peatio/ndex/blockchain.rb, line 9 def initialize(*) super @json_rpc_call_id = 0 @json_rpc_endpoint = URI.parse(blockchain.server) end
Public Instance Methods
build_transaction(tx, current_block, currency)
click to toggle source
# File lib/peatio/ndex/blockchain.rb, line 42 def build_transaction(tx, current_block, currency) { id: normalize_txid(tx.fetch('transaction')), block_number: current_block, entries: [ { amount: convert_from_base_unit(tx.fetch('amountNQT'), currency), address: normalize_address(tx['recipientRS']) } ] } end
endpoint()
click to toggle source
# File lib/peatio/ndex/blockchain.rb, line 15 def endpoint @json_rpc_endpoint end
get_block(block_hash)
click to toggle source
# File lib/peatio/ndex/blockchain.rb, line 25 def get_block(block_hash) json_rpc({requestType: 'getBlock', block: block_hash, includeTransactions: true}) end
get_block_hash(height)
click to toggle source
# File lib/peatio/ndex/blockchain.rb, line 29 def get_block_hash(height) current_block = height || 0 json_rpc({requestType: 'getBlockId', height: current_block}).fetch('block') end
get_raw_transaction(txid)
click to toggle source
# File lib/peatio/ndex/blockchain.rb, line 38 def get_raw_transaction(txid) json_rpc({ requestType: 'getTransaction', transaction: txid}) end
get_unconfirmed_txns()
click to toggle source
# File lib/peatio/ndex/blockchain.rb, line 34 def get_unconfirmed_txns json_rpc({ requestType: 'getUnconfirmedTransactions'}).fetch('unconfirmedTransactions') end
invalid_transaction?(tx)
click to toggle source
# File lib/peatio/ndex/blockchain.rb, line 62 def invalid_transaction?(tx) !valid_transaction?(tx) end
latest_block_number()
click to toggle source
# File lib/peatio/ndex/blockchain.rb, line 19 def latest_block_number Rails.cache.fetch "latest_#{self.class.name.underscore}_block_number", expires_in: 5.seconds do json_rpc({requestType: 'getBlocks', lastIndex: 0}).fetch('blocks')[0].fetch('height') end end
to_address(tx)
click to toggle source
# File lib/peatio/ndex/blockchain.rb, line 54 def to_address(tx) [normalize_address(tx.fetch('recipientRS'))] end
valid_transaction?(tx)
click to toggle source
# File lib/peatio/ndex/blockchain.rb, line 58 def valid_transaction?(tx) [0, '0'].include?(tx['type']) end
Protected Instance Methods
connection()
click to toggle source
# File lib/peatio/ndex/blockchain.rb, line 68 def connection Faraday.new(@json_rpc_endpoint).tap do |connection| unless @json_rpc_endpoint.user.blank? connection.basic_auth(@json_rpc_endpoint.user, @json_rpc_endpoint.password) end end end
json_rpc(params = {})
click to toggle source
# File lib/peatio/ndex/blockchain.rb, line 77 def json_rpc(params = {}) response = connection.post do |req| req.url '/nxt?', req.body = params end response.assert_success! response = JSON.parse(response.body) response['errorDescription'].tap { |error| raise Error, error.inspect if error } response end