Class: Stellar::Operation

Inherits:
XDR::Struct
  • Object
show all
Includes:
XDR::Namespace
Defined in:
lib/stellar/operation.rb,
generated/stellar/operation.rb,
generated/stellar/operation/body.rb

Defined Under Namespace

Classes: Body

Class Method Summary collapse

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)


252
253
254
255
256
257
258
259
260
261
# File 'lib/stellar/operation.rb', line 252

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

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

  # TODO: add source_account support
  return 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)


223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/stellar/operation.rb', line 223

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

  trustor   = attributes[:trustor]
  authorize = attributes[:authorize]
  asset     = Asset.send(*attributes[:asset])

  raise ArgumentError, "Bad :trustor" unless trustor.is_a?(Stellar::KeyPair)
  raise ArgumentError, "Bad :authorize" unless authorize == !!authorize # check boolean
  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.authorize = authorize
  op.asset     = atc

  return make(attributes.merge({
    body:[:allow_trust, 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::Currrency)

    the asset to trust

  • :limit (Fixnum)

    the maximum amount to trust

Returns:

Raises:

  • (ArgumentError)


122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/stellar/operation.rb', line 122

def self.change_trust(attributes={})
  line  = Asset.send(*attributes[:line])
  limit = attributes[:limit]

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

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

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

.create_account(attributes = {}) ⇒ Object

Raises:

  • (ArgumentError)


96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/stellar/operation.rb', line 96

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

  raise ArgumentError unless destination.is_a?(KeyPair)

  op = CreateAccountOp.new()
  op.destination = destination.
  op.starting_balance = starting_balance

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

.create_passive_offer(attributes = {}) ⇒ Object



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

def self.create_passive_offer(attributes={})
  buying     = Asset.send(*attributes[:buying])
  selling    = Asset.send(*attributes[:selling])
  amount     = attributes[:amount]
  price      = interpret_price(attributes[:price])

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

  return make(attributes.merge({
    body:[:create_passive_offer, op]
  }))
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)


270
271
272
273
274
275
276
277
278
279
# File 'lib/stellar/operation.rb', line 270

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

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

  # TODO: add source_account support
  return 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:



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

def self.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

  return op
end

.manage_offer(attributes = {}) ⇒ Object



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

def self.manage_offer(attributes={})
  buying     = Asset.send(*attributes[:buying])
  selling    = Asset.send(*attributes[:selling])
  amount     = attributes[:amount]
  offer_id   = attributes[:offer_id] || 0
  price      = interpret_price(attributes[:price])

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

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

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

Helper method to create a valid PathPaymentOp, 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

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



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/stellar/operation.rb', line 75

def self.path_payment(attributes={})
  destination             = attributes[:destination]
  asset, amount           = extract_amount(attributes[:amount])
  send_asset, send_max    = extract_amount(attributes[:with])
  path                    = (attributes[:path] || []).map{|p| Stellar::Asset.send(*p)}

  raise ArgumentError unless destination.is_a?(KeyPair)

  op               = PathPaymentOp.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

  return make(attributes.merge({
    body:[:path_payment, 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:



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

def self.payment(attributes={})
  destination = attributes[:destination]
  asset, amount = extract_amount(attributes[:amount])

  raise ArgumentError unless destination.is_a?(KeyPair)


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

  return make(attributes.merge({
    body:[:payment, 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:



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/stellar/operation.rb', line 187

def self.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


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