Class: OrionX::Endpoints::Markets

Inherits:
Object
  • Object
show all
Defined in:
lib/orionx/endpoints/markets.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_client) ⇒ Markets

Returns a new instance of Markets.



4
5
6
7
# File 'lib/orionx/endpoints/markets.rb', line 4

def initialize(api_client)
  @api = api_client
  @logger = api_client.logger
end

Instance Method Details

#get_market(market_code) ⇒ Object

Get information for a specific market



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/orionx/endpoints/markets.rb', line 10

def get_market(market_code)
  raise OrionX::ValidationError, "Market code cannot be nil or empty" if market_code.nil? || market_code.empty?

  query = <<~GRAPHQL
    query sdk_market($code: ID) {
      market(code: $code) {
        code
        name
        mainCurrency {
          code
          name
          units
        }
        secondaryCurrency {
          code
          name
          units
        }
        lastTrade {
          price
        }
      }
    }
  GRAPHQL

  @logger.debug("Fetching market data for: #{market_code}")
  response = @api.call(query, { code: market_code })
  
  result = response.dig("market")
  @logger.info("Market #{market_code} data retrieved successfully") if result
  result
end

#get_market_stats(market_code) ⇒ Object

Get market statistics



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/orionx/endpoints/markets.rb', line 108

def get_market_stats(market_code)
  market = get_market(market_code)
  orderbook = get_orderbook(market_code, limit: 1)

  return nil unless market && orderbook

  {
    code: market["code"],
    name: market["name"],
    last_price: market.dig("lastTrade", "price"),
    spread: orderbook["spread"],
    mid_price: orderbook["mid"],
    best_bid: orderbook.dig("buy", 0, "limitPrice"),
    best_ask: orderbook.dig("sell", 0, "limitPrice"),
    main_currency: market.dig("mainCurrency", "code"),
    secondary_currency: market.dig("secondaryCurrency", "code")
  }
end

#get_markets ⇒ Object

Get all available markets



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/orionx/endpoints/markets.rb', line 44

def get_markets
  query = <<~GRAPHQL
    query sdk_markets {
      markets {
        code
        name
        mainCurrency {
          code
          name
          units
        }
        secondaryCurrency {
          code
          name
          units
        }
        lastTrade {
          price
        }
      }
    }
  GRAPHQL

  @logger.debug("Fetching all markets")
  response = @api.call(query)
  
  result = response.dig("markets")
  @logger.info("All markets retrieved: #{result&.length || 0} markets") if result
  result
end

#get_orderbook(market_code, limit: 50) ⇒ Object

Get order book for a specific market



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/orionx/endpoints/markets.rb', line 76

def get_orderbook(market_code, limit: 50)
  raise OrionX::ValidationError, "Market code cannot be nil or empty" if market_code.nil? || market_code.empty?
  raise OrionX::ValidationError, "Limit must be positive" if limit <= 0

  query = <<~GRAPHQL
    query sdk_marketOrderBook($marketCode: ID!, $limit: Int) {
      marketOrderBook(marketCode: $marketCode, limit: $limit) {
        sell {
          amount
          limitPrice
        }
        buy {
          amount
          limitPrice
        }
        spread
        mid
      }
    }
  GRAPHQL

  @logger.debug("Fetching orderbook for #{market_code} (limit: #{limit})")
  response = @api.call(query, { marketCode: market_code, limit: limit })
  
  result = response.dig("marketOrderBook")
  if result
    @logger.info("Orderbook for #{market_code} retrieved - Buy orders: #{result.dig('buy')&.length || 0}, Sell orders: #{result.dig('sell')&.length || 0}")
  end
  result
end