line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Finance::btce; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
36692
|
use 5.012004; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
39
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
37
|
|
5
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
43
|
|
6
|
1
|
|
|
1
|
|
2678
|
use JSON; |
|
1
|
|
|
|
|
21132
|
|
|
1
|
|
|
|
|
5
|
|
7
|
1
|
|
|
1
|
|
1339
|
use LWP::UserAgent; |
|
1
|
|
|
|
|
51723
|
|
|
1
|
|
|
|
|
34
|
|
8
|
1
|
|
|
1
|
|
8
|
use Carp qw(croak); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
60
|
|
9
|
1
|
|
|
1
|
|
2516
|
use Digest::SHA qw( hmac_sha512_hex); |
|
1
|
|
|
|
|
4639
|
|
|
1
|
|
|
|
|
83
|
|
10
|
1
|
|
|
1
|
|
1181
|
use WWW::Mechanize; |
|
1
|
|
|
|
|
162748
|
|
|
1
|
|
|
|
|
1336
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
require Exporter; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( 'all' => [ qw(BtceConversion BTCtoUSD LTCtoBTC LTCtoUSD getInfo) ] ); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
our @EXPORT = qw(new); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
our $VERSION = '0.1'; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
our $json = JSON->new->allow_nonref; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub BTCtoUSD |
34
|
|
|
|
|
|
|
{ |
35
|
1
|
|
|
1
|
0
|
7
|
return BtceConversion('btc_usd'); |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub LTCtoBTC |
39
|
|
|
|
|
|
|
{ |
40
|
1
|
|
|
1
|
0
|
1197
|
return BtceConversion('ltc_btc'); |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub LTCtoUSD |
44
|
|
|
|
|
|
|
{ |
45
|
1
|
|
|
1
|
0
|
1143
|
return BtceConversion('ltc_usd'); |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub BtceConversion |
49
|
|
|
|
|
|
|
{ |
50
|
3
|
|
|
3
|
0
|
9
|
my ($exchange) = @_; |
51
|
3
|
|
|
|
|
9
|
return _apiprice('Mozilla/4.76 [en] (Win98; U)', $exchange); |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
### Authenticated API calls |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub new |
58
|
|
|
|
|
|
|
{ |
59
|
1
|
|
|
1
|
0
|
1624
|
my ($class, $args) = @_; |
60
|
1
|
50
|
33
|
|
|
13
|
if($args->{'apikey'} && $args->{'secret'}) |
61
|
|
|
|
|
|
|
{ |
62
|
|
|
|
|
|
|
#check for existence of keys |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
else |
65
|
|
|
|
|
|
|
{ |
66
|
0
|
|
|
|
|
0
|
croak "You must provide an apikey and secret"; |
67
|
|
|
|
|
|
|
} |
68
|
1
|
|
|
|
|
4
|
return bless $args, $class; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub getInfo |
72
|
|
|
|
|
|
|
{ |
73
|
1
|
|
|
1
|
0
|
699
|
my ($self) = @_; |
74
|
1
|
|
|
|
|
12
|
my $mech = WWW::Mechanize->new(); |
75
|
1
|
|
|
|
|
29360
|
$mech->stack_depth(0); |
76
|
1
|
|
|
|
|
9
|
$mech->agent_alias('Windows IE 6'); |
77
|
1
|
|
|
|
|
108
|
my $url = "https://btc-e.com/tapi"; |
78
|
1
|
|
|
|
|
6
|
my $nonce = $self->_createnonce; |
79
|
1
|
|
|
|
|
11
|
my $data = "method=getInfo&nonce=".$nonce; |
80
|
1
|
|
|
|
|
5
|
my $hash = $self->_signdata($data); |
81
|
1
|
|
|
|
|
32
|
$mech->add_header('Key' => $self->_apikey); |
82
|
1
|
|
|
|
|
13
|
$mech->add_header('Sign' => $hash); |
83
|
1
|
|
|
|
|
20
|
$mech->post($url, ['method' => 'getInfo', 'nonce' => $nonce]); |
84
|
0
|
|
|
|
|
0
|
my %apireturn = %{$json->decode($mech->content())}; |
|
0
|
|
|
|
|
0
|
|
85
|
|
|
|
|
|
|
|
86
|
0
|
|
|
|
|
0
|
return \%apireturn; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub TransHistory |
90
|
|
|
|
|
|
|
{ |
91
|
0
|
|
|
0
|
0
|
0
|
my ($self, $args) = @_; |
92
|
0
|
|
|
|
|
0
|
my $data = "method=TransHistory&"; |
93
|
0
|
|
|
|
|
0
|
my %arguments = %{$args}; |
|
0
|
|
|
|
|
0
|
|
94
|
0
|
|
|
|
|
0
|
my $mech = WWW::Mechanize->new(); |
95
|
0
|
|
|
|
|
0
|
$mech->stack_depth(0); |
96
|
0
|
|
|
|
|
0
|
$mech->agent_alias('Windows IE 6'); |
97
|
0
|
|
|
|
|
0
|
my $url = "https://btc-e.com/tapi"; |
98
|
0
|
|
|
|
|
0
|
my $nonce = $self->_createnonce; |
99
|
|
|
|
|
|
|
|
100
|
0
|
|
|
|
|
0
|
foreach my $key(keys %arguments) |
101
|
|
|
|
|
|
|
{ |
102
|
0
|
|
|
|
|
0
|
$data += "$key=$arguments{$key}&"; |
103
|
|
|
|
|
|
|
} |
104
|
0
|
|
|
|
|
0
|
$data += "nonce=".$nonce; |
105
|
0
|
|
|
|
|
0
|
my $hash = $self->_signdata($data); |
106
|
0
|
|
|
|
|
0
|
$mech->add_header('Key' => $self->_apikey); |
107
|
0
|
|
|
|
|
0
|
$mech->add_header('Sign' => $hash); |
108
|
0
|
|
|
|
|
0
|
$mech->post($url, ['method' => 'TransHistory', 'nonce' => $nonce]); |
109
|
0
|
|
|
|
|
0
|
my %apireturn = %{$json->decode($mech->content())}; |
|
0
|
|
|
|
|
0
|
|
110
|
|
|
|
|
|
|
|
111
|
0
|
|
|
|
|
0
|
return \%apireturn; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
#private methods |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
sub _apikey |
117
|
|
|
|
|
|
|
{ |
118
|
1
|
|
|
1
|
|
2
|
my ($self) = @_; |
119
|
1
|
|
|
|
|
8
|
return $self->{'apikey'}; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
sub _apiprice |
123
|
|
|
|
|
|
|
{ |
124
|
3
|
|
|
3
|
|
6
|
my ($version, $exchange) = @_; |
125
|
|
|
|
|
|
|
|
126
|
3
|
|
|
|
|
9
|
my $browser = Finance::btce::_newagent($version); |
127
|
3
|
|
|
|
|
136
|
my $resp = $browser->get("https://btc-e.com/api/2/".$exchange."/ticker"); |
128
|
3
|
|
|
|
|
36100
|
my $apiresponse = $resp->content; |
129
|
3
|
|
|
|
|
34
|
my %ticker; |
130
|
3
|
|
|
|
|
6
|
eval { |
131
|
3
|
|
|
|
|
5
|
%ticker = %{$json->decode($apiresponse)}; |
|
3
|
|
|
|
|
62
|
|
132
|
|
|
|
|
|
|
}; |
133
|
3
|
50
|
|
|
|
296
|
if ($@) { |
134
|
3
|
|
|
|
|
409
|
printf STDERR "ApiPirce(%s, %s): %s\n", $version, $exchange, $@; |
135
|
3
|
|
|
|
|
8
|
my %price; |
136
|
3
|
|
|
|
|
343
|
return \%price; |
137
|
|
|
|
|
|
|
} |
138
|
0
|
|
|
|
|
0
|
my %prices = %{$ticker{'ticker'}}; |
|
0
|
|
|
|
|
0
|
|
139
|
0
|
|
|
|
|
0
|
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
|
|
|
|
|
|
|
'sell' => $prices{'sell'}, |
147
|
|
|
|
|
|
|
); |
148
|
|
|
|
|
|
|
|
149
|
0
|
|
|
|
|
0
|
return \%price; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
sub _createnonce |
153
|
|
|
|
|
|
|
{ |
154
|
1
|
|
|
1
|
|
4
|
return time; |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
sub _secretkey |
158
|
|
|
|
|
|
|
{ |
159
|
1
|
|
|
1
|
|
2
|
my ($self) = @_; |
160
|
1
|
|
|
|
|
46
|
return $self->{'secret'}; |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
sub _signdata |
164
|
|
|
|
|
|
|
{ |
165
|
1
|
|
|
1
|
|
3
|
my ($self, $params) = @_; |
166
|
1
|
|
|
|
|
6
|
return hmac_sha512_hex($params,$self->_secretkey); |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
sub _newagent |
170
|
|
|
|
|
|
|
{ |
171
|
3
|
|
|
3
|
|
4
|
my ($version) = @_; |
172
|
3
|
|
|
|
|
29
|
my $agent = LWP::UserAgent->new(ssl_opts => {verify_hostname => 1}, env_proxy => 1); |
173
|
3
|
50
|
|
|
|
8372
|
if (defined($version)) { |
174
|
3
|
|
|
|
|
13
|
$agent->agent($version); |
175
|
|
|
|
|
|
|
} |
176
|
3
|
|
|
|
|
180
|
return $agent; |
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
1; |
181
|
|
|
|
|
|
|
__END__ |