Class: Bitex::Rates
- Inherits:
-
Object
- Object
- Bitex::Rates
- Defined in:
- lib/bitex/rates.rb
Overview
Exchange Rates Tree. Example calculation for turning 1000 ARS in cash to USD credited to your bitex balance using more_mt as the funding option.
Bitex::Rates.calculate(1000, [:ars, :cash, :usd, :bitex, :more_mt])
Also available backwards: How many ARS should I deposit using more_mt to end up with 200 USD in my bitex balance?
Bitex::Rates.calculate_back([:ars, :cash, :usd, :bitex, :more_mt], 200)
Class Method Summary collapse
-
.calculate_path(value, path) ⇒ Object
rubocop:disable Metrics/AbcSize.
- .calculate_path_backwards(path, value) ⇒ Object
- .clear_tree_cache ⇒ Object
-
.path_to_calculator(path) ⇒ Object
rubocop:enable Metrics/AbcSize.
-
.tree ⇒ Object
Full exchange rates tree, gets cached locally for 60 seconds.
Class Method Details
.calculate_path(value, path) ⇒ Object
rubocop:disable Metrics/AbcSize
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/bitex/rates.rb', line 26 def self.calculate_path(value, path) value = value.to_d path_to_calculator(path).each do |step| step.symbolize_keys! case step[:type].to_sym when :exchange value *= step[:rate].to_d when :percentual_fee value *= 1 - (step[:percentage].to_d / 100.to_d) when :fixed_fee value -= step[:amount].to_d when :minimum_fee value -= [step[:minimum].to_d, value * step[:percentage].to_d / 100.to_d].max end end value end |
.calculate_path_backwards(path, value) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/bitex/rates.rb', line 44 def self.calculate_path_backwards(path, value) value = value.to_d path_to_calculator(path).each do |step| step.symbolize_keys! case step[:type].to_sym when :exchange value /= step[:rate].to_d when :percentual_fee value /= 1 - (step[:percentage].to_d / 100.to_d) when :fixed_fee value += step[:amount].to_d when :minimum_fee value = [value + step[:minimum].to_d, value / (1 - (step[:percentage].to_d / 100.to_d))].max end end value end |
.clear_tree_cache ⇒ Object
20 21 22 23 |
# File 'lib/bitex/rates.rb', line 20 def self.clear_tree_cache @tree = nil @last_tree_fetch = nil end |
.path_to_calculator(path) ⇒ Object
rubocop:enable Metrics/AbcSize
63 64 65 66 67 68 69 70 71 |
# File 'lib/bitex/rates.rb', line 63 def self.path_to_calculator(path) steps = tree begin path.each { |step| steps = steps[step] } rescue StandardError raise "InvalidPath: #{path}" end steps end |
.tree ⇒ Object
Full exchange rates tree, gets cached locally for 60 seconds.
12 13 14 15 16 17 18 |
# File 'lib/bitex/rates.rb', line 12 def self.tree if @tree.nil? || @last_tree_fetch.to_i < (Time.now.to_i - 60) @tree = Api.public('/rates/tree').deep_symbolize_keys @last_tree_fetch = Time.now.to_i end @tree end |