class Flow::Client

The client class is used to access the Flow API

Constants

NODES

Public Class Methods

new(node: :mainnet) click to toggle source

@param node [Symbol, String] the node address used in the stub

# File lib/flow/client.rb, line 17
def initialize(node: :mainnet)
  @stub = Access::AccessAPI::Stub.new(detect_node(node), :this_channel_is_insecure)
end

Public Instance Methods

execute_script(script, args = []) click to toggle source

Execute a read-only Cadence script against the latest sealed execution state

@param script [String] cadence script @param args [Array] array of args

@return [OpenStruct]

# File lib/flow/client.rb, line 241
def execute_script(script, args = [])
  req = Access::ExecuteScriptAtLatestBlockRequest.new(
    script: script,
    arguments: args
  )

  res = @stub.execute_script_at_latest_block(req)
  parse_json(res.value)
end
get_account_at_block_height(address, block_height) click to toggle source

Get an account by address at the given block height

@param address [String] @param block_height [uint64]

@return [Flow::Entities::Account]

# File lib/flow/client.rb, line 219
def get_account_at_block_height(address, block_height)
  req = Access::GetAccountAtBlockHeightRequest.new(
    address: to_bytes(address),
    block_height: block_height
  )

  res = @stub.get_account_at_latest_block(req)
  res.account
end
get_account_at_latest_block(address) click to toggle source

Get an account by address

@param address [String]

@return [Flow::Entities::Account]

# File lib/flow/client.rb, line 205
def get_account_at_latest_block(address)
  req = Access::GetAccountAtLatestBlockRequest.new(address: to_bytes(address))
  res = @stub.get_account_at_latest_block(req)
  res.account
end
get_block_by_height(height) click to toggle source

Get a full block by height

@param height [uint64]

@return [Flow::Entities::Block]

# File lib/flow/client.rb, line 118
def get_block_by_height(height)
  req = Access::GetBlockByHeightRequest.new(height: height)
  res = @stub.get_block_by_height(req)
  res.block
end
get_block_by_id(id) click to toggle source

Get a full block by ID

@todo The ID should be bytes or it should be formatted automatically

@param id [String]

@return [Flow::Entities::Block]

# File lib/flow/client.rb, line 105
def get_block_by_id(id)
  req = Access::GetBlockByIDRequest.new(id: id)
  res = @stub.get_block_by_id(req)
  res.block
end
get_block_header_by_height(height) click to toggle source

Get a block header by height

@param height [uint64]

@return [Flow::Entities::BlockHeader]

# File lib/flow/client.rb, line 71
def get_block_header_by_height(height)
  req = Access::GetBlockHeaderByHeightRequest.new(height: height)
  res = @stub.get_block_header_by_height(req)
  res.block
end
get_block_header_by_id(id) click to toggle source

Get a block header by ID

@todo The ID should be bytes or it should be formatted automatically

@param id [String]

@return [Flow::Entities::BlockHeader]

# File lib/flow/client.rb, line 58
def get_block_header_by_id(id)
  req = Access::GetBlockHeaderByIDRequest.new(id: id)
  res = @stub.get_block_header_by_id(req)
  res.block
end
get_collection_by_id(id) click to toggle source

Gets a collection by ID

@todo The ID should be bytes or it should be formatted automatically

@param id [String]

@return [Flow::Entities::Collection]

# File lib/flow/client.rb, line 139
def get_collection_by_id(id)
  req = Access::GetCollectionByIDRequest.new(id: id)
  res = @stub.get_collection_by_id(req)
  res.collection
end
get_events_for_block_ids(type, block_ids = []) click to toggle source

Retrieve events for the specified block IDs and event type

@todo Scope the response further

@param type [String] @param block_ids [Array]

@return [EventsResponse]

# File lib/flow/client.rb, line 288
def get_events_for_block_ids(type, block_ids = [])
  req = Access::GetEventsForBlockIDsRequest.new(
    type: type,
    block_ids: block_ids
  )

  @stub.get_events_for_block_ids(req)
end
get_events_for_height_range(type, start_height: 2, end_height: 3) click to toggle source

Retrieve events emitted within the specified block range

@todo Scope the response further

@param type [String] @param start_height [uint64] @param end_height [uint64]

@return [EventsResponse]

# File lib/flow/client.rb, line 268
def get_events_for_height_range(type, start_height: 2, end_height: 3)
  req = Access::GetEventsForHeightRangeRequest.new(
    type: type,
    start_height: start_height,
    end_height: end_height
  )

  @stub.get_events_for_height_range(req)
end
get_latest_block(is_sealed: false) click to toggle source

Get the full payload of the latest sealed or unsealed block

@param is_sealed [Boolean]

@return [Flow::Entities::Block]

# File lib/flow/client.rb, line 90
def get_latest_block(is_sealed: false)
  req = Access::GetLatestBlockRequest.new(is_sealed: is_sealed)
  res = @stub.get_latest_block(req)
  res.block
end
get_latest_block_header(is_sealed: false) click to toggle source

Get the latest sealed or unsealed block header

@param is_sealed [Boolean]

@return [Flow::Entities::BlockHeader]

# File lib/flow/client.rb, line 43
def get_latest_block_header(is_sealed: false)
  req = Access::GetLatestBlockHeaderRequest.new(is_sealed: is_sealed)
  res = @stub.get_latest_block_header(req)
  res.block
end
get_latest_protocol_state_snapshot() click to toggle source

Retrieve the latest Protocol state snapshot serialized as a byte array.

It is used by Flow nodes joining the network to bootstrap a space-efficient local state

@todo Fix. This currently fails with unimplemented_error

@return

# File lib/flow/client.rb, line 330
def get_latest_protocol_state_snapshot
  req = Access::GetLatestProtocolStateSnapshotRequest.new
  @stub.get_latest_protocol_state_snapshot(req)
end
get_network_parameters() click to toggle source

Retrieve the network parameters

@return [Hash]

# File lib/flow/client.rb, line 310
def get_network_parameters
  req = Access::GetNetworkParametersRequest.new
  res = @stub.get_network_parameters(req)
  res.to_h
end
get_transaction(id) click to toggle source

Get a transaction by ID

@todo The ID should be bytes or it should be formatted automatically

@param id [String]

@return [Flow::Entities::Transaction]

# File lib/flow/client.rb, line 172
def get_transaction(id)
  req = Access::GetTransactionRequest.new(id: id)
  res = @stub.get_transaction(req)
  res.transaction
end
get_transaction_result(id) click to toggle source

Get the execution result of a transaction

@todo The ID should be bytes or it should be formatted automatically @todo We might want to change the return value here, TransactionReturnResponse has

these available keys: status, status_code, error_message, events

@param id [String]

@return [Flow::Access::TransactionResultResponse]

# File lib/flow/client.rb, line 189
def get_transaction_result(id)
  req = Access::GetTransactionRequest.new(id: id)
  @stub.get_transaction_result(req)
end
ping() click to toggle source

Check if the Access API is ready and available

@return [Flow::Access::PingResponse]

# File lib/flow/client.rb, line 26
def ping
  @stub.ping(Access::PingRequest.new)
end

Private Instance Methods

detect_node(node) click to toggle source
# File lib/flow/client.rb, line 337
def detect_node(node)
  case node.to_sym
  when :mainnet   then NODES[:mainnet]
  when :testnet   then NODES[:testnet]
  when :canarynet then NODES[:canarynet]
  else                 node
  end
end
parse_json(event_payload) click to toggle source
# File lib/flow/client.rb, line 346
def parse_json(event_payload)
  JSON.parse(event_payload, object_class: OpenStruct)
end
to_bytes(string) click to toggle source
# File lib/flow/client.rb, line 350
def to_bytes(string)
  [string].pack("H*")
end
to_string(bytes) click to toggle source
# File lib/flow/client.rb, line 354
def to_string(bytes)
  bytes.unpack1("H*")
end