class Flow::Client
The client class is used to access the Flow
API
Constants
- NODES
Public Class Methods
@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 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 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 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 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 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 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 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
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
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
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 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 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
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
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 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 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
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
# 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
# File lib/flow/client.rb, line 346 def parse_json(event_payload) JSON.parse(event_payload, object_class: OpenStruct) end
# File lib/flow/client.rb, line 350 def to_bytes(string) [string].pack("H*") end
# File lib/flow/client.rb, line 354 def to_string(bytes) bytes.unpack1("H*") end