Contract/ContractTransaction.pm

filter: Statementstmt 52.0% 26 / 50 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 60.0% 6 / 10 Podpod 100% 3 / 3 totaltotal 30.9% 35 / 113 CC 16 SCAR 35.7
CC
16
Cov
30%
get_contract_address
72.0
call_transaction
30.0
send_transaction
30.0
CRAP 35.4
« Contract.pm ↑ Summary Contract/ContractResponse.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.03';
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, $block) = @_;
Subroutine call_transaction: not called CC=5 SCAR=34.0
Pod documented
49  
50 0 ▶ my $res = $self->rpc_client->eth_call([{
Condition0%0/2
$block // "latest"
l!l
00
MC/DC0%0/1
$block // "latest"
$block
Truth table0%0/2
A
A= $block
Aresult
11
00
51 to => $self->contract_address,
52 data => $self->data,
53 },
54 $block // "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 || "Can't call transaction");
Condition0%0/2
$res || q[Can't call transaction]
l!l
00
MC/DC0%0/1
$res || q[Can't call transaction]
$res
Truth table0%0/2
A
A= $res
Aresult
11
00
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=5 SCAR=34.0
Pod documented
76  
77 0 my $future = Future->new;
78  
79 0 my $params = {
80 to => $self->contract_address,
81 from => $self->from,
82 gasPrice => $self->gas_price,
83 data => $self->data,
84 };
85  
86 0 ▶ $params->{gas} = Ethereum::RPC::Contract::Helper::UnitConversion::to_wei($self->gas) if $self->gas;
Branch: 1 branch
BranchTrueFalseTotal
if $self->gas 0 0 0
87 0 my $res = $self->rpc_client->eth_sendTransaction([$params]);
88  
89 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
90 0 ▶ return $future->fail($res || "Can't send transaction");
Condition0%0/2
$res || q[Can't send transaction]
l!l
00
MC/DC0%0/1
$res || q[Can't send transaction]
$res
Truth table0%0/2
A
A= $res
Aresult
11
00
91  
92 }
93  
94 =head2 get_contract_address
95  
96 Try to get a contract address based on a transaction hash
97  
98 Parameters:
99 $wait_seconds ( Optional - max time to wait for the contract address response ),
100 $send_response ( Optional - response of the send method, if not informed send a new transaction and then try to get the address ),
101  
102 Return:
103 Ethereum::Contract::ContractResponse
104  
105 =cut
106  
107 sub get_contract_address {
108  
109 0 ▶ my ($self, $wait_seconds, $send_response) = @_;
Subroutine get_contract_address: not called CC=8 SCAR=42.8
Pod documented
110  
111 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
112 0 ▶ return $transaction if $transaction->is_failed;
Branch: 1 branch
BranchTrueFalseTotal
if $transaction->is_failed 0 0 0
113  
114 0 my $deployed = $self->rpc_client->eth_getTransactionReceipt($transaction->get->response);
115  
116 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
117 0 sleep(1);
118 0 $wait_seconds--;
119 0 $deployed = $self->rpc_client->eth_getTransactionReceipt($transaction->get->response);
120 }
121  
122 0 my $future = Future->new;
123 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
124 if $deployed and ref($deployed) eq 'HASH';
125  
126 0 return $future->fail("Can't get the contract address for transaction: " . $transaction->get->response);
127  
128 }
129  
130 6 1;
« Contract.pm ↑ Summary Contract/ContractResponse.pm »