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

#buyerObject

Returns the value of attribute buyer.



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

def buyer
  @buyer
end

#confirmedObject

Returns the value of attribute confirmed.



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

def confirmed
  @confirmed
end

#filledObject

filled amount



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

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

#matcherObject

Returns the value of attribute matcher.



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

def matcher
  @matcher
end

#priceObject

Returns the value of attribute price.



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

def price
  @price
end

#sellerObject

Returns the value of attribute seller.



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

def seller
  @seller
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



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

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

.allObject

get all orders for WAVES_PUBLIC_KEY



15
16
17
18
19
20
21
22
23
24
25
26
27
# 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

Instance Method Details

#amount_assetObject



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

def amount_asset
  WavesRubyClient::AMOUNT_ASSET
end

#cancelObject

cancel order any error is raised



59
60
61
62
63
# 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

#deleteObject

delete order after it has been cancelled any error is raised



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

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

#pending?Boolean

order is waiting to be filled

Returns:

  • (Boolean)


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

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

#placeObject

place order any error is raised



47
48
49
50
51
52
53
54
55
# 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_assetObject



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

def price_asset
  WavesRubyClient::PRICE_ASSET
end

#refresh_statusObject

query order status



81
82
83
84
85
# 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