Module: MixinBot::API::LegacyTransaction

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

Constant Summary collapse

LEGACY_TX_VERSION =
0x04
RAW_TRANSACTION_ARGUMENTS =

use safe transaction protocol instead kwargs:

senders: [ uuid ],
senders_threshold: integer,
receivers: [ uuid ],
receivers_threshold: integer,
asset_id: uuid,
amount: string / float,
memo: string,

%i[utxos senders senders_threshold receivers receivers_threshold amount].freeze
MULTISIG_TRANSACTION_ARGUMENTS =

use safe transaction protocol instead

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

use safe transaction protocol instead

%i[asset_id opponent_id amount].freeze

Instance Method Summary collapse

Instance Method Details

#build_raw_transaction(**kwargs) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/mixin_bot/api/legacy_transaction.rb', line 20

def build_raw_transaction(**kwargs)
  warn_legacy_mixin_api!('LegacyTransaction#build_raw_transaction')
  unless RAW_TRANSACTION_ARGUMENTS.all? do |param|
           kwargs.keys.include? param
         end
    raise ArgumentError,
          "#{RAW_TRANSACTION_ARGUMENTS.join(', ')} are needed for build raw transaction"
  end

  senders             = kwargs[:senders]
  senders_threshold   = kwargs[:senders_threshold]
  receivers           = kwargs[:receivers]
  receivers_threshold = kwargs[:receivers_threshold]
  amount              = kwargs[:amount]
  asset_id            = kwargs[:asset_id]
  asset_mixin_id      = kwargs[:asset_mixin_id]
  utxos               = kwargs[:utxos]
  extra               = kwargs[:extra]
  access_token        = kwargs[:access_token]
  outputs             = kwargs[:outputs] || []
  hint                = kwargs[:hint]
  version             = kwargs[:version] || LEGACY_TX_VERSION

  raise 'access_token required!' if access_token.nil? && !senders.include?(config.app_id)

  amount = amount.to_d.round(8)
  input_amount = utxos.map(
    &lambda { |utxo|
      utxo['amount'].to_d
    }
  ).sum

  if input_amount < amount
    raise format(
      'not enough amount! %<input_amount>s < %<amount>s',
      input_amount:,
      amount:
    )
  end

  inputs = utxos.map(
    &lambda { |utx|
      {
        'hash' => utx['transaction_hash'],
        'index' => utx['output_index']
      }
    }
  )

  if outputs.empty?
    receivers_threshold = 1 if receivers.size == 1
    output0 = build_output(
      receivers:,
      index: 0,
      amount:,
      threshold: receivers_threshold,
      hint:
    )
    outputs.push output0

    if input_amount > amount
      output1 = build_output(
        receivers: senders,
        index: 1,
        amount: input_amount - amount,
        threshold: senders_threshold,
        hint:
      )
      outputs.push output1
    end
  end

  asset = asset_mixin_id || SHA3::Digest::SHA256.hexdigest(asset_id)
  {
    version:,
    asset:,
    inputs:,
    outputs:,
    extra:
  }
end

#create_mainnet_transaction(pin, **options) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/mixin_bot/api/legacy_transaction.rb', line 141

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

  asset_id = options[:asset_id]
  opponent_id = options[:opponent_id]
  amount = format('%.8f', options[:amount].to_d)
  memo = options[:memo]
  trace_id = options[:trace_id] || SecureRandom.uuid

  path = '/transactions'
  payload = {
    asset_id:,
    opponent_id:,
    amount:,
    trace_id:,
    memo:
  }

  payload.update(
    tip_or_legacy_pin_payload(pin, 'TIP:TRANSACTION:CREATE:', asset_id, opponent_id, amount, trace_id, memo)
  )

  client.post path, **payload
end

#create_multisig_transaction(pin, **options) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/mixin_bot/api/legacy_transaction.rb', line 104

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

  asset_id = options[:asset_id]
  receivers = options[:receivers].sort
  threshold = options[:threshold]
  amount = format('%.8f', options[:amount].to_d.to_r)
  memo = options[:memo]
  trace_id = options[:trace_id] || SecureRandom.uuid

  path = '/transactions'
  payload = {
    asset_id:,
    opponent_multisig: {
      receivers:,
      threshold:
    },
    amount:,
    trace_id:,
    memo:
  }

  payload.update(
    tip_or_legacy_pin_payload(pin, 'TIP:TRANSACTION:CREATE:', asset_id, receivers.join, threshold, amount, trace_id, memo)
  )

  client.post path, **payload
end

#transactions(**options) ⇒ Object

use safe transaction protocol instead



173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/mixin_bot/api/legacy_transaction.rb', line 173

def transactions(**options)
  warn_legacy_mixin_api!('LegacyTransaction#transactions')
  path = '/external/transactions'
  params = {
    limit: options[:limit],
    offset: options[:offset],
    asset: options[:asset],
    destination: options[:destination],
    tag: options[:tag]
  }

  client.get path, **params
end