Contract.pm

filter: Statementstmt 37.8% 36 / 95 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 42.8% 9 / 21 Podpod 100% 7 / 7 totaltotal 32.1% 52 / 162 CC 20 SCAR 36.7
CC
20
Cov
28%
get_function_id
110.0
BUILD
56.0
get_hex_param
12.0
CRAP 39.4
↑ Summary Contract/ContractTransaction.pm »
1 package Ethereum::RPC::Contract;
2 # ABSTRACT: Support for interacting with Ethereum contracts using the geth RPC interface
3  
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 our $VERSION = '0.03';
8  
9 =head1 NAME
10  
11 Ethereum::Contract - Support for interacting with Ethereum contracts using the geth RPC interface
12  
13 =cut
14  
15 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use Math::BigInt;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 5 ▶use Ethereum::RPC::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 5 ▶use Ethereum::RPC::Contract::ContractResponse;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 ▶use Ethereum::RPC::Contract::ContractTransaction;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 5 ▶use Ethereum::RPC::Contract::Helper::UnitConversion;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 5 has contract_address => (is => 'rw');
25 5 has contract_abi => (
26 is => 'ro',
27 required => 1
28 );
29 5 has rpc_client => (
30 is => 'ro',
31 lazy => 1
32 );
33  
34 sub _build_rpc_client {
35 0 ▶ return Ethereum::RPC::Client->new;
Subroutine _build_rpc_client: not called CC=1 SCAR=6.9
36 }
37  
38 5 has from => (
39 is => 'rw',
40 lazy => 1
41 );
42  
43 sub _build_from {
44 0 ▶ return shift->rpc_client->eth_coinbase();
Subroutine _build_from: not called CC=1 SCAR=6.9
45 }
46  
47 5 has gas_price => (
48 is => 'rw',
49 lazy => 1
50 );
51  
52 sub _build_gas_price {
53 0 ▶ return shift->rpc_client->eth_gasPrice();
Subroutine _build_gas_price: not called CC=1 SCAR=6.9
54 }
55  
56 5 has gas => (is => 'rw');
57  
58 has contract_decoded => (
59 is => 'ro',
60 0 ▶ default => sub { {} });
Subroutine __ANON__: not called CC=1 SCAR=1.2
61  
62 =head2 BUILD
63  
64 Constructor: Here we get all functions from the passed ABI and bring it to contract class subs.
65  
66 Parameters:
67 contract_address ( Optional - only if the contract already exists ),
68 contract_abi ( Required - https://solidity.readthedocs.io/en/develop/abi-spec.html ),
69 rpc_client ( Optional - Ethereum::RPC::Client(https://github.com/binary-com/perl-Ethereum-RPC-Client) - if not given, new instance will be created );
70 from ( Optional - Address )
71 gas ( Optional - Integer gas )
72 gas_price ( Optional - Integer gasPrice )
73  
74 Return:
75 New contract instance
76  
77 =cut
78  
79 sub BUILD {
80 0 ▶ my ($self) = @_;
Subroutine BUILD: not called CC=7 SCAR=40.3
Pod documented
81  
82 0 my @decoded_json = @{decode_json($self->contract_abi)};
83  
84 0 for my $json_input (@decoded_json) {
85 0 ▶ if ($json_input->{type} =~ /^function|event$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($json_input->{"type"} =~ /^function|event$/) 0 0 0
86 0 ▶ $self->contract_decoded->{$json_input->{name}} ||= [];
Condition0%0/2
$self->contract_decoded->{$json_input->{"name"}} ||= []
l!l
00
MC/DC0%0/1
$self->contract_decoded->{$json_input->{"name"}} ||= []
$self->contract_decoded->{$json_input->{"name"}}
Truth table0%0/2
A
A= $self->contract_decoded->{$json_input->{"name"}}
Aresult
11
00
87 0 ▶ push(@{$self->contract_decoded->{$json_input->{name}}}, $json_input->{inputs}) if scalar @{$json_input->{inputs}} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$json_input->{"inputs"};} > 0 0 0 0
88 }
89 }
90  
91 0 ▶ $self->from($self->rpc_client->eth_coinbase()) unless $self->from;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->from 0 0 0
92 0 ▶ $self->gas_price($self->rpc_client->eth_gasPrice()) unless $self->gas_price;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->gas_price 0 0 0
93  
94 0 return;
95  
96 }
97  
98 =head2 invoke
99  
100 Invokes all calls from ABI to the contract.
101  
102 Parameters:
103 name (Required - the string function name )
104 params (Optional - the parameters)
105  
106 Return:
107 Ethereum::Contract::ContractTransaction
108  
109 =cut
110  
111 sub invoke {
112 0 ▶ my ($self, $name, @params) = @_;
Subroutine invoke: not called CC=1 SCAR=6.9
Pod documented
113  
114 0 my $function_id = substr($self->get_function_id($name, scalar @params), 0, 10);
115  
116 0 my $res = $self->_prepare_transaction($function_id, \@params);
117  
118 0 return $res;
119 }
120  
121 =head2 get_function_id
122  
123 Get the function and parameters and merge to create the hashed ethereum function ID
124  
125 Ex: function approve with the inputs address _spender and uint value must be represented as:
126 SHA3("approve(address,uint)")
127  
128 Parameters:
129 function_string (Required - the string function name )
130 inputs (Required - the input list given on the contract ABI)
131  
132 Return:
133 New function ID hash
134  
135 =cut
136  
137 sub get_function_id {
138 0 ▶ my ($self, $function_string, $params_size) = @_;
Subroutine get_function_id: not called CC=10 SCAR=47.0
Pod documented
139  
140 0 my @inputs = @{$self->contract_decoded->{$function_string}};
141 0 my @selected_data = ();
142 0 ▶ if (scalar @inputs > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @inputs > 0) 0 0 0
143 0 for my $v (@inputs) {
144 0 ▶ @selected_data = @{$v} if ($params_size and @{$v} == $params_size) or not $params_size;
Branch: 1 branch
BranchTrueFalseTotal
if $params_size and @{$v;} == $params_size or not $params_size 0 0 0
Condition0%0/6
$params_size and @{$v;} == $params_size or not $params_size
l!l&&r!l&&!r
000
$params_size and @{$v;} == $params_size
!ll&&!rl&&r
000
MC/DC0%0/3
$params_size and @{$v;} == $params_size or not $params_size
$params_size @{$v;} == $params_size not $params_size
Truth table0%0/5
A and B or not(C)
A= $params_sizeB= @{$v;} == $params_sizeC= not $params_size
ABCresult
11X1
0X11
1011
0X00
1000
145 0 ▶ last if scalar @selected_data > 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @selected_data > 0 0 0 0
146 }
147 }
148  
149 0 $function_string .= "(";
150 0 ▶ $function_string .= $_->{type} ? "$_->{type}," : "" for @selected_data;
Branch: 1 branch
BranchTrueFalseTotal
$_->{'type'} ? : 0 0 0
151 0 ▶ chop($function_string) if scalar @selected_data > 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @selected_data > 0 0 0 0
152 0 $function_string .= ")";
153  
154 0 my $hex_function = $self->append_prefix(unpack("H*", $function_string));
155  
156 0 my $sha3_hex_function = $self->rpc_client->web3_sha3($hex_function);
157  
158 0 return $sha3_hex_function;
159  
160 }
161  
162 =head2 _prepare_transaction
163  
164 Join the data and parameters and return a prepared transaction to be called as send, call or deploy.
165  
166 Parameters:
167 $compiled_data ( Required - the hashed function string name with parameters or the compiled contract bytecode )
168 params ( Required - the parameters args given by the method call )
169  
170 Return:
171 Future object
172 on_done: Ethereum::Contract::ContractTransaction
173 on_fail: string message
174  
175 =cut
176  
177 sub _prepare_transaction {
178 0 ▶ my ($self, $compiled_data, $params) = @_;
Subroutine _prepare_transaction: not called CC=1 SCAR=6.9
179  
180 0 my $data = join("", $compiled_data, map { $self->get_hex_param($_) } @{$params});
181  
182 0 return Ethereum::RPC::Contract::ContractTransaction->new(
183 contract_address => $self->contract_address,
184 rpc_client => $self->rpc_client,
185 data => $self->append_prefix($data),
186 from => $self->from,
187 gas => $self->gas,
188 gas_price => $self->gas_price,
189 );
190  
191 }
192  
193 =head2 get_hex_param
194  
195 Convert the given value to hexadecimal format
196  
197 Parameters:
198 function_id (Required - arg to be converted to hexadecimal)
199  
200 Return:
201 Hexadecimal string
202  
203 =cut
204  
205 sub get_hex_param {
206 0 ▶ my ($self, $param) = @_;
Subroutine get_hex_param: not called CC=3 SCAR=24.8
Pod documented
207  
208 0 my $new_param;
209 # Is hexadecimal string
210 0 ▶ if ($param =~ /^0x[0-9A-F]+$/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($param =~ /^0x[0-9A-F]+$/i) { } 0 0 0
elsif ($param =~ /^[+-]?[0-9?e+]+$/) { } 0 0 0
211 0 $new_param = sprintf("%064s", substr($param, 2));
212 # Is integer
213 } elsif ($param =~ /^[+-]?[0-9?e+]+$/) {
214 0 $new_param = sprintf("%064s", substr(Math::BigInt->new($param)->as_hex, 2));
215 # Is string
216 } else {
217 0 $new_param = sprintf("%064s", unpack("H*", $param));
218 }
219  
220 0 return $new_param;
221  
222 }
223  
224 =head2 read_all_events_from_block
225  
226 Create a filter based on the given block to listen all events sent by the contract.
227  
228 The filter is killed before the list return, so for each request a new filter will be created.
229  
230 Parameters:
231 from_block ( Optional - start search block )
232 function ( Required - function name )
233  
234 Return:
235 https://github.com/ethereum/wiki/wiki/JSON-RPC#returns-42
236  
237 =cut
238  
239 sub read_all_events_from_block {
240 0 ▶ my ($self, $from_block, $function) = @_;
Subroutine read_all_events_from_block: not called CC=2 SCAR=17.9
Pod documented
241  
242 0 my $function_id = $self->get_function_id($function);
243  
244 0 ▶ $from_block = $self->append_prefix(unpack("H*", $from_block // "latest"));
Condition0%0/2
$from_block // "latest"
l!l
00
MC/DC0%0/1
$from_block // "latest"
$from_block
Truth table0%0/2
A
A= $from_block
Aresult
11
00
245  
246 0 my $res = $self->rpc_client->eth_getLogs([{
247 address => $self->contract_address,
248 fromBlock => $from_block,
249 topics => [$function_id]}]);
250  
251 0 return $res;
252  
253 }
254  
255 =head2 invoke_deploy
256  
257 Prepare a deploy transaction,
258  
259 Parameters:
260 compiled ( Required - contract bytecode)
261 params ( Required - contract constructor params
262  
263 Return:
264 Ethereum::Contract::ContractTransaction
265  
266 =cut
267  
268 sub invoke_deploy {
269 0 ▶ my ($self, $compiled_data, @params) = @_;
Subroutine invoke_deploy: not called CC=1 SCAR=6.9
Pod documented
270 0 return $self->_prepare_transaction($compiled_data, \@params);
271 }
272  
273 =head2 append_prefix
274  
275 Ensure that the given hexadecimal string starts with 0x.
276  
277 =cut
278  
279 sub append_prefix {
280 0 ▶ my ($self, $str) = @_;
Subroutine append_prefix: not called CC=2 SCAR=17.9
Pod documented
281 0 ▶ return "0x$str" unless $str =~ /^0x/;
Branch: 1 branch
BranchTrueFalseTotal
unless $str =~ /^0x/ 0 0 0
282 0 return $str;
283 }
284  
285 5 1;
↑ Summary Contract/ContractTransaction.pm »