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.zero?

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

  bytes.reverse
end

#encode_raw_transaction(txn) ⇒ 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(txn)
  if txn.is_a? String
    begin
      txn = JSON.parse txn
    rescue JSON::ParserError
      txn
    end
  end

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

  txn = txn.with_indifferent_access

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

#encode_uint16(int) ⇒ Object

Raises:



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

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

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

#encode_uint32(int) ⇒ Object

Raises:



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

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

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

#encode_uint64(int) ⇒ Object

Raises:



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

def encode_uint64(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