File Coverage

blib/lib/REFECO/Blockchain/Contract/Solidity/ABI/Decoder.pm
Criterion Covered Total %
statement 37 39 94.8
branch 1 2 50.0
condition 2 4 50.0
subroutine 11 12 91.6
pod 2 6 33.3
total 53 63 84.1


line stmt bran cond sub pod time code
1              
2             use strict;
3 1     1   55700 use warnings;
  1         11  
  1         25  
4 1     1   5 no indirect;
  1         2  
  1         22  
5 1     1   372  
  1         907  
  1         5  
6             =head1 NAME
7              
8             REFECO::Blockchain::Contract::Solidity::ABI::Decoder - Contract Application Binary Interface argument decoder
9              
10             =head1 VERSION
11              
12             Version 0.001
13              
14             =cut
15              
16             our $VERSION = '0.001';
17              
18             =head1 SYNOPSIS
19              
20             The Contract Application Binary Interface (ABI) is the standard way to interact
21             with contracts (Ethereum), this module aims to be an utility to encode the given
22             data according ABI type specification.
23              
24             my $decoder = REFECO::Blockchain::Contract::Solidity::ABI::Decoder->new();
25             $decoder
26             ->append('uint256')
27             ->append('bytes[]')
28             ->decode('0x...');
29             ...
30              
31             =head1 AUTHOR
32              
33             Reginaldo Costa, C<< <refeco at cpan.org> >>
34              
35             =head1 BUGS
36              
37             Please report any bugs or feature requests to C<bug-refeco-blockchain-smartcontracts-solidity-abi-encoder at rt.cpan.org>, or through
38             the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=REFECO-Blockchain-Contract-Solidity-ABI-Encoder>. I will be notified, and then you'll
39             automatically be notified of progress on your bug as I make changes.
40              
41             =head1 SUPPORT
42              
43             You can find documentation for this module with the perldoc command.
44              
45             perldoc REFECO::Blockchain::Contract::Solidity::ABI::Decoder
46              
47              
48             You can also look for information at:
49              
50             =over 4
51              
52             =item * RT: CPAN's request tracker (report bugs here)
53              
54             L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=REFECO-Blockchain-Contract-Solidity-ABI-Encoder>
55              
56             =item * CPAN Ratings
57              
58             L<https://cpanratings.perl.org/d/REFECO-Blockchain-Contract-Solidity-ABI-Encoder>
59              
60             =item * Search CPAN
61              
62             L<https://metacpan.org/release/REFECO-Blockchain-Contract-Solidity-ABI-Encoder>
63              
64             =back
65              
66              
67             =head1 ACKNOWLEDGEMENTS
68              
69              
70             =head1 LICENSE AND COPYRIGHT
71              
72             This software is Copyright (c) 2022 by Reginaldo Costa.
73              
74             This is free software, licensed under:
75              
76             The Artistic License 2.0 (GPL Compatible)
77              
78             =cut
79              
80             use Carp;
81 1     1   67 use REFECO::Blockchain::Contract::Solidity::ABI::Type;
  1         2  
  1         63  
82 1     1   372 use REFECO::Blockchain::Contract::Solidity::ABI::Type::Tuple;
  1         3  
  1         36  
83 1     1   367  
  1         14  
  1         383  
84             my ($class, %params) = @_;
85              
86 1     1 0 82 my $self = {};
87             bless $self, $class;
88 1         3 return $self;
89 1         3 }
90 1         4  
91             my $self = shift;
92             return $self->{instances} //= [];
93             }
94 15     15 0 23  
95 15   100     73 my $self = shift;
96             return $self->{data};
97             }
98              
99 0     0 0 0 =head2 append
100 0         0  
101             Appends type signature
102              
103             =over 4
104              
105             =item * C<%param> type signature
106              
107             =back
108              
109             return same L<REFECO::Blockchain::Contract::Solidity::ABI::Decoder> instance
110              
111             =cut
112              
113             my ($self, $param) = @_;
114              
115             push $self->instances->@*, REFECO::Blockchain::Contract::Solidity::ABI::Type::new_type(signature => $param);
116             return $self;
117             }
118 8     8 1 10708  
119             =head2 encode
120 8         21  
121 8         36 Decodes all appended type signatures
122              
123             =over 4
124              
125             =back
126              
127             Array ref containing all decoded values
128              
129             =cut
130              
131             my ($self, $hex_data) = @_;
132              
133             croak 'Invalid hexadecimal value ' . $hex_data // 'undef'
134             unless $hex_data =~ /^(?:0x|0X)?([a-fA-F0-9]+)$/;
135              
136             my $hex = $1;
137 7     7 0 16 my @data = unpack("(A64)*", $hex);
138              
139 7 50 0     49 my $tuple = REFECO::Blockchain::Contract::Solidity::ABI::Type::Tuple->new;
140             $tuple->{instances} = $self->instances;
141             $tuple->{data} = \@data;
142 7         26 return $tuple->decode;
143 7         45 }
144              
145 7         36 =head2 clean
146 7         18  
147 7         13 Clean all the appended type signatures
148 7         41  
149             =over 4
150              
151             =back
152              
153             undef
154              
155             =cut
156              
157             my $self = shift;
158             delete $self->{instances};
159             }
160              
161             1;
162