Class: Sibit::Cryptoapis

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

Overview

Cryptoapis.io API.

Author

Yegor Bugayenko (yegor256@gmail.com)

Copyright

Copyright (c) 2019-2026 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Constructor Details

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

Constructor.



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

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

Instance Method Details

#balance(address) ⇒ Object

Gets the balance of the address, in satoshi.



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

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

#block(hash) ⇒ Object

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



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

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.



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

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

#height(hash) ⇒ Object

The height of the block.



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

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

#latestObject

Gets the hash of the latest block.



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

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.debug("The latest block hash is #{hash}")
  hash
end

#next_of(hash) ⇒ Object

Get hash of the block after this one.

Raises:



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

def next_of(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']
  raise(Sibit::Error, "The block #{hash} not found") if head.nil?
  nxt = head['nextblockhash']
  nxt = nil if nxt == '0000000000000000000000000000000000000000000000000000000000000000'
  @log.debug("The block #{hash} is the latest, there is no next block") if nxt.nil?
  @log.debug("The next block of #{hash} is #{nxt}") unless nxt.nil?
  nxt
end

#price(_currency = 'USD') ⇒ Object

Current price of BTC in USD (float returned).



30
31
32
# File 'lib/sibit/cryptoapis.rb', line 30

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.



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

def push(hex)
  Sibit::Json.new(http: @http, log: @log).post(
    Iri.new('https://api.cryptoapis.io/v1/bc/btc/mainnet/txs/send'),
    JSON.pretty_generate(hex: hex),
    headers: headers.merge('Content-Type' => 'application/json')
  )
end

#utxos(_sources) ⇒ Object

Fetch all unspent outputs per address.



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

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