bitex 0.2.2 → 0.2.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 +44 -0
- data/lib/bitex/rates.rb +66 -6
- data/lib/bitex/version.rb +1 -1
- data/spec/rates_spec.rb +23 -13
- metadata +2 -4
- data/spec/fixtures/rates.json +0 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c0f409f2edf134480559d42b2a79d57a09d4fefa
|
4
|
+
data.tar.gz: 198bf2f788ea24c148c947e5f01d2c17d96747e8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 85597c781f7eb8ef612dca37f546593ad453d8ebb507e3b2f7a6e63196c4cc9fdbf17eba2ac4cc76c3d400ba843956ff3e01557c9b869b24b052ae1ab4dd674c
|
7
|
+
data.tar.gz: 9fb13004f9956c6a4b449d3e844e43cd6c001d5e2a7c24c944186cbeb897689432814263c9fab0cda314da5dde75a85581e64d9fd232d1aad862860572a75a91
|
data/README.md
CHANGED
@@ -40,6 +40,19 @@ fetching all public market data available.
|
|
40
40
|
ruby > Bitex::BitcoinMarketData.transactions
|
41
41
|
=> [[1404501180, 1335, 632.0, 0.95], [1404501159, 1334, 632.0, 0.95], ...]
|
42
42
|
|
43
|
+
## Exchange Rates
|
44
|
+
|
45
|
+
Learn more here: [https://sandbox.bitex.la/developers#rates](Rates docs)
|
46
|
+
|
47
|
+
|
48
|
+
From 1000 ARS in CASH to USD in my BITEX balance, via MORE_MT
|
49
|
+
|
50
|
+
ruby > Bitex::Rates.calculate_path(1000, [:ars, :cash, :usd, :bitex, :more_mt])
|
51
|
+
|
52
|
+
How many ARS in CASH should I deposit via MORE_MT to get 50 USD in BITEX?
|
53
|
+
|
54
|
+
ruby > Bitex::Rates.calculate_path_backwards([:ars, :cash, :usd, :bitex, :more_mt], 50)
|
55
|
+
|
43
56
|
## Use for Private Trading
|
44
57
|
|
45
58
|
### Authentication
|
@@ -71,6 +84,37 @@ on that page. Once done you can start using it as follows:
|
|
71
84
|
|
72
85
|
ruby > Bitex::Transaction.all
|
73
86
|
|
87
|
+
# Payments
|
88
|
+
|
89
|
+
Check the [https://sandbox.bitex.la/developers#payments-overview](Payaments Developer Docs)
|
90
|
+
to learn more about payment processing with Bitex
|
91
|
+
|
92
|
+
### Create a new payment for 4 USD in Bitcoin
|
93
|
+
|
94
|
+
ruby > Bitex::Payment.create!(
|
95
|
+
currency_id: 3,
|
96
|
+
amount: 4,
|
97
|
+
callback_url: "https://example.com/ipn",
|
98
|
+
keep: 25.0,
|
99
|
+
customer_reference: "An Alto latte, no sugar",
|
100
|
+
merchant_reference: "invoice#1234")
|
101
|
+
|
102
|
+
### List all your payments
|
103
|
+
|
104
|
+
ruby > Bitex::Payment.all
|
105
|
+
|
106
|
+
### Validate and deserialize payments from callbacks.
|
107
|
+
|
108
|
+
ruby > Bitex::Payment.from_callback(the_callback_json)
|
109
|
+
|
110
|
+
### Setup your Web Point of Sale
|
111
|
+
|
112
|
+
ruby > Bitex::Payment.pos_setup!(
|
113
|
+
merchant_name: "Tierra Buena",
|
114
|
+
merchant_slug: "tierrabuena",
|
115
|
+
merchant_logo: "https://t.co/logo.png",
|
116
|
+
merchant_keep: 0)
|
117
|
+
|
74
118
|
## Sandbox
|
75
119
|
|
76
120
|
Bitex.la has a sandbox environment available at https://sandbox.bitex.la, you
|
data/lib/bitex/rates.rb
CHANGED
@@ -1,11 +1,71 @@
|
|
1
1
|
module Bitex
|
2
|
-
# Exchange
|
2
|
+
# Exchange Rates Tree.
|
3
|
+
# Example calculation for turning 1000 ARS in cash to USD credited
|
4
|
+
# to your bitex balance using more_mt as the funding option.
|
5
|
+
# Bitex::Rates.calculate(1000, [:ars, :cash, :usd, :bitex, :more_mt])
|
6
|
+
# Also available backwards: How many ARS should I deposit using more_mt
|
7
|
+
# to end up with 200 USD in my bitex balance?
|
8
|
+
# Bitex::Rates.calculate_back([:ars, :cash, :usd, :bitex, :more_mt], 200)
|
9
|
+
# @see https://bitex.la/developers#rates
|
3
10
|
class Rates
|
4
|
-
|
5
|
-
#
|
6
|
-
|
7
|
-
|
8
|
-
|
11
|
+
|
12
|
+
# Full exchange rates tree, gets cached locally for 60 seconds.
|
13
|
+
def self.tree
|
14
|
+
if @tree.nil? || @last_tree_fetch.to_i < (Time.now.to_i - 60)
|
15
|
+
@tree = Api.public("/rates/tree").deep_symbolize_keys
|
16
|
+
@last_tree_fetch = Time.now.to_i
|
17
|
+
end
|
18
|
+
@tree
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.clear_tree_cache
|
22
|
+
@tree = nil
|
23
|
+
@last_tree_fetch = nil
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.calculate_path(value, path)
|
27
|
+
value = value.to_d
|
28
|
+
path_to_calculator(path).each do |step|
|
29
|
+
case step[:type].to_sym
|
30
|
+
when :exchange
|
31
|
+
value *= step[:rate].to_d
|
32
|
+
when :percentual_fee
|
33
|
+
value *= 1 - (step[:percentage].to_d / 100.to_d)
|
34
|
+
when :fixed_fee
|
35
|
+
value -= step[:amount].to_d
|
36
|
+
when :minimum_fee
|
37
|
+
value -= [step[:minimum].to_d, value * step[:percentage].to_d / 100.to_d].max
|
38
|
+
end
|
39
|
+
end
|
40
|
+
value
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.calculate_path_backwards(path, value)
|
44
|
+
value = value.to_d
|
45
|
+
path_to_calculator(path).each do |step|
|
46
|
+
case step[:type].to_sym
|
47
|
+
when :exchange
|
48
|
+
value /= step[:rate].to_d
|
49
|
+
when :percentual_fee
|
50
|
+
value /= 1 - (step[:percentage].to_d / 100.to_d)
|
51
|
+
when :fixed_fee
|
52
|
+
value += step[:amount].to_d
|
53
|
+
when :minimum_fee
|
54
|
+
value = [value + step[:minimum].to_d,
|
55
|
+
value / (1 - (step[:percentage].to_d / 100.to_d))].max
|
56
|
+
end
|
57
|
+
end
|
58
|
+
value
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.path_to_calculator(path)
|
62
|
+
steps = tree
|
63
|
+
begin
|
64
|
+
path.each { |step| steps = steps[step] }
|
65
|
+
rescue StandardError => e
|
66
|
+
raise "InvalidPath: #{path}"
|
67
|
+
end
|
68
|
+
steps
|
9
69
|
end
|
10
70
|
end
|
11
71
|
end
|
data/lib/bitex/version.rb
CHANGED
data/spec/rates_spec.rb
CHANGED
@@ -1,19 +1,29 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Bitex::Rates do
|
4
|
-
|
4
|
+
before(:each){ Bitex::Rates.clear_tree_cache }
|
5
|
+
|
6
|
+
it "gets the full exchange rates tree and caches for 1 minute" do
|
7
|
+
Bitex::Rates.clear_tree_cache
|
5
8
|
Bitex.api_key = 'valid_api_key'
|
6
|
-
|
7
|
-
Bitex::Rates.
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
9
|
+
stub = stub_get("/rates/tree", 'rates_tree')
|
10
|
+
Bitex::Rates.tree.keys.should ==
|
11
|
+
%w(btc usd ars clp eur uyu brl cop pen mxn bob nio hnl dop gbp cad vef).collect(&:to_sym)
|
12
|
+
2.times{ Bitex::Rates.tree }
|
13
|
+
Timecop.travel 2.minutes.from_now
|
14
|
+
Bitex::Rates.tree
|
15
|
+
stub.should have_been_requested.twice
|
16
|
+
end
|
17
|
+
|
18
|
+
it "calculates a given path using the tree" do
|
19
|
+
stub_get("/rates/tree", 'rates_tree')
|
20
|
+
Bitex::Rates.calculate_path(1000, [:ars, :cash, :usd, :bitex, :more_mt])
|
21
|
+
.should == '64.332439179'.to_d
|
22
|
+
end
|
23
|
+
|
24
|
+
it "calculates backwards using a path" do
|
25
|
+
stub_get("/rates/tree", 'rates_tree')
|
26
|
+
Bitex::Rates.calculate_path_backwards([:ars, :cash, :usd, :bitex, :more_mt], 64)
|
27
|
+
.should == '994.832479799730206496'.to_d
|
18
28
|
end
|
19
29
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bitex
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nubis
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-08-
|
12
|
+
date: 2015-08-07 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -193,7 +193,6 @@ files:
|
|
193
193
|
- spec/fixtures/order_book.json
|
194
194
|
- spec/fixtures/orders.json
|
195
195
|
- spec/fixtures/profile.json
|
196
|
-
- spec/fixtures/rates.json
|
197
196
|
- spec/fixtures/specie_deposit.json
|
198
197
|
- spec/fixtures/specie_deposits.json
|
199
198
|
- spec/fixtures/specie_withdrawal.json
|
@@ -267,7 +266,6 @@ test_files:
|
|
267
266
|
- spec/fixtures/order_book.json
|
268
267
|
- spec/fixtures/orders.json
|
269
268
|
- spec/fixtures/profile.json
|
270
|
-
- spec/fixtures/rates.json
|
271
269
|
- spec/fixtures/specie_deposit.json
|
272
270
|
- spec/fixtures/specie_deposits.json
|
273
271
|
- spec/fixtures/specie_withdrawal.json
|
data/spec/fixtures/rates.json
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
{"AR":{"astropay":{"USDARS":[8.7592,null,1412969688]},"reference":{"USDARS":[null,null,0]}},"BR":{"astropay":{"USDBRL":[2.3779,null,1412969689]}},"CL":{"astropay":{"USDCLP":[611.673,null,1412969691]}},"CO":{"astropay":{"USDCOP":[2006.368,null,1412969693]}},"PE":{"astropay":{"USDPEN":[2.9807,null,1412969696]}},"UY":{"astropay":{"USDUYU":[24.751,null,1412969698]}},"MX":{"astropay":{"USDMXN":[13.7471,null,1412969700]}}}
|