File Coverage

blib/lib/Blockchain/Ethereum/ABI/Type/Int.pm
Criterion Covered Total %
statement 43 44 97.7
branch 13 14 92.8
condition 10 12 83.3
subroutine 11 11 100.0
pod 3 3 100.0
total 80 84 95.2


line stmt bran cond sub pod time code
1 7     7   4438 use v5.26;
  7         25  
2 7     7   40 use Object::Pad;
  7         16  
  7         87  
3              
4             package Blockchain::Ethereum::ABI::Type::Int 0.012;
5             class Blockchain::Ethereum::ABI::Type::Int
6             :isa(Blockchain::Ethereum::ABI::Type)
7 1     1   693 :does(Blockchain::Ethereum::ABI::TypeRole);
  1         3  
  1         24  
8              
9             =encoding utf8
10              
11             =head1 NAME
12              
13             Blockchain::Ethereum::ABI::Int - Interface for solidity uint/int/bool type
14              
15             =head1 SYNOPSIS
16              
17             Allows you to define and instantiate a solidity address type:
18              
19             my $type = Blockchain::Ethereum::ABI::Int->new(
20             signature => $signature,
21             data => $value
22             );
23              
24             $type->encode();
25              
26             In most cases you don't want to use this directly, use instead:
27              
28             =over 4
29              
30             =item * B: L
31              
32             =item * B: L
33              
34             =back
35              
36             =cut
37              
38 7     7   2561 use Carp;
  7         17  
  7         690  
39 7     7   10680 use Math::BigInt try => 'GMP';
  7         274809  
  7         34  
40 7     7   205269 use Scalar::Util qw(looks_like_number);
  7         25  
  7         502  
41              
42 7     7   57 use constant DEFAULT_INT_SIZE => 256;
  7         20  
  7         9303  
43              
44 64     64   154 method _configure { return }
  64         396  
45              
46             =head2 encode
47              
48             Encodes the given data to the type of the signature
49              
50             Usage:
51              
52             encode() -> encoded string
53              
54             =over 4
55              
56             =back
57              
58             ABI encoded hex string
59              
60             =cut
61              
62 87     87 1 162 method encode {
63              
64 87 100       195 return $self->_encoded if $self->_encoded;
65              
66 39 100       96 croak "Invalid numeric data @{[$self->data]}" unless looks_like_number($self->data);
  1         2  
67              
68 38         91 my $bdata = Math::BigInt->new($self->data);
69              
70 38 50       3420 croak "Invalid numeric data @{[$self->data]}" if $bdata->is_nan;
  0         0  
71              
72 38 100 66     271 croak "Invalid data length, signature: @{[$self->fixed_length]}, data length: @{[$bdata->length]}"
  1         5  
  1         6  
73             if $self->fixed_length && $bdata->length > $self->fixed_length;
74              
75 37 100 66     119 croak "Invalid negative numeric data @{[$self->data]}"
  2         8  
76             if $bdata->is_neg && $self->signature =~ /^uint|bool/;
77              
78 35 100 100     279 croak "Invalid bool data it must be 1 or 0 but given @{[$self->data]}"
  1   100     4  
79             if !$bdata->is_zero && !$bdata->is_one && $self->signature =~ /^bool/;
80              
81 34         365 $self->_push_static($self->pad_left($bdata->to_hex));
82              
83 34         103 return $self->_encoded;
84             }
85              
86             =head2 decode
87              
88             Decodes the given data to the type of the signature
89              
90             Usage:
91              
92             decoded() -> L
93              
94             =over 4
95              
96             =back
97              
98             L
99              
100             =cut
101              
102 14     14 1 25 method decode {
103              
104 14         32 return Math::BigInt->from_hex($self->data->[0]);
105             }
106              
107 77     77 1 771 method fixed_length :override {
108              
109 77 100       179 if ($self->signature =~ /[a-z](\d+)/) {
110 59         336 return $1;
111             }
112 18         87 return DEFAULT_INT_SIZE;
113             }
114              
115             1;
116              
117             __END__