yolodice-client 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +77 -26
- data/lib/yolodice_client.rb +66 -22
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4777876d939b40f70b4bdba8a119d73afacdf1a9
|
4
|
+
data.tar.gz: d20844e3e1163eeb47d26a1ec4971732b5043877
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 638e0e46a0a2f8d128a7b0e2468370fb2940d236f15dee52e5ab1e80962d01925db278628815d5821311b6833a0fca46adaf6412f6c6bd10b04d8916f989f8b2
|
7
|
+
data.tar.gz: a2751949033cc6edcfca2440f1d52d43ce5015eb831a8a588c662e5e723eeb4b3be50d606d1812a1fbe582e154d1604ddfe291d88b5c59b85808ad52081655ab
|
data/README.md
CHANGED
@@ -32,15 +32,15 @@ YOLOdice API requires authentication for most of it's methods. A Bitcoin key/add
|
|
32
32
|
|
33
33
|
1. Generate an Bitcoin public and private key:
|
34
34
|
|
35
|
-
|
35
|
+
```ruby
|
36
36
|
require 'bitcoin'
|
37
37
|
|
38
38
|
btc_key = Bitcoin::Key.generate
|
39
39
|
auth_key = btc_key.to_base58 # this is your secret code, store it in a secure place
|
40
40
|
auth_addr = btc_key.addr # paste this in your YD settings as a new key
|
41
|
+
```
|
41
42
|
|
42
43
|
2. Go to [YOLOdice account Settings](https://yolodice.com/#settings), create a new key and paste the `auth_addr` generated above. Set permissions as you wish.
|
43
|
-
|
44
44
|
3. Use `auth_key` in your code to authenticate.
|
45
45
|
|
46
46
|
Just a quick note — this address is used ONLY to authenticate. No coins will be ever sent to it.
|
@@ -48,9 +48,11 @@ Just a quick note — this address is used ONLY to authenticate. No coins wi
|
|
48
48
|
|
49
49
|
## Connecting
|
50
50
|
|
51
|
-
|
52
|
-
|
53
|
-
|
51
|
+
```ruby
|
52
|
+
yd = YolodiceClient.new
|
53
|
+
yd.connect
|
54
|
+
yd.authenticate auth_key
|
55
|
+
```
|
54
56
|
|
55
57
|
It's important to authenticate immediately after connecting. Otherwise the connection will be closed by the server.
|
56
58
|
|
@@ -61,13 +63,15 @@ The client automatically sends a `ping` requests to the server every 30 seconds
|
|
61
63
|
|
62
64
|
To preview the actuall messages sent back and forth you could provide your own logger object and set log level to `DEBUG` like this:
|
63
65
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
66
|
+
```ruby
|
67
|
+
yd = YolodiceClient.new
|
68
|
+
logger = Logger.new STDERR
|
69
|
+
logger.level = Logger::DEBUG
|
70
|
+
yd.logger = logger
|
68
71
|
|
69
|
-
|
70
|
-
|
72
|
+
yd.connect
|
73
|
+
yd.authenticate auth_key
|
74
|
+
```
|
71
75
|
|
72
76
|
This would result in the output similar to this:
|
73
77
|
|
@@ -97,22 +101,69 @@ There are two error classes:
|
|
97
101
|
|
98
102
|
`RemoteError` has two extra attributes: `code` and `data` that are mapped to values in the error object returned from the server.
|
99
103
|
|
104
|
+
## Helper methods
|
105
|
+
|
106
|
+
#### .satoshi_to_btc(v)
|
107
|
+
|
108
|
+
Converts an amount value from satoshis (integer) to bitcoin (float).
|
109
|
+
|
110
|
+
```ruby
|
111
|
+
YolodiceClient.satoshi_to_btc(10_000)
|
112
|
+
# => 0.0001
|
113
|
+
```
|
114
|
+
|
115
|
+
#### .btc_to_satoshi(v)
|
116
|
+
|
117
|
+
Converts an amount value from bitcoin (float) to satoshi (integer).
|
118
|
+
|
119
|
+
```ruby
|
120
|
+
YolodiceClient.btc_to_satoshi(0.12312312)
|
121
|
+
# => 12312312
|
122
|
+
```
|
123
|
+
|
124
|
+
#### .target_from_multiplier(m)
|
125
|
+
|
126
|
+
It calculates target value given the multiplier for the bet. If you want to find our the target that corresponds e.g. to multiplier `4`, try this:
|
127
|
+
|
128
|
+
```ruby
|
129
|
+
target = YolodiceClient.target_from_multiplier(4)
|
130
|
+
# => 247500
|
131
|
+
```
|
132
|
+
|
133
|
+
You can then use the target as an attribute to API method `create_bet`.
|
134
|
+
|
135
|
+
#### .target_from_probability(p)
|
136
|
+
|
137
|
+
It calculates target value given the win probability. Note: probability is a value ranged from 0 (0%) to 1 (100%). To find target that corresponds to win probability of 50% do this:
|
138
|
+
|
139
|
+
```ruby
|
140
|
+
target = YolodiceClient.target_from_probability(0.5)
|
141
|
+
# => 500000
|
142
|
+
```
|
143
|
+
|
144
|
+
|
100
145
|
## An example
|
101
146
|
|
102
147
|
Here is a script that connects to the server, authenticates, fetches user data, rolls a few 50% chance bets and reads user data again (make sure to use your own credential):
|
103
148
|
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
149
|
+
```ruby
|
150
|
+
require 'yolodice_client'
|
151
|
+
require 'pp'
|
152
|
+
|
153
|
+
auth_key = 'cPFVHENWNjs5UKNXXynDSWiRkBEph8hcrjHKkXK5SW9QHxx7i4jC'
|
154
|
+
yd = YolodiceClient.new
|
155
|
+
yd.connect
|
156
|
+
user = yd.authenticate auth_key
|
157
|
+
user_data = yd.read_user_data selector: {id: user['id']}
|
158
|
+
puts "Your account balance is: #{user_data['balance']} satoshis."
|
159
|
+
10.times do
|
160
|
+
b = yd.create_bet attrs: {amount: 100, range: 'lo', target: 500000}
|
161
|
+
puts "Bet profit: #{b['profit']}"
|
162
|
+
end
|
163
|
+
user_data = yd.read_user_data selector: {id: user['id']}
|
164
|
+
puts "Your account balance is: #{user_data['balance']} satoshis."
|
165
|
+
```
|
166
|
+
|
167
|
+
# Even more examples
|
168
|
+
|
169
|
+
We have created a new Github repository with Ruby script examples to get you started. If you are looking for inspiration, check this out: https://github.com/ethan-nx/yolodice-examples.
|
data/lib/yolodice_client.rb
CHANGED
@@ -62,28 +62,33 @@ class YolodiceClient
|
|
62
62
|
@listening_thread = Thread.new do
|
63
63
|
log.debug 'Listening thread started'
|
64
64
|
loop do
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
65
|
+
begin
|
66
|
+
msg = @connection.gets
|
67
|
+
next if msg == nil
|
68
|
+
log.debug{ "<<< #{msg}" }
|
69
|
+
message = JSON.parse msg
|
70
|
+
if message['id'] && (message.has_key?('result') || message.has_key?('error'))
|
71
|
+
# definitealy a response
|
72
|
+
callback = @thread_semaphore.synchronize{ @current_requests.delete message['id'] }
|
73
|
+
raise Error, "Unknown id in response" unless callback
|
74
|
+
if callback.is_a? Integer
|
75
|
+
# it's a thread
|
76
|
+
@receive_queues[callback] << message
|
77
|
+
elsif callback.is_a? Proc
|
78
|
+
# A thread pool would be better.
|
79
|
+
Thread.new do
|
80
|
+
callback.call message
|
81
|
+
end
|
79
82
|
end
|
80
|
-
end
|
81
|
-
else
|
82
|
-
if message['id']
|
83
|
-
# It must be a request from the server. We do not support it yet.
|
84
83
|
else
|
85
|
-
|
84
|
+
if message['id']
|
85
|
+
# It must be a request from the server. We do not support it yet.
|
86
|
+
else
|
87
|
+
# No id, it must be a notification then. This can be implemented later.
|
88
|
+
end
|
86
89
|
end
|
90
|
+
rescue StandardError => e
|
91
|
+
log.error e
|
87
92
|
end
|
88
93
|
end
|
89
94
|
end
|
@@ -91,8 +96,12 @@ class YolodiceClient
|
|
91
96
|
@pinging_thread = Thread.new do
|
92
97
|
log.debug 'Pinging thread started'
|
93
98
|
loop do
|
94
|
-
|
95
|
-
|
99
|
+
begin
|
100
|
+
sleep 30
|
101
|
+
call :ping
|
102
|
+
rescue StandardError => e
|
103
|
+
log.error e
|
104
|
+
end
|
96
105
|
end
|
97
106
|
end
|
98
107
|
true
|
@@ -165,6 +174,7 @@ class YolodiceClient
|
|
165
174
|
# a regular blocking request
|
166
175
|
@thread_semaphore.synchronize{ @current_requests[id] = Thread.current.object_id }
|
167
176
|
queue = (@receive_queues[Thread.current.object_id] ||= Queue.new)
|
177
|
+
queue.clear
|
168
178
|
log.debug{ "Calling remote method #{method}(#{params.inspect if params != nil})" }
|
169
179
|
log.debug{ ">>> #{request.to_json}" }
|
170
180
|
@connection.puts request.to_json
|
@@ -213,8 +223,42 @@ class YolodiceClient
|
|
213
223
|
def initialize(error_obj = {'code' => -1, 'message' => "RPC Error"})
|
214
224
|
@code = error_obj['code'] || -1
|
215
225
|
@data = error_obj['data'] if error_obj['data']
|
216
|
-
|
226
|
+
msg = "#{@code}: #{error_obj['message']}"
|
227
|
+
if @code == 422 && @data && @data.has_key?('errors')
|
228
|
+
msg += ': ' + @data['errors'].to_json
|
229
|
+
end
|
230
|
+
super msg
|
217
231
|
end
|
218
232
|
end
|
219
233
|
|
234
|
+
class << self
|
235
|
+
|
236
|
+
##
|
237
|
+
# Returns bet terget given the required multiplier.
|
238
|
+
|
239
|
+
def target_from_multiplier m
|
240
|
+
edge = 0.01
|
241
|
+
(1_000_000.0 * (1.0 - edge) / m).round
|
242
|
+
end
|
243
|
+
|
244
|
+
##
|
245
|
+
# Returns bet target given the required win probability.
|
246
|
+
|
247
|
+
def target_from_probability p
|
248
|
+
(p * 1_000_000.0).round
|
249
|
+
end
|
250
|
+
|
251
|
+
##
|
252
|
+
# Converts amount from satoshi (integer) to amount in bitcoins (float).
|
253
|
+
|
254
|
+
def satoshi_to_btc v
|
255
|
+
(v.to_f / 100_000_000).round(8)
|
256
|
+
end
|
257
|
+
|
258
|
+
## Converts amount from bitcoins (float) to satoshi (integer).
|
259
|
+
def btc_to_satoshi v
|
260
|
+
(v * 100_000_000).round
|
261
|
+
end
|
262
|
+
|
263
|
+
end
|
220
264
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yolodice-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ethan_nx
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-03-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bitcoin-ruby
|