Class: Bitcoin::Account
- Inherits:
-
Object
- Object
- Bitcoin::Account
- Defined in:
- lib/bc.rb
Overview
This is a bitcoind account. (c.f. en.bitcoin.it/wiki/Accounts_explained)
Instance Attribute Summary collapse
-
#bc ⇒ Object
readonly
This is the Bitcoin::Client instance we are connected to.
-
#name ⇒ Object
readonly
This (String) is our account designation.
Instance Method Summary collapse
-
#addresses ⇒ Object
This is an Array of every Address associated with this account.
-
#balance ⇒ Object
Fetch the balance of this account.
-
#donate(amount) ⇒ Object
Donate
amount
to katmagic (bc’s author). -
#initialize(bc, account_name) ⇒ Account
constructor
bc
is a Bitcoin::Client instance. -
#inspect ⇒ Object
:nodoc:.
-
#new_address ⇒ Object
Get a new Address associated with this account.
-
#send(dest, amount) ⇒ Object
Send
amount
Bitcoin todest
. -
#send_to_many(dests) ⇒ Object
dests
is a Hash whose keys are Address or String instances and whose values are positive real numbers. -
#to_s ⇒ Object
:nodoc:.
-
#transactions ⇒ Object
Get every Transaction associated with this account.
-
#unused_address ⇒ Object
Get an unused Address associated with this account, or create one if one doesn’t already exist.
Constructor Details
#initialize(bc, account_name) ⇒ Account
bc
is a Bitcoin::Client instance. account_name
is the (String) name of the account we’re associated with. account_name
may be “”, in which case we represent the default account.
305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/bc.rb', line 305 def initialize(bc, account_name) @bc = bc @name = account_name.freeze unless @name.is_a?(String) raise TypeError, "account_name must be a String (#{@name.class} given)" end unless @bc.is_a?(Bitcoin::Client) raise TypeError, "bc must be a Bitcoin::Client (#{@bc.class} given)" end end |
Instance Attribute Details
#bc ⇒ Object (readonly)
This is the Bitcoin::Client instance we are connected to.
297 298 299 |
# File 'lib/bc.rb', line 297 def bc @bc end |
#name ⇒ Object (readonly)
This (String) is our account designation.
300 301 302 |
# File 'lib/bc.rb', line 300 def name @name end |
Instance Method Details
#addresses ⇒ Object
This is an Array of every Address associated with this account.
346 347 348 |
# File 'lib/bc.rb', line 346 def addresses @bc.jr.getaddressesbyaccount(@name).map(&@bc.method(:get_address)) end |
#balance ⇒ Object
Fetch the balance of this account.
341 342 343 |
# File 'lib/bc.rb', line 341 def balance @bc.jr.getbalance(@name) end |
#donate(amount) ⇒ Object
Donate amount
to katmagic (bc’s author).
436 437 438 439 440 441 442 443 444 |
# File 'lib/bc.rb', line 436 def donate(amount) tx = send('1LzDffumxiCSh8wEpxWE8fUozb2LUTcL8L', amount) if STDOUT.tty? puts('katmagic l♥ves y♥u ♥♥dles!') end tx end |
#inspect ⇒ Object
:nodoc:
450 451 452 |
# File 'lib/bc.rb', line 450 def inspect # :nodoc: "#<Bitcoin::Account #{@name.inspect}>" end |
#new_address ⇒ Object
Get a new Address associated with this account.
365 366 367 368 369 370 371 372 373 374 375 |
# File 'lib/bc.rb', line 365 def new_address begin @bc.get_address(@bc.jr.getnewaddress(@name)) rescue Jr::ServerError => ex if ex.code == -12 raise EmptyKeypool else raise end end end |
#send(dest, amount) ⇒ Object
Send amount
Bitcoin to dest
. amount
should be a positive real number; dest
can either be a (String) bitcoin address, or an Address instance. We return a Transaction.
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 |
# File 'lib/bc.rb', line 380 def send(dest, amount) dest = dest.to_s begin txid = @bc.jr.sendfrom(@name, dest, amount) rescue Jr::ServerError => ex case ex.code when -13 raise LockedWallet when -6 raise InsufficientFunds.new(amount, balance) when -5 raise InvalidAddress, dest else raise end end @bc.get_transaction(txid) end |
#send_to_many(dests) ⇒ Object
dests
is a Hash whose keys are Address or String instances and whose values are positive real numbers. Each key is sent the amount of Bitcoin specified by its value. We return a Transaction.
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 |
# File 'lib/bc.rb', line 407 def send_to_many(dests) dests = Hash[ dests.map do |dest, amount| [dest.to_s, amount] end ] begin txid = @bc.jr.sendmany(@name, dest, amount) rescue Jr::ServerError => ex case ex.code when -13 raise LockedWallet when -6 raise InsufficientFunds.new(amount.values.reduce(&:+), balance) when -5 raise InvalidAddress, ex.split(':').fetch(1) else raise end end @bc.get_transaction(txid) end |
#to_s ⇒ Object
:nodoc:
446 447 448 |
# File 'lib/bc.rb', line 446 def to_s # :nodoc: @name end |
#transactions ⇒ Object
Get every Transaction associated with this account.
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/bc.rb', line 319 def transactions grab = 20 position = 0 transactions = Array.new loop do new_transactions = @bc.jr.listtransactions(@name, grab, position) transactions += new_transactions.map{|tx| tx.fetch('txid')} if new_transactions.length < grab break else position += grab end end transactions.uniq.map do |tx| Transaction.new(@bc, tx) end end |
#unused_address ⇒ Object
Get an unused Address associated with this account, or create one if one doesn’t already exist.
352 353 354 355 356 357 358 359 360 361 362 |
# File 'lib/bc.rb', line 352 def unused_address begin @bc.get_address(@bc.jr.getaccountaddress(@name)) rescue Jr::ServerError => ex if ex.code == -12 raise EmptyKeypool else raise end end end |