-
Notifications
You must be signed in to change notification settings - Fork 0
/
bter.rb
172 lines (136 loc) · 3.76 KB
/
bter.rb
1
2
3
4
5
6
7
8
9
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
42
43
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
74
75
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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
168
169
170
171
172
#encoding: utf-8
#
# Usage:
# b = Bter.new key, secret
# b.balance
#
# Bter.btc_price => {"avg"=>6582.07, "sell"=>6300, "buy"=>6270}"
# Bter.debug = true/false
# Bter.retry_limit = 3
# b.buy 8000, 0.1
# b.sell 8000, 0.1
#
require './log'
require 'json'
require 'digest/sha2'
require 'digest/hmac'
require 'rest_client'
require 'debugger'
class Bter
class << self
def debug= debug
@debug = debug
end
def debug
@debug || false
end
def retry_limit= times
@retry_limit = times
end
def retry_limit
@retry_limit || 3 # default: 3
end
def configure
yield self
end
end
def initialize key, secret
@key = key
@secret = secret
@balance_address = 'https://bter.com/api/1/private/getfunds'
@place_order_address = 'https://bter.com/api/1/private/placeorder'
end
def self.btc_price
btc_orders_address = 'https://bter.com/api/1/depth/btc_cny'
retry_times ||= 0
resource = RestClient::Resource.new(btc_orders_address, :open_timeout => 15, timeout: 15)
res = resource.get
result = JSON.parse res.to_str
if result['result'] != 'true' || !result['bids'] || !result['asks'] || result['bids'].size == 0 || result['asks'].size == 0
raise '无法从Bter获取价格信息'
end
sell_price = result['asks'].last[0]
buy_price = result['bids'][0][0]
if sell_price < buy_price
raise "价格异常..卖价:#{sell_price} 买价#{buy_price}"
end
return {"sell"=> sell_price, 'buy' => buy_price}
rescue Exception => e
print "Bter: 无法获取价格信息 #{e}" if Bter.debug
if retry_times < Bter.retry_limit * 3
retry_times += 1
puts "正在重试..." if Bter.debug
sleep 1
retry
else
raise e
end
end
def buy price, amount
place_order price, amount, "BUY"
end
def sell price, amount
place_order price, amount, "SELL"
end
def balance
retry_times ||= 0
print '正在获取Bter余额信息...' if Bter.debug
result = post @balance_address, ''
if result['result'] != 'true'
raise '无法从Bter获取价格信息 ' + result['message']
end
funds = result['available_funds']
raise 'Bter:获取余额失败!' if funds.size == 0
puts "成功!" if Bter.debug
return {'BTC' => funds['BTC'].to_f, 'CNY' => funds['CNY'].to_f }
rescue SystemExit, Interrupt
raise
rescue Exception => e
puts "Bter: 无法获取价格信息#{e} 正在重试..." if Bter.debug
if retry_times < Bter.retry_limit
retry_times += 1
puts "正在重试..." if Bter.debug
retry
else
raise e
end
end
private
def get_sign params
Digest::HMAC.hexdigest(params, @secret, Digest::SHA512)
end
def post url, data
# result = RestClient.post url, data, 'KEY' => @key, 'SIGN' => get_sign(data), timeout
resource = RestClient::Resource.new(url, :open_timeout => 15, timeout: 15)
result = resource.post data, 'KEY' => @key, 'SIGN' => get_sign(data)
JSON.parse result.to_str
end
def place_order price, amount, type
retry_times ||= 0
res = post @place_order_address, URI.encode_www_form(pair: "btc_cny", type: type, rate: price, amount: amount)
if !res || res['result'] != true
raise "在Bter#{type}比特币失败 原因:#{res["msg"]}"
end
return res
rescue Exception => e
puts e if Bter.debug
Log.error e
if retry_times < Bter.retry_limit
retry_times += 1
info = "正在重试..."
puts info if Bter.debug
Log.info info
retry
else
raise e
end
end
end
=begin
b = Bter.new '86EB2B7B-848A-423E-8E63-5FAC295193AB', '08b78689ef43f6f23deb6d2125d841e7c3cb799652137cd45501c095c2d2bbb1'
Bter.debug = true
puts b.balance
puts Bter.btc_price
#debugger
a = 10
=end