Module: MixinBot::API::Transaction

Included in:
MixinBot::API
Defined in:
lib/mixin_bot/api/transaction.rb

Constant Summary collapse

MULTISIG_TRANSACTION_ARGUMENTS =
%i[asset_id receivers threshold amount].freeze
MAINNET_TRANSACTION_ARGUMENTS =
%i[asset_id opponent_key amount].freeze

Instance Method Summary collapse

Instance Method Details

#create_mainnet_transaction(pin, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/mixin_bot/api/transaction.rb', line 38

def create_mainnet_transaction(pin, options = {})
  raise ArgumentError, "#{MAINNET_TRANSACTION_ARGUMENTS.join(', ')} are needed for create main net transactions" unless MAINNET_TRANSACTION_ARGUMENTS.all? { |param| options.keys.include? param }

  asset_id = options[:asset_id]
  opponent_key = options[:opponent_key]
  amount = options[:amount].to_f
  memo = options[:memo]
  trace_id = options[:trace_id] || SecureRandom.uuid
  encrypted_pin = options[:encrypted_pin] || encrypt_pin(pin)

  path = '/transactions'
  payload = {
    asset_id: asset_id,
    opponent_key: opponent_key,
    pin: encrypted_pin,
    amount: format('%.8f', amount),
    trace_id: trace_id,
    memo: memo
  }

  access_token = options[:access_token]
  access_token ||= access_token('POST', path, payload.to_json)
  authorization = format('Bearer %<access_token>s', access_token: access_token)
  client.post(path, headers: { 'Authorization': authorization }, json: payload)
end

#create_multisig_transaction(pin, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mixin_bot/api/transaction.rb', line 7

def create_multisig_transaction(pin, options = {})
  raise ArgumentError, "#{MULTISIG_TRANSACTION_ARGUMENTS.join(', ')} are needed for create multisig transaction" unless MULTISIG_TRANSACTION_ARGUMENTS.all? { |param| options.keys.include? param }

  asset_id = options[:asset_id]
  receivers = options[:receivers]
  threshold = options[:threshold]
  amount = options[:amount].to_f
  memo = options[:memo]
  trace_id = options[:trace_id] || SecureRandom.uuid
  encrypted_pin = options[:encrypted_pin] || encrypt_pin(pin)

  path = '/transactions'
  payload = {
    asset_id: asset_id,
    opponent_multisig: {
      receivers: receivers,
      threshold: threshold
    },
    pin: encrypted_pin,
    amount: format('%.8f', amount),
    trace_id: trace_id,
    memo: memo
  }

  access_token = options[:access_token]
  access_token ||= access_token('POST', path, payload.to_json)
  authorization = format('Bearer %<access_token>s', access_token: access_token)
  client.post(path, headers: { 'Authorization': authorization }, json: payload)
end