File Coverage

blib/lib/Catalyst/Model/Bitcoin.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package Catalyst::Model::Bitcoin;
2              
3 1     1   19531 use 5.8.0;
  1         4  
  1         37  
4 1     1   4 use strict;
  1         9  
  1         28  
5 1     1   4 use warnings;
  1         2  
  1         26  
6 1     1   521 use Moose;
  0            
  0            
7             use Finance::Bitcoin;
8             use Carp qw( croak );
9              
10             extends 'Catalyst::Model';
11              
12             our $VERSION = '0.02';
13              
14             has jsonrpc_uri => (is => 'rw');
15             has api => (is => 'rw');
16             has wallet => (is => 'rw');
17              
18             sub new {
19             my $self = shift->next::method(@_);
20             my $class = ref($self);
21             my ($c, $arg_ref) = @_;
22              
23             croak "->config->{uri} must be set for $class\n"
24             unless $self->{uri};
25              
26             $self->api( Finance::Bitcoin::API->new( endpoint => $self->{uri} ) );
27             $self->wallet( Finance::Bitcoin::Wallet->new( $self->api ) );
28              
29             return $self;
30             }
31              
32              
33             sub find {
34             my ($self, $address) = @_;
35              
36             my $address_object = Finance::Bitcoin::Address->new(
37             $self->api,
38             $address,
39             );
40              
41             return $address_object;
42             }
43              
44             sub get_received_by_address {
45             my ($self, $address) = @_;
46              
47             my $address_object = $self->find($address);
48              
49             return $address_object->received();
50             }
51              
52              
53             sub send_to_address {
54             my ($self, $address, $amount) = @_;
55              
56             # This is required to force $amount to be json-coded as real type,
57             # not string, in following JSON-RPC request
58             $amount += 0;
59            
60             return $self->wallet->pay($address, $amount);
61             }
62              
63              
64             sub get_new_address {
65             my $self = shift;
66              
67             my $address = $self->wallet->create_address();
68             return $address->address;
69             }
70              
71              
72             sub get_balance {
73             my $self = shift;
74              
75             return $self->wallet->balance();
76             }
77              
78              
79             1;
80             __END__
81              
82             =head1 NAME
83              
84             Catalyst::Model::Bitcoin - Catalyst model class that interfaces
85             with Bitcoin Server via JSON RPC
86              
87             =head1 SYNOPSIS
88              
89             Use the helper to add a Bitcoin model to your application:
90              
91             ./script/myapp_create.pl model BitcoinServer Bitcoin
92              
93             After new model created, edit config:
94              
95             # ./lib/MyApp/Model/BitcoinServer.pm
96             __PACKAGE__->config(
97             uri => 'http://rpcuser:rpcpassword@localhost:8332',
98             );
99              
100             In controller:
101              
102             # Get address object (see Finance::Bitcoin::Address)
103             my $address = $c->model('BitcoinServer')->find($address_string);
104              
105             # Send coins to address.
106             $c->model('BitcoinServer')->send_to_address($address_string, $amount);
107              
108             # Generate new address to receive coins.
109             my $new_address_string = $c->model('BitcoinServer')->get_new_address();
110              
111             # Get current wallet balance.
112             my $balance = $c->model('BitcoinServer')->get_balance();
113              
114             =head1 DESCRIPTION
115              
116             This model class uses L<Finance::Bitcoin> to access Bitcoin Server
117             via JSON RPC.
118              
119             =head1 SEE ALSO
120              
121             L<https://github.com/hippich/Catalyst--Model--Bitcoin>, L<https://www.bitcoin.org>, L<Finance::Bitcoin>, L<Catalyst>
122              
123             =head1 AUTHOR
124              
125             Pavel Karoukin
126             E<lt>pavel@yepcorp.comE<gt>
127             http://www.yepcorp.com
128              
129             =head1 COPYRIGHT AND LICENSE
130              
131             Copyright (C) 2010 by Pavel Karoukin
132              
133             This library is free software; you can redistribute it and/or modify
134             it under the same terms as Perl itself, either Perl version 5.10.1 or,
135             at your option, any later version of Perl 5 you may have available.
136              
137              
138             =cut