Module: Straight::GatewayModule::Includable
- Defined in:
- lib/straight/gateway.rb
Instance Method Summary collapse
-
#address_for_keychain_id(id) ⇒ Object
Returns a Base58-encoded Bitcoin address to which the payment transaction is expected to arrive.
-
#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.
- #current_exchange_rate(currency = self.default_currency) ⇒ Object
- #fetch_balance_for(address) ⇒ Object
- #fetch_transaction(tid, address: nil) ⇒ Object
- #fetch_transactions_for(address) ⇒ Object
- #keychain ⇒ Object
-
#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.
-
#order_status_changed(order) ⇒ Object
This is a callback method called from each order whenever an order status changes.
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.
86 87 88 89 |
# File 'lib/straight/gateway.rb', line 86 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.
120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/straight/gateway.rb', line 120 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) do |a| a.convert_from_currency(amount, currency: currency) end end |
#current_exchange_rate(currency = self.default_currency) ⇒ Object
133 134 135 136 137 138 |
# File 'lib/straight/gateway.rb', line 133 def current_exchange_rate(currency=self.default_currency) currency = currency.to_s.upcase try_adapters(@exchange_rate_adapters) do |a| a.rate_for(currency) end end |
#fetch_balance_for(address) ⇒ Object
99 100 101 |
# File 'lib/straight/gateway.rb', line 99 def fetch_balance_for(address) try_adapters(@blockchain_adapters) { |b| b.fetch_balance_for(address) } end |
#fetch_transaction(tid, address: nil) ⇒ Object
91 92 93 |
# File 'lib/straight/gateway.rb', line 91 def fetch_transaction(tid, address: nil) try_adapters(@blockchain_adapters) { |b| b.fetch_transaction(tid, address: address) } end |
#fetch_transactions_for(address) ⇒ Object
95 96 97 |
# File 'lib/straight/gateway.rb', line 95 def fetch_transactions_for(address) try_adapters(@blockchain_adapters) { |b| b.fetch_transactions_for(address) } end |
#keychain ⇒ Object
103 104 105 |
# File 'lib/straight/gateway.rb', line 103 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.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/straight/gateway.rb', line 66 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.
109 110 111 112 113 |
# File 'lib/straight/gateway.rb', line 109 def order_status_changed(order) @order_callbacks.each do |c| c.call(order) end end |