lib/stellar/transaction.rb in stellar-base-0.0.2 vs lib/stellar/transaction.rb in stellar-base-0.0.3

- old
+ new

@@ -1,65 +1,89 @@ module Stellar Transaction.class_eval do + # + # @see Stellar::Operation.payment def self.payment(attributes={}) - destination = attributes[:destination] - amount = attributes[:amount] - path = attributes[:path] || [] - path = path.map{|p| Stellar::Currency.send(*p)} - - raise ArgumentError unless destination.is_a?(KeyPair) - - for_account(attributes).tap do |result| - payment = PaymentOp.send(*amount) - payment.destination = destination.public_key - payment.path = path - payment.apply_defaults - - result.operations = [payment.to_operation] - end + make :payment, attributes end + # + # @see Stellar::Operation.change_trust def self.change_trust(attributes={}) - line = Currency.send(*attributes[:line]) - limit = attributes[:limit] + make :change_trust, attributes + end - raise ArgumentError, "Bad :limit #{limit}" unless limit.is_a?(Integer) + # + # @see Stellar::Operation.create_offer + def self.create_offer(attributes={}) + make :create_offer, attributes + end - for_account(attributes).tap do |result| - details = ChangeTrustOp.new(line: line, limit: limit) - result.operations = [details.to_operation] - end + # + # @see Stellar::Operation.set_options + def self.set_options(attributes={}) + make :set_options, attributes end - def self.create_offer(attributes={}) - taker_pays = Currency.send(*attributes[:taker_pays]) - taker_gets = Currency.send(*attributes[:taker_gets]) - amount = attributes[:amount] - offer_id = attributes[:offer_id] || 0 - price = Price.from_f(attributes[:price]) + # + # @see Stellar::Operation.allow_trust + def self.allow_trust(attributes={}) + make :allow_trust, attributes + end + + # + # @see Stellar::Operation.account_merge + def self.account_merge(attributes={}) + make :account_merge, attributes + end + + # + # @see Stellar::Operation.inflation + def self.inflation(attributes={}) + make :inflation, attributes + end + # + # Helper method to create a transaction with a single + # operation of the provided type. See class methods + # on Stellar::Operation for available values for + # operation_type. + # + # @see Stellar::Operation + # + # @param operation_type [Symbol] the operation to use + # @param attributes={} [Hash] attributes to use for both the transaction and the operation + # + # @return [Stellar::Transaction] the resulting transaction + def self.make(operation_type, attributes={}) for_account(attributes).tap do |result| - details = CreateOfferOp.new({ - taker_pays: taker_pays, - taker_gets: taker_gets, - amount: amount, - price: price, - offer_id: offer_id - }) - result.operations = [details.to_operation] + result.operations = [Operation.send(operation_type, attributes)] end end + + # + # Helper method to create the skeleton of a transaction. + # The resulting transaction will have its source account, + # sequence, fee, min ledger and max ledger set. + # + # + # @param attributes={} [type] [description] + # + # @return [Stellar::Transaction] the resulting skeleton def self.for_account(attributes={}) - account = attributes[:account] - sequence = attributes[:sequence] + account = attributes[:account] + sequence = attributes[:sequence] + max_fee = attributes[:max_fee] raise ArgumentError, "Bad :account" unless account.is_a?(KeyPair) && account.sign? raise ArgumentError, "Bad :sequence #{sequence}" unless sequence.is_a?(Integer) + raise ArgumentError, "Bad :max_fee #{sequence}" if max_fee.present? && !max_fee.is_a?(Integer) new.tap do |result| result.seq_num = sequence + result.max_fee = max_fee result.source_account = account.public_key result.apply_defaults end end \ No newline at end of file