File Coverage

blib/lib/Blockchain/Ethereum/ABI/Type/Tuple.pm
Criterion Covered Total %
statement 50 50 100.0
branch 10 12 83.3
condition 1 2 50.0
subroutine 10 10 100.0
pod 2 2 100.0
total 73 76 96.0


line stmt bran cond sub pod time code
1             package Blockchain::Ethereum::ABI::Type::Tuple;
2              
3 11     11   153027 use v5.26;
  11         38  
4 11     11   58 use strict;
  11         2118  
  11         2499  
5 11     11   87 use warnings;
  11         1982  
  11         5179  
6              
7             # ABSTRACT: Solidity uint/int/bool type interface
8             our $AUTHORITY = 'cpan:REFECO'; # AUTHORITY
9             our $VERSION = '0.021'; # VERSION
10              
11 11     11   3254 use parent 'Blockchain::Ethereum::ABI::Type';
  11         2401  
  11         75  
12              
13 11     11   2686 use Carp;
  11         25  
  11         10018  
14              
15             sub _configure {
16 12     12   19 my $self = shift;
17              
18 12 100       47 return unless $self->{data};
19              
20 8         25 my @splited_signatures = $self->_split_tuple_signature->@*;
21              
22 8         31 for (my $sig_index = 0; $sig_index < @splited_signatures; $sig_index++) {
23             push $self->{instances}->@*,
24             Blockchain::Ethereum::ABI::Type->new(
25             signature => $splited_signatures[$sig_index],
26 29         105 data => $self->{data}->[$sig_index]);
27             }
28              
29             }
30              
31             sub _split_tuple_signature {
32 18     18   22 my $self = shift;
33              
34             # remove the parentheses from tuple signature
35 18         67 $self->{signature} =~ /^\((.*)\)$/;
36 18         39 my $tuple_signatures = $1;
37              
38 18 50       47 croak "Invalid tuple signature" unless $tuple_signatures;
39              
40             # this looks through tuple signature recursively and break it into lines
41             # this is to help splitting tuples inside tuples that also contains comma
42 18         209 $tuple_signatures =~ s/((\((?>[^)(]*(?2)?)*\))|[^,()]*)(*SKIP),/$1\n/g;
43 18         55 my @types = split('\n', $tuple_signatures);
44 18         73 return \@types;
45             }
46              
47             sub encode {
48 48     48 1 101 my $self = shift;
49              
50 48 100       165 return $self->_encoded if $self->_encoded;
51              
52 25         97 my $offset = $self->_get_initial_offset;
53              
54 19         53 for my $instance ($self->{instances}->@*) {
55 52         161 $instance->encode;
56 52 100       122 if ($instance->is_dynamic) {
57 15         54 $self->_push_static($self->_encode_offset($offset));
58 15         37 $self->_push_dynamic($instance->_encoded);
59 15         40 $offset += scalar $instance->_encoded->@*;
60 15         46 next;
61             }
62              
63 37         104 $self->_push_static($instance->_encoded);
64             }
65              
66 19         44 return $self->_encoded;
67             }
68              
69             sub decode {
70 13     13 1 33 my $self = shift;
71              
72 13 100       55 unless (scalar $self->{instances}->@* > 0) {
73 4         10 push $self->{instances}->@*, Blockchain::Ethereum::ABI::Type->new(signature => $_) for $self->_split_tuple_signature->@*;
74             }
75              
76 13         49 return $self->_read_stack_set_data;
77             }
78              
79             sub _static_size {
80 6     6   6 my $self = shift;
81              
82 6 50       11 return 1 if $self->is_dynamic;
83              
84 6         8 my $size = 1;
85 6         6 my $instance_size = 0;
86 6         11 for my $signature ($self->_split_tuple_signature->@*) {
87 12         69 my $instance = Blockchain::Ethereum::ABI::Type->new(signature => $signature);
88 12   50     19 $instance_size += $instance->_static_size // 0;
89             }
90              
91 6         12 return $size * $instance_size;
92             }
93              
94             1;
95              
96             __END__