Class: Pochette::Backends::Trendy

Inherits:
Object
  • Object
show all
Defined in:
lib/pochette/backends/trendy.rb

Overview

The Trendy backend delegates calls to Toshi or BitcoinCore backends to list unspent outputs, incoming payments, etcetera. It chooses the backend to use based on its latest block, trying to always use the most up to date one. Its public instance methods are the contract to be used by any other backend, all Pochette backends must define thes public methods (Except for the initializer).

Instance Method Summary collapse

Constructor Details

#initialize(backends) ⇒ Trendy

Returns a new instance of Trendy.



9
10
11
# File 'lib/pochette/backends/trendy.rb', line 9

def initialize(backends)
  @backends = backends
end

Instance Method Details

#balances_for(addresses, confirmations) ⇒ Object

Gets the total received, spent and balance for a list of addresses. Confirmed balances are enforced to have a number of confirmation, appearing in a block is not enough. Returns a hash with: { address: [received, sent, total,

          unconfirmed_received, unconfirmed_sent, unconfirmed_total],
...}


36
37
38
# File 'lib/pochette/backends/trendy.rb', line 36

def balances_for(addresses, confirmations)
  backend.balances_for(addresses, confirmations)
end

#block_heightObject



83
84
85
# File 'lib/pochette/backends/trendy.rb', line 83

def block_height
  backend.block_height
end

#incoming_for(addresses, min_date) ⇒ Object

Lists all bitcoins received by a list of addresses after a given date. Includes both confirmed and unconfirmed transactions, unconfirmed transactions have a nil block height. Returns a list of lists as following:

amount: Amount received (in satoshis)
address: Public address receiving the amount.
txid: The hash for the transaction that received it.
confirmations: Transaction confirmations
output position: To disambiguate in case address received more than once.
sender addresses: Comma separated list of input addresses,
  used to identify deposits from trusted parties.
  can be used to identify deposits from trusted parties.


25
26
27
# File 'lib/pochette/backends/trendy.rb', line 25

def incoming_for(addresses, min_date)
  backend.incoming_for(addresses, min_date)
end

#list_transactions(txids) ⇒ Object

Gets information for the given transactions returns a list of objects, like so: [

{ hash: txid,
  version: 1,
  lock_time: 0,
  inputs: [
    { prev_hash: txid,
      prev_index: 0,
      sequence: 0,
      script_sig: hex_signature
    },
    ...
  ],
  bin_outputs: [
    { amount: amount (as satoshis),
      script_pubkey: hex_script
    },
    ...
  ]
}


75
76
77
# File 'lib/pochette/backends/trendy.rb', line 75

def list_transactions(txids)
  backend.list_transactions(txids)
end

#list_unspent(addresses) ⇒ Object

Get unspent utxos for the given addresses, returns a list of lists like so:

[address, txid, position (vout), amount (in satoshis)], …


43
44
45
46
47
48
49
50
51
52
# File 'lib/pochette/backends/trendy.rb', line 43

def list_unspent(addresses)
  backend.list_unspent(addresses)
rescue OpenURI::HTTPError => e
  # Blockchain.info returns 500 when there are no unspent outputs
  if e.io.read == "No free outputs to spend"
    return []
  else
    raise
  end
end

#pushtx(hex) ⇒ Object



79
80
81
# File 'lib/pochette/backends/trendy.rb', line 79

def pushtx(hex)
  backend.pushtx(hex)
end