Class: WavesRubyClient::Order

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
lib/waves_ruby_client/order.rb

Overview

A limit order

Constant Summary collapse

JSON_HEADERS =
{ 'Content-Type' => 'application/json', 'Accept' => 'application/json' }.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#amountObject

Returns the value of attribute amount.



10
11
12
# File 'lib/waves_ruby_client/order.rb', line 10

def amount
  @amount
end

#filledObject

filled amount



39
40
41
42
# File 'lib/waves_ruby_client/order.rb', line 39

def filled
  refresh_from_collection
  @filled
end

#idObject

Returns the value of attribute id.



10
11
12
# File 'lib/waves_ruby_client/order.rb', line 10

def id
  @id
end

#priceObject

Returns the value of attribute price.



10
11
12
# File 'lib/waves_ruby_client/order.rb', line 10

def price
  @price
end

#statusObject

Returns the value of attribute status.



10
11
12
# File 'lib/waves_ruby_client/order.rb', line 10

def status
  @status
end

#timestampObject

Returns the value of attribute timestamp.



10
11
12
# File 'lib/waves_ruby_client/order.rb', line 10

def timestamp
  @timestamp
end

#typeObject

Returns the value of attribute type.



10
11
12
# File 'lib/waves_ruby_client/order.rb', line 10

def type
  @type
end

Class Method Details

.activeObject

get all orders waiting to be filled for WAVES_PUBLIC_KEY



29
30
31
# File 'lib/waves_ruby_client/order.rb', line 29

def self.active
  all.select { |o| o.status == 'Accepted' || o.status == 'PartiallyFilled' }
end

.allObject

get all orders for WAVES_PUBLIC_KEY



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/waves_ruby_client/order.rb', line 14

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

Instance Method Details

#amount_assetObject



75
76
77
# File 'lib/waves_ruby_client/order.rb', line 75

def amount_asset
  WavesRubyClient::AMOUNT_ASSET
end

#cancelObject

cancel order any error is raised



58
59
60
61
62
# File 'lib/waves_ruby_client/order.rb', line 58

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

#deleteObject

delete order after it has been cancelled any error is raised



66
67
68
69
# File 'lib/waves_ruby_client/order.rb', line 66

def delete
  res = remove('delete')
  raise res.to_s unless res['status'] == 'OrderDeleted'
end

#pending?Boolean

order is waiting to be filled

Returns:

  • (Boolean)


34
35
36
# File 'lib/waves_ruby_client/order.rb', line 34

def pending?
  status != 'Filled' && status != 'PartiallyFilled'
end

#placeObject

place order any error is raised



46
47
48
49
50
51
52
53
54
# File 'lib/waves_ruby_client/order.rb', line 46

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_assetObject



71
72
73
# File 'lib/waves_ruby_client/order.rb', line 71

def price_asset
  WavesRubyClient::PRICE_ASSET
end

#refresh_statusObject

query order status



80
81
82
83
84
# File 'lib/waves_ruby_client/order.rb', line 80

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