Module: ScaleRb::CallHelper

Defined in:
lib/scale_rb/call_helper.rb

Class Method Summary collapse

Class Method Details

.decode_call(callbytes, metadata) ⇒ Object

callbytes’s structure is: pallet_index + call_index + argsbytes

callbytes examples:

"0x0901"._to_bytes
"0x05000a1287977578f888bdc1c7627781af1cc000e6ab1300004c31b8d9a798"._to_bytes


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/scale_rb/call_helper.rb', line 10

def self.decode_call(callbytes, )
  pallet_index = callbytes[0]
  pallet = Metadata.get_module_by_index(pallet_index, )

  # Remove the pallet_index
  # The callbytes we used below should not contain the pallet index.
  # This is because the pallet index is not part of the call type.
  # Its structure is: call_index + call_args
  callbytes_without_pallet_index = callbytes[1..]
  calls_type_id = pallet._get(:calls, :type)
  decoded = Codec.decode(
    calls_type_id,
    callbytes_without_pallet_index,
    Metadata.build_registry()
  )&.first

  {
    pallet_name: pallet._get(:name),
    call_name: decoded.is_a?(::Hash) ? decoded.keys.first.to_s : decoded.to_s,
    call: decoded
  }
end

.encode_call(call, metadata) ⇒ Object

call examples:

{:pallet_name=>"Deposit", :call_name=>"claim", :call=>:claim]}
{:pallet_name=>"Balances", :call_name=>"transfer", :call=>{:transfer=>{:dest=>[10, 18, 135, 151, 117, 120, 248, 136, 189, 193, 199, 98, 119, 129, 175, 28, 192, 0, 230, 171], :value=>11000000000000000000}}]}


36
37
38
39
40
# File 'lib/scale_rb/call_helper.rb', line 36

def self.encode_call(call, )
  calls_type_id = Metadata.get_calls_type_id(call[:pallet_name], )
  pallet_index = Metadata.get_module(call[:pallet_name], )._get(:index)
  [pallet_index] + Codec.encode(calls_type_id, call[:call], Metadata.build_registry())
end