File Coverage

blib/lib/Blockchain/Ethereum/ABI/Decoder.pm
Criterion Covered Total %
statement 35 35 100.0
branch 1 2 50.0
condition 2 4 50.0
subroutine 11 11 100.0
pod 2 3 66.6
total 51 55 92.7


line stmt bran cond sub pod time code
1             package Blockchain::Ethereum::ABI::Decoder;
2              
3 1     1   68666 use v5.26;
  1         13  
4 1     1   7 use strict;
  1         2  
  1         24  
5 1     1   4 use warnings;
  1         2  
  1         36  
6              
7 1     1   7 use Carp;
  1         2  
  1         74  
8              
9 1     1   445 use Blockchain::Ethereum::ABI::Type;
  1         3  
  1         31  
10 1     1   434 use Blockchain::Ethereum::ABI::Type::Tuple;
  1         3  
  1         291  
11              
12             sub new {
13 1     1 0 92 return bless {}, shift;
14             }
15              
16             sub _instances {
17 15     15   25 my $self = shift;
18 15   100     83 return $self->{instances} //= [];
19             }
20              
21             sub append {
22 8     8 1 17836 my ($self, $param) = @_;
23              
24 8         22 push $self->_instances->@*, Blockchain::Ethereum::ABI::Type::new_type(signature => $param);
25 8         44 return $self;
26             }
27              
28             sub decode {
29 7     7 1 16 my ($self, $hex_data) = @_;
30              
31 7 50 0     67 croak 'Invalid hexadecimal value ' . $hex_data // 'undef'
32             unless $hex_data =~ /^(?:0x|0X)?([a-fA-F0-9]+)$/;
33              
34 7         26 my $hex = $1;
35 7         54 my @data = unpack("(A64)*", $hex);
36              
37 7         47 my $tuple = Blockchain::Ethereum::ABI::Type::Tuple->new;
38 7         18 $tuple->{instances} = $self->_instances;
39 7         15 $tuple->{data} = \@data;
40 7         32 my $data = $tuple->decode;
41              
42 7         23 $self->_clean;
43 7         84 return $data;
44             }
45              
46             sub _clean {
47 7     7   11 my $self = shift;
48 7         17 delete $self->{instances};
49             }
50              
51             1;
52              
53             __END__