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
TRUST_LINE_FLAGS_MAPPING =
{
  full: Stellar::TrustLineFlags.authorized_flag,
  maintain_liabilities: Stellar::TrustLineFlags.authorized_to_maintain_liabilities_flag,
  clawback_enabled: Stellar::TrustLineFlags.trustline_clawback_enabled_flag
}.freeze

Class Method Summary collapse

Methods included from DSL

Account, Asset, ClaimPredicate, Claimant, KeyPair, SignerKey

Class Method Details

.account_merge(destination:, source_account: nil) ⇒ Stellar::Operation

Account Merge operation builder

Parameters:

  • source_account (Stellar::KeyPair, nil) (defaults to: nil)

    the source account for the operation

  • destination (Stellar::KeyPair)

    the account to merge into

Returns:

Raises:

  • (ArgumentError)


55
56
57
58
59
# File 'lib/stellar/operation.rb', line 55

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

  make(source_account: , body: [:account_merge, destination.])
end

.begin_sponsoring_future_reserves(sponsored:, source_account: nil) ⇒ Operation

Begin Sponsoring Future Reserves operation builder

Parameters:

  • source_account (KeyPair, nil) (defaults to: nil)

    the source account for the operation

Returns:



441
442
443
444
445
446
447
# File 'lib/stellar/operation.rb', line 441

def begin_sponsoring_future_reserves(sponsored:, source_account: nil)
  op = BeginSponsoringFutureReservesOp.new(
    sponsored_id: KeyPair(sponsored).
  )

  make(source_account: , body: [:begin_sponsoring_future_reserves, op])
end

.bump_sequence(bump_to:, source_account: nil) ⇒ Stellar::Operation

Bump Sequence operation builder

Parameters:

  • source_account (Stellar::KeyPair) (defaults to: nil)

    the source account for the operation

  • bump_to (Integer)

    the target sequence number for the account

Returns:

Raises:

  • (ArgumentError)


95
96
97
98
99
100
101
102
103
# File 'lib/stellar/operation.rb', line 95

def bump_sequence(bump_to:, source_account: nil)
  raise ArgumentError, ":bump_to too big" unless bump_to <= MAX_INT64

  op = BumpSequenceOp.new(
    bump_to: bump_to
  )

  make(source_account: , body: [:bump_sequence, op])
end

.change_trust(asset: nil, limit: nil, source_account: nil, **attrs) ⇒ Stellar::Operation

Change Trust operation builder

Parameters:

  • source_account (KeyPair, nil) (defaults to: nil)

    the source account for the operation

  • asset (Asset) (defaults to: nil)

    the asset to trust

  • limit (String, Numeric) (defaults to: nil)

    the maximum amount to trust, defaults to max int64 (0 deletes the trustline)

Returns:



132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/stellar/operation.rb', line 132

def change_trust(asset: nil, limit: nil, source_account: nil, **attrs)
  if attrs.key?(:line) && !asset
    Stellar::Deprecation.warn("`line` parameter is deprecated, use `asset` instead")
    asset = attrs[:line]
  end

  op = ChangeTrustOp.new(
    line: Asset(asset).to_change_trust_asset,
    limit: limit ? interpret_amount(limit) : MAX_INT64
  )

  make(source_account: , body: [:change_trust, op])
end

.claim_claimable_balance(balance_id:, source_account: nil) ⇒ Operation

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

Parameters:

  • source_account (KeyPair, nil) (defaults to: nil)

    the source account for the operation

  • balance_id (ClaimableBalanceID)

    unique ID of claimable balance

Returns:

See Also:



218
219
220
221
222
# File 'lib/stellar/operation.rb', line 218

def claim_claimable_balance(balance_id:, source_account: nil)
  op = ClaimClaimableBalanceOp.new(balance_id: balance_id)

  make(source_account: , body: [:claim_claimable_balance, op])
end

.clawback(from:, amount:, source_account: nil) ⇒ Stellar::Operation

Clawback operation builder

Parameters:

  • source_account (Stellar::KeyPair) (defaults to: nil)

    the source account for the operation

  • from (String|Account|PublicKey|SignerKey|KeyPair)

    the account to clawback from

  • amount ((Asset, Numeric))

    the amount of asset to subtract from the balance

Returns:



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/stellar/operation.rb', line 171

def clawback(from:, amount:, source_account: nil)
  asset, amount = get_asset_amount(amount)

  if amount == 0
    raise ArgumentError, "Amount can not be zero"
  end

  if amount < 0
    raise ArgumentError, "Negative amount is not allowed"
  end

  op = ClawbackOp.new(
    amount: amount,
    from: KeyPair(from).,
    asset: asset
  )

  make(source_account: , body: [:clawback, op])
end

.clawback_claimable_balance(balance_id:, source_account: nil) ⇒ Stellar::Operation

Clawback Claimable Balance operation builder

Parameters:

  • source_account (Stellar::KeyPair) (defaults to: nil)

    the source account for the operation

  • balance_id (String)

    claimable balance ID as a hexadecimal string

Returns:



230
231
232
233
234
235
236
237
# File 'lib/stellar/operation.rb', line 230

def clawback_claimable_balance(balance_id:, source_account: nil)
  balance_id = Stellar::ClaimableBalanceID.from_xdr(balance_id, :hex)
  op = ClawbackClaimableBalanceOp.new(balance_id: balance_id)

  make(source_account: , body: [:clawback_claimable_balance, op])
rescue XDR::ReadError
  raise ArgumentError, "Claimable balance id '#{balance_id}' is invalid"
end

.create_account(destination:, starting_balance:, source_account: nil) ⇒ Stellar::Operation

Create Account operation builder

Parameters:

  • source_account (KeyPair, nil) (defaults to: nil)

    the source account for the operation

  • destination (KeyPair)

    the account to create

  • starting_balance (String, Numeric)

    the amount to deposit to the newly created account

Returns:



40
41
42
43
44
45
46
47
# File 'lib/stellar/operation.rb', line 40

def (destination:, starting_balance:, source_account: nil)
  op = CreateAccountOp.new(
    destination: KeyPair(destination).,
    starting_balance: interpret_amount(starting_balance)
  )

  make(source_account: , body: [:create_account, op])
end

.create_claimable_balance(asset:, amount:, claimants:, source_account: nil) ⇒ Operation

Create Claimable Balance operation builder.

Parameters:

  • source_account (KeyPair, nil) (defaults to: nil)

    the source account for the operation

  • 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:



202
203
204
205
206
# File 'lib/stellar/operation.rb', line 202

def create_claimable_balance(asset:, amount:, claimants:, source_account: nil)
  op = CreateClaimableBalanceOp.new(asset: asset, amount: amount, claimants: claimants)

  make(source_account: , 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(selling:, buying:, amount:, price:, source_account: nil) ⇒ Operation

Create Passive Sell Offer operation builder

Parameters:

  • source_account (KeyPair, nil) (defaults to: nil)

    the source account for the operation

  • selling (Asset)

    the asset to sell

  • buying (Asset)

    the asset to buy

  • amount (String, Numeric)

    the amount of asset to sell

  • price (String, Numeric, Price)

    the price of the selling asset in terms of buying asset

Returns:



376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/stellar/operation.rb', line 376

def create_passive_sell_offer(selling:, buying:, amount:, price:, source_account: nil)
  selling = Asset.send(*selling) if selling.is_a?(Array)
  buying = Asset.send(*buying) if buying.is_a?(Array)

  op = CreatePassiveSellOfferOp.new(
    buying: buying,
    selling: selling,
    amount: interpret_amount(amount),
    price: Price.from(price)
  )

  make(source_account: , body: [:create_passive_sell_offer, op])
end

.end_sponsoring_future_reserves(source_account: nil) ⇒ Operation

End Sponsoring Future Reserves operation builder

Parameters:

  • source_account (KeyPair, nil) (defaults to: nil)

    the source account for the operation

Returns:



454
455
456
# File 'lib/stellar/operation.rb', line 454

def end_sponsoring_future_reserves(source_account: nil)
  make(source_account: , body: [:end_sponsoring_future_reserves])
end

.inflation(source_account: nil) ⇒ Stellar::Operation

Inflation operation builder

Parameters:

  • source_account (Stellar::KeyPair, nil) (defaults to: nil)

    the source account for the operation

Returns:



482
483
484
# File 'lib/stellar/operation.rb', line 482

def inflation(source_account: nil)
  make(source_account: , body: [:inflation])
end

.liquidity_pool_deposit(liquidity_pool_id:, max_amount_a:, max_amount_b:, min_price:, max_price:, source_account: nil) ⇒ Stellar::Operation

Liquidity Pool Deposit operation builder

Parameters:

  • source_account (Stellar::KeyPair) (defaults to: nil)

    the source account for the operation

  • liquidity_pool_id (String)

    the liquidity pool id as hexadecimal string

  • max_amount_a (String, Numeric)

    the maximum amount of asset A to deposit

  • max_amount_b (String, Numeric)

    the maximum amount of asset B to deposit

  • min_price (String, Numeric, Stellar::Price)

    the minimum valid price of asset A in terms of asset B

  • max_price (String, Numeric, Stellar::Price)

    the maximum valid price of asset A in terms of asset B

Returns:



400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'lib/stellar/operation.rb', line 400

def liquidity_pool_deposit(liquidity_pool_id:, max_amount_a:, max_amount_b:, min_price:, max_price:, source_account: nil)
  op = LiquidityPoolDepositOp.new(
    liquidity_pool_id: PoolID.from_xdr(liquidity_pool_id, :hex),
    max_amount_a: interpret_amount(max_amount_a),
    max_amount_b: interpret_amount(max_amount_b),
    min_price: Price.from(min_price),
    max_price: Price.from(max_price)
  )

  make(source_account: , body: [:liquidity_pool_deposit, op])
rescue XDR::ReadError
  raise ArgumentError, "invalid liquidity pool ID '#{balance_id}'"
end

.liquidity_pool_withdraw(liquidity_pool_id:, amount:, min_amount_a:, min_amount_b:, source_account: nil) ⇒ Stellar::Operation

Liquidity Pool Withdraw operation builder

Parameters:

  • source_account (Stellar::KeyPair) (defaults to: nil)

    the source account for the operation

  • liquidity_pool_id (String)

    the liquidity pool id as hexadecimal string

  • amount (String, Numeric)

    the number of pool shares to withdraw

  • min_amount_a (String, Numeric)

    the minimum amount of asset A to withdraw

  • min_amount_b (String, Numeric)

    the minimum amount of asset B to withdraw

Returns:



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

def liquidity_pool_withdraw(liquidity_pool_id:, amount:, min_amount_a:, min_amount_b:, source_account: nil)
  op = LiquidityPoolWithdrawOp.new(
    liquidity_pool_id: PoolID.from_xdr(liquidity_pool_id, :hex),
    amount: interpret_amount(amount),
    min_amount_a: interpret_amount(min_amount_a),
    min_amount_b: interpret_amount(min_amount_b)
  )

  make(source_account: , body: [:liquidity_pool_withdraw, op])
rescue XDR::ReadError
  raise ArgumentError, "invalid liquidity pool ID '#{balance_id}'"
end

.make(body:, source_account: nil) ⇒ Stellar::Operation

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

Parameters:

  • source_account (KeyPair, nil) (defaults to: nil)

    the source account for the operation

  • body ((Symbol, XDR::Struct))

    a tuple containing operation type and operation object

Returns:

Raises:

  • (ArgumentError)


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

def make(body:, source_account: nil)
  raise ArgumentError, "Bad :source_account" if  && !.is_a?(Stellar::KeyPair)

  body = Stellar::Operation::Body.new(*body)

  Stellar::Operation.new(
    source_account: &.,
    body: body
  )
end

.manage_buy_offer(buying:, selling:, amount:, price:, offer_id: 0, source_account: nil) ⇒ Operation

Manage Buy Offer operation builder

Parameters:

  • source_account (KeyPair, nil) (defaults to: nil)

    the source account for the operation

  • buying (Asset)

    the asset to buy

  • selling (Asset)

    the asset to sell

  • amount (String, Numeric)

    the amount of asset to buy

  • price (String, Numeric, Price)

    the price of the buying asset in terms of the selling asset

  • offer_id (Integer) (defaults to: 0)

    the offer ID to modify (0 to create a new offer)

Returns:



352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/stellar/operation.rb', line 352

def manage_buy_offer(buying:, selling:, amount:, price:, offer_id: 0, source_account: nil)
  buying = Asset.send(*buying) if buying.is_a?(Array)
  selling = Asset.send(*selling) if selling.is_a?(Array)

  op = ManageBuyOfferOp.new(
    buying: buying,
    selling: selling,
    buy_amount: interpret_amount(amount),
    price: Price.from(price),
    offer_id: offer_id
  )

  make(source_account: , body: [:manage_buy_offer, op])
end

.manage_data(name:, value: nil, source_account: nil) ⇒ Stellar::Operation

Manage Data operation builder

Parameters:

  • source_account (Stellar::KeyPair, nil) (defaults to: nil)

    the source account for the operation

  • name (String)

    the name of the data entry

  • value (String, nil) (defaults to: nil)

    the value of the data entry (nil to remove the entry)

Returns:

Raises:

  • (ArgumentError)


112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/stellar/operation.rb', line 112

def manage_data(name:, value: nil, source_account: nil)
  raise ArgumentError, "Invalid :name" unless name.is_a?(String)
  raise ArgumentError, ":name too long" if name.bytesize > 64
  raise ArgumentError, ":value too long" if value && value.bytesize > 64

  op = ManageDataOp.new(
    data_name: name,
    data_value: value
  )

  make(source_account: , 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(selling:, buying:, amount:, price:, offer_id: 0, source_account: nil) ⇒ Operation

Manage Sell Offer operation builder

Parameters:

  • source_account (KeyPair, nil) (defaults to: nil)

    the source account for the operation

  • selling (Asset)

    the asset to sell

  • buying (Asset)

    the asset to buy

  • amount (String, Numeric)

    the amount of asset to sell

  • price (String, Numeric, Price)

    the price of the selling asset in terms of buying asset

  • offer_id (Integer) (defaults to: 0)

    the offer ID to modify (0 to create a new offer)

Returns:



327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/stellar/operation.rb', line 327

def manage_sell_offer(selling:, buying:, amount:, price:, offer_id: 0, source_account: nil)
  selling = Asset.send(*selling) if selling.is_a?(Array)
  buying = Asset.send(*buying) if buying.is_a?(Array)

  op = ManageSellOfferOp.new(
    buying: buying,
    selling: selling,
    amount: interpret_amount(amount),
    price: Price.from(price),
    offer_id: offer_id
  )

  make(source_account: , body: [:manage_sell_offer, op])
end

.path_payment_strict_receive(destination:, amount:, with:, path: [], source_account: nil) ⇒ Stellar::Operation Also known as: path_payment

Path Payment Strict Receive operation builder.

Parameters:

  • source_account (KeyPair, nil) (defaults to: nil)

    the source account for the operation

  • 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>) (defaults to: [])

    the payment path to use

Returns:

Raises:

  • (ArgumentError)


271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/stellar/operation.rb', line 271

def path_payment_strict_receive(destination:, amount:, with:, path: [], source_account: nil)
  raise ArgumentError unless destination.is_a?(KeyPair)

  dest_asset, dest_amount = get_asset_amount(amount)
  send_asset, send_max = get_asset_amount(with)

  op = PathPaymentStrictReceiveOp.new(
    destination: destination.,
    dest_asset: dest_asset,
    dest_amount: dest_amount,
    send_asset: send_asset,
    send_max: send_max,
    path: path.map { |p| Asset(p) }
  )

  make(source_account: , body: [:path_payment_strict_receive, op])
end

.path_payment_strict_send(destination:, amount:, with:, path: [], source_account: nil) ⇒ Stellar::Operation

Path Payment Strict Receive operation builder.

Parameters:

  • source_account (KeyPair, nil) (defaults to: nil)

    the source account for the operation

  • 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>) (defaults to: [])

    the payment path to use

Returns:

Raises:

  • (ArgumentError)


299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/stellar/operation.rb', line 299

def path_payment_strict_send(destination:, amount:, with:, path: [], source_account: nil)
  raise ArgumentError unless destination.is_a?(KeyPair)

  dest_asset, dest_min = get_asset_amount(amount)
  send_asset, send_amount = get_asset_amount(with)

  op = PathPaymentStrictSendOp.new(
    destination: destination.,
    send_asset: send_asset,
    send_amount: send_amount,
    dest_asset: dest_asset,
    dest_min: dest_min,
    path: path.map { |p| Asset(p) }
  )

  make(source_account: , body: [:path_payment_strict_send, op])
end

.payment(destination:, amount:, source_account: nil) ⇒ Stellar::Operation

Payment Operation builder

Parameters:

  • source_account (KeyPair, nil) (defaults to: nil)

    the source account for the operation

  • destination (Stellar::KeyPair)

    the receiver of the payment

  • amount ((Asset, Numeric))

    the amount to pay

Returns:

Raises:

  • (ArgumentError)


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

def payment(destination:, amount:, source_account: nil)
  raise ArgumentError unless destination.is_a?(KeyPair)
  asset, amount = get_asset_amount(amount)

  op = PaymentOp.new(
    asset: asset,
    amount: amount,
    destination: destination.
  )

  make(
    source_account: ,
    body: [:payment, op]
  )
end

.revoke_sponsorship(sponsored:, source_account: nil, **attributes) ⇒ Operation

Revoke Sponsorship operation builder

Parameters:

  • source_account (KeyPair, nil) (defaults to: nil)

    the source account for the operation

  • sponsored (#to_keypair)

    owner of sponsored entry

Returns:

Raises:

  • (ArgumentError)


464
465
466
467
468
469
470
471
472
473
474
475
# File 'lib/stellar/operation.rb', line 464

def revoke_sponsorship(sponsored:, source_account: nil, **attributes)
  key_fields = attributes.slice(:offer_id, :data_name, :balance_id, :liquidity_pool_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(source_account: , body: [:revoke_sponsorship, op])
end

.set_options(set: [], clear: [], home_domain: nil, signer: nil, inflation_dest: nil, source_account: nil, **attributes) ⇒ Stellar::Operation

Set Options operation builder.

Parameters:

  • source_account (KeyPair, nil) (defaults to: nil)

    the source account for the operation

  • home_domain (String, nil\) (defaults to: nil)

    the home domain of the account

  • signer (Signer, nil) (defaults to: nil)

    add, remove or adjust weight of the co-signer

  • set (Array<AccountFlags>) (defaults to: [])

    flags to set

  • clear (Array<AccountFlags>) (defaults to: [])

    flags to clear

  • inflation_dest (KeyPair, nil) (defaults to: nil)

    the inflation destination of the account

Returns:

Raises:

  • (ArgumentError)


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/stellar/operation.rb', line 71

def set_options(set: [], clear: [], home_domain: nil, signer: nil, inflation_dest: nil, source_account: nil, **attributes)
  raise ArgumentError, "Bad inflation_dest" if inflation_dest && !inflation_dest.is_a?(KeyPair)

  op = SetOptionsOp.new(
    set_flags: Stellar::AccountFlags.make_mask(set),
    clear_flags: Stellar::AccountFlags.make_mask(clear),
    master_weight: attributes[:master_weight],
    low_threshold: attributes[:low_threshold],
    med_threshold: attributes[:med_threshold],
    high_threshold: attributes[:high_threshold],
    signer: signer,
    home_domain: home_domain,
    inflation_dest: inflation_dest&.
  )

  make(source_account: , body: [:set_options, op])
end

.set_trust_line_flags(asset:, trustor:, flags: {}, source_account: nil) ⇒ Stellar::Operation

Set Trustline Flags operation builder

Parameters:

Returns:



154
155
156
157
158
159
160
161
162
# File 'lib/stellar/operation.rb', line 154

def set_trust_line_flags(asset:, trustor:, flags: {}, source_account: nil)
  op = Stellar::SetTrustLineFlagsOp.new(
    trustor: KeyPair(trustor).,
    asset: Asset(asset),
    attributes: TrustLineFlags.set_clear_masks(flags)
  )

  make(source_account: , body: [:set_trust_line_flags, op])
end