cbx 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/README.md +100 -0
- data/cbx.gemspec +16 -0
- data/lib/cbx.rb +68 -0
- data/lib/cbx/cbx_signature_maker.rb +18 -0
- data/lib/cbx/feed.rb +35 -0
- data/lib/cbx/market_data.rb +21 -0
- data/lib/cbx/pagination.rb +13 -0
- data/lib/cbx/trading.rb +64 -0
- data/lib/cbx/version.rb +3 -0
- metadata +91 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f63ca10d81b008dedae9cbae3d368aae9b15913c
|
4
|
+
data.tar.gz: 57838a381e03f8e9b984727113332ac0068dc065
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 947eb218522c03eaa6baca54fa9005aad22bb032c5852f92215a8fcac35ac6b94795aa6483e2adf880019997739eda9cc9f7d7a558be82fd0e2843fe32df8768
|
7
|
+
data.tar.gz: f7722a5e01299cd37dcaf0e11cc97abaae1cecb68a9883abd2a5cb26b8928db584b0f24acda945f033e9357cb35a3f8b08ce0a6a777d63e14458b5c18bf03a63
|
data/README.md
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
This is a fork of a project originally authored by Daniel Silver
|
2
|
+
(https://github.com/dan-silver/cbx)
|
3
|
+
|
4
|
+
The fork features convenience methods for all endpoint functionality and
|
5
|
+
allows for unauthenticated access to Market Data and the WebSocket feed.
|
6
|
+
|
7
|
+
# Coinbase Exchange Ruby Client
|
8
|
+
The library wraps the http request and message signing. Create an account at https://exchange.coinbase.com to get started.
|
9
|
+
|
10
|
+
### NOTE - As a launch promo, there are [no Coinbase Exchange trading fees](http://blog.coinbase.com/post/109202118547/coinbase-launches-first-regulated-bitcoin-exchange) through the end of March.
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Include this in your gemfile:
|
15
|
+
|
16
|
+
```gem 'cbx', :git=> 'git://github.com/mikerodrigues/cbx.git'```
|
17
|
+
|
18
|
+
## Example
|
19
|
+
```ruby
|
20
|
+
require "cbx"
|
21
|
+
|
22
|
+
# For unauthenticated access:
|
23
|
+
cbe = CBX.new
|
24
|
+
|
25
|
+
# List products
|
26
|
+
cbe.products
|
27
|
+
=> [{"id"=>"BTC-USD", "base_currency"=>"BTC", "quote_currency"=>"USD", "base_min_size"=>0.01, "base_max_size"=>10000, "quote_increment"=>0.01, "display_name"=>"BTC/USD"}]
|
28
|
+
|
29
|
+
# Get product order book at level 1, 2, or 3, (Defaults to level: 1, product_id "BTC-USD")
|
30
|
+
cbe.book(1, 'BTC-USD')
|
31
|
+
=> {"sequence"=>29349454, "bids"=>[["285.22000000", "0.34800000", 3]], "asks"=>[["285.33000000", "0.28930000", 4]]}
|
32
|
+
|
33
|
+
# Product tickers (defaults to 'BTC-USD')
|
34
|
+
cbe.ticker("BTC-USD")
|
35
|
+
=> {"trade_id"=>125681, "price"=>"226.20000000", "size"=>"0.01570000", "time"=>"2015-02-08T04:46:17.352746Z"}
|
36
|
+
|
37
|
+
# Product trades (defaults to 'BTC-USD')
|
38
|
+
cbe.trades('BTC-USD')
|
39
|
+
=> [{"time"=>"2015-03-15 04:43:48.7943+00", "trade_id"=>774500, "price"=>"285.44000000", "size"=>"0.01000000", "side"=>"sell"},
|
40
|
+
{"time"=>"2015-03-15 04:42:54.432661+00", "trade_id"=>774499, "price"=>"285.47000000", "size"=>"0.05340000", "side"=>"sell"},
|
41
|
+
{"time"=>"2015-03-15 04:42:54.432306+00", "trade_id"=>774498, "price"=>"285.45000000", "size"=>"0.09100000", "side"=>"sell"}]
|
42
|
+
|
43
|
+
|
44
|
+
# For authenticated access:
|
45
|
+
cbe = CBX.new API_KEY, API_SECRET, API_PASSPHRASE
|
46
|
+
|
47
|
+
# List accounts
|
48
|
+
cbe.accounts
|
49
|
+
=> [{"id"=>"000ea663...", "currency"=>"USD", "balance"=>"90.0000114750000000", "hold"=>"0.0000000000000000", "available"=>"0.9000114750000000", "profile_id"=>"4409df27..."}, {"id"=>"8bfe", "currency"=>"BTC", "balance"=>"9.4426882700000000", "hold"=>"0.0000000000000000", "available"=>"5.4426882700000000", "profile_id"=>"a8f2d8..."}]
|
50
|
+
|
51
|
+
# List orders
|
52
|
+
cbe.orders
|
53
|
+
|
54
|
+
# List orders with pagination
|
55
|
+
cbe.order({'limit'=>5, after=>1})
|
56
|
+
|
57
|
+
# Get specific order by order_id
|
58
|
+
cbe.order('4488340..')
|
59
|
+
|
60
|
+
# Place an order (size, price, side)
|
61
|
+
cbe.place_order("0.01", "250.000", "buy")
|
62
|
+
cbe.place_order("0.02", "265.000", "sell")
|
63
|
+
|
64
|
+
# Cancel an order by order_id
|
65
|
+
cbe.cancel_order('488224434...')
|
66
|
+
|
67
|
+
# Product fills
|
68
|
+
cbe.fills
|
69
|
+
|
70
|
+
# Get a live feed from the websocket. You'll need to create a lambda to pass
|
71
|
+
messages to as they are received:
|
72
|
+
feed = CBX::Feed.new(->{|msg| puts msg.fetch('type')})
|
73
|
+
|
74
|
+
# Close the feed if needed
|
75
|
+
feed.close
|
76
|
+
|
77
|
+
|
78
|
+
```
|
79
|
+
|
80
|
+
Block syntax is fully supported
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
|
84
|
+
cbe.accounts do |response|
|
85
|
+
puts response
|
86
|
+
end
|
87
|
+
|
88
|
+
cbe.post('orders', {
|
89
|
+
|
90
|
+
"size" => 1.01,
|
91
|
+
"price" => 1.100,
|
92
|
+
"side" => "buy",
|
93
|
+
"product_id" => "BTC-USD"
|
94
|
+
}) do |response|
|
95
|
+
puts response
|
96
|
+
end
|
97
|
+
|
98
|
+
```
|
99
|
+
|
100
|
+
Use at your own risk. I assume no liability for gains or losses you incur while using this gem.
|
data/cbx.gemspec
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'cbx'
|
3
|
+
s.version = '0.0.1'
|
4
|
+
s.date = '2015-03-15'
|
5
|
+
s.summary = "Ruby wrapper for the Coinbase Exchange API"
|
6
|
+
s.description = "A complete interface to Coinbase Exchange's trading API."
|
7
|
+
s.authors = ["Michael Rodrigues","Dan Silver"]
|
8
|
+
s.email = ['mikebrodrigues@gmail.com', 'dannysilver3@gmail.com']
|
9
|
+
s.files = Dir.glob('{bin,config,lib,test,doc}/**/*') + ['cbx.gemspec']
|
10
|
+
s.extra_rdoc_files = ['README.md']
|
11
|
+
s.license = 'MIT'
|
12
|
+
s.homepage = 'https://github.com/mikerodrigues/cbx'
|
13
|
+
s.metadata = { "parent_project_homepage" => "https://github.com/dan-silver/coinbase_exchange"}
|
14
|
+
s.add_runtime_dependency 'unirest', '~> 1.1', '>= 1.1.2'
|
15
|
+
s.add_runtime_dependency 'websocket-client-simple'
|
16
|
+
end
|
data/lib/cbx.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'unirest'
|
2
|
+
require "base64"
|
3
|
+
require 'openssl'
|
4
|
+
require 'json'
|
5
|
+
require 'websocket-client-simple'
|
6
|
+
require 'cbx/feed'
|
7
|
+
require 'cbx/pagination'
|
8
|
+
require 'cbx/trading'
|
9
|
+
require 'cbx/market_data'
|
10
|
+
require 'cbx/coinbase_signature_maker'
|
11
|
+
require 'cbx/version'
|
12
|
+
|
13
|
+
class CBX
|
14
|
+
include MarketData
|
15
|
+
attr_reader :authenticated
|
16
|
+
|
17
|
+
API_URL = 'https://api.exchange.coinbase.com/'
|
18
|
+
|
19
|
+
def initialize(key=nil, secret=nil, passphrase=nil)
|
20
|
+
if key && secret && passphrase
|
21
|
+
@key = key
|
22
|
+
@secret = secret
|
23
|
+
@passphrase = passphrase
|
24
|
+
@coinbaseExchange = CBXSignatureMaker.new(key, secret, passphrase)
|
25
|
+
@authenticated = true
|
26
|
+
extend Trading
|
27
|
+
else
|
28
|
+
@authenticated = false
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def request(method, uri, json=nil)
|
33
|
+
params = json.to_json if json
|
34
|
+
if authenticated
|
35
|
+
headers = {
|
36
|
+
'CB-ACCESS-SIGN'=> @coinbaseExchange.signature('/'+uri, params, nil, method),
|
37
|
+
'CB-ACCESS-TIMESTAMP'=> Time.now.to_i,
|
38
|
+
'CB-ACCESS-KEY'=> @key,
|
39
|
+
'CB-ACCESS-PASSPHRASE'=> @passphrase,
|
40
|
+
'Content-Type' => 'application/json'
|
41
|
+
}
|
42
|
+
else
|
43
|
+
headers = { 'Content-Type' => 'application/json' }
|
44
|
+
end
|
45
|
+
|
46
|
+
if method == :get
|
47
|
+
r=Unirest.get(API_URL + uri, headers: headers)
|
48
|
+
elsif method == :post
|
49
|
+
r=Unirest.post(API_URL + uri, headers: headers, parameters: params)
|
50
|
+
elsif method == :delete
|
51
|
+
r=Unirest.delete(API_URL + uri, headers: headers, parameters: params)
|
52
|
+
end
|
53
|
+
yield r.body if block_given?
|
54
|
+
return r.body
|
55
|
+
end
|
56
|
+
|
57
|
+
def get(uri, json=nil, &block)
|
58
|
+
request(:get, uri, json, &block)
|
59
|
+
end
|
60
|
+
|
61
|
+
def post(uri, json=nil, &block)
|
62
|
+
request(:post, uri, json, &block)
|
63
|
+
end
|
64
|
+
|
65
|
+
def delete(uri, json=nil, &block)
|
66
|
+
request(:delete, uri, json, &block)
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class CBXSignatureMaker
|
2
|
+
def initialize(key, secret, passphrase)
|
3
|
+
@key = key
|
4
|
+
@secret = secret
|
5
|
+
@passphrase = passphrase
|
6
|
+
end
|
7
|
+
|
8
|
+
def signature(request_url='', body='', timestamp=nil, method='GET')
|
9
|
+
body = body.to_json if body.is_a?(Hash)
|
10
|
+
timestamp = Time.now.to_i if !timestamp
|
11
|
+
|
12
|
+
what = "#{timestamp}#{method.upcase}#{request_url}#{body}";
|
13
|
+
# create a sha256 hmac with the secret
|
14
|
+
secret = Base64.decode64(@secret)
|
15
|
+
hash = OpenSSL::HMAC.digest('sha256', secret, what)
|
16
|
+
Base64.strict_encode64(hash)
|
17
|
+
end
|
18
|
+
end
|
data/lib/cbx/feed.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
class CBX
|
2
|
+
class Feed
|
3
|
+
API_URL = 'wss://ws-feed.exchange.coinbase.com'
|
4
|
+
def initialize(on_message, on_close=nil, on_error=nil)
|
5
|
+
subscription_request = { "type" => "subscribe", "product_id" => "BTC-USD" }
|
6
|
+
ws = WebSocket::Client::Simple.connect API_URL
|
7
|
+
@ws = ws
|
8
|
+
ws.on :message do |event|
|
9
|
+
msg = JSON.parse event.data
|
10
|
+
on_message.call(msg)
|
11
|
+
end
|
12
|
+
|
13
|
+
ws.on :open do
|
14
|
+
ws.send subscription_request.to_json
|
15
|
+
end
|
16
|
+
|
17
|
+
ws.on :close do |e|
|
18
|
+
if on_close
|
19
|
+
on_close.call(e)
|
20
|
+
end
|
21
|
+
exit 1
|
22
|
+
end
|
23
|
+
|
24
|
+
ws.on :error do |e|
|
25
|
+
if on_error
|
26
|
+
on_error.call(e)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def close
|
32
|
+
@ws.close
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class CBX
|
2
|
+
module MarketData
|
3
|
+
include ::CBX::Pagination
|
4
|
+
|
5
|
+
def products(&block)
|
6
|
+
get('products', nil, &block)
|
7
|
+
end
|
8
|
+
|
9
|
+
def orderbook(level='1', product_id='BTC-USD', &block)
|
10
|
+
get('products/' + product_id + '/book?level=' + String(level), nil, &block)
|
11
|
+
end
|
12
|
+
|
13
|
+
def ticker(product_id='BTC-USD', &block)
|
14
|
+
get('products/' + product_id + '/ticker', nil, &block)
|
15
|
+
end
|
16
|
+
|
17
|
+
def trades(product_id='BTC-USD', &block)
|
18
|
+
get('products/' + product_id + '/trades', nil, &block)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/cbx/trading.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
class CBX
|
2
|
+
module Trading
|
3
|
+
include ::CBX::Pagination
|
4
|
+
|
5
|
+
# Account methods
|
6
|
+
#
|
7
|
+
def accounts(account_id=nil, &block)
|
8
|
+
if account_id.nil? then
|
9
|
+
get('accounts', nil, &block)
|
10
|
+
else
|
11
|
+
get('accounts/' + account_id.to_s, nil, &block)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def ledger(account_id, pagination = {}, &block)
|
16
|
+
get('accounts/' + account_id.to_s + '/ledger' + paginate(pagination), nil, &block)
|
17
|
+
end
|
18
|
+
|
19
|
+
def holds(account_id, pagination = {}, &block)
|
20
|
+
get('accounts/' + account_id.to_s + '/holds' + paginate(pagination), nil, &block)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Order methods
|
24
|
+
#
|
25
|
+
def orders(pagination = {}, &block)
|
26
|
+
get('orders' + paginate(pagination), nil, &block)
|
27
|
+
end
|
28
|
+
|
29
|
+
def order(order_id=nil, &block)
|
30
|
+
if order_id.nil? then
|
31
|
+
get('orders', nil, &block)
|
32
|
+
else
|
33
|
+
get('orders/' + order_id.to_s, nil, &block)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def cancel_order(order_id, &block)
|
38
|
+
delete('orders/' + order_id.to_s, nil, &block)
|
39
|
+
end
|
40
|
+
|
41
|
+
def place_order(size, price, side, product_id='BTC-USD', &block)
|
42
|
+
order = { 'size' => size, 'price' => price, 'side' => side, 'product_id' => product_id}
|
43
|
+
post('orders', order, &block)
|
44
|
+
end
|
45
|
+
|
46
|
+
# Transfer funds methods
|
47
|
+
#
|
48
|
+
def deposit(amount, coinbase_account_id, &block)
|
49
|
+
order = { 'type' => 'deposit', 'amount' => amount, 'coinbase_account_id' => coinbase_account_id }
|
50
|
+
post('orders', order, &block)
|
51
|
+
end
|
52
|
+
|
53
|
+
def withdraw(amount, coinbase_account_id, &block)
|
54
|
+
order = { 'type' => 'withdraw', 'amount' => amount, 'coinbase_account_id' => coinbase_account_id }
|
55
|
+
post('orders', order, &block)
|
56
|
+
end
|
57
|
+
|
58
|
+
# Fill methods
|
59
|
+
#
|
60
|
+
def fills(pagination = {}, &block)
|
61
|
+
get('fills' + paginate(pagination), nil, &block)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
data/lib/cbx/version.rb
ADDED
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cbx
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michael Rodrigues
|
8
|
+
- Dan Silver
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2015-03-15 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: unirest
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.1'
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.1.2
|
24
|
+
type: :runtime
|
25
|
+
prerelease: false
|
26
|
+
version_requirements: !ruby/object:Gem::Requirement
|
27
|
+
requirements:
|
28
|
+
- - "~>"
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: '1.1'
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.1.2
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: websocket-client-simple
|
36
|
+
requirement: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
type: :runtime
|
42
|
+
prerelease: false
|
43
|
+
version_requirements: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
description: A complete interface to Coinbase Exchange's trading API.
|
49
|
+
email:
|
50
|
+
- mikebrodrigues@gmail.com
|
51
|
+
- dannysilver3@gmail.com
|
52
|
+
executables: []
|
53
|
+
extensions: []
|
54
|
+
extra_rdoc_files:
|
55
|
+
- README.md
|
56
|
+
files:
|
57
|
+
- README.md
|
58
|
+
- cbx.gemspec
|
59
|
+
- lib/cbx.rb
|
60
|
+
- lib/cbx/cbx_signature_maker.rb
|
61
|
+
- lib/cbx/feed.rb
|
62
|
+
- lib/cbx/market_data.rb
|
63
|
+
- lib/cbx/pagination.rb
|
64
|
+
- lib/cbx/trading.rb
|
65
|
+
- lib/cbx/version.rb
|
66
|
+
homepage: https://github.com/mikerodrigues/cbx
|
67
|
+
licenses:
|
68
|
+
- MIT
|
69
|
+
metadata:
|
70
|
+
parent_project_homepage: https://github.com/dan-silver/coinbase_exchange
|
71
|
+
post_install_message:
|
72
|
+
rdoc_options: []
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
requirements: []
|
86
|
+
rubyforge_project:
|
87
|
+
rubygems_version: 2.4.5
|
88
|
+
signing_key:
|
89
|
+
specification_version: 4
|
90
|
+
summary: Ruby wrapper for the Coinbase Exchange API
|
91
|
+
test_files: []
|