Class: Sibit::Cryptoapis

Inherits:
Object
  • Object
show all
Defined in:
lib/sibit/cryptoapis.rb

Overview

Btc.com API.

Instance Method Summary collapse

Constructor Details

#initialize(key, log: Loog::NULL, http: Sibit::Http.new, dry: false) ⇒ Cryptoapis

Constructor.



24
25
26
27
28
29
# File 'lib/sibit/cryptoapis.rb', line 24

def initialize(key, log: Loog::NULL, http: Sibit::Http.new, dry: false)
  @key = key
  @http = http
  @log = log
  @dry = dry
end

Instance Method Details

#balance(address) ⇒ Object

Gets the balance of the address, in satoshi.



59
60
61
62
63
64
65
66
67
# File 'lib/sibit/cryptoapis.rb', line 59

def balance(address)
  json = Sibit::Json.new(http: @http, log: @log).get(
    Iri.new('https://api.cryptoapis.io/v1/bc/btc/mainnet/address').append(address),
    headers: headers
  )['payload']
  b = (json['balance'].to_f * 100_000_000).to_i
  @log.info("The balance of #{address} is #{b} satoshi")
  b
end

#block(hash) ⇒ Object

This method should fetch a Blockchain block and return as a hash.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/sibit/cryptoapis.rb', line 99

def block(hash)
  head = Sibit::Json.new(http: @http, log: @log).get(
    Iri.new('https://api.cryptoapis.io/v1/bc/btc/mainnet/blocks').append(hash),
    headers: headers
  )['payload']
  {
    provider: self.class.name,
    hash: head['hash'],
    orphan: false,
    next: head['nextblockhash'],
    previous: head['previousblockhash'],
    txns: txns(hash)
  }
end

#feesObject

Get recommended fees, in satoshi per byte.



70
71
72
# File 'lib/sibit/cryptoapis.rb', line 70

def fees
  raise Sibit::NotSupportedError, 'Cryptoapis doesn\'t provide recommended fees'
end

#height(hash) ⇒ Object

The height of the block.



48
49
50
51
52
53
54
55
56
# File 'lib/sibit/cryptoapis.rb', line 48

def height(hash)
  json = Sibit::Json.new(http: @http, log: @log).get(
    Iri.new('https://api.cryptoapis.io/v1/bc/btc/mainnet/blocks').append(hash),
    headers: headers
  )['payload']
  h = json['height']
  @log.info("The height of #{hash} is #{h}")
  h
end

#latestObject

Gets the hash of the latest block.



75
76
77
78
79
80
81
82
# File 'lib/sibit/cryptoapis.rb', line 75

def latest
  hash = Sibit::Json.new(http: @http, log: @log).get(
    Iri.new('https://api.cryptoapis.io/v1/bc/btc/mainnet/blocks/latest'),
    headers: headers
  )['payload']['hash']
  @log.info("The latest block hash is #{hash}")
  hash
end

#next_of(hash) ⇒ Object

Get hash of the block after this one.



37
38
39
40
41
42
43
44
45
# File 'lib/sibit/cryptoapis.rb', line 37

def next_of(hash)
  nxt = Sibit::Json.new(http: @http, log: @log).get(
    Iri.new('https://api.cryptoapis.io/v1/bc/btc/mainnet/blocks').append(hash),
    headers: headers
  )['payload']['hash']
  @log.info("The block #{hash} is the latest, there is no next block") if nxt.nil?
  @log.info("The next block of #{hash} is #{nxt}") unless nxt.nil?
  nxt
end

#price(_currency = 'USD') ⇒ Object

Current price of BTC in USD (float returned).



32
33
34
# File 'lib/sibit/cryptoapis.rb', line 32

def price(_currency = 'USD')
  raise Sibit::NotSupportedError, 'Cryptoapis doesn\'t provide BTC price'
end

#push(hex) ⇒ Object

Push this transaction (in hex format) to the network.



90
91
92
93
94
95
96
# File 'lib/sibit/cryptoapis.rb', line 90

def push(hex)
  Sibit::Json.new(http: @http, log: @log).post(
    Iri.new('https://api.cryptoapis.io/v1/bc/btc/testnet/txs/send'),
    JSON.pretty_generate(hex: hex),
    headers: headers
  )
end

#utxos(_sources) ⇒ Object

Fetch all unspent outputs per address.



85
86
87
# File 'lib/sibit/cryptoapis.rb', line 85

def utxos(_sources)
  raise Sibit::NotSupportedError, 'Not implemented yet'
end