Module: MixinBot::API::Transfer

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

Overview

API methods for creating transfers using the Safe API.

The Safe API is the recommended way to transfer assets on Mixin Network. It provides better security, lower fees, and more flexibility than legacy transfers.

Transfer Process

A Safe transfer involves several steps:

  1. Select UTXOs (unspent transaction outputs) as inputs

  2. Build the transaction with outputs

  3. Sign the transaction with spend key

  4. Submit the signed transaction

This module handles all these steps automatically.

Usage

# Simple transfer to a single user
api.create_transfer(
  members: 'user-uuid',
  asset_id: 'asset-uuid',
  amount: '0.01',
  memo: 'Payment'
)

# Multisig transfer
api.create_transfer(
  members: ['user1-uuid', 'user2-uuid', 'user3-uuid'],
  threshold: 2,
  asset_id: 'asset-uuid',
  amount: '0.01'
)

Instance Method Summary collapse

Instance Method Details

#build_utxos(asset_id:, amount:) ⇒ Array<Hash>

Builds a UTXO set for a transfer.

Selects unspent outputs (UTXOs) from the bot’s wallet that sum up to at least the requested amount. This is used internally by create_safe_transfer but can be called directly if needed.

The method:

  • Fetches unspent outputs for the asset

  • Sorts them by amount (smallest first)

  • Selects outputs until the amount is reached

  • Limits to 256 UTXOs maximum

Examples:

utxos = api.build_utxos(
  asset_id: '965e5c6e-434c-3fa9-b780-c50f43cd955c',
  amount: '0.01'
)
puts "Selected #{utxos.length} UTXOs"
puts "Total: #{utxos.sum { |u| u['amount'].to_d }}"

Parameters:

  • asset_id (String)

    the asset UUID

  • amount (String, Float, BigDecimal)

    the amount needed

Returns:

  • (Array<Hash>)

    array of selected UTXOs

Raises:



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/mixin_bot/api/transfer.rb', line 191

def build_utxos(asset_id:, amount:)
  amount = amount.to_d
  outputs = safe_outputs(state: 'unspent', asset: asset_id, limit: 500)['data'].sort_by { |o| o['amount'].to_d }

  utxos = []
  outputs.each do |output|
    break if utxos.size >= 256

    utxos << output
    break if utxos.sum { |o| o['amount'].to_d } >= amount
  end

  total_in = utxos.sum { |o| o['amount'].to_d }
  if total_in < amount
    raise MixinBot::UtxoInsufficientError.new(
      'insufficient utxo',
      total_input: total_in,
      total_output: amount,
      output_size: utxos.size
    )
  end

  utxos
end

#create_safe_transfer(**kwargs) ⇒ Object

Raises:



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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/mixin_bot/api/transfer.rb', line 107

def create_safe_transfer(**kwargs)
  utxos = kwargs[:utxos]
  raise ArgumentError, 'utxos must be array' if utxos.present? && !utxos.is_a?(Array)

  asset_id =
    if utxos.present?
      utxos.first['asset_id']
    else
      kwargs[:asset_id]
    end

  raise ArgumentError, 'utxos or asset_id required' if utxos.blank? && asset_id.blank?

  amount = kwargs[:amount]&.to_d
  raise ArgumentError, 'amount required' if amount.blank?

  members = [kwargs[:members]].flatten.compact
  raise ArgumentError, 'members required' if members.blank?

  threshold = kwargs[:threshold] || members.length
  request_id = kwargs[:request_id] || kwargs[:trace_id] || SecureRandom.uuid
  memo = kwargs[:memo] || ''

  # step 1: select inputs
  utxos ||= build_utxos(asset_id:, amount:)

  # step 2: build transaction
  tx = build_safe_transaction(
    utxos:,
    receivers: [{
      members:,
      threshold:,
      amount:
    }],
    extra: memo
  )
  raw = MixinBot.utils.encode_raw_transaction tx

  # step 3: verify transaction
  request = create_safe_transaction_request(request_id, raw)['data']

  # step 4: sign transaction
  spend_key = MixinBot.utils.decode_key(kwargs[:spend_key]) || config.spend_key
  signed_raw = MixinBot.api.sign_safe_transaction(
    raw:,
    utxos:,
    request: request[0],
    spend_key:
  )

  # step 5: submit transaction
  send_safe_transaction(
    request_id,
    signed_raw
  )
end

#create_transfer(pin = nil, **kwargs) ⇒ Hash

Creates a Safe API transfer.

This is the main method for sending assets on Mixin Network. It handles the complete transfer process including UTXO selection, transaction building, signing, and submission.

Examples:

Simple transfer

result = api.create_transfer(
  members: '6ae1c7ae-1df1-498e-8f21-d48cb6d129b5',
  asset_id: '965e5c6e-434c-3fa9-b780-c50f43cd955c',
  amount: '0.01',
  memo: 'Test payment'
)
puts result['snapshot_id']

Multisig transfer (2-of-3)

result = api.create_transfer(
  members: [
    '6ae1c7ae-1df1-498e-8f21-d48cb6d129b5',
    '8017d200-7870-4b82-b53f-74bae1d2dad7',
    'e8e8cd79-cd40-4796-8c54-3a13cfe50115'
  ],
  threshold: 2,
  asset_id: '965e5c6e-434c-3fa9-b780-c50f43cd955c',
  amount: '0.01'
)

Parameters:

  • kwargs (Hash)

    transfer options

Options Hash (**kwargs):

  • :members (String, Array<String>)

    recipient user ID(s)

  • :threshold (Integer)

    multisig threshold (defaults to members.length)

  • :asset_id (String)

    the asset UUID to transfer

  • :amount (String, Float)

    the amount to transfer

  • :trace_id (String)

    unique trace ID (defaults to random UUID)

  • :request_id (String)

    alias for trace_id

  • :memo (String)

    transaction memo (max 140 characters)

  • :spend_key (String)

    spend private key (defaults to config.spend_key)

  • :utxos (Array<Hash>)

    specific UTXOs to use (optional, will auto-select if not provided)

Returns:

  • (Hash)

    the transfer result including transaction hash and status

Raises:



84
85
86
87
88
89
90
# File 'lib/mixin_bot/api/transfer.rb', line 84

def create_transfer(pin = nil, **kwargs)
  if pin.is_a?(String) && kwargs[:opponent_id].present?
    create_legacy_transfer(pin, **kwargs)
  else
    create_safe_transfer(**(pin.is_a?(Hash) ? pin : kwargs))
  end
end

#send_transactionObject Also known as: send_transfer_transaction, send_transaction_with_outputs



92
93
94
# File 'lib/mixin_bot/api/transfer.rb', line 92

def send_transaction(**)
  create_safe_transfer(**)
end

#send_transaction_until_sufficientObject



98
99
100
# File 'lib/mixin_bot/api/transfer.rb', line 98

def send_transaction_until_sufficient(**)
  create_safe_transfer(**)
end

#send_transaction_with_change_outputsObject Also known as: send_transaction_with_utxos_and_change_outputs



102
103
104
# File 'lib/mixin_bot/api/transfer.rb', line 102

def send_transaction_with_change_outputs(**)
  create_safe_transfer(**)
end