Class: Flow::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/flow/client.rb

Overview

The client class is used to access the Flow API

Constant Summary collapse

NODES =
{
  mainnet: "access.mainnet.nodes.onflow.org:9000",
  testnet: "access.devnet.nodes.onflow.org:9000",
  canarynet: "access.canary.nodes.onflow.org:9000"
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(node: :mainnet) ⇒ Client

Returns a new instance of Client.

Parameters:

  • node (Symbol, String) (defaults to: :mainnet)

    the node address used in the stub



17
18
19
# File 'lib/flow/client.rb', line 17

def initialize(node: :mainnet)
  @stub = Access::AccessAPI::Stub.new(detect_node(node), :this_channel_is_insecure)
end

Instance Method Details

#execute_script(script, args = []) ⇒ OpenStruct

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

Parameters:

  • script (String)

    cadence script

  • args (Array) (defaults to: [])

    array of args

Returns:

  • (OpenStruct)


241
242
243
244
245
246
247
248
249
# 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) ⇒ Flow::Entities::Account

Get an account by address at the given block height

Parameters:

  • address (String)
  • block_height (uint64)

Returns:



219
220
221
222
223
224
225
226
227
# File 'lib/flow/client.rb', line 219

def (address, block_height)
  req = Access::GetAccountAtBlockHeightRequest.new(
    address: to_bytes(address),
    block_height: block_height
  )

  res = @stub.(req)
  res.
end

#get_account_at_latest_block(address) ⇒ Flow::Entities::Account

Get an account by address

Parameters:

  • address (String)

Returns:



205
206
207
208
209
# File 'lib/flow/client.rb', line 205

def (address)
  req = Access::GetAccountAtLatestBlockRequest.new(address: to_bytes(address))
  res = @stub.(req)
  res.
end

#get_block_by_height(height) ⇒ Flow::Entities::Block

Get a full block by height

Parameters:

  • height (uint64)

Returns:



118
119
120
121
122
# 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) ⇒ Flow::Entities::Block

TODO:

The ID should be bytes or it should be formatted automatically

Get a full block by ID

Parameters:

  • id (String)

Returns:



105
106
107
108
109
# 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) ⇒ Flow::Entities::BlockHeader

Get a block header by height

Parameters:

  • height (uint64)

Returns:



71
72
73
74
75
# 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) ⇒ Flow::Entities::BlockHeader

TODO:

The ID should be bytes or it should be formatted automatically

Get a block header by ID

Parameters:

  • id (String)

Returns:



58
59
60
61
62
# 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) ⇒ Flow::Entities::Collection

TODO:

The ID should be bytes or it should be formatted automatically

Gets a collection by ID

Parameters:

  • id (String)

Returns:



139
140
141
142
143
# 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 = []) ⇒ EventsResponse

TODO:

Scope the response further

Retrieve events for the specified block IDs and event type

Parameters:

  • type (String)
  • block_ids (Array) (defaults to: [])

Returns:

  • (EventsResponse)


288
289
290
291
292
293
294
295
# 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) ⇒ EventsResponse

TODO:

Scope the response further

Retrieve events emitted within the specified block range

Parameters:

  • type (String)
  • start_height (uint64) (defaults to: 2)
  • end_height (uint64) (defaults to: 3)

Returns:

  • (EventsResponse)


268
269
270
271
272
273
274
275
276
# 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) ⇒ Flow::Entities::Block

Get the full payload of the latest sealed or unsealed block

Parameters:

  • is_sealed (Boolean) (defaults to: false)

Returns:



90
91
92
93
94
# 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) ⇒ Flow::Entities::BlockHeader

Get the latest sealed or unsealed block header

Parameters:

  • is_sealed (Boolean) (defaults to: false)

Returns:



43
44
45
46
47
# 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_snapshotObject

TODO:

Fix. This currently fails with unimplemented_error

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

Returns:



330
331
332
333
# 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_parametersHash

Retrieve the network parameters

Returns:

  • (Hash)


310
311
312
313
314
# 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) ⇒ Flow::Entities::Transaction

TODO:

The ID should be bytes or it should be formatted automatically

Get a transaction by ID

Parameters:

  • id (String)

Returns:



172
173
174
175
176
# 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) ⇒ Flow::Access::TransactionResultResponse

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

Get the execution result of a transaction

Parameters:

  • id (String)

Returns:



189
190
191
192
# File 'lib/flow/client.rb', line 189

def get_transaction_result(id)
  req = Access::GetTransactionRequest.new(id: id)
  @stub.get_transaction_result(req)
end

#pingFlow::Access::PingResponse

Check if the Access API is ready and available



26
27
28
# File 'lib/flow/client.rb', line 26

def ping
  @stub.ping(Access::PingRequest.new)
end