class Stellar::Operation

Public Class Methods

account_merge(attributes={}) click to toggle source

Helper method to create an account merge operation

@param [Hash] attributes the attributes to create the operation with @option attributes [Stellar::KeyPair] :destination

@return [Stellar::Operation] the built operation

# File lib/stellar/operation.rb, line 319
def self.account_merge(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.account_id]
  }))
end
allow_trust(attributes={}) click to toggle source

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

@param [Hash] attributes the attributes to create the operation with @option attributes [Stellar::KeyPair] :trustor @option attributes [Stellar::Asset] :asset

@return [Stellar::Operation] the built operation, containing a

Stellar::AllowTrustOp body
# File lib/stellar/operation.rb, line 290
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.account_id
  op.authorize = authorize
  op.asset     = atc

  return make(attributes.merge({
    body:[:allow_trust, op]
  }))
end
change_trust(attributes={}) click to toggle source

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

@param [Hash] attributes the attributes to create the operation with @option attributes [Stellar::Currrency] :line the asset to trust @option attributes [Fixnum] :limit the maximum amount to trust

@return [Stellar::Operation] the built operation, containing a

Stellar::ChangeTrustOp body
# File lib/stellar/operation.rb, line 189
def self.change_trust(attributes={})
  line  = Asset.send(*attributes[:line])
  limit =interpret_amount(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={}) click to toggle source
# File lib/stellar/operation.rb, line 96
def self.create_account(attributes={})
  destination      = attributes[:destination]
  code_kyc         = attributes[:code_kyc]
  raise ArgumentError unless destination.is_a?(KeyPair)

  op = CreateAccountOp.new()
  op.destination = destination.account_id
  op.code_kyc = code_kyc
  return make(attributes.merge({
    body:[:create_account, op]
  }))
end
create_lot(attributes={}) click to toggle source
# File lib/stellar/operation.rb, line 139
def self.create_lot(attributes={})
  lot_name      = attributes[:lot_name]
  type = attributes[:type]
  lot_code = attributes[:lot_code]
  lot_address = attributes[:lot_address]
  lot_branch = attributes[:lot_branch]
  lot_location = attributes[:lot_location]
  start_price = attributes[:start_price]
  best_price = attributes[:best_price]
  min_step = attributes[:min_step]
  max_step = attributes[:max_step]
  published = attributes[:published]
  auc_started = attributes[:auc_started]
  duration = attributes[:duration]
  pledge = attributes[:pledge]
  publisher_id = attributes[:publisher_id]
  details = attributes[:details]
  op = CreateLotOp.new()
  op.lot_name = lot_name
  op.type = type
  op.lot_code = lot_code
  op.lot_address = lot_address
  op.lot_branch = lot_branch
  op.lot_location = lot_location
  op.start_price = start_price
  op.best_price = best_price
  op.min_step = min_step
  op.max_step = max_step
  op.published = published
  op.auc_started = auc_started
  op.duration = duration
  op.pledge = pledge
  op.details = details
  op.publisher_id = publisher_id
  return make(attributes.merge({
    body:[:create_lot, op]
  }))
end
create_passive_offer(attributes={}) click to toggle source
# File lib/stellar/operation.rb, line 222
def self.create_passive_offer(attributes={})
  buying     = Asset.send(*attributes[:buying])
  selling    = Asset.send(*attributes[:selling])
  amount     = interpret_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={}) click to toggle source

Helper method to create an inflation operation

@param [Hash] attributes the attributes to create the operation with @option attributes [Integer] :sequence

@return [Stellar::Operation] the built operation

# File lib/stellar/operation.rb, line 337
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={}) click to toggle source

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

@param [Hash] attributes the attributes to create the operation with @option attributes [Stellar::KeyPair] :source_account @option attributes [Stellar::Operation::Body] :body

@return [Stellar::Operation] the built operation

# File lib/stellar/operation.rb, line 16
def self.make(attributes={})
  source_account = attributes[:source_account]
  body           = Stellar::Operation::Body.new(*attributes[:body])

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

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

  return op
end
manage_offer(attributes={}) click to toggle source
# File lib/stellar/operation.rb, line 202
def self.manage_offer(attributes={})
  buying     = Asset.send(*attributes[:buying])
  selling    = Asset.send(*attributes[:selling])
  amount     = interpret_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={}) click to toggle source

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

@see Stellar::Asset

@param [Hash] attributes the attributes to create the operation with @option attributes [Stellar::KeyPair] :destination the receiver of the payment @option attributes [Array] :amount the amount to pay @option attributes [Array] :with the source asset and maximum allowed source amount to pay with @option attributes [Array<Stellar::Asset>] :path the payment path to use

@return [Stellar::Operation] the built operation, containing a

Stellar::PaymentOp body
# 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.account_id
  op.dest_asset    = asset
  op.dest_amount   = amount
  op.path          = path

  return make(attributes.merge({
    body:[:path_payment, op]
  }))
end
payment(attributes={}) click to toggle source

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

@see Stellar::Asset

@param [Hash] attributes the attributes to create the operation with @option attributes [Stellar::KeyPair] :destination the receiver of the payment @option attributes [Array] :amount the amount to pay @return [Stellar::Operation] the built operation, containing a

Stellar::PaymentOp body
# 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.account_id

  return make(attributes.merge({
    body:[:payment, op]
  }))
end
provide_proof(attributes={}) click to toggle source
# File lib/stellar/operation.rb, line 109
def self.provide_proof(attributes={})
  lot_id      = attributes[:lot_id]
  quantity = attributes[:quantity]
  proof = attributes[:proof]
  bank_id = attributes[:bank_id]

  op = ProvideProofOp.new()
  op.lot_id = lot_id
  op.quantity = quantity
  op.proof = attributes[:proof]
  op.bank_id = attributes[:bank_id]
  return make(attributes.merge({
    body:[:provide_proof, op]
  }))
end
register_participant(attributes={}) click to toggle source
# File lib/stellar/operation.rb, line 125
def self.register_participant(attributes={})
  lot_id      = attributes[:lot_id]
  account_id = attributes[:account_id]
  best_bid      = attributes[:best_bid]

  op = RegisterParticipantOp.new()
  op.lot_id = lot_id
  op.account_id = account_id.account_id
  op.best_bid = best_bid
  return make(attributes.merge({
    body:[:register_participant, op]
  }))
end
set_options(attributes={}) click to toggle source

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

@param [Hash] attributes the attributes to create the operation with @option attributes [Stellar::KeyPair] :inflation_dest @option attributes [Array<Stellar::AccountFlags>] :set flags to set @option attributes [Array<Stellar::AccountFlags>] :clear flags to clear @option attributes [String] :thresholds @option attributes [Stellar::Signer] :signer

@return [Stellar::Operation] the built operation, containing a

Stellar::SetOptionsOp body
# File lib/stellar/operation.rb, line 254
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.account_id
  end


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

Private Class Methods

extract_amount(a) click to toggle source
# File lib/stellar/operation.rb, line 349
def self.extract_amount(a)
  amount   = interpret_amount(a.last)
  asset    = Stellar::Asset.send(*a[0...-1])

  return asset, amount
end
interpret_amount(amount) click to toggle source
# File lib/stellar/operation.rb, line 356
def self.interpret_amount(amount)
  case amount
  when String
    (BigDecimal.new(amount) * Stellar::ONE).floor
  when Integer
    amount * Stellar::ONE
  when Numeric
    (amount * Stellar::ONE).floor
  else
    raise ArgumentError, "Invalid amount type: #{amount.class}. Must be String or Numeric"
  end
end
interpret_price(price) click to toggle source
# File lib/stellar/operation.rb, line 370
def self.interpret_price(price)
  case price
  when String
    bd = BigDecimal.new(price)
    Price.from_f(bd)
  when Numeric
    Price.from_f(price)
  when Stellar::Price
    price
  else
    raise ArgumentError, "Invalid price type: #{price.class}. Must be String, Numeric, or Stellar::Price"
  end
end