Class: Trx::Abi::Encoder

Inherits:
Object
  • Object
show all
Defined in:
lib/trx/abi/encoder.rb

Instance Method Summary collapse

Instance Method Details

#do_encode_fixed(value, n) ⇒ Object



49
50
51
# File 'lib/trx/abi/encoder.rb', line 49

def do_encode_fixed(value, n)
  encode_uint((value * 2**n).to_i)
end

#encode(type, value) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/trx/abi/encoder.rb', line 6

def encode(type, value)
  is_array, arity, array_subtype = Abi::parse_array_type(type)
  if is_array && arity
    encode_static_array(arity, array_subtype, value)
  elsif is_array
    encode_dynamic_array(array_subtype, value)
  else
    core, subtype = Abi::parse_type(type)
    method_name = "encode_#{core}".to_sym
    self.send(method_name, value, subtype)
  end
end

#encode_address(value, _) ⇒ Object

Raises:

  • (ArgumentError)


79
80
81
82
83
# File 'lib/trx/abi/encoder.rb', line 79

def encode_address(value, _)
  value = "0" * 24 + value.gsub(/^0x/,'')
  raise ArgumentError if value.size != 64
  value
end

#encode_arguments(inputs, args) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/trx/abi/encoder.rb', line 89

def encode_arguments(inputs, args)
  raise "Wrong number of arguments" if inputs.length != args.length
  @head = ""
  @tail = ""
  @inputs = inputs
  inputs.each.with_index do |input, index|
    encoded = encode(input.type, args[index])
    if encoded.is_a? Array
      @head << encoded[0]
      @tail << encoded[1]
    else
      @head << encoded
    end
  end
  @head + @tail
end

#encode_bool(value, _) ⇒ Object



40
41
42
# File 'lib/trx/abi/encoder.rb', line 40

def encode_bool(value, _)
  (value ? "1" : "0").rjust(64, '0')
end

#encode_bytes(value, subtype) ⇒ Object



57
58
59
# File 'lib/trx/abi/encoder.rb', line 57

def encode_bytes(value, subtype)
  subtype.nil? ? encode_dynamic_bytes(value) : encode_static_bytes(value)
end

#encode_dynamic_array(array_subtype, array) ⇒ Object



24
25
26
27
28
29
# File 'lib/trx/abi/encoder.rb', line 24

def encode_dynamic_array(array_subtype, array)
  location = encode_uint(@inputs ? size_of_inputs(@inputs) + @tail.size/2 : 32)
  size = encode_uint(array.size)
  data = array.inject("") { |a, e| a << encode(array_subtype, e) }
  [location, size + data]
end

#encode_dynamic_bytes(value) ⇒ Object



65
66
67
68
69
70
# File 'lib/trx/abi/encoder.rb', line 65

def encode_dynamic_bytes(value)
  location = encode_uint(@inputs ? size_of_inputs(@inputs) + @tail.size/2 : 32)
  size = encode_uint(value.size)
  content = encode_static_bytes(value)
  [location, size + content]
end

#encode_fixed(value, subtype) ⇒ Object



44
45
46
47
# File 'lib/trx/abi/encoder.rb', line 44

def encode_fixed(value, subtype)
  n = subtype.nil? ? 128 : /(\d+)x(\d+)/.match(subtype)[2].to_i
  do_encode_fixed(value, n)
end

#encode_int(value, _ = nil) ⇒ Object



31
32
33
# File 'lib/trx/abi/encoder.rb', line 31

def encode_int(value, _ = nil)
  to_twos_complement(value).to_s(16).rjust(64, '0')
end

#encode_static_array(arity, array_subtype, array) ⇒ Object



19
20
21
22
# File 'lib/trx/abi/encoder.rb', line 19

def encode_static_array(arity, array_subtype, array)
  raise "Wrong number of arguments" if arity != array.size
  array.inject("") { |a, e| a << encode(array_subtype, e) }
end

#encode_static_bytes(value) ⇒ Object



61
62
63
# File 'lib/trx/abi/encoder.rb', line 61

def encode_static_bytes(value)
  value.bytes.map {|x| x.to_s(16).rjust(2, '0')}.join("").ljust(64, '0')
end

#encode_string(value, _) ⇒ Object



72
73
74
75
76
77
# File 'lib/trx/abi/encoder.rb', line 72

def encode_string(value, _)
  location = encode_uint(@inputs ? size_of_inputs(@inputs) + @tail.size/2 : 32)
  size = encode_uint(value.bytes.size)
  content = value.bytes.map {|x| x.to_s(16).rjust(2, '0')}.join("").ljust(64, '0')
  [location, size + content]
end

#encode_ufixed(_value, _) ⇒ Object

Raises:

  • (NotImplementedError)


53
54
55
# File 'lib/trx/abi/encoder.rb', line 53

def encode_ufixed(_value, _)
  raise NotImplementedError
end

#encode_uint(value, _ = nil) ⇒ Object

Raises:

  • (ArgumentError)


35
36
37
38
# File 'lib/trx/abi/encoder.rb', line 35

def encode_uint(value, _ = nil)
  raise ArgumentError if value < 0
  encode_int(value)
end

#ensure_prefix(value) ⇒ Object



85
86
87
# File 'lib/trx/abi/encoder.rb', line 85

def ensure_prefix(value)
  value.start_with?("0x") ? value : ("0x" + value)
end