Class: PeatioIrix::Crypto
- Inherits:
-
Peatio::Upstream::Base
- Object
- Peatio::Upstream::Base
- PeatioIrix::Crypto
- Defined in:
- lib/peatio_irix/crypto.rb
Constant Summary collapse
- MIN_INCREMENT_COUNT_TO_SNAPSHOT =
100
- MIN_PERIOD_TO_SNAPSHOT =
5
- MAX_PERIOD_TO_SNAPSHOT =
60
Instance Attribute Summary collapse
-
#asks ⇒ Object
Returns the value of attribute asks.
-
#bids ⇒ Object
Returns the value of attribute bids.
-
#increment_count ⇒ Object
Returns the value of attribute increment_count.
-
#sequence_number ⇒ Object
Returns the value of attribute sequence_number.
-
#snap ⇒ Object
Returns the value of attribute snap.
-
#snapshot_time ⇒ Object
Returns the value of attribute snapshot_time.
Instance Method Summary collapse
- #detect_kline(msg) ⇒ Object
- #detect_order(msg) ⇒ Object
- #detect_trade(msg) ⇒ Object
- #fill_increment(inc) ⇒ Object
- #fill_side(inc, side) ⇒ Object
- #heart_beat(ws) ⇒ Object
-
#initialize(config) ⇒ Crypto
constructor
A new instance of Crypto.
- #publish_increment ⇒ Object
- #publish_snapshot ⇒ Object
- #subscribe_channels(market, ws) ⇒ Object
- #subscribe_klines(market, ws) ⇒ Object
- #subscribe_orderbook(market, ws) ⇒ Object
- #ws_connect ⇒ Object
- #ws_read_message(msg) ⇒ Object
- #ws_read_public_message(msg) ⇒ Object
Constructor Details
#initialize(config) ⇒ Crypto
Returns a new instance of Crypto.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/peatio_irix/crypto.rb', line 15 def initialize(config) super @connection = Faraday.new(url: (config['rest']).to_s) do |builder| builder.response :json builder.response :logger if config['debug'] builder.adapter(@adapter) unless config['verify_ssl'].nil? builder.ssl[:verify] = config['verify_ssl'] end end @ping_set = false @rest = (config['rest']).to_s @ws_url = (config['websocket']).to_s end |
Instance Attribute Details
#asks ⇒ Object
Returns the value of attribute asks.
12 13 14 |
# File 'lib/peatio_irix/crypto.rb', line 12 def asks @asks end |
#bids ⇒ Object
Returns the value of attribute bids.
12 13 14 |
# File 'lib/peatio_irix/crypto.rb', line 12 def bids @bids end |
#increment_count ⇒ Object
Returns the value of attribute increment_count.
12 13 14 |
# File 'lib/peatio_irix/crypto.rb', line 12 def increment_count @increment_count end |
#sequence_number ⇒ Object
Returns the value of attribute sequence_number.
12 13 14 |
# File 'lib/peatio_irix/crypto.rb', line 12 def sequence_number @sequence_number end |
#snap ⇒ Object
Returns the value of attribute snap.
12 13 14 |
# File 'lib/peatio_irix/crypto.rb', line 12 def snap @snap end |
#snapshot_time ⇒ Object
Returns the value of attribute snapshot_time.
12 13 14 |
# File 'lib/peatio_irix/crypto.rb', line 12 def snapshot_time @snapshot_time end |
Instance Method Details
#detect_kline(msg) ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/peatio_irix/crypto.rb', line 43 def detect_kline(msg) p "msg => #{msg}" # return if msg.empty? # # msg.dig("result", "data").each do |k_line| # @peatio_mq.enqueue_event('public', market.id, # event_name, point) # end end |
#detect_order(msg) ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/peatio_irix/crypto.rb', line 53 def detect_order(msg) if @increment_count < MIN_INCREMENT_COUNT_TO_SNAPSHOT && @snapshot_time <= Time.now - MAX_PERIOD_TO_SNAPSHOT publish_snapshot @increment_count = 0 elsif @increment_count >= MIN_INCREMENT_COUNT_TO_SNAPSHOT && @snapshot_time < Time.now - MIN_PERIOD_TO_SNAPSHOT publish_snapshot @increment_count = 0 end fill_increment(msg) end |
#detect_trade(msg) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/peatio_irix/crypto.rb', line 112 def detect_trade(msg) msg.map do |t| trade = { 'tid' => t['d'], 'amount' => t['q'].to_d, 'price' => t['p'].to_d, 'date' => t['t'] / 1000, 'taker_type' => t['s'] } notify_public_trade(trade) end end |
#fill_increment(inc) ⇒ Object
64 65 66 67 68 |
# File 'lib/peatio_irix/crypto.rb', line 64 def fill_increment(inc) fill_side(inc[0], "bids") fill_side(inc[0], "asks") @increment_count += 1 end |
#fill_side(inc, side) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/peatio_irix/crypto.rb', line 70 def fill_side(inc, side) inc[side].each do |price_point| price = price_point[0] amount = price_point[1] if amount.zero? @snap[side].delete_if { |point| point[0] == price.to_s } else @snap[side].delete_if { |point| point[0] == price.to_s } @snap[side] << [price.to_s, amount.to_s] end if side == "bids" @bids.delete_if { |point| point[0] == price } @bids << [price.to_s, amount.to_s] elsif side == "asks" @asks.delete_if { |point| point[0] == price } @asks << [price.to_s, amount.to_s] end end end |
#heart_beat(ws) ⇒ Object
213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/peatio_irix/crypto.rb', line 213 def heart_beat(ws) return unless @config['trade_proxy'] sub = { id: Time.now.to_i * 1000, method: 'public/respond-heartbeat' } EM.next_tick do ws.send(JSON.generate(sub)) end end |
#publish_increment ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/peatio_irix/crypto.rb', line 90 def publish_increment inc = {} inc['bids'] = @bids.sort.reverse if @bids.present? inc['asks'] = @asks.sort if @asks.present? if inc.present? @sequence_number += 1 @peatio_mq.enqueue_event('public', @market, 'ob-inc', 'bids' => inc['bids'], 'asks' => inc['asks'], 'sequence' => @sequence_number) end @bids = [] @asks = [] end |
#publish_snapshot ⇒ Object
104 105 106 107 108 109 110 |
# File 'lib/peatio_irix/crypto.rb', line 104 def publish_snapshot @snapshot_time = Time.now @peatio_mq.enqueue_event('public', @market, 'ob-snap', 'bids' => @snap['bids'].sort.reverse, 'asks' => @snap['asks'].sort, 'sequence' => @sequence_number) end |
#subscribe_channels(market, ws) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/peatio_irix/crypto.rb', line 169 def subscribe_channels(market, ws) @sequence_number = 0 @increment_count = 0 @snapshot_time = Time.now @bids = [] @asks = [] @snap = { 'asks' => [], 'bids' => [] } channels = [] channels += ["trade.#{market}"] if @config['trade_proxy'] channels += ["book.#{market}.150"] if @config['orderbook_proxy'] sub = { id: 11, method: 'subscribe', params: { channels: channels }, nonce: Time.now.to_i * 1000 } EM.next_tick do ws.send(JSON.generate(sub)) end end |
#subscribe_klines(market, ws) ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/peatio_irix/crypto.rb', line 196 def subscribe_klines(market, ws) return unless @config['trade_proxy'] sub = { id: 11, method: 'subscribe', params: { channels: ["candlestick.1m.#{market}"] }, nonce: Time.now.to_i * 1000 } EM.next_tick do ws.send(JSON.generate(sub)) end end |
#subscribe_orderbook(market, ws) ⇒ Object
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/peatio_irix/crypto.rb', line 226 def subscribe_orderbook(market, ws) return unless @config['orderbook_proxy'] @sequence_number = 0 @increment_count = 0 @snapshot_time = Time.now @bids = [] @asks = [] @snap = { 'asks' => [], 'bids' => [] } sub = { id: 11, method: 'subscribe', params: { channels: ["book.#{market}.150"] }, nonce: Time.now.to_i * 1000 } EM.next_tick do ws.send(JSON.generate(sub)) end Fiber.new do EM::Synchrony.add_periodic_timer(0.2) do publish_increment end end.resume end |
#ws_connect ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/peatio_irix/crypto.rb', line 133 def ws_connect logger.info { "Websocket connecting to #{@ws_url}" } raise "websocket url missing for account #{id}" unless @ws_url @ws = Faye::WebSocket::Client.new(@ws_url) @ws.on(:open) do |_e| subscribe_channels(@target, @ws) logger.info { "Websocket connected" } end @ws.on(:message) do |msg| (msg) end @ws.on(:close) do |e| @ws = nil @ws_status = false logger.error "Websocket disconnected: #{e.code} Reason: #{e.reason}" Fiber.new do EM::Synchrony.sleep(WEBSOCKET_CONNECTION_RETRY_DELAY) ws_connect end.resume end return if @ping_set Fiber.new do EM::Synchrony.add_periodic_timer(5) do heart_beat(@ws) end end.resume @ping_set = true end |
#ws_read_message(msg) ⇒ Object
126 127 128 129 130 131 |
# File 'lib/peatio_irix/crypto.rb', line 126 def (msg) logger.info { "received websocket message: #{msg.data}" } object = JSON.parse(msg.data) (object) end |
#ws_read_public_message(msg) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/peatio_irix/crypto.rb', line 30 def (msg) if msg['result'].present? case msg.dig('result','subscription') when /trade\.#{@target}/ detect_trade(msg.dig('result', 'data')) when /book\.#{@target}\.#{msg.dig('result', 'depth')}/ detect_order(msg.dig('result', 'data')) # when /candlestick.#{msg.dig('result', 'interval')}.#{@target}/ # detect_kline(msg.dig('result', 'data')) end end end |