| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Web3::Tiny::RPC; |
|
2
|
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
8
|
use strict; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
43
|
|
|
4
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
31
|
|
|
5
|
1
|
|
|
1
|
|
689
|
use HTTP::Tiny; |
|
|
1
|
|
|
|
|
40723
|
|
|
|
1
|
|
|
|
|
43
|
|
|
6
|
1
|
|
|
1
|
|
5
|
use JSON::PP qw(encode_json decode_json); |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
363
|
|
|
7
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub new { |
|
11
|
0
|
|
|
0
|
0
|
|
my ($class, %opts) = @_; |
|
12
|
0
|
0
|
|
|
|
|
die "Web3::Tiny::RPC: 'url' is required\n" unless $opts{url}; |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
my $self = { |
|
15
|
|
|
|
|
|
|
url => $opts{url}, |
|
16
|
|
|
|
|
|
|
_id => 0, |
|
17
|
|
|
|
|
|
|
_http => HTTP::Tiny->new( |
|
18
|
0
|
|
0
|
|
|
|
timeout => $opts{timeout} || 30, |
|
19
|
|
|
|
|
|
|
agent => 'Web3-Tiny/' . $VERSION, |
|
20
|
|
|
|
|
|
|
default_headers => { 'Content-Type' => 'application/json' }, |
|
21
|
|
|
|
|
|
|
), |
|
22
|
|
|
|
|
|
|
}; |
|
23
|
0
|
|
|
|
|
|
return bless $self, $class; |
|
24
|
|
|
|
|
|
|
} |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# call($method, @params) -> $result (already JSON-decoded) |
|
27
|
|
|
|
|
|
|
# dies on transport failure or a JSON-RPC "error" response. |
|
28
|
|
|
|
|
|
|
sub call { |
|
29
|
0
|
|
|
0
|
0
|
|
my ($self, $method, @params) = @_; |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
my $body = encode_json({ |
|
32
|
|
|
|
|
|
|
jsonrpc => '2.0', |
|
33
|
|
|
|
|
|
|
id => ++$self->{_id}, |
|
34
|
0
|
|
|
|
|
|
method => $method, |
|
35
|
|
|
|
|
|
|
params => [@params], |
|
36
|
|
|
|
|
|
|
}); |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
my $resp = $self->{_http}->post( |
|
39
|
|
|
|
|
|
|
$self->{url}, |
|
40
|
0
|
|
|
|
|
|
{ content => $body }, |
|
41
|
|
|
|
|
|
|
); |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
die "Web3::Tiny::RPC: HTTP request to $self->{url} failed: $resp->{status} $resp->{reason}\n" |
|
44
|
0
|
0
|
|
|
|
|
unless $resp->{success}; |
|
45
|
|
|
|
|
|
|
|
|
46
|
0
|
|
|
|
|
|
my $decoded = eval { decode_json($resp->{content}) }; |
|
|
0
|
|
|
|
|
|
|
|
47
|
0
|
0
|
|
|
|
|
die "Web3::Tiny::RPC: invalid JSON response: $@\n" if $@; |
|
48
|
|
|
|
|
|
|
|
|
49
|
0
|
0
|
|
|
|
|
if (my $err = $decoded->{error}) { |
|
50
|
0
|
|
0
|
|
|
|
my $msg = $err->{message} // 'unknown error'; |
|
51
|
0
|
|
0
|
|
|
|
my $code = $err->{code} // '?'; |
|
52
|
0
|
|
|
|
|
|
die "Web3::Tiny::RPC: $method failed [$code]: $msg\n"; |
|
53
|
|
|
|
|
|
|
} |
|
54
|
|
|
|
|
|
|
|
|
55
|
0
|
|
|
|
|
|
return $decoded->{result}; |
|
56
|
|
|
|
|
|
|
} |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
1; |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
__END__ |