class WavesRubyClient::Order

A limit order

Constants

JSON_HEADERS

Attributes

amount[RW]
buyer[RW]
confirmed[RW]
filled[W]
id[RW]
matcher[RW]
price[RW]
seller[RW]
status[RW]
timestamp[RW]
type[RW]

Public Class Methods

active() click to toggle source

get all orders waiting to be filled for WAVES_PUBLIC_KEY

# File lib/waves_ruby_client/order.rb, line 30
def self.active
  all.select { |o| o.status == 'Accepted' || o.status == 'PartiallyFilled' }
end
all() click to toggle source

get all orders for WAVES_PUBLIC_KEY

# File lib/waves_ruby_client/order.rb, line 15
def self.all
  url = ['/orderbook', WavesRubyClient::AMOUNT_ASSET.url_id,
         WavesRubyClient::PRICE_ASSET.url_id,
         'publicKey', WavesRubyClient::WAVES_PUBLIC_KEY].join('/')
  data = WavesRubyClient::OrderData::UserOrders.new.data_with_signature
  orders = WavesRubyClient::Api.instance.call_matcher(url, :get, headers: data)
  orders.map do |order_hash|
    attributes = %i[filled price amount].map do |attribute|
      { attribute => order_hash[attribute.to_s].to_f / WavesRubyClient::NUMBER_MULTIPLIKATOR }
    end.reduce({}, :merge)
    new(order_hash.slice('id', 'status', 'type', 'timestamp').merge(attributes))
  end
end

Public Instance Methods

amount_asset() click to toggle source
# File lib/waves_ruby_client/order.rb, line 76
def amount_asset
  WavesRubyClient::AMOUNT_ASSET
end
cancel() click to toggle source

cancel order any error is raised

# File lib/waves_ruby_client/order.rb, line 59
def cancel
  res = remove('cancel')
  raise WavesRubyClient::OrderAlreadyFilled if res['message']&.match?(/Order is already Filled/)
  raise res.to_s unless res['status'] == 'OrderCanceled'
end
delete() click to toggle source

delete order after it has been cancelled any error is raised

# File lib/waves_ruby_client/order.rb, line 67
def delete
  res = remove('delete')
  raise res.to_s unless res['status'] == 'OrderDeleted'
end
filled() click to toggle source

filled amount

# File lib/waves_ruby_client/order.rb, line 40
def filled
  refresh_from_collection
  @filled
end
pending?() click to toggle source

order is waiting to be filled

# File lib/waves_ruby_client/order.rb, line 35
def pending?
  status != 'Filled' && status != 'PartiallyFilled'
end
place() click to toggle source

place order any error is raised

# File lib/waves_ruby_client/order.rb, line 47
def place
  data = WavesRubyClient::OrderData::Place.new(self).data_with_signature
  res = WavesRubyClient::Api.instance.call_matcher('/orderbook', :post,
                                                   body: data.to_json,
                                                   headers: JSON_HEADERS)
  raise res.to_s unless res['status'] == 'OrderAccepted'
  self.id = res['message']['id']
  self
end
price_asset() click to toggle source
# File lib/waves_ruby_client/order.rb, line 72
def price_asset
  WavesRubyClient::PRICE_ASSET
end
refresh_status() click to toggle source

query order status

# File lib/waves_ruby_client/order.rb, line 81
def refresh_status
  url = "/orderbook/#{amount_asset.url_id}/#{price_asset.url_id}/#{id}"
  response = WavesRubyClient::Api.instance.call_matcher(url, :get)
  self.status = response['status']
end

Private Instance Methods

refresh_from_collection() click to toggle source

There is no api method to query data of a single order So all orders are retrieved

# File lib/waves_ruby_client/order.rb, line 91
def refresh_from_collection
  order = self.class.all.select { |o| o.id == id }.first
  return unless order
  assign_attributes(order.instance_values)
end
remove(action) click to toggle source
# File lib/waves_ruby_client/order.rb, line 97
def remove(action)
  data = WavesRubyClient::OrderData::Cancel.new(self).data_with_signature
  url = "/orderbook/#{amount_asset.url_id}/#{price_asset.url_id}/#{action}"
  WavesRubyClient::Api.instance.call_matcher(url, :post,
                                             body: data.to_json,
                                             headers: JSON_HEADERS)
end