Crypto-NanoRPC

 view release on metacpan or  search on metacpan

lib/Crypto/NanoRPC.pm  view on Meta::CPAN

        shift @_;
        while (@_) {
            my ($key,$value) = (shift,shift);
            $self->{$key} = $value;
        }
    }
    our $AUTOLOAD;
    my $action = $AUTOLOAD;
    $action =~ s/.*:://;
    if (defined $rpc_actions->{$action}) {
        my $options;
        my $json = '{"action": "'.$action.'"';
        foreach my $param (@{$rpc_actions->{$action}}) {
            return { error => "missing parameter $param" } unless defined $self->{$param};
            if (ref $param eq 'ARRAY') {
                $options = $param; next;
            }
            # assumes strings when params are not arrays
            $json .= ', "'.$param.'": "'.$self->{$param}.'"' if ref $self->{$param} ne 'ARRAY';
            $json .= ', "'.$param.'": '.$self->{$param} if ref $self->{$param} eq 'ARRAY';
        }
        if (ref $options eq 'ARRAY') {
            foreach my $option (@$options) {
                next unless defined $self->{$option};
                # assumes strings when options are not arrays
                $json .= ', "'.$option.'": "'.$self->{$option}.'"' if ref $self->{$option} ne 'ARRAY';
                $json .= ', "'.$option.'": '.$self->{$option} if ref $self->{$option} eq 'ARRAY';
            }
        }
        $json .= '}';
        return __do_rpc($self,$json);
    }
    return { error => "action $action not defined" };
}

sub rai_to_raw {
    my $rai = shift;
    return $rai * 1000000000000000000000000;
}

sub mrai_to_raw {
    my $rai = shift;
    return $rai * 1000000000000000000000000000000;
}

sub raw_to_rai {
    my $raw = shift;
    return $raw / 1000000000000000000000000;
}

sub raw_to_mrai {
    my $raw = shift;
    return $raw / 1000000000000000000000000000000;
}

sub __do_rpc {
    my ($self,$json) = @_;
    $self->{request}->content($json);
    my $response = $self->{ua}->request($self->{request});
    if ($response->is_success) {
        return decode_json($response->decoded_content);
    }
    return { error => "RPC call failed" };
}

1;
__END__

=head1 NAME

Crypto::NanoRPC - Perl module for interacting with Nano node

=head1 SYNOPSIS

  use Crypto::NanoRPC;
  $rpc = NanoRPC->new();
  $rpc = NanoRPC->new( 'http://[::1]:7076' );
 
  $rpc->set_params(
                        wallet => '000XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
                        account => 'nano_111111111111111111111111111111111111111111111111111111111111',
                  );
 
  $response = $rpc->account_balance();
  
  printf "Balance: %s\n", $response->{balance} unless defined $response->{error};


=head1 DESCRIPTION

=over 1

Object Oriented perl class for interacting with a Nano (rai) node

Implemented RPC calls are defined in the array rpc_actions in NanoRPC.pm. The required arguments can
be set using the set_params() method. The most common arguments, "wallet" and "account", have their own
set_ methods.

=back

=head1 METHODS

See L<https://docs.nano.org/commands/rpc-protocol/> for a list of RPC calls. This module implements the following RPCs:

=head2 Node RPCs

account_balance account_block_count account_get account_history account_info account_key account_representative account_weight accounts_balances accounts_frontiers accounts_pending active_difficulty available_supply block_account block_count block_co...

=head2 Wallet RPCs

account_create account_list account_move account_remove account_representative_set accounts_create password_change password_enter password_valid receive receive_minimum receive_minimum_set search_pending search_pending_all send wallet_add wallet_add_...

=head2 Unit Conversion RPCs

krai_from_raw krai_to_raw mrai_from_raw mrai_to_raw rai_from_raw rai_to_raw

=head1 DEPENDENCIES

These modules are required:

=over 1



( run in 0.693 second using v1.01-cache-2.11-cpan-39bf76dae61 )