Class: CryptocoinPayable::Adapters::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/cryptocoin_payable/adapters/base.rb

Direct Known Subclasses

Bitcoin, Ethereum

Instance Method Summary collapse

Instance Method Details

#convert_main_to_subunit(main) ⇒ Object



43
44
45
# File 'lib/cryptocoin_payable/adapters/base.rb', line 43

def convert_main_to_subunit(main)
  (main * self.class.subunit_in_main).to_i
end

#convert_subunit_to_main(subunit) ⇒ Object

Uses a predefined seed to generate HD addresses based on an index/id passed into the method. def self.create_address(id) end



39
40
41
# File 'lib/cryptocoin_payable/adapters/base.rb', line 39

def convert_subunit_to_main(subunit)
  subunit / self.class.subunit_in_main.to_f
end

#create_address(id) ⇒ Object



62
63
64
65
66
67
# File 'lib/cryptocoin_payable/adapters/base.rb', line 62

def create_address(id)
  raise MissingMasterPublicKey, 'master_public_key is required' unless coin_config.master_public_key

  master = MoneyTree::Node.from_bip32(coin_config.master_public_key)
  master.node_for_path(coin_config.node_path + id.to_s)
end

#fetch_rateObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/cryptocoin_payable/adapters/base.rb', line 47

def fetch_rate
  currency = CryptocoinPayable.configuration.currency.to_s.upcase
  symbol = self.class.coin_symbol
  amount =
    begin
      response = get_request("https://api.coinbase.com/v2/prices/#{symbol}-#{currency}/spot")
      JSON.parse(response.body)['data']['amount'].to_f
    rescue StandardError
      response = get_request("https://api.gemini.com/v1/pubticker/#{symbol}#{currency}")
      JSON.parse(response.body)['last'].to_f
    end

  (amount * 100).to_i
end