Class: Cryptsy::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/cryptsy/client.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  url: 'https://api.cryptsy.com',
  ssl: {
    verify: false
  }
}
ORDER_TYPE_BUY =
'Buy'
ORDER_TYPE_SELL =
'Sell'

Instance Method Summary collapse

Constructor Details

#initialize(public_key, private_key, options = {}) ⇒ Client

Returns a new instance of Client.

Parameters:

  • public_key (String)
  • private_key (String)
  • options (Hash) (defaults to: {})


21
22
23
24
25
26
# File 'lib/cryptsy/client.rb', line 21

def initialize(public_key, private_key, options = {})
  @public_key = public_key
  @private_key = private_key
  @digest = OpenSSL::Digest::SHA512.new
  @connection = Faraday.new(DEFAULT_OPTIONS.merge(options))
end

Instance Method Details

#all_ordersObject



51
52
53
# File 'lib/cryptsy/client.rb', line 51

def all_orders
  call(:allmyorders)
end

#all_tradesObject



59
60
61
# File 'lib/cryptsy/client.rb', line 59

def all_trades
  call(:allmytrades)
end

#balance(currency_code) ⇒ Object



32
33
34
# File 'lib/cryptsy/client.rb', line 32

def balance(currency_code)
  info.balances_available.fetch(currency_code.upcase).to_f
end

#calculate_buy_fees(quantity, price) ⇒ Object



112
113
114
# File 'lib/cryptsy/client.rb', line 112

def calculate_buy_fees(quantity, price)
  calculate_fees(ORDER_TYPE_BUY, quantity, price)
end

#calculate_fees(order_type, quantity, price) ⇒ Object



120
121
122
123
124
125
126
# File 'lib/cryptsy/client.rb', line 120

def calculate_fees(order_type, quantity, price)
  call(:calculatefees,
    ordertype: order_type,
    quantity: format_number(quantity),
    price: format_number(price)
  )
end

#calculate_sell_fees(quantity, price) ⇒ Object



116
117
118
# File 'lib/cryptsy/client.rb', line 116

def calculate_sell_fees(quantity, price)
  calculate_fees(ORDER_TYPE_SELL, quantity, price)
end

#call(method_name, params = {}) ⇒ Object

Parameters:

  • method_name (Symbol)
  • params (Hash) (defaults to: {})

Returns:

  • (Object)

Raises:



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/cryptsy/client.rb', line 144

def call(method_name, params = {})
  request = {
    method: method_name,
    nonce: (Time.now.to_f * 1000).to_i
  }.merge(params)

  body = URI.encode_www_form(request)
  signature = OpenSSL::HMAC.hexdigest(@digest, @private_key, body)

  response = @connection.post do |req|
    req.url 'api'
    req.body = body

    req.headers['Content-Type'] = 'application/x-www-form-urlencoded'
    req.headers['Key'] = @public_key
    req.headers['Sign'] = signature
  end

  process_response(response)
end

#cancel_all_ordersObject



108
109
110
# File 'lib/cryptsy/client.rb', line 108

def cancel_all_orders
  call(:cancelallorders)
end

#cancel_order(order_id) ⇒ Object



100
101
102
# File 'lib/cryptsy/client.rb', line 100

def cancel_order(order_id)
  call(:cancelorder, orderid: order_id)
end

#cancel_orders(market_id) ⇒ Object



104
105
106
# File 'lib/cryptsy/client.rb', line 104

def cancel_orders(market_id)
  call(:cancelmarketorders, marketid: market_id)
end

#create_buy_order(market_id, quantity, price) ⇒ Object



83
84
85
# File 'lib/cryptsy/client.rb', line 83

def create_buy_order(market_id, quantity, price)
  create_order(market_id, ORDER_TYPE_BUY, quantity, price)
end

#create_order(market_id, order_type, quantity, price) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/cryptsy/client.rb', line 91

def create_order(market_id, order_type, quantity, price)
  call(:createorder,
    marketid: market_id,
    ordertype: order_type,
    quantity: format_number(quantity),
    price: format_number(price)
  )
end

#create_sell_order(market_id, quantity, price) ⇒ Object



87
88
89
# File 'lib/cryptsy/client.rb', line 87

def create_sell_order(market_id, quantity, price)
  create_order(market_id, ORDER_TYPE_SELL, quantity, price)
end

#format_number(number) ⇒ String

Parameters:

  • number (Numeric)

Returns:

  • (String)


184
185
186
# File 'lib/cryptsy/client.rb', line 184

def format_number(number)
  format('%.8f', number)
end

#generate_new_address(currency) ⇒ Object



128
129
130
131
132
133
134
# File 'lib/cryptsy/client.rb', line 128

def generate_new_address(currency)
  if currency.is_a?(Integer)
    call(:generatenewaddress, currencyid: currency).address
  else
    call(:generatenewaddress, currencycode: normalize_currency_code(currency)).address
  end
end

#infoObject



28
29
30
# File 'lib/cryptsy/client.rb', line 28

def info
  call(:getinfo)
end

#make_withdrawal(address, amount) ⇒ Object



136
137
138
# File 'lib/cryptsy/client.rb', line 136

def make_withdrawal(address, amount)
  call(:makewithdrawal, address: address, amount: amount)
end

#market_by_pair(primary_code, secondary_code) ⇒ Object



40
41
42
43
44
45
# File 'lib/cryptsy/client.rb', line 40

def market_by_pair(primary_code, secondary_code)
  markets.find do |market|
    market.primary_currency_code == normalize_currency_code(primary_code) &&
      market.secondary_currency_code == normalize_currency_code(secondary_code)
  end
end

#market_depth(market_id) ⇒ Object



71
72
73
# File 'lib/cryptsy/client.rb', line 71

def market_depth(market_id)
  call(:depth, marketid: market_id)
end

#market_orders(market_id) ⇒ Object



75
76
77
# File 'lib/cryptsy/client.rb', line 75

def market_orders(market_id)
  call(:marketorders, marketid: market_id)
end

#market_trades(market_id) ⇒ Object



79
80
81
# File 'lib/cryptsy/client.rb', line 79

def market_trades(market_id)
  call(:markettrades, marketid: market_id)
end

#marketsObject



36
37
38
# File 'lib/cryptsy/client.rb', line 36

def markets
  call(:getmarkets)
end

#normalize_currency_code(code) ⇒ String

Parameters:

  • code (Object)

Returns:

  • (String)


190
191
192
# File 'lib/cryptsy/client.rb', line 190

def normalize_currency_code(code)
  code.to_s.upcase
end

#orders(market_id) ⇒ Object



47
48
49
# File 'lib/cryptsy/client.rb', line 47

def orders(market_id)
  call(:myorders, marketid: market_id)
end

#process_response(response) ⇒ Object

Parameters:

  • response (Faraday::Response)

Returns:

  • (Object)

Raises:



168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/cryptsy/client.rb', line 168

def process_response(response)
  body = Hashie::Mash.new(JSON.parse(response.body))

  unless body.success.to_i == 1
    raise ClientError, body.error
  end

  if body.return
    body.return
  else
    body
  end
end

#trades(market_id, limit = 200) ⇒ Object



55
56
57
# File 'lib/cryptsy/client.rb', line 55

def trades(market_id, limit = 200)
  call(:mytrades, marketid: market_id, limit: limit)
end

#transactionsObject



63
64
65
# File 'lib/cryptsy/client.rb', line 63

def transactions
  call(:mytransactions)
end

#transfersObject



67
68
69
# File 'lib/cryptsy/client.rb', line 67

def transfers
  call(:mytransfers)
end