Module: Straight::GatewayModule::Includable

Defined in:
lib/straight/gateway.rb

Instance Method Summary collapse

Instance Method Details

#address_for_keychain_id(id) ⇒ Object

Returns a Base58-encoded Bitcoin address to which the payment transaction is expected to arrive. id is an an integer > 0 (hopefully not too large and hopefully the one a user of this class is going to properly increment) that is used to generate a an BIP32 bitcoin address deterministically.



89
90
91
92
# File 'lib/straight/gateway.rb', line 89

def address_for_keychain_id(id)
  # The 'm/0/n' notation is used by both Electrum and Mycelium
  keychain.node_for_path("m/0/#{id.to_s}").to_address
end

#amount_from_exchange_rate(amount, currency:, btc_denomination: :satoshi) ⇒ Object

Gets exchange rates from one of the exchange rate adapters, then calculates how much BTC does the amount in the given currency represents.

You can also feed this method various bitcoin denominations. It will always return amount in Satoshis.



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/straight/gateway.rb', line 123

def amount_from_exchange_rate(amount, currency:, btc_denomination: :satoshi)
  currency         = self.default_currency if currency.nil?
  btc_denomination = :satoshi              if btc_denomination.nil?
  currency = currency.to_s.upcase
  if currency == 'BTC'
    return Satoshi.new(amount, from_unit: btc_denomination).to_i
  end

  try_adapters(@exchange_rate_adapters, type: "exchange rate") do |a|
    a.convert_from_currency(amount, currency: currency)
  end
end

#current_exchange_rate(currency = self.default_currency) ⇒ Object



136
137
138
139
140
141
# File 'lib/straight/gateway.rb', line 136

def current_exchange_rate(currency=self.default_currency)
  currency = currency.to_s.upcase
  try_adapters(@exchange_rate_adapters, type: "exchange rate") do |a|
    a.rate_for(currency)
  end
end

#fetch_balance_for(address) ⇒ Object



102
103
104
# File 'lib/straight/gateway.rb', line 102

def fetch_balance_for(address)
  try_adapters(@blockchain_adapters, type: "blockchain") { |b| b.fetch_balance_for(address) }
end

#fetch_transaction(tid, address: nil) ⇒ Object



94
95
96
# File 'lib/straight/gateway.rb', line 94

def fetch_transaction(tid, address: nil)
  try_adapters(@blockchain_adapters, type: "blockchain") { |b| b.fetch_transaction(tid, address: address) }
end

#fetch_transactions_for(address) ⇒ Object



98
99
100
# File 'lib/straight/gateway.rb', line 98

def fetch_transactions_for(address)
  try_adapters(@blockchain_adapters, type: "blockchain") { |b| b.fetch_transactions_for(address) }
end

#keychainObject



106
107
108
# File 'lib/straight/gateway.rb', line 106

def keychain
  @keychain ||= MoneyTree::Node.from_serialized_address(pubkey)
end

#order_for_keychain_id(amount:, keychain_id:, currency: nil, btc_denomination: :satoshi) ⇒ Object

Creates a new order for the address derived from the pubkey and the keychain_id argument provided. See explanation of this keychain_id argument is in the description for the #address_for_keychain_id method.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/straight/gateway.rb', line 69

def order_for_keychain_id(amount:, keychain_id:, currency: nil, btc_denomination: :satoshi)

  amount = amount_from_exchange_rate(
    amount,
    currency:         currency,
    btc_denomination: btc_denomination
  )

  order             = Kernel.const_get(order_class).new
  order.amount      = amount
  order.gateway     = self
  order.address     = address_for_keychain_id(keychain_id)
  order.keychain_id = keychain_id
  order
end

#order_status_changed(order) ⇒ Object

This is a callback method called from each order whenever an order status changes.



112
113
114
115
116
# File 'lib/straight/gateway.rb', line 112

def order_status_changed(order)
  @order_callbacks.each do |c|
    c.call(order)
  end
end