Class: NEAR::Block

Inherits:
Object
  • Object
show all
Defined in:
lib/near/block.rb

Overview

Represents a NEAR block.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(height: nil, hash: nil, data: nil) ⇒ Block

Returns a new instance of Block.

Parameters:

  • height (Integer, #to_i) (defaults to: nil)
  • hash (String, #to_s) (defaults to: nil)
  • data (Hash, #to_h) (defaults to: nil)


30
31
32
33
34
# File 'lib/near/block.rb', line 30

def initialize(height: nil, hash: nil, data: nil)
  @height = height.to_i if height
  @hash = hash.to_s if hash
  @data = data.to_h if data
end

Instance Attribute Details

#authorString (readonly)

Returns:

  • (String)


56
57
58
# File 'lib/near/block.rb', line 56

def author
  @author
end

#chunksArray<Hash> (readonly)

Returns:

  • (Array<Hash>)


72
73
74
# File 'lib/near/block.rb', line 72

def chunks
  @chunks
end

#dataHash (readonly)

The block data, if fetched.

Returns:

  • (Hash)


52
53
54
# File 'lib/near/block.rb', line 52

def data
  @data
end

#hashString (readonly)

The hash of the block.

Returns:

  • (String)


46
47
48
# File 'lib/near/block.rb', line 46

def hash
  @hash
end

#headerHash (readonly)

The block header.

Returns:

  • (Hash)


65
66
67
# File 'lib/near/block.rb', line 65

def header
  @header
end

#heightInteger (readonly)

The height of the block.

Returns:

  • (Integer)


40
41
42
# File 'lib/near/block.rb', line 40

def height
  @height
end

#shardsArray<Hash> (readonly)

Returns:

  • (Array<Hash>)


79
80
81
# File 'lib/near/block.rb', line 79

def shards
  @shards
end

Class Method Details

.at_hash(hash) ⇒ NEAR::Block

Parameters:

  • hash (String, #to_s)

Returns:



22
23
24
# File 'lib/near/block.rb', line 22

def self.at_hash(hash)
  self.new(hash: hash)
end

.at_height(height) ⇒ NEAR::Block

Parameters:

  • height (Integer, #to_i)

Returns:



15
16
17
# File 'lib/near/block.rb', line 15

def self.at_height(height)
  self.new(height: height)
end

.nowNEAR::Block

Returns:



8
9
10
# File 'lib/near/block.rb', line 8

def self.now
  self.new
end

Instance Method Details

#each_action {|NEAR::Action, NEAR::Transaction| ... } ⇒ Enumerator

Enumerates the actions in this block.

Yields:

Yield Parameters:

Yield Returns:

  • (void)

Returns:

  • (Enumerator)

    if no block is given



127
128
129
130
131
132
133
134
# File 'lib/near/block.rb', line 127

def each_action(&)
  return enum_for(:each_action) unless block_given?
  self.each_transaction do |tx|
    tx.each_action do |action|
      yield action, tx
    end
  end
end

#each_chunk {|Hash| ... } ⇒ Enumerator

Enumerates the chunks in this block.

Yields:

  • (Hash)

Yield Parameters:

  • chunk (Hash)

Yield Returns:

  • (void)

Returns:

  • (Enumerator)

    if no block is given



167
168
169
170
171
172
173
# File 'lib/near/block.rb', line 167

def each_chunk(&)
  return enum_for(:each_chunk) unless block_given?
  self.shards.each do |shard|
    chunk = shard['chunk']
    yield chunk if chunk
  end
end

#each_transaction {|NEAR::Transaction| ... } ⇒ Enumerator

Enumerates the transactions in this block.

Yields:

Yield Parameters:

Yield Returns:

  • (void)

Returns:

  • (Enumerator)

    if no block is given



109
110
111
112
113
114
115
116
117
# File 'lib/near/block.rb', line 109

def each_transaction(&)
  return enum_for(:each_transaction) unless block_given?
  self.each_chunk do |chunk|
    next if !chunk['transactions']
    chunk['transactions'].each do |tx|
      yield NEAR::Transaction.parse(tx)
    end
  end
end

#find_actions(type, signer: nil, receiver: nil, method_name: nil) {|NEAR::Action, NEAR::Transaction| ... } ⇒ Enumerator

Returns if no block is given.

Parameters:

  • type (Symbol, #to_sym)
  • signer (String, Regexp) (defaults to: nil)
  • receiver (String, Regexp) (defaults to: nil)
  • method_name (String, Regexp) (defaults to: nil)

Yields:

Yield Parameters:

Yield Returns:

  • (void)

Returns:

  • (Enumerator)

    if no block is given



146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/near/block.rb', line 146

def find_actions(type, signer: nil, receiver: nil, method_name: nil, &)
  return enum_for(:each_action) unless block_given?
  type = type.to_sym
  self.each_transaction do |tx|
    next if signer && !(signer === tx.signer_id)
    next if receiver && !(receiver === tx.receiver_id)
    tx.each_action do |action|
      next unless type == action.type
      next if method_name && !(method_name === action.method_name)
      yield action, tx
    end
  end
end

#inspectString

Returns:

  • (String)


199
200
201
# File 'lib/near/block.rb', line 199

def inspect
  "#<#{self.class.name} height: #{@height}, hash: #{@hash.inspect}>"
end

#receiversArray<NEAR::Account>

The set of receivers in the transactions in this block.

Returns:



97
98
99
100
# File 'lib/near/block.rb', line 97

def receivers
  self.collect_transaction_field('receiver_id')
    .map { |id| NEAR::Account.new(id) }
end

#signersArray<NEAR::Account>

The set of signers in the transactions in this block.

Returns:



88
89
90
91
# File 'lib/near/block.rb', line 88

def signers
  self.collect_transaction_field('signer_id')
    .map { |id| NEAR::Account.new(id) }
end

#to_cli_argsArray<String>

Returns:

  • (Array<String>)


177
178
179
180
181
# File 'lib/near/block.rb', line 177

def to_cli_args
  return ['at-block-height', @height] if @height
  return ['at-block-hash', @hash] if @hash
  ['now']
end

#to_hHash

Returns:

  • (Hash)


195
# File 'lib/near/block.rb', line 195

def to_h; @data; end

#to_iInteger

Returns:

  • (Integer)


185
# File 'lib/near/block.rb', line 185

def to_i; @height; end

#to_sString

Returns:

  • (String)


189
190
191
# File 'lib/near/block.rb', line 189

def to_s
  (@height || @hash || :now).to_s
end