File Coverage

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