| 1 | package Finance::btce; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.012004; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Digest::SHA qw( hmac_sha512_hex); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use WWW::Mechanize; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | require Exporter; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 1 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | # Items to export into callers namespace by default. Note: do not export | ||||||||||||||||||||||||||||
| 17 | # names by default without a very good reason. Use EXPORT_OK instead. | ||||||||||||||||||||||||||||
| 18 | # Do not simply export all your public functions/methods/constants. | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | # This allows declaration use Finance::btce ':all'; | ||||||||||||||||||||||||||||
| 21 | # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK | ||||||||||||||||||||||||||||
| 22 | # will save memory. | ||||||||||||||||||||||||||||
| 23 | 1 | our %EXPORT_TAGS = ( 'all' => [ qw(BtceConversion BTCtoUSD LTCtoBTC LTCtoUSD getInfo) ] ); | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 1 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | our @EXPORT = qw(new); | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 1 | our $VERSION = '0.1'; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 1 | our $json = JSON->new->allow_nonref; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | sub BTCtoUSD | ||||||||||||||||||||||||||||
| 34 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
BTCtoUSD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 35 | 1 | return BtceConversion('btc_usd'); | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | sub LTCtoBTC | ||||||||||||||||||||||||||||
| 39 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
LTCtoBTC: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 40 | 1 | return BtceConversion('ltc_btc'); | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | sub LTCtoUSD | ||||||||||||||||||||||||||||
| 44 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
LTCtoUSD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 45 | 1 | return BtceConversion('ltc_usd'); | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | sub BtceConversion | ||||||||||||||||||||||||||||
| 49 | 3 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
BtceConversion: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 50 | 3 | my ($exchange) = @_; | |||||||||||||||||||||||||||
| 51 | 3 | return _apiprice('Mozilla/4.76 [en] (Win98; U)', $exchange); | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | ### Authenticated API calls | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | sub new | ||||||||||||||||||||||||||||
| 58 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=13.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 59 | 1 | my ($class, $args) = @_; | |||||||||||||||||||||||||||
| 60 | 1 | ▶ | if($args->{'apikey'} && $args->{'secret'}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$args->{"apikey"} and $args->{"secret"}
MC/DC0%0/2
$args->{"apikey"} and $args->{"secret"}
$args->{"apikey"}
$args->{"secret"}
Truth table33%1/3
A and B
A= $args->{"apikey"}B= $args->{"secret"}
| |||||||||||||||||||||||||||||
| 61 | { | ||||||||||||||||||||||||||||
| 62 | #check for existence of keys | ||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | else | ||||||||||||||||||||||||||||
| 65 | { | ||||||||||||||||||||||||||||
| 66 | 0 | croak "You must provide an apikey and secret"; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | 1 | return bless $args, $class; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | sub getInfo | ||||||||||||||||||||||||||||
| 72 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
getInfo: called CC=1 SCAR=0.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 73 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 74 | 1 | my $mech = WWW::Mechanize->new(); | |||||||||||||||||||||||||||
| 75 | 1 | $mech->stack_depth(0); | |||||||||||||||||||||||||||
| 76 | 1 | $mech->agent_alias('Windows IE 6'); | |||||||||||||||||||||||||||
| 77 | 1 | my $url = "https://btc-e.com/tapi"; | |||||||||||||||||||||||||||
| 78 | 1 | my $nonce = $self->_createnonce; | |||||||||||||||||||||||||||
| 79 | 1 | my $data = "method=getInfo&nonce=".$nonce; | |||||||||||||||||||||||||||
| 80 | 1 | my $hash = $self->_signdata($data); | |||||||||||||||||||||||||||
| 81 | 1 | $mech->add_header('Key' => $self->_apikey); | |||||||||||||||||||||||||||
| 82 | 1 | $mech->add_header('Sign' => $hash); | |||||||||||||||||||||||||||
| 83 | 1 | $mech->post($url, ['method' => 'getInfo', 'nonce' => $nonce]); | |||||||||||||||||||||||||||
| 84 | 0 | my %apireturn = %{$json->decode($mech->content())}; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | return \%apireturn; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | sub TransHistory | ||||||||||||||||||||||||||||
| 90 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
TransHistory: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 91 | 0 | my ($self, $args) = @_; | |||||||||||||||||||||||||||
| 92 | 0 | my $data = "method=TransHistory&"; | |||||||||||||||||||||||||||
| 93 | 0 | my %arguments = %{$args}; | |||||||||||||||||||||||||||
| 94 | 0 | my $mech = WWW::Mechanize->new(); | |||||||||||||||||||||||||||
| 95 | 0 | $mech->stack_depth(0); | |||||||||||||||||||||||||||
| 96 | 0 | $mech->agent_alias('Windows IE 6'); | |||||||||||||||||||||||||||
| 97 | 0 | my $url = "https://btc-e.com/tapi"; | |||||||||||||||||||||||||||
| 98 | 0 | my $nonce = $self->_createnonce; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 0 | foreach my $key(keys %arguments) | |||||||||||||||||||||||||||
| 101 | { | ||||||||||||||||||||||||||||
| 102 | 0 | $data += "$key=$arguments{$key}&"; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | 0 | $data += "nonce=".$nonce; | |||||||||||||||||||||||||||
| 105 | 0 | my $hash = $self->_signdata($data); | |||||||||||||||||||||||||||
| 106 | 0 | $mech->add_header('Key' => $self->_apikey); | |||||||||||||||||||||||||||
| 107 | 0 | $mech->add_header('Sign' => $hash); | |||||||||||||||||||||||||||
| 108 | 0 | $mech->post($url, ['method' => 'TransHistory', 'nonce' => $nonce]); | |||||||||||||||||||||||||||
| 109 | 0 | my %apireturn = %{$json->decode($mech->content())}; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 0 | return \%apireturn; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | #private methods | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | sub _apikey | ||||||||||||||||||||||||||||
| 117 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_apikey: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 118 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 119 | 1 | return $self->{'apikey'}; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | sub _apiprice | ||||||||||||||||||||||||||||
| 123 | 3 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_apiprice: called CC=2 SCAR=7.4
| |||||||||||||||||||||||||||||
| 124 | 3 | my ($version, $exchange) = @_; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 3 | my $browser = Finance::btce::_newagent($version); | |||||||||||||||||||||||||||
| 127 | 3 | my $resp = $browser->get("https://btc-e.com/api/2/".$exchange."/ticker"); | |||||||||||||||||||||||||||
| 128 | 3 | my $apiresponse = $resp->content; | |||||||||||||||||||||||||||
| 129 | 3 | my %ticker; | |||||||||||||||||||||||||||
| 130 | 3 | eval { | |||||||||||||||||||||||||||
| 131 | 3 | %ticker = %{$json->decode($apiresponse)}; | |||||||||||||||||||||||||||
| 132 | }; | ||||||||||||||||||||||||||||
| 133 | 3 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 3 | printf STDERR "ApiPirce(%s, %s): %s\n", $version, $exchange, $@; | |||||||||||||||||||||||||||
| 135 | 3 | my %price; | |||||||||||||||||||||||||||
| 136 | 3 | return \%price; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | 0 | my %prices = %{$ticker{'ticker'}}; | |||||||||||||||||||||||||||
| 139 | my %price = ( | ||||||||||||||||||||||||||||
| 140 | 'updated' => $prices{'updated'}, | ||||||||||||||||||||||||||||
| 141 | 'last' => $prices{'last'}, | ||||||||||||||||||||||||||||
| 142 | 'high' => $prices{'high'}, | ||||||||||||||||||||||||||||
| 143 | 'low' => $prices{'low'}, | ||||||||||||||||||||||||||||
| 144 | 'avg' => $prices{'avg'}, | ||||||||||||||||||||||||||||
| 145 | 'buy' => $prices{'buy'}, | ||||||||||||||||||||||||||||
| 146 | 0 | 'sell' => $prices{'sell'}, | |||||||||||||||||||||||||||
| 147 | ); | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 0 | return \%price; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | sub _createnonce | ||||||||||||||||||||||||||||
| 153 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_createnonce: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 154 | 1 | return time; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | sub _secretkey | ||||||||||||||||||||||||||||
| 158 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_secretkey: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 159 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 160 | 1 | return $self->{'secret'}; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | sub _signdata | ||||||||||||||||||||||||||||
| 164 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_signdata: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 165 | 1 | my ($self, $params) = @_; | |||||||||||||||||||||||||||
| 166 | 1 | return hmac_sha512_hex($params,$self->_secretkey); | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | sub _newagent | ||||||||||||||||||||||||||||
| 170 | 3 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_newagent: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 171 | 3 | my ($version) = @_; | |||||||||||||||||||||||||||
| 172 | 3 | my $agent = LWP::UserAgent->new(ssl_opts => {verify_hostname => 1}, env_proxy => 1); | |||||||||||||||||||||||||||
| 173 | 3 | ▶ | if (defined($version)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 3 | $agent->agent($version); | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | 3 | return $agent; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 1 | 1; | |||||||||||||||||||||||||||
| 181 | __END__ | ||||||||||||||||||||||||||||