Contract.pm

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