Finance-Bitcoin

 view release on metacpan or  search on metacpan

examples/FB_low_level_api.pl  view on Meta::CPAN

#!/usr/bin/perl

use 5.010;
use Finance::Bitcoin::API;

my $creds   = shift @ARGV or die "Please provide username:password as a parameter.\n";
my $uri     = 'http://'.$creds.'@127.0.0.1:8332/';
my $api     = Finance::Bitcoin::API->new( endpoint => $uri );
my $balance = $api->call('getbalance');
say($balance || $api->error);

lib/Finance/Bitcoin/API.pm  view on Meta::CPAN

BEGIN {
	$Finance::Bitcoin::API::AUTHORITY = 'cpan:TOBYINK';
	$Finance::Bitcoin::API::VERSION   = '0.902';
}

use 5.010;
use Moo;
use JSON::RPC::Legacy::Client;
use Scalar::Util qw( blessed );

has endpoint => (is => "rw",   default => sub { "http://127.0.0.1:8332/" });
has jsonrpc  => (is => "lazy", default => sub { "JSON::RPC::Legacy::Client"->new });
has error    => (is => "rwp");

sub call
{
	my $self = shift;
	my ($method, @params) = @_;
	
	$self->_set_error(undef);
	
	my $return = $self->jsonrpc->call($self->endpoint, {
		method    => $method,
		params    => \@params,
	});
	
	if (blessed $return and $return->can('is_success') and $return->is_success)
	{
		$self->_set_error(undef);
		return $return->result;
	}
	elsif (blessed $return and $return->can('error_message'))

lib/Finance/Bitcoin/API.pm  view on Meta::CPAN


=head1 NAME

Finance::Bitcoin::API - wrapper for the Bitcoin JSON-RPC API

=head1 SYNOPSIS

 use Finance::Bitcoin::API;
 
 my $uri     = 'http://user:password@127.0.0.1:8332/';
 my $api     = Finance::Bitcoin::API->new( endpoint => $uri );
 my $balance = $api->call('getbalance');
 print $balance;

=head1 DESCRIPTION

This module provides a low-level API for accessing a running
Bitcoin instance.

=over 4

=item C<< new( %args ) >>

Constructor. %args is a hash of named arguments. You need to provide the
'endpoint' URL as an argument.

=item C<< call( $method, @params ) >>

Call a method. If successful returns the result; otherwise returns undef.

B<< Caveat: >> The protocol used to communicate with the Bitcoin daemon
is JSON-RPC based. JSON differentiates between numbers and strings:
C<< "1" >> and C<< 1 >> are considered to be different values. Perl
(mostly) does not. Thus the L<JSON> module often needs to guess whether
a parameter (i.e. an item in C<< @params >>) is supposed to be a number

lib/Finance/Bitcoin/API.pm  view on Meta::CPAN

For example:

   $api->call(
      "sendfrom",
      "$from_adr",  # Str
      "$to_adr",    # Str
      $btc + 0,     # Num
      6,            # literal: perl already knows this is a Num
   );

=item C<< endpoint >>

Get/set the endpoint URL.

=item C<< jsonrpc >>

Retrieve a reference to the L<JSON::RPC::Legacy::Client> object being used. In particular
C<< $api->jsonrpc->ua >> can be useful if you need to alter timeouts or HTTP proxy
settings.

=item C<< error >>

Returns the error message (if any) that resulted from the last C<call>.

lib/Finance/Bitcoin/Address.pm  view on Meta::CPAN

   print $address->received . "\n\n";
 }

=head1 DESCRIPTION

This module is part of the high-level API for accessing a running
Bitcoin instance.

=over 4

=item C<< new($endpoint, $string) >>

Constructor. $endpoint may be the JSON RPC endpoint URL, or may be a
Finance::Bitcoin::API object; $string is an address string.

=begin trustme

=item BUILDARGS

=end trustme

=item C<< address >>

lib/Finance/Bitcoin/Role/HasAPI.pm  view on Meta::CPAN

{
	my $orig  = shift;
	my $class = shift;
	
	if (scalar @_ == 1 and blessed $_[0])
	{
		return $class->$orig(api => @_);
	}
	elsif (scalar @_ == 1 and $_[0] =~ /^http/)
	{
		my $api = "Finance::Bitcoin::API"->new(endpoint => "$_[0]");
		return $class->$orig(api => $api);
	}
	
	return $class->$orig(@_);
};

1;

__END__

lib/Finance/Bitcoin/Wallet.pm  view on Meta::CPAN

   print $address->label . "\n";
 }

=head1 DESCRIPTION

This module is part of the high-level API for accessing a running
Bitcoin instance.

=over 4

=item C<< new($endpoint) >>

Constructor. $endpoint may be the JSON RPC endpoint URL, or may be a
Finance::Bitcoin::API object.

=begin trustme

=item BUILDARGS

=end trustme

=item C<< balance >>

t/01basic.t  view on Meta::CPAN

use Test::More tests => 2;
BEGIN { use_ok('Finance::Bitcoin') };

my $api = Finance::Bitcoin::API->new(
	endpoint => 'http://user:pass@www.example.com:1234/',
	);

is(
	$api->endpoint,
	'http://user:pass@www.example.com:1234/',
	'Accessors seem to work.',
	);

# There's not much else to do without a JSONRPC server to actually connect to.



( run in 1.477 second using v1.01-cache-2.11-cpan-2b1a40005be )