Class: DogeCoin::Client

Inherits:
Object
  • Object
show all
Includes:
Faraday
Defined in:
lib/doge_coin/client.rb

Constant Summary collapse

VALID_FLOAT_REGEXP =
/\A(\d)+(\.\d+)?\z/

Instance Method Summary collapse

Instance Method Details

#address_balance(address) ⇒ Object

Returns the address balance (received - sent) Raise error if address is invalid



29
30
31
32
33
34
35
# File 'lib/doge_coin/client.rb', line 29

def address_balance address
  balance = call_blockchain_api("addressbalance/#{address}")

  raise DogeCoin::InvalidAddress unless is_a_float?(balance)

  balance.to_f
end

#address_to_hash(address) ⇒ Object

Shows the 160-bit hash encoded in ADDRESS. For BBE compatibility, the address is not checked for validity.



70
71
72
# File 'lib/doge_coin/client.rb', line 70

def address_to_hash address
  call_blockchain_api("addresstohash/#{address}")
end

#decode_address(address) ⇒ Object

Shows ADDRESS’s version byte(s) and public key hash as hex strings separated by colon (“:”).



87
88
89
# File 'lib/doge_coin/client.rb', line 87

def decode_address address
  call_blockchain_api("decode_address/#{address}")
end

#get_block_countObject

Returns the current block count



10
11
12
# File 'lib/doge_coin/client.rb', line 10

def get_block_count
  call_blockchain_api('getblockcount').to_i
end

#get_difficultyObject

Returns the current difficulty



14
15
16
# File 'lib/doge_coin/client.rb', line 14

def get_difficulty
  call_blockchain_api('getdifficulty').to_f
end

#get_total_minedObject

Returns the total of mined doges



18
19
20
# File 'lib/doge_coin/client.rb', line 18

def get_total_mined
  call_blockchain_api('totalbc').to_f
end

#hash_to_address(address) ⇒ Object

Converts a 160-bit hash and address version to an address.



75
76
77
# File 'lib/doge_coin/client.rb', line 75

def hash_to_address address
  call_blockchain_api("hashtoaddress/#{address}")
end

#nethash(interval = 500, start = 0, stop = false) ⇒ Object

shows statistics about difficulty and network power

/nethash/INTERVAL/START/STOP Default INTERVAL=500, START=0, STOP=infinity.

See dogechain.info/chain/Dogecoin/q/nethash



43
44
45
46
# File 'lib/doge_coin/client.rb', line 43

def nethash interval = 500, start = 0, stop = false
  suffixe = stop ? "/#{stop}" : ''
  ::JSON.parse(call_blockchain_api("nethash/#{interval}/#{start}#{suffixe}?format=json"))
end

#total_received(address) ⇒ Object

Returns the total sent Raise error if address is invalid



50
51
52
53
54
55
56
# File 'lib/doge_coin/client.rb', line 50

def total_received address
  balance = call_blockchain_api("getreceivedbyaddress/#{address}")

  raise DogeCoin::InvalidAddress unless is_a_float?(balance)

  balance.to_f
end

#total_sent(address) ⇒ Object

Returns the total sent Raise error if address is invalid



60
61
62
63
64
65
66
# File 'lib/doge_coin/client.rb', line 60

def total_sent address
  balance = call_blockchain_api("getsentbyaddress/#{address}")

  raise DogeCoin::InvalidAddress unless is_a_float?(balance)

  balance.to_f
end

#transactionsObject

Returns the amount transactions of the last blocks as an Array object



23
24
25
# File 'lib/doge_coin/client.rb', line 23

def transactions
  ::JSON.parse(call_blockchain_api("transactions"))
end

#valid_address?(address) ⇒ Boolean

Returns true if the adress is valid, and false otherwise

Returns:

  • (Boolean)


80
81
82
83
84
# File 'lib/doge_coin/client.rb', line 80

def valid_address? address
  code = call_blockchain_api("checkaddress/#{address}")

  !['X5', 'SZ', 'CK'].include?(code)
end