cf-trade-client 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,130 @@
1
+ =begin
2
+ Copyright 2014 Coinfloor LTD.
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ =end
16
+ require 'faye/websocket'
17
+ module Coinfloor
18
+
19
+ class CFClient
20
+ def initialize(domain,uid,pass,pkey=nil)
21
+
22
+ if domain.include?("ws://")||domain.include?("wss://")
23
+ @messages=[]
24
+ if uid==0
25
+ @con=CFAcon.new(uid,pass)
26
+ else
27
+ if pkey
28
+ @con=CFcon.new(uid,pass,pkey)
29
+ else
30
+ @con=CFcon.new(uid,pass)
31
+ end
32
+ end
33
+ @domain=domain
34
+ else
35
+ puts "domain must be a ws:// or wss:// formatted string, if port is not 80 it must be specified"
36
+ end
37
+
38
+ end
39
+ def auth(ws,msg)
40
+ @messages.push(:auth)
41
+ nonce=msg["nonce"]
42
+ result=@con.auth(nonce)
43
+ ws.send(result)
44
+ end
45
+
46
+ def begin_connection(sendata)
47
+ output=nil
48
+ EM.run {
49
+ ws = Faye::WebSocket::Client.new(@domain)
50
+ ws.on :open do |event|
51
+ end
52
+
53
+ ws.on :message do |event|
54
+ evtdata=event.data
55
+ msg=JSON.parse(evtdata)
56
+ if msg["notice"]&&msg["notice"]=="Welcome"
57
+ auth(ws,msg)
58
+ elsif msg["notice"].nil?
59
+ ret_msg=@messages.delete_at(0)
60
+ case ret_msg
61
+ when nil
62
+ ws.close
63
+ ws=nil
64
+ EM.stop
65
+ when :auth
66
+ if msg["error_code"]==0
67
+ @messages.push(:reply)
68
+ ws.send(sendata)
69
+ else
70
+ ws.close
71
+ ws=nil
72
+ EM.stop
73
+ end
74
+ when :reply
75
+ if msg["error_code"]==0
76
+ output=msg
77
+ ws.close
78
+ EM.stop
79
+ else
80
+ output=msg
81
+ ws.close
82
+ EM.stop
83
+ end
84
+ else
85
+ ws.close
86
+ EM.stop
87
+ raise "Reply for unknown action"
88
+ end
89
+ elsif msg["notice"] && (@messages.count > 0)
90
+ #we just ignore this until we get a message we want
91
+ elsif msg["notice"] && (@messages.count == 0)
92
+ ws.close
93
+ EM.stop
94
+ else
95
+ raise "Somethings gone wrong with the client"
96
+ end
97
+ end# end of on message event
98
+
99
+ ws.on :error do |event|
100
+ EM.stop
101
+ ws = nil
102
+ end
103
+
104
+ ws.on :close do |event|
105
+ EM.stop
106
+ ws = nil
107
+ end
108
+ }
109
+ return output
110
+ end
111
+
112
+ def exec(cfcon_method,arguments)
113
+
114
+ data=@con.send(cfcon_method.to_s,arguments)
115
+
116
+ begin_connection(data)
117
+ end
118
+
119
+ def get_balance
120
+ self.exec(:getbalance,{})
121
+ # data=@con.getbalance(nil)
122
+ # begin_connection(data)
123
+ end
124
+
125
+ def public_key
126
+ @con.public_key
127
+ end
128
+ end
129
+
130
+ end
@@ -0,0 +1,106 @@
1
+ =begin
2
+ Copyright 2014 Coinfloor LTD.
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ =end
16
+
17
+ require 'libecp'
18
+ require 'json'
19
+
20
+ module Coinfloor
21
+ # The Coinfloor trade API
22
+ # All method calls return json which is to be sent to the API.
23
+ class CFcon
24
+ # coinfloor user trade id, password
25
+ def initialize(uid,password,pkey=nil)
26
+ @uid=uid
27
+ @uid_byte=LibEcp.gen_uid(uid)
28
+ if pkey
29
+ @private_key=pkey
30
+ else
31
+ @password=password
32
+ @private_key=LibEcp.private_key(@uid_byte,password)
33
+
34
+ end
35
+ @public_key=LibEcp.gen_pub(@private_key)
36
+ @cookie=LibEcp.gen_cookie(@uid_byte)
37
+ @password=nil
38
+ end
39
+
40
+ attr_reader :public_key
41
+
42
+ # Authenticate using your generated private key, pass in the nonce given by the server on initial authenticate open
43
+ def auth(server_nonce)
44
+ nonce=LibEcp.gen_nonce
45
+ while nonce.bytes.count==15
46
+ nonce=LibEcp.gen_nonce
47
+ end
48
+ sigs=LibEcp.sign(@uid_byte,Base64.decode64(server_nonce),nonce,@private_key)
49
+ {
50
+ "method"=>"Authenticate",
51
+ "user_id"=>@uid,
52
+ "cookie"=>@cookie,
53
+ "nonce"=>Base64.encode64(nonce).rstrip,
54
+ "signature"=>[Base64.encode64(sigs[0]).rstrip,Base64.encode64(sigs[1]).rstrip]
55
+ }.to_json.gsub(/\s+/, "")
56
+ end
57
+
58
+ # Cancels users existing order, arguments: {:id=>order_id_integer}
59
+ def cancelorder(args)
60
+ {
61
+ :method=>"CancelOrder",
62
+ :id=>args[:id]
63
+ }.to_json
64
+ end
65
+
66
+ # return any available balance, no arguments
67
+ def getbalance(args={})
68
+ {
69
+ :method=> "GetBalances"
70
+ }.to_json
71
+ end
72
+
73
+ # return a list of open orders, no arguments
74
+ def getorders(args={})
75
+ {
76
+ :method=> "GetOrders"
77
+ }.to_json
78
+ end
79
+
80
+ # Place an order,arguments:
81
+ # {:base_asset_id=>integer,:counter_asset_id=>integer,:quantity=>integer,:price=>integer}
82
+ # quantity is positive for a buy, negative for a sell
83
+ def placeorder(arguments)
84
+
85
+ {
86
+ :method => "PlaceOrder",
87
+ :base => arguments[:base_asset_id],
88
+ :counter => arguments[:counter_asset_id],
89
+ :quantity => arguments[:quantity],
90
+ :price => arguments[:price]
91
+ }.delete_if { |k, v| v.empty? }.to_json #removes empty values that might not be given as arguments
92
+ end
93
+
94
+ # Gets a copy of the orderbook, does not subscribe
95
+ def orderbook(arguments)
96
+ {
97
+ :method => "WatchOrders",
98
+ :base => arguments[:base_asset_id],
99
+ :counter => arguments[:counter_asset_id],
100
+ :watch => false
101
+ }.to_json
102
+ end
103
+ end
104
+
105
+ end
106
+
@@ -0,0 +1,18 @@
1
+ =begin
2
+ Copyright 2014 Coinfloor LTD.
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ =end
16
+
17
+ require 'trade_api/trade_api'
18
+ require 'client/client'
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cf-trade-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Coinfloor
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-01-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: faye-websocket
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.7.2
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 0.7.2
30
+ - !ruby/object:Gem::Dependency
31
+ name: cf-ruby-libecp
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 0.1.0
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 0.1.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: json
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 1.8.1
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.8.1
62
+ description: This client allows a user to trade on the coinfloor plaform using their
63
+ API, it uses EventMachine and Faye websockets library as well as depending on the
64
+ coinfloor libecp gem
65
+ email: development@coinfloor.co.uk
66
+ executables: []
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - lib/trade_client.rb
71
+ - lib/client/client.rb
72
+ - lib/trade_api/trade_api.rb
73
+ homepage: https://github.com/coinfloor/trade-client
74
+ licenses:
75
+ - APACHE 2.0
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: 1.9.3
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 1.8.25
95
+ signing_key:
96
+ specification_version: 3
97
+ summary: A ruby client library to trade on the Coinfloor platform using their API
98
+ test_files: []
99
+ has_rdoc: