File Coverage

blib/lib/Blockchain/Ethereum/ABI/Decoder.pm
Criterion Covered Total %
statement 35 35 100.0
branch 1 2 50.0
condition 0 2 0.0
subroutine 10 10 100.0
pod 2 3 66.6
total 48 52 92.3


line stmt bran cond sub pod time code
1             package Blockchain::Ethereum::ABI::Decoder;
2              
3 5     5   344355 use v5.26;
  5         17  
4 5     5   22 use strict;
  5         8  
  5         112  
5 5     5   24 use warnings;
  5         8  
  5         401  
6              
7             # ABSTRACT: ABI utility for decoding ethereum contract arguments
8             our $AUTHORITY = 'cpan:REFECO'; # AUTHORITY
9             our $VERSION = '0.021'; # VERSION
10              
11 5     5   31 use Carp;
  5         8  
  5         405  
12              
13 5     5   1086 use Blockchain::Ethereum::ABI::Type;
  5         24  
  5         153  
14 5     5   1050 use Blockchain::Ethereum::ABI::Type::Tuple;
  5         10  
  5         1749  
15              
16             sub new {
17 3     3 0 175791 my $class = shift;
18 3         13 my $self = {
19             instances => [],
20             };
21              
22 3         15 return bless $self, $class;
23             }
24              
25             sub append {
26 10     10 1 22952 my ($self, $param) = @_;
27              
28 10         81 push $self->{instances}->@*, Blockchain::Ethereum::ABI::Type->new(signature => $param);
29 10         46 return $self;
30             }
31              
32             sub decode {
33 9     9 1 29 my ($self, $hex_data) = @_;
34              
35 9 50 0     76 croak 'Invalid hexadecimal value ' . $hex_data // 'undef'
36             unless $hex_data =~ /^(?:0x|0X)?([a-fA-F0-9]+)$/;
37              
38 9         52 my $hex = $1;
39 9         62 my @data = unpack("(A64)*", $hex);
40              
41 9         52 my $tuple = Blockchain::Ethereum::ABI::Type::Tuple->new;
42 9         31 $tuple->{instances} = $self->{instances};
43 9         21 $tuple->{data} = \@data;
44 9         48 my $data = $tuple->decode;
45              
46 9         32 $self->_clean;
47 9         141 return $data;
48             }
49              
50             sub _clean {
51 9     9   14 my $self = shift;
52              
53 9         27 $self->{instances} = [];
54             }
55              
56             1;
57              
58             __END__