File Coverage

blib/lib/Finance/Bitcoin/Wallet.pm
Criterion Covered Total %
statement 17 37 45.9
branch 0 10 0.0
condition n/a
subroutine 6 10 60.0
pod 4 4 100.0
total 27 61 44.2


line stmt bran cond sub pod time code
1             package Finance::Bitcoin::Wallet;
2              
3             BEGIN {
4 1     1   3 $Finance::Bitcoin::Wallet::AUTHORITY = 'cpan:TOBYINK';
5 1         21 $Finance::Bitcoin::Wallet::VERSION = '0.902';
6             }
7              
8 1     1   22 use 5.010;
  1         4  
  1         44  
9 1     1   5 use Moo;
  1         1  
  1         6  
10 1     1   427 use Carp;
  1         1  
  1         79  
11 1     1   5 use Finance::Bitcoin;
  1         2  
  1         24  
12 1     1   5 use Scalar::Util qw( blessed );
  1         2  
  1         394  
13              
14             with "Finance::Bitcoin::Role::HasAPI";
15              
16             sub balance
17             {
18 0     0 1   my $self = shift;
19 0           return $self->api->call('getbalance');
20             }
21              
22             sub pay
23             {
24 0     0 1   my $self = shift;
25 0           my ($address, $amount) = @_;
26            
27 0 0         croak "Must provide an address" unless $address;
28 0 0         croak "Must provide an amount" unless $amount;
29            
30 0 0         $address = $address->address if blessed $address;
31            
32 0           return $self->api->call(sendtoaddress => $address, $amount);
33             }
34              
35             sub create_address
36             {
37 0     0 1   my $self = shift;
38 0           my ($label) = @_;
39            
40 0           my $address_id = $self->api->call('getnewaddress');
41 0           my $address = "Finance::Bitcoin::Address"->new($self->api, $address_id);
42 0 0         $address->label($label) if $label;
43            
44 0           return $address;
45             }
46              
47             sub addresses
48             {
49 0     0 1   my ($self) = @_;
50            
51 0           my $list = $self->api->call('listreceivedbyaddress', 0, JSON::true);
52 0 0         return unless ref($list) eq 'ARRAY';
53            
54             return
55 0           map { "Finance::Bitcoin::Address"->new($self->api, $_->{address}); }
  0            
56 0           grep { $_->{amount} > 0 }
57             @$list;
58             }
59              
60             1;
61              
62             __END__
63              
64             =head1 NAME
65              
66             Finance::Bitcoin::Wallet - a bitcoin wallet
67              
68             =head1 SYNOPSIS
69              
70             use Finance::Bitcoin;
71            
72             my $uri = 'http://user:password@127.0.0.1:8332/';
73             my $wallet = Finance::Bitcoin::Wallet->new($uri);
74            
75             print "Have: " . $wallet->balance . "\n";
76             $wallet->pay($destination_address, $amount);
77             print "Now have: " . $wallet->balance . "\n";
78            
79             foreach my $address ($wallet->addresses)
80             {
81             print $address->label . "\n";
82             }
83              
84             =head1 DESCRIPTION
85              
86             This module is part of the high-level API for accessing a running
87             Bitcoin instance.
88              
89             =over 4
90              
91             =item C<< new($endpoint) >>
92              
93             Constructor. $endpoint may be the JSON RPC endpoint URL, or may be a
94             Finance::Bitcoin::API object.
95              
96             =begin trustme
97              
98             =item BUILDARGS
99              
100             =end trustme
101              
102             =item C<< balance >>
103              
104             Returns the current balance of the wallet.
105              
106             =item C<< pay($dest, $amount) >>
107              
108             Pays some bitcoins to an account, causing the balance of the wallet to
109             decrease. $dest may be a Finance::Bitcoin::Address, or an address string.
110              
111             =item C<< addresses >>
112              
113             Returns a list of receiving addresses - i.e. addresses that can be used
114             by other people to send money to this wallet. Each item on the list is
115             a Finance::Bitcoin::Address object.
116              
117             This list may be non-exhaustive!
118              
119             =item C<< create_address($label) >>
120              
121             Creates a new receiving address - i.e. an address that can be used by
122             other people to send money to this wallet. $label is an optional
123             human-friendly name for the address. Returns a Finance::Bitcoin::Address
124             object.
125              
126             =item C<< api >>
127              
128             Retrieve a reference to the L<Finance::Bitcoin::API> object being used.
129              
130             =back
131              
132             =head1 BUGS
133              
134             Please report any bugs to L<http://rt.cpan.org/>.
135              
136             =head1 SEE ALSO
137              
138             L<Finance::Bitcoin>, L<Finance::Bitcoin::Address>.
139              
140             L<http://www.bitcoin.org/>.
141              
142             =head1 AUTHOR
143              
144             Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
145              
146             =head1 COPYRIGHT
147              
148             Copyright 2010, 2011, 2013, 2014 Toby Inkster
149              
150             This library is free software; you can redistribute it and/or modify it
151             under the same terms as Perl itself.
152              
153             =head1 DISCLAIMER OF WARRANTIES
154              
155             THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
156             WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
157             MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.