Module: FlowClient::Utils

Defined in:
lib/flow_client/utils.rb

Overview

A collection of utilities.

Class Method Summary collapse

Class Method Details

.left_pad_bytes(byte_array, length) ⇒ Object

Left pads a byte array with 0 to length



7
8
9
10
11
12
13
14
# File 'lib/flow_client/utils.rb', line 7

def self.left_pad_bytes(byte_array, length)
  required_pad_count = length - byte_array.count
  padding = []
  (1..required_pad_count).each do |_i|
    padding << 0
  end
  padding + byte_array
end

.openstruct_to_json(struct) ⇒ Object



48
49
50
# File 'lib/flow_client/utils.rb', line 48

def self.openstruct_to_json(struct)
  struct.deep_to_h.to_json
end

.parse_protobuf_timestamp(timestamp) ⇒ Object



43
44
45
46
# File 'lib/flow_client/utils.rb', line 43

def self.parse_protobuf_timestamp(timestamp)
  epoch_micros = timestamp.nanos / 10 ** 6
  Time.at(timestamp.seconds, epoch_micros)
end

.right_pad_bytes(byte_array, length) ⇒ Object

Right pads a byte array with 0 to length



17
18
19
20
21
22
23
24
# File 'lib/flow_client/utils.rb', line 17

def self.right_pad_bytes(byte_array, length)
  required_pad_count = length - byte_array.count
  padding = []
  (1..required_pad_count).each do |_i|
    padding << 0
  end
  byte_array + padding
end

.strip_address_prefix(address) ⇒ Object



39
40
41
# File 'lib/flow_client/utils.rb', line 39

def self.strip_address_prefix(address)
  address[0..1]
end

.substitute_address_aliases(script_or_transaction, aliases = {}) ⇒ Object

Substitutes Candence import statements using aliases with addresses e.g. import FungibleToken from 0xFUNGIBLE_TOKEN_ADDRESS.

aliases is a hash with aliases as string keys and addresses as values, e.g. { “0xFUNGIBLE_TOKEN_ADDRESS”: “0x0” }



31
32
33
34
35
36
37
# File 'lib/flow_client/utils.rb', line 31

def self.substitute_address_aliases(script_or_transaction, aliases = {})
  new_string = script_or_transaction
  aliases.each do |key, value|
    new_string = new_string.gsub(key.to_s, value.to_s)
  end
  new_string
end