Class: Peatio::Ndex::Blockchain
- Inherits:
-
Blockchain::Abstract
- Object
- Blockchain::Abstract
- Peatio::Ndex::Blockchain
- Defined in:
- lib/peatio/ndex/blockchain.rb
Overview
TODO: Processing of unconfirmed transactions from mempool isn’t supported now.
Constant Summary collapse
- DEFAULT_FEATURES =
{case_sensitive: true, cash_addr_format: false}.freeze
Instance Method Summary collapse
- #build_transaction(tx, current_block, currency) ⇒ Object
- #endpoint ⇒ Object
- #get_block(block_hash) ⇒ Object
- #get_block_hash(height) ⇒ Object
- #get_raw_transaction(txid) ⇒ Object
- #get_unconfirmed_txns ⇒ Object
-
#initialize ⇒ Blockchain
constructor
A new instance of Blockchain.
- #invalid_transaction?(tx) ⇒ Boolean
- #latest_block_number ⇒ Object
- #to_address(tx) ⇒ Object
- #valid_transaction?(tx) ⇒ Boolean
Constructor Details
#initialize ⇒ Blockchain
Returns a new instance of Blockchain.
9 10 11 12 13 |
# File 'lib/peatio/ndex/blockchain.rb', line 9 def initialize(*) super @json_rpc_call_id = 0 @json_rpc_endpoint = URI.parse(blockchain.server) end |
Instance Method Details
#build_transaction(tx, current_block, currency) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 |
# 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 ⇒ Object
15 16 17 |
# File 'lib/peatio/ndex/blockchain.rb', line 15 def endpoint @json_rpc_endpoint end |
#get_block(block_hash) ⇒ Object
25 26 27 |
# 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) ⇒ Object
29 30 31 32 |
# 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) ⇒ Object
38 39 40 |
# File 'lib/peatio/ndex/blockchain.rb', line 38 def get_raw_transaction(txid) json_rpc({ requestType: 'getTransaction', transaction: txid}) end |
#get_unconfirmed_txns ⇒ Object
34 35 36 |
# File 'lib/peatio/ndex/blockchain.rb', line 34 def get_unconfirmed_txns json_rpc({ requestType: 'getUnconfirmedTransactions'}).fetch('unconfirmedTransactions') end |
#invalid_transaction?(tx) ⇒ Boolean
62 63 64 |
# File 'lib/peatio/ndex/blockchain.rb', line 62 def invalid_transaction?(tx) !valid_transaction?(tx) end |
#latest_block_number ⇒ Object
19 20 21 22 23 |
# 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) ⇒ Object
54 55 56 |
# File 'lib/peatio/ndex/blockchain.rb', line 54 def to_address(tx) [normalize_address(tx.fetch('recipientRS'))] end |
#valid_transaction?(tx) ⇒ Boolean
58 59 60 |
# File 'lib/peatio/ndex/blockchain.rb', line 58 def valid_transaction?(tx) [0, '0'].include?(tx['type']) end |