Class: Stellar::Operation

Inherits:
Object
  • Object
show all
Extended by:
DSL
Defined in:
lib/stellar/compat.rb,
lib/stellar/operation.rb

Constant Summary collapse

MAX_INT64 =
2**63 - 1

Class Method Summary collapse

Methods included from DSL

Asset, ClaimPredicate, Claimant, KeyPair, SignerKey

Class Method Details

.account_merge(attributes = {}) ⇒ Stellar::Operation

Helper method to create an account merge operation

Parameters:

  • attributes (Hash) (defaults to: {})

    the attributes to create the operation with

Options Hash (attributes):

Returns:

Raises:

  • (ArgumentError)


425
426
427
428
429
430
431
432
433
434
# File 'lib/stellar/operation.rb', line 425

def (attributes = {})
  destination = attributes[:destination]

  raise ArgumentError, "Bad :destination" unless destination.is_a?(KeyPair)

  # TODO: add source_account support
  make(attributes.merge({
    body: [:account_merge, destination.]
  }))
end

.allow_trust(attributes = {}) ⇒ Stellar::Operation

Helper method to create a valid AllowTrustOp, wrapped in the necessary XDR structs to be included within a transactions ‘operations` array.

Parameters:

  • attributes (Hash) (defaults to: {})

    the attributes to create the operation with

Options Hash (attributes):

Returns:

Raises:

  • (ArgumentError)


386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# File 'lib/stellar/operation.rb', line 386

def allow_trust(attributes = {})
  op = AllowTrustOp.new

  trustor = attributes[:trustor]
  authorize = attributes[:authorize]
  asset = attributes[:asset]
  if asset.is_a?(Array)
    asset = Asset.send(*asset)
  end

  raise ArgumentError, "Bad :trustor" unless trustor.is_a?(Stellar::KeyPair)

  op.authorize = case authorize
  when :none, false then 0 # we handle booleans here for the backward compatibility
  when :full, true then TrustLineFlags.authorized_flag.value
  when :maintain_liabilities then TrustLineFlags.authorized_to_maintain_liabilities_flag.value
  else
    raise ArgumentError, "Bad :authorize, supported values: :full, :maintain_liabilities, :none"
  end

  raise ArgumentError, "Bad :asset" unless asset.type == Stellar::AssetType.asset_type_credit_alphanum4

  atc = AllowTrustOp::Asset.new(:asset_type_credit_alphanum4, asset.code)

  op.trustor = trustor.
  op.asset = atc

  make(attributes.merge({
    body: [:allow_trust, op]
  }))
end

.begin_sponsoring_future_reserves(sponsored:, **attributes) ⇒ Object



235
236
237
238
239
240
241
# File 'lib/stellar/operation.rb', line 235

def begin_sponsoring_future_reserves(sponsored:, **attributes)
  op = BeginSponsoringFutureReservesOp.new(
    sponsored_id: KeyPair(sponsored).
  )

  make(attributes.merge(body: [:begin_sponsoring_future_reserves, op]))
end

.bump_sequence(attributes = {}) ⇒ Object

Raises:

  • (ArgumentError)


482
483
484
485
486
487
488
489
490
491
492
493
494
# File 'lib/stellar/operation.rb', line 482

def bump_sequence(attributes = {})
  op = BumpSequenceOp.new

  bump_to = attributes[:bump_to]

  raise ArgumentError, ":bump_to too big" unless bump_to <= MAX_INT64

  op.bump_to = bump_to

  make(attributes.merge({
    body: [:bump_sequence, op]
  }))
end

.change_trust(attributes = {}) ⇒ Stellar::Operation

Helper method to create a valid ChangeTrustOp, wrapped in the necessary XDR structs to be included within a transactions ‘operations` array.

Parameters:

  • attributes (Hash) (defaults to: {})

    the attributes to create the operation with

Options Hash (attributes):

  • :line (Stellar::Asset)

    the asset to trust

  • :limit (Fixnum)

    the maximum amount to trust, defaults to max int64, if the limit is set to 0 it deletes the trustline.

Returns:

Raises:

  • (ArgumentError)


183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/stellar/operation.rb', line 183

def change_trust(attributes = {})
  line = attributes[:line]
  unless line.is_a?(Asset)
    unless Asset::TYPES.include?(line[0])
      fail ArgumentError, "must be one of #{Asset::TYPES}"
    end
    line = Asset.send(*line)
  end

  limit = attributes.key?(:limit) ? interpret_amount(attributes[:limit]) : MAX_INT64

  raise ArgumentError, "Bad :limit #{limit}" unless limit.is_a?(Integer)

  op = ChangeTrustOp.new(line: line, limit: limit)

  make(attributes.merge({
    body: [:change_trust, op]
  }))
end

.claim_claimable_balance(balance_id:, **attributes) ⇒ Operation

Helper method to create a valid CreateClaimableBalanceOp, ready to be used within a transactions ‘operations` array.

Parameters:

  • balance_id (ClaimableBalanceID)

    unique ID of claimable balance

Returns:

  • (Operation)

    the built operation, containing a Stellar::ChangeTrustOp body

See Also:



229
230
231
232
233
# File 'lib/stellar/operation.rb', line 229

def claim_claimable_balance(balance_id:, **attributes)
  op = ClaimClaimableBalanceOp.new(balance_id: balance_id)

  make(attributes.merge(body: [:claim_claimable_balance, op]))
end

.create_account(attributes = {}) ⇒ Object

Raises:

  • (ArgumentError)


157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/stellar/operation.rb', line 157

def (attributes = {})
  destination = attributes[:destination]
  starting_balance = interpret_amount(attributes[:starting_balance])

  raise ArgumentError unless destination.is_a?(KeyPair)

  op = CreateAccountOp.new
  op.destination = destination.
  op.starting_balance = starting_balance

  make(attributes.merge({
    body: [:create_account, op]
  }))
end

.create_claimable_balance(asset:, amount:, claimants:, **attributes) ⇒ Operation

Helper method to create a valid CreateClaimableBalanceOp, ready to be used within a transactions ‘operations` array.

Parameters:

  • asset (Asset)

    the asset to transfer to a claimable balance

  • amount (Fixnum)

    the amount of ‘asset` to put into a claimable balance

  • claimants (Array<Claimant>)

    accounts authorized to claim the balance in the future

Returns:

See Also:



214
215
216
217
218
# File 'lib/stellar/operation.rb', line 214

def create_claimable_balance(asset:, amount:, claimants:, **attributes)
  op = CreateClaimableBalanceOp.new(asset: asset, amount: amount, claimants: claimants)

  make(attributes.merge(body: [:create_claimable_balance, op]))
end

.create_passive_offerObject



3
# File 'lib/stellar/compat.rb', line 3

alias_method :create_passive_offer, :create_passive_sell_offer

.create_passive_sell_offer(attributes = {}) ⇒ Object



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/stellar/operation.rb', line 313

def create_passive_sell_offer(attributes = {})
  buying = attributes[:buying]
  if buying.is_a?(Array)
    buying = Asset.send(*buying)
  end
  selling = attributes[:selling]
  if selling.is_a?(Array)
    selling = Asset.send(*selling)
  end
  amount = interpret_amount(attributes[:amount])
  price = interpret_price(attributes[:price])

  op = CreatePassiveSellOfferOp.new({
    buying: buying,
    selling: selling,
    amount: amount,
    price: price
  })

  make(attributes.merge({
    body: [:create_passive_sell_offer, op]
  }))
end

.end_sponsoring_future_reserves(**attributes) ⇒ Object



243
244
245
# File 'lib/stellar/operation.rb', line 243

def end_sponsoring_future_reserves(**attributes)
  make(attributes.merge(body: [:end_sponsoring_future_reserves]))
end

.inflation(attributes = {}) ⇒ Stellar::Operation

Helper method to create an inflation operation

Parameters:

  • attributes (Hash) (defaults to: {})

    the attributes to create the operation with

Options Hash (attributes):

  • :sequence (Integer)

Returns:

Raises:

  • (ArgumentError)


443
444
445
446
447
448
449
450
451
452
# File 'lib/stellar/operation.rb', line 443

def inflation(attributes = {})
  sequence = attributes[:sequence]

  raise ArgumentError, "Bad :sequence #{sequence}" unless sequence.is_a?(Integer)

  # TODO: add source_account support
  make(attributes.merge({
    body: [:inflation]
  }))
end

.make(attributes = {}) ⇒ Stellar::Operation

Construct a new Stellar::Operation from the provided source account and body

Parameters:

  • attributes (Hash) (defaults to: {})

    the attributes to create the operation with

Options Hash (attributes):

Returns:



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/stellar/operation.rb', line 18

def make(attributes = {})
   = attributes[:source_account]
  body = Stellar::Operation::Body.new(*attributes[:body])

  op = Stellar::Operation.new(body: body)

  if 
    raise ArgumentError, "Bad :source_account" unless .is_a?(Stellar::KeyPair)
    op. = .
  end

  op
end

.manage_buy_offer(attributes = {}) ⇒ Object



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/stellar/operation.rb', line 287

def manage_buy_offer(attributes = {})
  buying = attributes[:buying]
  if buying.is_a?(Array)
    buying = Asset.send(*buying)
  end
  selling = attributes[:selling]
  if selling.is_a?(Array)
    selling = Asset.send(*selling)
  end
  amount = interpret_amount(attributes[:amount])
  offer_id = attributes[:offer_id] || 0
  price = interpret_price(attributes[:price])

  op = ManageBuyOfferOp.new({
    buying: buying,
    selling: selling,
    amount: amount,
    price: price,
    offer_id: offer_id
  })

  make(attributes.merge({
    body: [:manage_buy_offer, op]
  }))
end

.manage_data(attributes = {}) ⇒ Stellar::Operation

Helper method to create an manage data operation

Parameters:

  • attributes (Hash) (defaults to: {})

    the attributes to create the operation with

Options Hash (attributes):

  • :sequence (Integer)

Returns:

Raises:

  • (ArgumentError)


461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# File 'lib/stellar/operation.rb', line 461

def manage_data(attributes = {})
  op = ManageDataOp.new

  name = attributes[:name]
  value = attributes[:value]

  raise ArgumentError, "Invalid :name" unless name.is_a?(String)
  raise ArgumentError, ":name too long" unless name.bytesize <= 64

  if value.present?
    raise ArgumentError, ":value too long" unless value.bytesize <= 64
  end

  op.data_name = name
  op.data_value = value

  make(attributes.merge({
    body: [:manage_data, op]
  }))
end

.manage_offerObject



2
# File 'lib/stellar/compat.rb', line 2

alias_method :manage_offer, :manage_sell_offer

.manage_sell_offer(attributes = {}) ⇒ Object



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/stellar/operation.rb', line 261

def manage_sell_offer(attributes = {})
  buying = attributes[:buying]
  if buying.is_a?(Array)
    buying = Asset.send(*buying)
  end
  selling = attributes[:selling]
  if selling.is_a?(Array)
    selling = Asset.send(*selling)
  end
  amount = interpret_amount(attributes[:amount])
  offer_id = attributes[:offer_id] || 0
  price = interpret_price(attributes[:price])

  op = ManageSellOfferOp.new({
    buying: buying,
    selling: selling,
    amount: amount,
    price: price,
    offer_id: offer_id
  })

  make(attributes.merge({
    body: [:manage_sell_offer, op]
  }))
end

.path_payment(attributes = {}) ⇒ Stellar::Operation

Deprecated.

Please use Operation.path_payment_strict_receive

Helper method to create a valid PathPaymentStrictReceiveOp, wrapped in the necessary XDR structs to be included within a transactions ‘operations` array.

Parameters:

  • attributes (Hash) (defaults to: {})

    the attributes to create the operation with

Options Hash (attributes):

  • :destination (Stellar::KeyPair)

    the receiver of the payment

  • :amount (Array)

    the destination asset and the amount to pay

  • :with (Array)

    the source asset and maximum allowed source amount to pay with

  • :path (Array<Stellar::Asset>)

    the payment path to use

Returns:

See Also:



77
78
79
# File 'lib/stellar/operation.rb', line 77

def path_payment(attributes = {})
  path_payment_strict_receive(attributes)
end

.path_payment_strict_receive(attributes = {}) ⇒ Stellar::Operation

Helper method to create a valid PathPaymentStrictReceiveOp, wrapped in the necessary XDR structs to be included within a transactions ‘operations` array.

Parameters:

  • attributes (Hash) (defaults to: {})

    the attributes to create the operation with

Options Hash (attributes):

  • :destination (Stellar::KeyPair)

    the receiver of the payment

  • :amount (Array)

    the destination asset and the amount to pay

  • :with (Array)

    the source asset and maximum allowed source amount to pay with

  • :path (Array<Stellar::Asset>)

    the payment path to use

Returns:

Raises:

  • (ArgumentError)

See Also:



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/stellar/operation.rb', line 96

def path_payment_strict_receive(attributes = {})
  destination = attributes[:destination]
  asset, amount = get_asset_amount(attributes[:amount])
  send_asset, send_max = get_asset_amount(attributes[:with])
  path = (attributes[:path] || []).map { |p|
    p.is_a?(Array) ? Stellar::Asset.send(*p) : p
  }

  raise ArgumentError unless destination.is_a?(KeyPair)

  op = PathPaymentStrictReceiveOp.new
  op.send_asset = send_asset
  op.send_max = send_max
  op.destination = destination.
  op.dest_asset = asset
  op.dest_amount = amount
  op.path = path

  make(attributes.merge({
    body: [:path_payment_strict_receive, op]
  }))
end

.path_payment_strict_send(attributes = {}) ⇒ Stellar::Operation

Helper method to create a valid PathPaymentStrictSendOp, wrapped in the necessary XDR structs to be included within a transactions ‘operations` array.

Parameters:

  • attributes (Hash) (defaults to: {})

    the attributes to create the operation with

Options Hash (attributes):

  • :destination (Stellar::KeyPair)

    the receiver of the payment

  • :amount (Array)

    the destination asset and the minimum amount of destination asset to be received

  • :with (Array)

    the source asset and amount to pay with

  • :path (Array<Stellar::Asset>)

    the payment path to use

Returns:

Raises:

  • (ArgumentError)

See Also:



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/stellar/operation.rb', line 134

def path_payment_strict_send(attributes = {})
  destination = attributes[:destination]
  asset, dest_min = get_asset_amount(attributes[:amount])
  send_asset, send_amount = get_asset_amount(attributes[:with])
  path = (attributes[:path] || []).map { |p|
    p.is_a?(Array) ? Stellar::Asset.send(*p) : p
  }

  raise ArgumentError unless destination.is_a?(KeyPair)

  op = PathPaymentStrictSendOp.new
  op.send_asset = send_asset
  op.send_amount = send_amount
  op.destination = destination.
  op.dest_asset = asset
  op.dest_min = dest_min
  op.path = path

  make(attributes.merge({
    body: [:path_payment_strict_send, op]
  }))
end

.payment(attributes = {}) ⇒ Stellar::Operation

Helper method to create a valid PaymentOp, wrapped in the necessary XDR structs to be included within a transactions ‘operations` array.

Parameters:

  • attributes (Hash) (defaults to: {})

    the attributes to create the operation with

Options Hash (attributes):

  • :destination (Stellar::KeyPair)

    the receiver of the payment

  • :amount (Array)

    the amount to pay

Returns:

Raises:

  • (ArgumentError)

See Also:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/stellar/operation.rb', line 44

def payment(attributes = {})
  destination = attributes[:destination]
  asset, amount = get_asset_amount(attributes[:amount])

  raise ArgumentError unless destination.is_a?(KeyPair)

  op = PaymentOp.new
  op.asset = asset
  op.amount = amount
  op.destination = destination.

  make(attributes.merge({
    body: [:payment, op]
  }))
end

.revoke_sponsorship(sponsored:, **attributes) ⇒ Object

Parameters:

  • sponsored (#to_keypair)

    owner of sponsored entry

Raises:

  • (ArgumentError)


248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/stellar/operation.rb', line 248

def revoke_sponsorship(sponsored:, **attributes)
  key_fields = attributes.slice(:offer_id, :data_name, :balance_id, :asset, :signer)
  raise ArgumentError, "conflicting attributes: #{key_fields.keys.join(", ")}" if key_fields.size > 1
   = KeyPair(sponsored).
  key, value = key_fields.first
  op = if key == :signer
    RevokeSponsorshipOp.signer(account_id: , signer_key: SignerKey(value))
  else
    RevokeSponsorshipOp.ledger_key(LedgerKey.from(account_id: , **key_fields))
  end
  make(attributes.merge(body: [:revoke_sponsorship, op]))
end

.set_options(attributes = {}) ⇒ Stellar::Operation

Helper method to create a valid SetOptionsOp, wrapped in the necessary XDR structs to be included within a transactions ‘operations` array.

Parameters:

  • attributes (Hash) (defaults to: {})

    the attributes to create the operation with

Options Hash (attributes):

Returns:



351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/stellar/operation.rb', line 351

def set_options(attributes = {})
  op = SetOptionsOp.new
  op.set_flags = Stellar::AccountFlags.make_mask attributes[:set]
  op.clear_flags = Stellar::AccountFlags.make_mask attributes[:clear]
  op.master_weight = attributes[:master_weight]
  op.low_threshold = attributes[:low_threshold]
  op.med_threshold = attributes[:med_threshold]
  op.high_threshold = attributes[:high_threshold]

  op.signer = attributes[:signer]
  op.home_domain = attributes[:home_domain]

  inflation_dest = attributes[:inflation_dest]
  if inflation_dest
    raise ArgumentError, "Bad :inflation_dest" unless inflation_dest.is_a?(Stellar::KeyPair)
    op.inflation_dest = inflation_dest.
  end

  make(attributes.merge({
    body: [:set_options, op]
  }))
end