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: Sibit::Log.new, http: Sibit::Http.new, dry: false) ⇒ Cryptoapis

Constructor.



41
42
43
44
45
46
# File 'lib/sibit/cryptoapis.rb', line 41

def initialize(key, log: Sibit::Log.new, 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.



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

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.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/sibit/cryptoapis.rb', line 116

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.



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

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

#height(hash) ⇒ Object

The height of the block.



65
66
67
68
69
70
71
72
73
# File 'lib/sibit/cryptoapis.rb', line 65

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.



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

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.



54
55
56
57
58
59
60
61
62
# File 'lib/sibit/cryptoapis.rb', line 54

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).



49
50
51
# File 'lib/sibit/cryptoapis.rb', line 49

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.



107
108
109
110
111
112
113
# File 'lib/sibit/cryptoapis.rb', line 107

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.



102
103
104
# File 'lib/sibit/cryptoapis.rb', line 102

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