Contract/ContractTransaction.pm

filter: Statementstmt 53.0% 26 / 49 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 60.0% 6 / 10 Podpod 100% 3 / 3 totaltotal 33.9% 35 / 103 CC 13 SCAR 34.6
CC
13
Cov
33%
get_contract_address
72.0
send_transaction
20.0
call_transaction
12.0
CRAP 31.8
↑ Summary Contract.pm »
1 package Ethereum::RPC::Contract::ContractTransaction;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 our $VERSION = '0.001';
7  
8 =head1 NAME
9  
10 Ethereum::RPC::Contract::ContractTransaction - Centralize contract transactions
11  
12 =cut
13  
14 6 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 6 ▶use Ethereum::RPC::Contract::ContractResponse;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 6 ▶use Ethereum::RPC::Contract::Helper::UnitConversion;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 6 has contract_address => (is => 'ro');
21 6 has rpc_client => (
22 is => 'ro',
23 lazy => 1
24 );
25  
26 sub _build_rpc_client {
27 0 ▶ return Ethereum::RPC::Client->new;
Subroutine _build_rpc_client: not called CC=1 SCAR=6.9
28 }
29  
30 6 has data => (
31 is => 'ro',
32 required => 1
33 );
34 6 has from => (is => 'ro');
35 6 has gas => (is => 'ro');
36 6 has gas_price => (is => 'ro');
37  
38 =head2 call_transaction
39  
40 Call a public functions and variables from a ethereum contract
41  
42 Return:
43 Ethereum::RPC::Contract::ContractResponse, error message
44  
45 =cut
46  
47 sub call_transaction {
48 0 ▶ my $self = shift;
Subroutine call_transaction: not called CC=3 SCAR=24.8
Pod documented
49  
50 0 my $res = $self->rpc_client->eth_call([{
51 to => $self->contract_address,
52 data => $self->data,
53 },
54 "latest"
55 ]);
56  
57 0 my $future = Future->new;
58 0 ▶ return $future->done(Ethereum::RPC::Contract::ContractResponse->new({response => $res})) if $res and $res =~ /^0x/;
Branch: 1 branch
BranchTrueFalseTotal
if $res and $res =~ /^0x/ 0 0 0
Condition0%0/3
$res and $res =~ /^0x/
!ll&&!rl&&r
000
MC/DC0%0/2
$res and $res =~ /^0x/
$res $res =~ /^0x/
Truth table0%0/3
A and B
A= $resB= $res =~ /^0x/
ABresult
0X0
100
111
59 0 return $future->fail($res);
60  
61 }
62  
63 =head2 send_transaction
64  
65 Send a transaction to a payable functions from a ethereum contract
66  
67 The parameter GAS is required to send a payable request.
68  
69 Return:
70 Ethereum::RPC::Contract::ContractResponse, error message
71  
72 =cut
73  
74 sub send_transaction {
75 0 ▶ my $self = shift;
Subroutine send_transaction: not called CC=4 SCAR=30.0
Pod documented
76  
77 0 my $future = Future->new;
78 0 ▶ return $future->fail("the transaction can't be sent without the GAS parameter") unless $self->gas;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->gas 0 0 0
79  
80 0 my $res = $self->rpc_client->eth_sendTransaction([{
81 to => $self->contract_address,
82 from => $self->from,
83 gas => Ethereum::RPC::Contract::Helper::UnitConversion::to_wei($self->gas),
84 gasPrice => $self->gas_price,
85 data => $self->data,
86 }]);
87  
88 0 ▶ return $future->done(Ethereum::RPC::Contract::ContractResponse->new({response => $res})) if $res and $res =~ /^0x/;
Branch: 1 branch
BranchTrueFalseTotal
if $res and $res =~ /^0x/ 0 0 0
Condition0%0/3
$res and $res =~ /^0x/
!ll&&!rl&&r
000
MC/DC0%0/2
$res and $res =~ /^0x/
$res $res =~ /^0x/
Truth table0%0/3
A and B
A= $resB= $res =~ /^0x/
ABresult
0X0
100
111
89 0 return $future->fail($res);
90  
91 }
92  
93 =head2 get_contract_address
94  
95 Try to get a contract address based on a transaction hash
96  
97 Parameters:
98 $wait_seconds ( Optional - max time to wait for the contract address response ),
99 $send_response ( Optional - response of the send method, if not informed send a new transaction and then try to get the address ),
100  
101 Return:
102 Ethereum::Contract::ContractResponse
103  
104 =cut
105  
106 sub get_contract_address {
107  
108 0 ▶ my ($self, $wait_seconds, $send_response) = @_;
Subroutine get_contract_address: not called CC=8 SCAR=42.8
Pod documented
109  
110 0 ▶ my $transaction = $send_response // $self->send_transaction();
Condition0%0/3
$send_response // $self->send_transaction
l!l&&r!l&&!r
000
MC/DC0%0/2
$send_response // $self->send_transaction
$send_response $self->send_transaction
Truth table0%0/3
A // B
A= $send_responseB= $self->send_transaction
ABresult
1X1
011
000
111 0 ▶ return $transaction if $transaction->is_failed;
Branch: 1 branch
BranchTrueFalseTotal
if $transaction->is_failed 0 0 0
112  
113 0 my $deployed = $self->rpc_client->eth_getTransactionReceipt($transaction->get->response);
114  
115 0 ▶ while ($wait_seconds and not $deployed and $wait_seconds > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wait_seconds and not $deployed and $wait_seconds > 0) 0 0 0
Condition0%0/6
$wait_seconds and not $deployed and $wait_seconds > 0
!ll&&!rl&&r
000
$wait_seconds and not $deployed
!ll&&!rl&&r
000
MC/DC0%0/3
$wait_seconds and not $deployed and $wait_seconds > 0
$wait_seconds not $deployed $wait_seconds > 0
Truth table0%0/4
A and not(B) and C
A= $wait_secondsB= not $deployedC= $wait_seconds > 0
ABCresult
0XX0
10X0
1100
1111
116 0 sleep(1);
117 0 $wait_seconds--;
118 0 $deployed = $self->rpc_client->eth_getTransactionReceipt($transaction->get->response);
119 }
120  
121 0 my $future = Future->new;
122 0 ▶ return $future->done(Ethereum::RPC::Contract::ContractResponse->new({response => $deployed->{contractAddress}}))
Branch: 1 branch
BranchTrueFalseTotal
if $deployed and ref $deployed eq "HASH" 0 0 0
Condition0%0/3
$deployed and ref $deployed eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$deployed and ref $deployed eq "HASH"
$deployed ref $deployed eq "HASH"
Truth table0%0/3
A and B
A= $deployedB= ref $deployed eq "HASH"
ABresult
0X0
100
111
123 if $deployed and ref($deployed) eq 'HASH';
124  
125 0 return $future->fail("Can't get the contract address for transaction: " . $transaction->get->response);
126  
127 }
128  
129 6 1;
↑ Summary Contract.pm »