File Coverage

blib/lib/Blockchain/Contract/Solidity/ABI/Decoder.pm
Criterion Covered Total %
statement 41 41 100.0
branch 1 2 50.0
condition 2 4 50.0
subroutine 12 12 100.0
pod 2 3 66.6
total 58 62 93.5


line stmt bran cond sub pod time code
1             package Blockchain::Contract::Solidity::ABI::Decoder;
2              
3 1     1   67172 use v5.26;
  1         19  
4 1     1   6 use strict;
  1         2  
  1         30  
5 1     1   6 use warnings;
  1         2  
  1         23  
6 1     1   465 no indirect;
  1         1113  
  1         5  
7              
8 1     1   80 use Carp;
  1         2  
  1         84  
9              
10 1     1   476 use Blockchain::Contract::Solidity::ABI::Type;
  1         3  
  1         62  
11 1     1   470 use Blockchain::Contract::Solidity::ABI::Type::Tuple;
  1         3  
  1         444  
12              
13             sub new {
14 1     1 0 85 my ($class, %params) = @_;
15              
16 1         4 my $self = {};
17 1         3 bless $self, $class;
18 1         4 return $self;
19             }
20              
21             sub _instances {
22 15     15   22 my $self = shift;
23 15   100     90 return $self->{instances} //= [];
24             }
25              
26             sub append {
27 8     8 1 17492 my ($self, $param) = @_;
28              
29 8         24 push $self->_instances->@*, Blockchain::Contract::Solidity::ABI::Type::new_type(signature => $param);
30 8         63 return $self;
31             }
32              
33             sub decode {
34 7     7 1 26 my ($self, $hex_data) = @_;
35              
36 7 50 0     67 croak 'Invalid hexadecimal value ' . $hex_data // 'undef'
37             unless $hex_data =~ /^(?:0x|0X)?([a-fA-F0-9]+)$/;
38              
39 7         30 my $hex = $1;
40 7         51 my @data = unpack("(A64)*", $hex);
41              
42 7         46 my $tuple = Blockchain::Contract::Solidity::ABI::Type::Tuple->new;
43 7         20 $tuple->{instances} = $self->_instances;
44 7         18 $tuple->{data} = \@data;
45 7         18 my $data = $tuple->decode;
46              
47 7         24 $self->_clean;
48 7         109 return $data;
49             }
50              
51             sub _clean {
52 7     7   14 my $self = shift;
53 7         19 delete $self->{instances};
54             }
55              
56             1;
57              
58             __END__
59              
60             =pod
61              
62             =encoding UTF-8
63              
64             =head1 NAME
65              
66             Blockchain::Contract::Solidity::ABI::Decoder - Contract ABI response decoder
67              
68             =head1 SYNOPSIS
69              
70             Allows you to decode contract ABI response
71              
72             my $decoder = Blockchain::Contract::Solidity::ABI::Decoder->new();
73             $decoder
74             ->append('uint256')
75             ->append('bytes[]')
76             ->decode('0x...');
77             ...
78              
79             =head1 METHODS
80              
81             =head2 append
82              
83             Appends type signature to the decoder.
84              
85             Usage:
86              
87             append(signature) -> L<Blockchain::Contract::Solidity::ABI::Encoder>
88              
89             =over 4
90              
91             =item * C<$param> type signature e.g. uint256
92              
93             =back
94              
95             Returns C<$self>
96              
97             =head2 decode
98              
99             Decodes appended signatures
100              
101             Usage:
102              
103             decode() -> []
104              
105             =over 4
106              
107             =back
108              
109             Returns an array reference containing all decoded values
110              
111             =head1 AUTHOR
112              
113             Reginaldo Costa, C<< <refeco at cpan.org> >>
114              
115             =head1 BUGS
116              
117             Please report any bugs or feature requests to L<https://github.com/refeco/perl-ABI>
118              
119             =head1 SUPPORT
120              
121             You can find documentation for this module with the perldoc command.
122              
123             perldoc Blockchain::Contract::Solidity::ABI::Encoder
124              
125             =head1 LICENSE AND COPYRIGHT
126              
127             This software is Copyright (c) 2022 by REFECO.
128              
129             This is free software, licensed under:
130              
131             The MIT License
132              
133             =cut
134