Contract/ContractTransaction.pm

filter: Statementstmt 50.9% 28 / 55 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 60.0% 6 / 10 Podpod 100% 3 / 3 totaltotal 29.8% 37 / 124 CC 19 SCAR 38.5
CC
19
Cov
29%
send_transaction
72.0
get_contract_address
72.0
call_transaction
30.0
CRAP 46.8
« Contract.pm ↑ Summary Client.pm »
1 package Ethereum::RPC::Contract::ContractTransaction;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 our $VERSION = '0.05';
7  
8 =head1 NAME
9  
10 Ethereum::RPC::Contract::ContractTransaction - Centralize contract transactions
11  
12 =cut
13  
14 7 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 7 ▶use Ethereum::RPC::Contract::ContractResponse;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 7 ▶use Ethereum::RPC::Contract::Helper::UnitConversion;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 7 has contract_address => (is => 'ro');
21 7 has rpc_client => (
22 is => 'ro',
23 lazy => 1
24 );
25  
26 0 ▶sub _build_rpc_client {
Subroutine _build_rpc_client: not called CC=1 SCAR=6.9
27 0 return Ethereum::RPC::Client->new;
28 }
29  
30 7 has data => (
31 is => 'ro',
32 required => 1
33 );
34  
35 7 has from => (is => 'ro');
36 7 has gas => (is => 'ro');
37 7 has gas_price => (is => 'ro');
38 7 has max_fee_per_gas => (is => 'ro');
39 7 has max_priority_fee_per_gas => (is => 'ro');
40  
41 =head2 call_transaction
42  
43 Call a public functions and variables from a ethereum contract
44  
45 Return:
46 Ethereum::RPC::Contract::ContractResponse, error message
47  
48 =cut
49  
50 0 ▶sub call_transaction {
Subroutine call_transaction: not called CC=5 SCAR=34.0
Pod documented
51 0 my ($self, $block) = @_;
52  
53 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
54 to => $self->contract_address,
55 data => $self->data,
56 },
57 $block // "latest"
58 ]);
59  
60 0 my $future = Future->new;
61 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
62 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
63  
64 }
65  
66 =head2 send_transaction
67  
68 Send a transaction to a payable functions from a ethereum contract
69  
70 The parameter GAS is required to send a payable request.
71  
72 Return:
73 Ethereum::RPC::Contract::ContractResponse, error message
74  
75 =cut
76  
77 0 ▶sub send_transaction {
Subroutine send_transaction: not called CC=8 SCAR=42.8
Pod documented
78 0 my $self = shift;
79  
80 0 my $future = Future->new;
81  
82 0 my $params = {
83 to => $self->contract_address,
84 from => $self->from,
85 data => $self->data,
86 };
87  
88 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
89 0 ▶ $params->{gasPrice} = $self->gas_price if $self->gas_price;
Branch: 1 branch
BranchTrueFalseTotal
if $self->gas_price 0 0 0
90 0 ▶ $params->{maxFeePerGas} = $self->max_fee_per_gas if $self->max_fee_per_gas;
Branch: 1 branch
BranchTrueFalseTotal
if $self->max_fee_per_gas 0 0 0
91 0 ▶ $params->{maxPriorityFeePerGas} = $self->max_priority_fee_per_gas if $self->max_priority_fee_per_gas;
Branch: 1 branch
BranchTrueFalseTotal
if $self->max_priority_fee_per_gas 0 0 0
92  
93 0 my $res = $self->rpc_client->eth_sendTransaction([$params]);
94  
95 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
96 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
97  
98 }
99  
100 =head2 get_contract_address
101  
102 Try to get a contract address based on a transaction hash
103  
104 Parameters:
105 $wait_seconds ( Optional - max time to wait for the contract address response ),
106 $send_response ( Optional - response of the send method, if not informed send a new transaction and then try to get the address ),
107  
108 Return:
109 Ethereum::Contract::ContractResponse
110  
111 =cut
112  
113 0 ▶sub get_contract_address {
Subroutine get_contract_address: not called CC=8 SCAR=42.8
Pod documented
114  
115 0 my ($self, $wait_seconds, $send_response) = @_;
116  
117 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
118 0 ▶ return $transaction if $transaction->is_failed;
Branch: 1 branch
BranchTrueFalseTotal
if $transaction->is_failed 0 0 0
119  
120 0 my $deployed = $self->rpc_client->eth_getTransactionReceipt($transaction->get->response);
121  
122 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
123 0 sleep(1);
124 0 $wait_seconds--;
125 0 $deployed = $self->rpc_client->eth_getTransactionReceipt($transaction->get->response);
126 }
127  
128 0 my $future = Future->new;
129 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
130 if $deployed and ref($deployed) eq 'HASH';
131  
132 0 return $future->fail("Can't get the contract address for transaction: " . $transaction->get->response);
133  
134 }
135  
136 7 1;
« Contract.pm ↑ Summary Client.pm »