Class: Nanook

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/nanook.rb,
lib/nanook/rpc.rb,
lib/nanook/node.rb,
lib/nanook/util.rb,
lib/nanook/block.rb,
lib/nanook/errors.rb,
lib/nanook/wallet.rb,
lib/nanook/account.rb,
lib/nanook/version.rb,
lib/nanook/work_peer.rb,
lib/nanook/public_key.rb,
lib/nanook/private_key.rb,
lib/nanook/wallet_account.rb

Overview

Initializing

Connect to the default RPC host at [::1]:7076 and with a timeout of 60 seconds:

nanook = Nanook.new

To connect to another host instead:

nanook = Nanook.new("http://ip6-localhost.com:7076")

To give a specific timeout value:

Nanook.new(timeout: 600)
Nanook.new("http://ip6-localhost.com:7076", timeout: 600)

Defined Under Namespace

Modules: Util Classes: Account, Block, Node, PrivateKey, PublicKey, Rpc, Wallet, WalletAccount, WorkPeer

Constant Summary collapse

UNITS =
%i[raw nano].freeze
DEFAULT_UNIT =
:nano
Error =
Class.new(StandardError)
ConnectionError =
Class.new(Error)
NanoUnitError =
Class.new(Error)
NodeRpcError =
Class.new(Error)
NodeRpcConfigurationError =
Class.new(NodeRpcError)
VERSION =
'3.0.0'

Constants included from Util

Util::STEP

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri = Nanook::Rpc::DEFAULT_URI, timeout: Nanook::Rpc::DEFAULT_TIMEOUT) ⇒ Nanook

Returns a new instance of Nanook.

Examples:

Connecting to [::1]:7076 with the default timeout of 60s:

Nanook.new

Setting a custom timeout:

Nanook.new(timeout: 10)

Connecting to a custom RPC host and setting a timeout:

Nanook.new("http://ip6-localhost:7076", timeout: 10)

Parameters:

  • uri (String) (defaults to: Nanook::Rpc::DEFAULT_URI)

    default is Nanook::Rpc::DEFAULT_URI. The RPC host to connect to

  • timeout (Integer) (defaults to: Nanook::Rpc::DEFAULT_TIMEOUT)

    default is Nanook::Rpc::DEFAULT_TIMEOUT. Connection timeout in number of seconds



60
61
62
# File 'lib/nanook.rb', line 60

def initialize(uri = Nanook::Rpc::DEFAULT_URI, timeout: Nanook::Rpc::DEFAULT_TIMEOUT)
  @rpc = Nanook::Rpc.new(uri, timeout: timeout)
end

Instance Attribute Details

#rpcNanook::Rpc (readonly)

Returns:



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

def rpc
  @rpc
end

Class Method Details

.default_unitSymbol

Returns the default unit for amounts to be in. will return DEFAULT_UNIT unless you define a new constant Nanook::UNIT (which must be one of UNITS).

Returns:

  • (Symbol)

    the default unit for amounts to be in. will return DEFAULT_UNIT unless you define a new constant Nanook::UNIT (which must be one of UNITS)



37
38
39
40
41
# File 'lib/nanook.rb', line 37

def self.default_unit
  return DEFAULT_UNIT unless defined?(UNIT)

  UNIT.to_sym
end

Instance Method Details

#account(account) ⇒ Nanook::Account

Returns a new instance of Account.

Example:

 = Nanook.new.("nano_3e3j5tkog48pnny9dmfzj1r16pg8t1e76dz5tmac6iq689wyjfpi00000000")

Parameters:

  • account (String)

    the id of the account you want to work with

Returns:



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

def ()
  ()
end

#block(block) ⇒ Nanook::Block

Returns a new instance of Block.

Example:

block = Nanook.new.block("FBF8B0E6623A31AB528EBD839EEAA91CAFD25C12294C46754E45FD017F7939EB")

Parameters:

  • block (String)

    the id/hash of the block you want to work with

Returns:



82
83
84
# File 'lib/nanook.rb', line 82

def block(block)
  as_block(block)
end

#network_telemetryNanook::WorkPeer

Return summarized metrics received from other nodes of the whole network.

Example:

Nanook.new.network_telemetry

Example response:

{
  block_count: 5777903,
  cemented_count: 688819,
  unchecked_count: 443468,
  account_count: 620750,
  bandwidth_cap: 1572864,
  peer_count: 32,
  protocol_version: 18,
  uptime: 556896,
  genesis_block: Nanook::Block,
  major_version: 21,
  minor_version: 0,
  patch_version: 0,
  pre_release_version: 0,
  maker: 0,
  timestamp: Time,
  active_difficulty: "ffffffcdbf40aa45"

}

Returns:



171
172
173
174
175
176
# File 'lib/nanook.rb', line 171

def network_telemetry
  response = call_rpc(:telemetry, _coerce: Hash)
  response[:genesis_block] = as_block(response[:genesis_block]) if response[:genesis_block]
  response[:timestamp] = as_time(response[:timestamp]) if response[:timestamp]
  response
end

#nodeNanook::Node

Returns a new instance of Node.

Example:

node = Nanook.new.node

Returns:



120
121
122
# File 'lib/nanook.rb', line 120

def node
  Nanook::Node.new(@rpc)
end

#private_key(key = nil) ⇒ Nanook::PrivateKey

Returns a new instance of PrivateKey.

Example:

key = Nanook.new.private_key("3068BB1CA04525BB0E416C485FE6A67FD52540227D267CC8B6E8DA958A7FA039")

Parameters:

  • key (String) (defaults to: nil)

    a private key

Returns:



99
100
101
# File 'lib/nanook.rb', line 99

def private_key(key = nil)
  as_private_key(key)
end

#public_key(key) ⇒ Nanook::PublicKey

Returns a new instance of PublicKey.

Example:

key = Nanook.new.public_key("3068BB1CA04525BB0E416C485FE6A67FD52540227D267CC8B6E8DA958A7FA039")

Parameters:

  • key (String)

    a public key

Returns:



110
111
112
# File 'lib/nanook.rb', line 110

def public_key(key)
  as_public_key(key)
end

#to_sString Also known as: inspect

Returns:

  • (String)


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

def to_s
  "#{self.class.name}(rpc: #{@rpc})"
end

#wallet(wallet = nil) ⇒ Nanook::Wallet

Returns a new instance of Wallet.

Example:

wallet = Nanook.new.wallet("000D1BAEC8EC208142C99059B393051BAC8380F9B5A2E6B2489A277D81789F3F")

Parameters:

  • wallet (String) (defaults to: nil)

    the id of the wallet you want to work with

Returns:



131
132
133
# File 'lib/nanook.rb', line 131

def wallet(wallet = nil)
  Nanook::Wallet.new(@rpc, wallet)
end

#work_peersNanook::WorkPeer

Returns a new instance of WorkPeer.

Example:

work_peers = Nanook.new.work_peers

Returns:



141
142
143
# File 'lib/nanook.rb', line 141

def work_peers
  Nanook::WorkPeer.new(@rpc)
end