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:
-
Select UTXOs (unspent transaction outputs) as inputs
-
Build the transaction with outputs
-
Sign the transaction with spend key
-
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
-
#build_utxos(asset_id:, amount:) ⇒ Array<Hash>
Builds a UTXO set for a transfer.
- #create_safe_transfer(**kwargs) ⇒ Object
-
#create_transfer(pin = nil, **kwargs) ⇒ Hash
Creates a Safe API transfer.
- #send_transaction ⇒ Object (also: #send_transfer_transaction, #send_transaction_with_outputs)
- #send_transaction_until_sufficient ⇒ Object
- #send_transaction_with_change_outputs ⇒ Object (also: #send_transaction_with_utxos_and_change_outputs)
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
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
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.
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_transaction ⇒ Object 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_sufficient ⇒ Object
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_outputs ⇒ Object 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 |