Module: PrivateApi

Included in:
BitX, BitX::Connection
Defined in:
lib/private_api.rb

Defined Under Namespace

Classes: BitXError

Instance Method Summary collapse

Instance Method Details

#api_auth(opt = {}) ⇒ Object



259
260
261
262
263
264
265
# File 'lib/private_api.rb', line 259

def api_auth(opt={})
  api_key_id = opt[:api_key_id] || self.configuration.api_key_id
  api_key_secret = opt[:api_key_secret] || self.configuration.api_key_secret
  conn = self.conn
  conn.basic_auth(api_key_id, api_key_secret)
  conn
end

#authed_request(path, opt = {}) ⇒ Object

opt can specify the method to be :post, :put, :delete opt can specify request params in :params opt could also include the api_key_id and api_key_secret

Raises:



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/private_api.rb', line 270

def authed_request(path, opt={})
  method = opt[:method] || :get
  conn = self.api_auth(opt)
  r = case method
  when :get
    conn.get(path, opt[:params])
  when :post
    conn.post(path, opt[:params])
  when :put
    conn.put(path, opt[:params])
  when :delete
    conn.delete(path, opt[:params])
  else
    raise BitXError.new("Request must be :post or :get")
  end
  raise BitXError.new("BitX #{path} error: #{r.status}") unless r.status == 200
  r
end

#balance(opt = {}) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/private_api.rb', line 100

def balance(opt={})
  path = '/api/1/balance'
  r = authed_request(path, opt)
  j = JSON.parse(r.body)

  balances = []
  if j['balance']
    j['balance'].each do |b|
      balance     = BigDecimal(b['balance'])
      reserved    = BigDecimal(b['reserved'])
      balances << {
        account_id:  b['account_id'],
        asset:       b['asset'],
        balance:     balance,
        reserved:    reserved,
        available:   balance - reserved,
        unconfirmed: BigDecimal(b['unconfirmed'])
      }
    end
  end
  balances
end

#balance_for(asset = 'XBT', opt = {}) ⇒ Object

Balance

Raises:



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/private_api.rb', line 83

def balance_for(asset='XBT', opt={})
  puts "BitX.balance_for will be deprecated. please use BitX.balance to get a list of account balances"
  opt.merge!({asset: asset})
  path = '/api/1/balance'
  r = authed_request(path, {params: opt})
  j = JSON.parse(r.body)
  raise BitXError.new("BitX balance error: #{j['error']}") if j['error']
  balance = BigDecimal(j['balance'][0]['balance'])
  reserved = BigDecimal(j['balance'][0]['reserved'])
  {
    asset:      asset,
    balance:    balance,
    reserved:   reserved,
    available:  balance - reserved
  }
end

#cancel_withdrawal(withdrawal_id) ⇒ Object



178
179
180
181
182
# File 'lib/private_api.rb', line 178

def cancel_withdrawal(withdrawal_id)
  path = "/api/1/withdrawals/#{withdrawal_id}"
  r = authed_request(path, {params: {}, method: :delete})
  JSON.parse(r.body, {symbolize_names: true})
end

#create_quote(pair, base_amount, type) ⇒ Object

<——- quotes .



214
215
216
217
218
219
# File 'lib/private_api.rb', line 214

def create_quote(pair, base_amount, type)
  #POST
  path = '/api/1/quotes'
  r = authed_request(path, {params: {type: type, pair: pair, base_amount: base_amount}, method: :post})
  extract_quote_from_body(r.body)
end

#discard_quote(quote_id) ⇒ Object



233
234
235
236
237
238
# File 'lib/private_api.rb', line 233

def discard_quote(quote_id)
  #DELETE
  path = "/api/1/quotes/#{quote_id}"
  r = authed_request(path, {method: :delete})
  extract_quote_from_body(r.body)
end

#exercise_quote(quote_id, pin = nil) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
# File 'lib/private_api.rb', line 221

def exercise_quote(quote_id, pin=nil)
  #PUT
  pin ||= self.configuration.api_key_pin rescue nil
  path = "/api/1/quotes/#{quote_id}"
  r = authed_request(path, {
    params: {
      pin: pin
    },
    method: :put})
  extract_quote_from_body(r.body)
end

#extract_quote_from_body(body) ⇒ Object



247
248
249
250
251
252
253
# File 'lib/private_api.rb', line 247

def extract_quote_from_body(body)
  quote = JSON.parse(body, {symbolize_names: true})
  return nil unless quote
  quote[:created_at] = Time.at(quote[:created_at]/1000) if quote[:created_at]
  quote[:expires_at] = Time.at(quote[:expires_at]/1000) if quote[:expires_at]
  quote
end

#format_order_hash(o) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/private_api.rb', line 58

def format_order_hash(o)
  {
    completed:    o[:state] == 'COMPLETE',
    state:        o[:state],
    created_at:   Time.at(o[:creation_timestamp].to_i/1000),
    expires_at:   Time.at(o[:expiration_timestamp].to_i/1000),
    order_id:     o[:order_id],
    limit_price:  BigDecimal(o[:limit_price]),
    limit_volume: BigDecimal(o[:limit_volume]),
    base:         BigDecimal(o[:base]),
    fee_base:     BigDecimal(o[:fee_base]),
    counter:      BigDecimal(o[:counter]),
    fee_counter:  BigDecimal(o[:fee_counter]),
    type:         o[:type].to_sym
  }
end

#funding_address(asset = 'XBT', opt = {}) ⇒ Object

Bitcoin Funding Address



145
146
147
148
149
# File 'lib/private_api.rb', line 145

def funding_address(asset='XBT', opt={})
  puts "BitX.funding_address will be deprecated. please use BitX.received_by_address with a specified :address option to get details for that address"
  opt.merge!({asset: asset})
  receive_address_request(opt, :get)
end

#get_order(order_id, opt = {}) ⇒ Object

Get Order



50
51
52
53
54
55
56
# File 'lib/private_api.rb', line 50

def get_order(order_id, opt={})
  path = "/api/1/orders/#{order_id}"
  r = authed_request(path, {params: opt})
  o = JSON.parse(r.body, {symbolize_names: true})

  format_order_hash(o)
end

#list_orders(pair, opt = {}) ⇒ Object

List Orders



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/private_api.rb', line 9

def list_orders(pair, opt={})
  opt.merge!({pair: pair})
  path = '/api/1/listorders'
  r = authed_request(path, {params: opt})
  j = JSON.parse(r.body, {symbolize_names: true})

  ol = []
  if j[:orders]
    j[:orders].each do |bo|
      ol << format_order_hash(bo)
    end
  end
  ol
end

#new_receive_address(opt = {}) ⇒ Object

<—– receive addresses .



128
129
130
# File 'lib/private_api.rb', line 128

def new_receive_address(opt={})
  receive_address_request(opt, :post)
end

#post_order(order_type, volume, price, pair, opt = {}) ⇒ Object

Post Order order_type ‘BID’/‘ASK’

Raises:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/private_api.rb', line 26

def post_order(order_type, volume, price, pair, opt={})
  params = {
    pair: pair,
    type: order_type,
    volume: volume.to_d.round(6).to_f.to_s,
    price: price.to_f.round.to_s
  }
  opt.merge!({params: params, method: :post})
  path = '/api/1/postorder'
  r = authed_request(path, opt)
  j = JSON.parse(r.body, {symbolize_names: true})
  raise BitXError.new("BitX postorder error: #{j['error']}") if j['error']
  j
end

#receive_address_request(opt, method) ⇒ Object



137
138
139
140
141
142
# File 'lib/private_api.rb', line 137

def receive_address_request(opt, method)
  opt.merge!({asset: 'XBT'})
  path = '/api/1/funding_address'
  r = authed_request(path, {params: opt, method: method})
  JSON.parse(r.body, {symbolize_names: true})
end

#received_by_address(opt = {}) ⇒ Object

if you have multiple XBT accounts you can specify an ‘address` option with the funding address



133
134
135
# File 'lib/private_api.rb', line 133

def received_by_address(opt={})
  receive_address_request(opt, :get)
end

#send(amount, address, currency = 'XBT', description = '', message = '', pin = nil) ⇒ Object

<——- send .



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/private_api.rb', line 188

def send(amount, address, currency='XBT', description='', message='', pin=nil)
  # valid types
  # ZAR_EFT,NAD_EFT,KES_MPESA,MYR_IBG,IDR_LLG

  pin ||= self.configuration.api_key_pin rescue nil

  path = '/api/1/send'
  r = authed_request(path, {
    params: {
      amount: amount.to_s,
      address: address,
      currency: currency,
      description: description,
      message: message,
      pin: pin
    },
    method: :post
  })
  JSON.parse(r.body, {symbolize_names: true})
end

#stop_order(order_id, opt = {}) ⇒ Object

Stop Order



42
43
44
45
46
47
# File 'lib/private_api.rb', line 42

def stop_order(order_id, opt={})
  options = {params: opt.merge!({order_id: order_id}), method: :post}
  path = '/api/1/stoporder'
  r = authed_request(path, options)
  JSON.parse(r.body, {symbolize_names: true})
end

#view_quote(quote_id) ⇒ Object



240
241
242
243
244
245
# File 'lib/private_api.rb', line 240

def view_quote(quote_id)
  #GET
  path = "/api/1/quotes/#{quote_id}"
  r = authed_request(path, {method: :get})
  extract_quote_from_body(r.body)
end

#withdraw(type, amount) ⇒ Object



163
164
165
166
167
168
169
170
# File 'lib/private_api.rb', line 163

def withdraw(type, amount)
  # valid types
  # ZAR_EFT,NAD_EFT,KES_MPESA,MYR_IBG,IDR_LLG

  path = '/api/1/withdrawals'
  r = authed_request(path, {params: {type: type, amount: amount}, method: :post})
  JSON.parse(r.body, {symbolize_names: true})
end

#withdrawal(withdrawal_id) ⇒ Object



172
173
174
175
176
# File 'lib/private_api.rb', line 172

def withdrawal(withdrawal_id)
  path = "/api/1/withdrawals/#{withdrawal_id}"
  r = authed_request(path, {params: {}, method: :get})
  JSON.parse(r.body, {symbolize_names: true})
end

#withdrawalsObject

<—– withdrawal requests .



154
155
156
157
158
159
160
161
# File 'lib/private_api.rb', line 154

def withdrawals
  path = '/api/1/withdrawals'
  r = authed_request(path, {params: {}, method: :get})
  j = JSON.parse(r.body, {symbolize_names: true})

  return j[:withdrawals] if j[:withdrawals]
  return j
end