Catalyst-Model-Bitcoin

 view release on metacpan or  search on metacpan

lib/Catalyst/Model/Bitcoin.pm  view on Meta::CPAN

package Catalyst::Model::Bitcoin;

use 5.8.0;
use strict;
use warnings;
use Moose;
use Finance::Bitcoin;
use Carp qw( croak ); 

extends 'Catalyst::Model';

our $VERSION = '0.02';

has jsonrpc_uri => (is => 'rw');
has api => (is => 'rw');
has wallet => (is => 'rw');

sub new {
  my $self = shift->next::method(@_);
  my $class = ref($self);
  my ($c, $arg_ref) = @_;

  croak "->config->{uri} must be set for $class\n"
    unless $self->{uri};

  $self->api( Finance::Bitcoin::API->new( endpoint => $self->{uri} ) );
  $self->wallet( Finance::Bitcoin::Wallet->new( $self->api ) );

  return $self;
}


sub find {
  my ($self, $address) = @_;

  my $address_object = Finance::Bitcoin::Address->new(
    $self->api,
    $address,
  );

  return $address_object;
}

sub get_received_by_address {
  my ($self, $address) = @_;

  my $address_object = $self->find($address);

  return $address_object->received();
}


sub send_to_address {
  my ($self, $address, $amount) = @_;

  # This is required to force $amount to be json-coded as real type,
  # not string, in following JSON-RPC request
  $amount += 0;
  
  return $self->wallet->pay($address, $amount);
}


sub get_new_address {
  my $self = shift;

  my $address = $self->wallet->create_address();
  return $address->address;
}


sub get_balance {
  my $self = shift;

  return $self->wallet->balance();
}


1;
__END__

=head1 NAME

Catalyst::Model::Bitcoin - Catalyst model class that interfaces 
with Bitcoin Server via JSON RPC



( run in 1.285 second using v1.01-cache-2.11-cpan-524268b4103 )