Module: MixinBot::Utils::Encoder

Included in:
MixinBot::Utils
Defined in:
lib/mixin_bot/utils/encoder.rb

Instance Method Summary collapse

Instance Method Details

#encode_int(int) ⇒ Object

Raises:



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/mixin_bot/utils/encoder.rb', line 40

def encode_int(int)
  raise ArgumentError, 'not integer' unless int.is_a?(Integer)

  bytes = []
  loop do
    break if int === 0

    bytes.push int & 255
    int = (int / (2**8)) | 0
  end

  bytes.reverse
end

#encode_raw_transaction(tx) ⇒ Object

Raises:



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/mixin_bot/utils/encoder.rb', line 6

def encode_raw_transaction(tx)
  if tx.is_a? String
    begin
      tx = JSON.parse tx
    rescue JSON::ParserError
      tx
    end
  end

  raise ArgumentError, "#{tx} is not a valid json" unless tx.is_a? Hash

  tx = tx.with_indifferent_access

  MixinBot::Transaction.new(**tx).encode.hex
end

#encode_uint_16(int) ⇒ Object

Raises:



22
23
24
25
26
# File 'lib/mixin_bot/utils/encoder.rb', line 22

def encode_uint_16(int)
  raise ArgumentError, "only support int #{int}" unless int.is_a?(Integer)

  [int].pack('S*').bytes.reverse
end

#encode_uint_32(int) ⇒ Object

Raises:



28
29
30
31
32
# File 'lib/mixin_bot/utils/encoder.rb', line 28

def encode_uint_32(int)
  raise ArgumentError, "only support int #{int}" unless int.is_a?(Integer)

  [int].pack('L*').bytes.reverse
end

#encode_uint_64(int) ⇒ Object

Raises:



34
35
36
37
38
# File 'lib/mixin_bot/utils/encoder.rb', line 34

def encode_uint_64(int)
  raise ArgumentError, "only support int #{int}" unless int.is_a?(Integer)

  [int].pack('Q*').bytes.reverse
end

#nft_memo(collection, token, extra) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/mixin_bot/utils/encoder.rb', line 54

def nft_memo(collection, token, extra)
  MixinBot::Nfo.new(
    collection:,
    token:,
    extra:
  ).mint_memo
end