nanook 1.0.2 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fa82959fcb3f2cd690b3a68e209a7c8ba72cb03f
4
- data.tar.gz: c56d17490352c48cfd933b8ee9ef7e270058f0d3
3
+ metadata.gz: f71938ce617e3ee52fe2fb04cf9eebe97191f5d5
4
+ data.tar.gz: 210a3c2b1ce7ad7b767c0139eaf5ae153c9e8fda
5
5
  SHA512:
6
- metadata.gz: 2763e3d7b87d63c2cd9cf4b582297fa43af8321965bd2c1ac638bc09f04aea715958ea84ec8a452a7639208dd7210219c1e7d10b9baf5d9729b05d15ea42ff6b
7
- data.tar.gz: 74b7058a9686f41b1aafa07c4432baf8df5e1a083947bc12d35237c5adf1d65e06ea3812cccf4b20582bef0e235977bd34024b2f357c9ed885f73027fde07a38
6
+ metadata.gz: 68baffb5984f1509bf4b1e6889b8c378906db8a41c070afc22902fb3369be03ee8b6db36dabb70befd79562f938380b74193a2007b033f6d2d25273871fbaa96
7
+ data.tar.gz: a6610e8c5d616b48940d4c2837bd0cc6974d84d3b6c20418eb22543719740f353bd4c5c59ef86990dd8fe75b5c0b6c9e1386beb64b505a3784298a4fd77fa24c
@@ -4,6 +4,43 @@ All notable changes to this project will be documented in this file.
4
4
  The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
5
5
  and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## 2.0.0
8
+
9
+ ### Added
10
+
11
+ - User can define `Nanook::UNIT = :raw` to set the default unit to `:raw` instead of `:nano`.
12
+ - `Nanook::Wallet#restore` to create a wallet, change its seed and create x number of accounts.
13
+ - `Nanook::WalletAccount#create` takes an optional argument to signal
14
+ how many accounts to create.
15
+ - New `Nanook::WalletAccount#change_representative` method to change an
16
+ account's representative.
17
+ - New `Nanook::Node#account_count` method to return number of known accounts in ledger.
18
+ - New `Nanook::Node#synchronizing_blocks` method to return information about "unchecked" synchronizing blocks.
19
+ - New `Nanook::Account#last_modified_at` method.
20
+ - Added ruby version requirement >= 2.0 to gemspec.
21
+
22
+ ### Changed
23
+ - `Nanook::Rpc#inspect` displays full hostname with scheme and port.
24
+ - `Nanook::Account#new` `account` param is now required.
25
+ - `Nanook::Account#info` now also returns the `id` of the account.
26
+ - `Nanook::Account#history` now returns `amount` in NANO by default, takes `unit: :raw` argument to return in raw.
27
+ - `Nanook::Account#info` now returns `balance` and `pending` in NANO by default, takes `unit: :raw` argument to return in raw.
28
+ - `Nanook::Account#exists?` now checks for open block.
29
+ - `Nanook::Account#pending` now takes additional arguments `detailed:` and `unit:`.
30
+ - `Nanook::Block#account` now returns a `Nanook::Account` instance.
31
+ - `Nanook::Block#info` now also returns the `id` of the block.
32
+ - `Nanook::Wallet#accounts` now returns `Nanook::WalletAccount` instances.
33
+ - `Nanook::Wallet#create` now returns a `Nanook::Wallet` instance.
34
+ - `Nanook::Wallet#pending` now takes additional arguments `detailed:` and `unit:`.
35
+ - `Nanook::Wallet#seed` alias method of `#id`.
36
+ - `Nanook::WalletAccount#create` now returns `Nanook::WalletAccount` instances.
37
+ - Changed documentation generating tool from rdoc to yard.
38
+
39
+ ### Fixed
40
+ - Missing `Nanook#rpc` accessor.
41
+ - `Nanook::Block#publish` can return false when publish fails.
42
+ - `Nanook::Block#info` correctly handles `allow_unchecked: true` errors.
43
+
7
44
  ## 1.0.1
8
45
 
9
46
  ### Fixed
@@ -31,6 +68,3 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
31
68
  - All pay methods continue to take NANO as the default unit, but can now
32
69
  also take an argument `unit:` which can be set to `:raw` to have the
33
70
  `amount` argument be treated as being in raw instead of NANO.
34
-
35
-
36
- ### Removed
data/README.md CHANGED
@@ -11,7 +11,7 @@ This is a Ruby library for managing a [nano currency](https://nano.org/) node, i
11
11
  Add this line to your application's Gemfile:
12
12
 
13
13
  ```ruby
14
- gem 'nanook', "~> 1.0"
14
+ gem 'nanook', "~> 2.0"
15
15
  ```
16
16
 
17
17
  And then execute:
@@ -120,17 +120,26 @@ wallet.receive(block_id, into: account_id)
120
120
 
121
121
  ## All commands
122
122
 
123
- Below is a quick reference list of commands. See the [full Nanook documentation](https://lukes.github.io/nanook/1.0.0/) for a searchable detailed description of every class and method, what the arguments mean, and example responses (Tip: expand the "**Nanook** < Object" item in the sidebar).
123
+ Below is a quick reference list of commands. See the [full Nanook documentation](https://lukes.github.io/nanook/2.0.0/) for a searchable detailed description of every class and method, what the arguments mean, and example responses (Tip: expand the "**Nanook** < Object" item in the sidebar).
124
124
 
125
125
  ### Wallets
126
126
 
127
- See the [full documentation for Nanook::Wallet](https://lukes.github.io/nanook/1.0.0/classes/Nanook/Wallet.html) for a detailed description of each method and example responses.
127
+ See the [full documentation for Nanook::Wallet](https://lukes.github.io/nanook/2.0.0/classes/Nanook/Wallet.html) for a detailed description of each method and example responses.
128
128
 
129
129
  #### Create wallet:
130
130
 
131
131
  ```ruby
132
132
  Nanook.new.wallet.create
133
133
  ```
134
+ #### Restoring a wallet from a seed
135
+
136
+ ```ruby
137
+ Nanook.new.wallet.restore(seed)
138
+ ```
139
+ Optionally also restore the wallet's accounts:
140
+ ```ruby
141
+ Nanook.new.wallet.restore(seed, accounts: 2)
142
+ ```
134
143
 
135
144
  #### Working with a single wallet:
136
145
 
@@ -142,6 +151,10 @@ wallet.balance(account_break_down: true)
142
151
  wallet.balance(unit: :raw)
143
152
  wallet.pay(from: your_account_id, to: recipient_account_id, amount: 2, id: unique_id)
144
153
  wallet.pay(from: your_account_id, to: recipient_account_id, amount: 2, unit: :raw, id: unique_id)
154
+ wallet.pending
155
+ wallet.pending(limit: 1)
156
+ wallet.pending(detailed: true)
157
+ wallet.pending(unit: :raw)
145
158
  wallet.receive(into: account_id)
146
159
  wallet.receive(pending_block_id, into: account_id)
147
160
 
@@ -164,6 +177,12 @@ wallet.destroy
164
177
  Nanook.new.wallet(wallet_id).account.create
165
178
  ```
166
179
 
180
+ #### Create multiple accounts:
181
+
182
+ ```ruby
183
+ Nanook.new.wallet(wallet_id).account.create(5)
184
+ ```
185
+
167
186
  #### Working with a single account:
168
187
 
169
188
  ```ruby
@@ -175,19 +194,25 @@ account.pay(to: recipient_account_id, amount: 2, id: unique_id)
175
194
  account.pay(to: recipient_account_id, amount: 2, unit: :raw, id: unique_id)
176
195
  account.pending
177
196
  account.pending(limit: 1)
197
+ account.pending(detailed: true)
198
+ account.pending(unit: :raw)
178
199
  account.receive
179
200
  account.receive(pending_block_id)
180
201
 
181
202
  account.exists?
182
203
  account.info
183
204
  account.info(detailed: true)
205
+ account.info(unit: :raw)
206
+ account.last_modified_at
184
207
  account.ledger
185
208
  account.ledger(limit: 10)
186
209
  account.history
187
210
  account.history(limit: 1)
211
+ account.history(unit: :raw)
188
212
  account.public_key
189
213
  account.delegators
190
214
  account.representative
215
+ account.change_representative(new_representative)
191
216
  account.weight
192
217
 
193
218
  account.destroy
@@ -195,7 +220,7 @@ account.destroy
195
220
 
196
221
  #### Working with any account (not necessarily in your wallet):
197
222
 
198
- See the [full documentation for Nanook::Account](https://lukes.github.io/nanook/1.0.0/classes/Nanook/Account.html) for a detailed description of each method and example responses.
223
+ See the [full documentation for Nanook::Account](https://lukes.github.io/nanook/2.0.0/classes/Nanook/Account.html) for a detailed description of each method and example responses.
199
224
 
200
225
  ```ruby
201
226
  account = Nanook.new.account(account_id)
@@ -204,14 +229,19 @@ account.balance
204
229
  account.balance(unit: :raw)
205
230
  account.pending
206
231
  account.pending(limit: 1)
232
+ account.pending(detailed: true)
233
+ account.pending(unit: :raw)
207
234
 
208
235
  account.exists?
209
236
  account.info
210
237
  account.info(detailed: true)
238
+ account.info(unit: :raw)
239
+ account.last_modified_at
211
240
  account.ledger
212
241
  account.ledger(limit: 10)
213
242
  account.history
214
243
  account.history(limit: 1)
244
+ account.history(unit: :raw)
215
245
  account.public_key
216
246
  account.delegators
217
247
  account.representative
@@ -220,7 +250,7 @@ account.weight
220
250
 
221
251
  ### Blocks
222
252
 
223
- See the [full documentation for Nanook::Block](https://lukes.github.io/nanook/1.0.0/classes/Nanook/Block.html) for a detailed description of each method and example responses.
253
+ See the [full documentation for Nanook::Block](https://lukes.github.io/nanook/2.0.0/classes/Nanook/Block.html) for a detailed description of each method and example responses.
224
254
 
225
255
  ```ruby
226
256
  block = Nanook.new.block(block_id)
@@ -250,14 +280,15 @@ block.is_valid_work?(work_id)
250
280
  ```ruby
251
281
  node = Nanook.new.node
252
282
 
283
+ node.account_count
253
284
  node.block_count
254
285
  node.block_count_type
255
286
  node.bootstrap_any
256
287
  node.bootstrap(address: "::ffff:138.201.94.249", port: 7075)
257
- node.frontier_count
258
288
  node.peers
259
289
  node.representatives
260
- node.representatives
290
+ node.synchronizing_blocks
291
+ node.synchronizing_blocks(limit: 1)
261
292
  node.sync_progress
262
293
  node.synced?
263
294
  node.version
@@ -310,15 +341,14 @@ To run the test suite:
310
341
 
311
342
  bundle exec rspec spec
312
343
 
313
- To update rdoc documentation:
344
+ To update the yard documentation:
314
345
 
315
- bundle exec rake rerdoc
346
+ bundle exec rake yard
316
347
 
317
348
  ## License
318
349
 
319
350
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
320
351
 
321
-
322
352
  ## Buy me a nano coffee
323
353
 
324
354
  This library is totally free to use, but feel free to send some nano [my way](https://www.nanode.co/account/xrb_3c3ek3k8135f6e8qtfy8eruk9q3yzmpebes7btzncccdest8ymzhjmnr196j) if you'd like to!
@@ -19,52 +19,106 @@ Dir[File.dirname(__FILE__) + '/nanook/*.rb'].each {|file| require file }
19
19
  # Nanook.new("http://ip6-localhost.com:7076", timeout: 600)
20
20
  class Nanook
21
21
 
22
- # ==== Arguments
23
- #
24
- # * +uri+ - RPC host to connect to (default is "http://localhost:7076")
25
- # * +timeout:+ - Connection timeout in number of seconds (default is 500)
26
- #
27
- # ==== Examples
28
- #
29
- # Nanook.new # Connect to http://localhost:7076 with 500s timeout
22
+ UNITS = [:raw, :nano]
23
+ DEFAULT_UNIT = :nano
24
+
25
+ # @return [Nanook::Rpc]
26
+ attr_reader :rpc
27
+
28
+ # @return [Symbol] the default unit for amounts to be in.
29
+ # will return {DEFAULT_UNIT} unless you define a new constant Nanook::UNIT
30
+ # (which must be one of {UNITS})
31
+ def self.default_unit
32
+ return DEFAULT_UNIT unless defined?(UNIT)
33
+
34
+ unless UNITS.include?(UNIT.to_sym)
35
+ raise Nanook::Error.new("UNIT #{UNIT} must be one of #{UNITS}")
36
+ end
37
+
38
+ UNIT.to_sym
39
+ end
40
+
41
+ # Returns a new instance of {Nanook}.
30
42
  #
43
+ # ==== Examples:
44
+ # Connecting to http://localhost:7076 with the default timeout of 600s:
45
+ # Nanook.new
46
+ # Setting a custom timeout:
31
47
  # Nanook.new(timeout: 600)
48
+ # Connecting to a custom RPC host and setting a timeout:
32
49
  # Nanook.new("http://ip6-localhost.com:7076", timeout: 600)
50
+ #
51
+ # @param uri [String] default is {Nanook::Rpc::DEFAULT_URI}. The RPC host to connect to
52
+ # @param timeout [Integer] default is {Nanook::Rpc::DEFAULT_TIMEOUT}. Connection timeout in number of seconds
33
53
  def initialize(uri=Nanook::Rpc::DEFAULT_URI, timeout:Nanook::Rpc::DEFAULT_TIMEOUT)
34
54
  @rpc = Nanook::Rpc.new(uri, timeout: timeout)
35
55
  end
36
56
 
37
- ##
38
- # Returns a Nanook::Account instance.
57
+ # Returns a new instance of {Nanook::Account}.
39
58
  #
40
- # nanook = Nanook.new
59
+ # ==== Example:
60
+ # account = Nanook.new.account("xrb_3e3j5tkog48pnny9dmfzj1r16pg8t1e76dz5tmac6iq689wyjfpi00000000")
41
61
  #
42
- # account = nanook.account
43
- # account = nanook.account("xrb_3e3j5tkog48pnny9dmfzj1r16pg8t1e76dz5tmac6iq689wyjfpi00000000")
44
- def account(account=nil)
62
+ # @param account [String] the id of the account you want to work with
63
+ # @return [Nanook::Account]
64
+ def account(account)
45
65
  Nanook::Account.new(@rpc, account)
46
66
  end
47
67
 
48
- def block(block=nil)
68
+ # Returns a new instance of {Nanook::Block}.
69
+ #
70
+ # ==== Example:
71
+ # block = Nanook.new.block("FBF8B0E6623A31AB528EBD839EEAA91CAFD25C12294C46754E45FD017F7939EB")
72
+ #
73
+ # @param block [String] the id/hash of the block you want to work with
74
+ # @return [Nanook::Block]
75
+ def block(block)
49
76
  Nanook::Block.new(@rpc, block)
50
77
  end
51
78
 
52
- def inspect # :nodoc
79
+ # @return [String]
80
+ def inspect
53
81
  "#{self.class.name}(rpc: #{@rpc.inspect}, object_id: \"#{"0x00%x" % (object_id << 1)}\")"
54
82
  end
55
83
 
84
+ # Returns a new instance of {Nanook::Key}.
85
+ #
86
+ # ==== Example:
87
+ # key = Nanook.new.key("3068BB1CA04525BB0E416C485FE6A67FD52540227D267CC8B6E8DA958A7FA039")
88
+ #
89
+ # @param key [String] a private key
90
+ # @return [Nanook::Key]
56
91
  def key(key=nil)
57
92
  Nanook::Key.new(@rpc, key)
58
93
  end
59
94
 
95
+ # Returns a new instance of {Nanook::Node}.
96
+ #
97
+ # ==== Example:
98
+ # node = Nanook.new.node
99
+ #
100
+ # @return [Nanook::Node]
60
101
  def node
61
102
  Nanook::Node.new(@rpc)
62
103
  end
63
104
 
105
+ # Returns a new instance of {Nanook::Wallet}.
106
+ #
107
+ # ==== Example:
108
+ # wallet = Nanook.new.wallet("000D1BAEC8EC208142C99059B393051BAC8380F9B5A2E6B2489A277D81789F3F")
109
+ #
110
+ # @param wallet [String] the id of the wallet you want to work with
111
+ # @return [Nanook::Wallet]
64
112
  def wallet(wallet=nil)
65
113
  Nanook::Wallet.new(@rpc, wallet)
66
114
  end
67
115
 
116
+ # Returns a new instance of {Nanook::WorkPeer}.
117
+ #
118
+ # ==== Example:
119
+ # work_peers = Nanook.new.work_peers
120
+ #
121
+ # @return [Nanook::WorkPeer]
68
122
  def work_peers
69
123
  Nanook::WorkPeer.new(@rpc)
70
124
  end
@@ -5,7 +5,7 @@ class Nanook
5
5
  #
6
6
  # === Initializing
7
7
  #
8
- # Initialize this class through the convenient Nanook#account method:
8
+ # Initialize this class through the convenient {Nanook#account} method:
9
9
  #
10
10
  # nanook = Nanook.new
11
11
  # account = nanook.account("xrb_...")
@@ -21,107 +21,143 @@ class Nanook
21
21
  @account = account
22
22
  end
23
23
 
24
- # ==== Example response
24
+ # Returns information about this account's delegators.
25
+ # === Example response:
25
26
  # {
26
- # "xrb_13bqhi1cdqq8yb9szneoc38qk899d58i5rcrgdk5mkdm86hekpoez3zxw5sd": "500000000000000000000000000000000000",
27
- # "xrb_17k6ug685154an8gri9whhe5kb5z1mf5w6y39gokc1657sh95fegm8ht1zpn": "961647970820730000000000000000000000"
27
+ # :xrb_13bqhi1cdqq8yb9szneoc38qk899d58i5rcrgdk5mkdm86hekpoez3zxw5sd=>500000000000000000000000000000000000,
28
+ # :xrb_17k6ug685154an8gri9whhe5kb5z1mf5w6y39gokc1657sh95fegm8ht1zpn=>961647970820730000000000000000000000
28
29
  # }
30
+ #
31
+ # @return [Hash{Symbol=>String}] Delegators
29
32
  def delegators
30
- account_required!
31
33
  rpc(:delegators)[:delegators]
32
34
  end
33
35
 
34
- # Returns a boolean indicating if account is known. Note, the
35
- # reliability of the check depends on the node host having
36
+ # Returns a boolean indicating if the account exists.
37
+ #
38
+ # Existence is determined by if the account has an _open_ block.
39
+ # An _open_ block is a special kind of block that gets published when
40
+ # an account receives a payment for the first time.
41
+ #
42
+ # The reliability of this check depends on the node host having
36
43
  # synchronized itself with most of the blocks on the nano network,
37
- # otherwise you may get a false +false+. You can check if a node's
38
- # synchronization is particular low using Nanook::Node#sync_progress.
44
+ # otherwise you may get +false+ when the account does exist.
45
+ # You can check if a node's synchronization is particular low
46
+ # using {Nanook::Node#sync_progress}.
39
47
  #
40
- # ==== Example response
41
- # true
48
+ # @return [Boolean] Indicates if this account exists in the nano network
42
49
  def exists?
43
- account_required!
44
- response = rpc(:validate_account_number)
45
- !response.empty? && response[:valid] == 1
50
+ response = rpc(:account_info)
51
+ !response.empty? && !response[:open_block].nil?
46
52
  end
47
53
 
48
54
  # Returns an account's history of send and receive payments.
49
- # Amounts are in {raw}[https://nano.org/en/faq#what-are-nano-units-].
50
- #
51
- # ==== Arguments
52
55
  #
53
- # [+limit:+] Integer representing the maximum number of history
54
- # items to return (default is 1000)
55
- #
56
- # ==== Example
56
+ # ==== Example:
57
57
  #
58
58
  # account.history
59
59
  # account.history(limit: 1)
60
60
  #
61
- # ==== Example response
61
+ # ==== Example response:
62
62
  # [
63
63
  # {
64
64
  # :type=>"send",
65
65
  # :account=>"xrb_1kdc5u48j3hr5r7eof9iao47szqh81ndqgq5e5hrsn1g9a3sa4hkkcotn3uq",
66
- # :amount=>200000000000000000000000000000,
66
+ # :amount=>2,
67
67
  # :hash=>"2C3C570EA8898443C0FD04A1C385A3E3A8C985AD792635FCDCEBB30ADF6A0570"
68
- # },
68
+ # }
69
+ # ]
70
+ # ==== Example:
71
+ #
72
+ # account.history
73
+ # account.history(unit: :raw)
74
+ #
75
+ # ==== Example response:
76
+ # [
69
77
  # {
70
- # :type=>"receive",
71
- # :account=>"xrb_1niabkx3gbxit5j5yyqcpas71dkffggbr6zpd3heui8rpoocm5xqbdwq44oh",
72
- # :amount=>7836413000000000000000000000000,
73
- # :hash=>"16743E8FF52F454E876E68EDD11F23094DCB96795A3B7F32F74F88563ACDDB04"
78
+ # :type=>"send",
79
+ # :account=>"xrb_1kdc5u48j3hr5r7eof9iao47szqh81ndqgq5e5hrsn1g9a3sa4hkkcotn3uq",
80
+ # :amount=>2000000000000000000000000000000,
81
+ # :hash=>"2C3C570EA8898443C0FD04A1C385A3E3A8C985AD792635FCDCEBB30ADF6A0570"
74
82
  # }
75
83
  # ]
76
- def history(limit: 1000)
77
- account_required!
78
- rpc(:account_history, count: limit)[:history]
84
+ #
85
+ # @param limit [Integer] maximum number of history items to return
86
+ # @param unit (see #balance)
87
+ # @return [Array<Hash{Symbol=>String}>] the history of send and receive payments for this account
88
+ def history(limit: 1000, unit: Nanook.default_unit)
89
+ unless Nanook::UNITS.include?(unit)
90
+ raise ArgumentError.new("Unsupported unit: #{unit}")
91
+ end
92
+
93
+ response = rpc(:account_history, count: limit)[:history]
94
+
95
+ if unit == :raw
96
+ return response
97
+ end
98
+
99
+ response.map! do |history|
100
+ history[:amount] = Nanook::Util.raw_to_NANO(history[:amount])
101
+ history
102
+ end
103
+ end
104
+
105
+ # @return [Time] last modified time of the account in UTC time zone.
106
+ def last_modified_at
107
+ response = rpc(:account_info)
108
+ Time.at(response[:modified_timestamp])
79
109
  end
80
110
 
81
111
  # Returns the public key belonging to an account.
82
112
  #
83
- # ==== Example response
113
+ # ==== Example response:
84
114
  # "3068BB1CA04525BB0E416C485FE6A67FD52540227D267CC8B6E8DA958A7FA039"
115
+ #
116
+ # @return [String] public key of this account
85
117
  def public_key
86
- account_required!
87
118
  rpc(:account_key)[:key]
88
119
  end
89
120
 
90
- # Returns a String of the representative account for the account.
121
+ # Returns the representative account for the account.
91
122
  # Representatives are accounts which cast votes in the case of a
92
123
  # fork in the network.
93
124
  #
94
125
  # ==== Example response
95
126
  #
96
127
  # "xrb_3pczxuorp48td8645bs3m6c3xotxd3idskrenmi65rbrga5zmkemzhwkaznh"
128
+ #
129
+ # @return [String] Representative account of this account
97
130
  def representative
98
- account_required!
99
131
  rpc(:account_representative)[:representative]
100
132
  end
101
133
 
102
- # Returns a Hash containing the account's balance. Units are in
103
- # {raw}[https://nano.org/en/faq#what-are-nano-units-].
134
+ # Returns a Hash containing the account's balance.
104
135
  #
105
- # [+:balance+] Account balance
106
- # [+:pending+] Amount pending and not yet received by the account
136
+ # ==== Example response:
137
+ # {
138
+ # balance=>2, # Account balance
139
+ # pending=>1.1 # Amount pending and not yet received by the account
140
+ # }
107
141
  #
108
- # ==== Arguments
142
+ # ==== Example balance returned in raw:
109
143
  #
110
- # [+unit:+] Symbol (default is +:nano+) Represents the unit that
111
- # the balances will be returned in.
112
- # Must be either +:nano+ or +:raw+. (Note: this method
113
- # interprets +:nano+ as NANO, which is technically Mnano
114
- # See {What are Nano's Units}[https://nano.org/en/faq#what-are-nano-units-])
144
+ # account.balance(unit: :raw)
115
145
  #
116
- # ===== Example response
117
146
  # {
118
- # "balance": "2",
119
- # "pending": "1"
147
+ # balance: 2000000000000000000000000000000,
148
+ # pending: 1100000000000000000000000000000
120
149
  # }
121
- def balance(unit: Nanook::WalletAccount::DEFAULT_UNIT)
122
- account_required!
123
-
124
- unless Nanook::WalletAccount::UNITS.include?(unit)
150
+ #
151
+ # @param unit [Symbol] default is {Nanook.default_unit}.
152
+ # Must be one of {Nanook::UNITS}.
153
+ # Represents the unit that the balances will be returned in.
154
+ # Note: this method interprets
155
+ # +:nano+ as NANO, which is technically Mnano
156
+ # See {https://nano.org/en/faq#what-are-nano-units- What are Nano's Units}
157
+ #
158
+ # @raise ArgumentError if an invalid +unit+ was given.
159
+ def balance(unit: Nanook.default_unit)
160
+ unless Nanook::UNITS.include?(unit)
125
161
  raise ArgumentError.new("Unsupported unit: #{unit}")
126
162
  end
127
163
 
@@ -133,35 +169,13 @@ class Nanook
133
169
  end
134
170
  end
135
171
 
136
- # Returns the id of the account
172
+ # Returns the id of the account.
173
+ # @return [String] the id of the account
137
174
  def id
138
- @block
175
+ @account
139
176
  end
140
177
 
141
- # Returns a Hash containing the following information about an
142
- # account:
143
- #
144
- # [+:frontier+] The latest block hash
145
- # [+:open_block+] The first block in every account's blockchain. When this block was published the account was officially open
146
- # [+:representative_block+] The block that named the representative for the account
147
- # [+:balance+] Amount in {NANO}[https://nano.org/en/faq#what-are-nano-units-]
148
- # [+:last_modified+] Unix timestamp
149
- # [+:block_count+] Number of blocks in the account's blockchain
150
- #
151
- # When <tt>detailed: true</tt> is passed as an argument, this method
152
- # makes four additional calls to the RPC to return more information
153
- # about an account:
154
- #
155
- # [+:weight+] See #weight
156
- # [+:pending+] See #balance
157
- # [+:representative+] See #representative
158
- # [+:public_key+] See #public_key
159
- #
160
- # ==== Arguments
161
- #
162
- # [+detailed:+] Boolean (default is false). When +true+, four
163
- # additional calls are made to the RPC to return more
164
- # information
178
+ # Returns a Hash containing information about the account.
165
179
  #
166
180
  # ==== Example 1
167
181
  #
@@ -169,6 +183,7 @@ class Nanook
169
183
  #
170
184
  # ==== Example 1 response
171
185
  # {
186
+ # :id=>"xrb_16u1uufyoig8777y6r8iqjtrw8sg8maqrm36zzcm95jmbd9i9aj5i8abr8u5"
172
187
  # :balance=>11.439597000000001,
173
188
  # :block_count=>4
174
189
  # :frontier=>"2C3C570EA8898443C0FD04A1C385A3E3A8C985AD792635FCDCEBB30ADF6A0570",
@@ -183,6 +198,7 @@ class Nanook
183
198
  #
184
199
  # ==== Example 2 response
185
200
  # {
201
+ # :id=>"xrb_16u1uufyoig8777y6r8iqjtrw8sg8maqrm36zzcm95jmbd9i9aj5i8abr8u5"
186
202
  # :balance=>11.439597000000001,
187
203
  # :block_count=>4,
188
204
  # :frontier=>"2C3C570EA8898443C0FD04A1C385A3E3A8C985AD792635FCDCEBB30ADF6A0570",
@@ -194,18 +210,46 @@ class Nanook
194
210
  # :representative_block=>"C82376314C387080A753871A32AD70F4168080C317C5E67356F0A62EB5F34FF9",
195
211
  # :weight=>0
196
212
  # }
197
- def info(detailed: false)
198
- account_required!
213
+ #
214
+ # @param detailed [Boolean] (default is false). When +true+, four
215
+ # additional calls are made to the RPC to return more information
216
+ # @param unit (see #balance)
217
+ # @return [Hash] information about the account containing:
218
+ # [+id] The account id
219
+ # [+frontier+] The latest block hash
220
+ # [+open_block+] The first block in every account's blockchain. When this block was published the account was officially open
221
+ # [+representative_block+] The block that named the representative for the account
222
+ # [+balance+] Amount in {NANO}[https://nano.org/en/faq#what-are-nano-units-]
223
+ # [+last_modified+] Unix timestamp
224
+ # [+block_count+] Number of blocks in the account's blockchain
225
+ #
226
+ # When <tt>detailed: true</tt> is passed as an argument, this method
227
+ # makes four additional calls to the RPC to return more information
228
+ # about an account:
229
+ #
230
+ # [+weight+] See {#weight}
231
+ # [+pending+] See {#balance}
232
+ # [+representative+] See {#representative}
233
+ # [+public_key+] See {#public_key}
234
+ def info(detailed: false, unit: Nanook.default_unit)
235
+ unless Nanook::UNITS.include?(unit)
236
+ raise ArgumentError.new("Unsupported unit: #{unit}")
237
+ end
199
238
 
200
239
  response = rpc(:account_info)
240
+ response.merge!(id: @account)
241
+
242
+ if unit == :nano
243
+ response[:balance] = Nanook::Util.raw_to_NANO(response[:balance])
244
+ end
201
245
 
202
246
  # Return the response if we don't need any more info
203
247
  return response unless detailed
204
248
 
205
249
  # Otherwise make additional calls
206
- response = response.merge({
250
+ response.merge!({
207
251
  weight: weight,
208
- pending: balance[:pending],
252
+ pending: balance(unit: unit)[:pending],
209
253
  representative: representative,
210
254
  public_key: public_key
211
255
  })
@@ -214,7 +258,7 @@ class Nanook
214
258
  Hash[response.sort].to_symbolized_hash
215
259
  end
216
260
 
217
- def inspect # :nodoc:
261
+ def inspect
218
262
  "#{self.class.name}(id: \"#{id}\", object_id: \"#{"0x00%x" % (object_id << 1)}\")"
219
263
  end
220
264
 
@@ -231,7 +275,7 @@ class Nanook
231
275
  #
232
276
  # ==== Example
233
277
  #
234
- # ledger(limit: 2)
278
+ # account.ledger(limit: 2)
235
279
  #
236
280
  # ==== Example response
237
281
  # {
@@ -246,64 +290,80 @@ class Nanook
246
290
  # :xrb_3c3ettq59kijuuad5fnaq35itc9schtr4r7r6rjhmwjbairowzq3wi5ap7h8=>{ ... }
247
291
  # }
248
292
  def ledger(limit: 1)
249
- account_required!
250
293
  rpc(:ledger, count: limit)[:accounts]
251
294
  end
252
295
 
253
-
254
- # Returns information about pending block hashes that are waiting to
255
- # be received by the account.
296
+ # Returns information about pending blocks (payments) that are
297
+ # waiting to be received by the account.
256
298
  #
257
- # The default response is an Array of block hashes.
258
- # With the +detailed:+ argument, the method can return a more
259
- # complex Hash containing the amount in
260
- # {raw}[https://nano.org/en/faq#what-are-nano-units-] of the pending
261
- # block and the source account that sent it.
299
+ # See also the #receive method of this class for how to receive a pending payment.
262
300
  #
263
- # ==== Arguments
301
+ # The default response is an Array of block ids.
264
302
  #
265
- # [+limit:+] Number of pending blocks to return (default is 1000)
266
- # [+detailed:+] Boolean to have this method return a more complex
267
- # Hash of pending block information (default is +false+)
303
+ # With the +detailed:+ argument, the method returns an Array of Hashes,
304
+ # which contain the source account id, amount pending and block id.
268
305
  #
269
- # ==== Example 1
306
+ # ==== Example 1:
270
307
  #
271
- # pending
308
+ # account.pending # => ["000D1BAEC8EC208142C99059B393051BAC8380F9B5A2E6B2489A277D81789F3F"]
272
309
  #
273
- # ==== Example 1 response
274
- #
275
- # ["000D1BAEC8EC208142C99059B393051BAC8380F9B5A2E6B2489A277D81789F3F"]
310
+ # ==== Example 2:
276
311
  #
277
- # ==== Example 2
312
+ # account.pending(detailed: true)
313
+ # # =>
314
+ # # [
315
+ # # {
316
+ # # :block=>"000D1BAEC8EC208142C99059B393051BAC8380F9B5A2E6B2489A277D81789F3F",
317
+ # # :amount=>6,
318
+ # # :source=>"xrb_3dcfozsmekr1tr9skf1oa5wbgmxt81qepfdnt7zicq5x3hk65fg4fqj58mbr"
319
+ # # },
320
+ # # { ... }
321
+ # # ]
278
322
  #
279
- # pending(detailed: true)
323
+ # ==== Example 3:
280
324
  #
281
- # ==== Example 2 response
325
+ # account.pending(detailed: true, unit: raw).first[:amount] # => 6000000000000000000000000000000
326
+ # @param limit [Integer] number of pending blocks to return (default is 1000)
327
+ # @param detailed [Boolean]return a more complex Hash of pending block information (default is +false+)
328
+ # @param unit (see #balance)
282
329
  #
283
- # {
284
- # "000D1BAEC8EC208142C99059B393051BAC8380F9B5A2E6B2489A277D81789F3F"=>{
285
- # "amount"=>"6000000000000000000000000000000",
286
- # "source"=>"xrb_3dcfozsmekr1tr9skf1oa5wbgmxt81qepfdnt7zicq5x3hk65fg4fqj58mbr"
287
- # }
288
- # }
289
- def pending(limit: 1000, detailed: false)
290
- account_required!
330
+ # @return [Array<String>]
331
+ # @return [Array<Hash{Symbol=>String|Integer}>]
332
+ def pending(limit: 1000, detailed: false, unit: Nanook.default_unit)
333
+ unless Nanook::UNITS.include?(unit)
334
+ raise ArgumentError.new("Unsupported unit: #{unit}")
335
+ end
291
336
 
292
- args = { count: limit }
293
- args[:source] = true if detailed
337
+ params = { count: limit }
338
+ params[:source] = true if detailed
294
339
 
295
- response = rpc(:pending, args)[:blocks]
296
- Nanook::Util.coerce_empty_string_to_type(response, (detailed ? Hash : Array))
340
+ response = rpc(:pending, params)[:blocks]
341
+ response = Nanook::Util.coerce_empty_string_to_type(response, (detailed ? Hash : Array))
342
+
343
+ return response unless detailed
344
+
345
+ response.map do |key, val|
346
+ p = val.merge(block: key.to_s)
347
+
348
+ if unit == :nano
349
+ p[:amount] = Nanook::Util.raw_to_NANO(p[:amount])
350
+ end
351
+
352
+ p
353
+ end
297
354
  end
298
355
 
299
- # Returns the account's weight. Weight is determined by the
300
- # account's balance, and represents the voting weight that account
301
- # has on the network if it is a representative.
356
+ # Returns the account's weight.
302
357
  #
303
- # ==== Example response
304
- # 1
358
+ # Weight is determined by the account's balance, and represents
359
+ # the voting weight that account has on the network. Only accounts
360
+ # with greater than 256 weight can vote.
361
+ #
362
+ # ==== Example:
363
+ # account.weight # => 0
364
+ #
365
+ # @return [Integer] the account's weight
305
366
  def weight
306
- account_required!
307
367
  rpc(:account_weight)[:weight]
308
368
  end
309
369
 
@@ -314,11 +374,5 @@ class Nanook
314
374
  @rpc.call(action, p.merge(params))
315
375
  end
316
376
 
317
- def account_required!
318
- if @account.nil?
319
- raise ArgumentError.new("Account must be present")
320
- end
321
- end
322
-
323
377
  end
324
378
  end