| 1 | package Net::Async::Blockchain::ETH; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | our $VERSION = '0.001'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | Net::Async::Blockchain::ETH - Ethereum based subscription. | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | my $loop = IO::Async::Loop->new; | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | $loop->add( | ||||||||||||||||||||||||||||
| 17 | my $eth_client = Net::Async::Blockchain::ETH->new( | ||||||||||||||||||||||||||||
| 18 | subscription_url => "ws://127.0.0.1:8546", | ||||||||||||||||||||||||||||
| 19 | blockchain_code => 'Ethereum', | ||||||||||||||||||||||||||||
| 20 | ) | ||||||||||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | $eth_client->subscribe("blocks")->each(sub { print shift->{hash} })->get; | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | Ethereum subscription using websocket node client | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | =over 4 | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | =back | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | =cut | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 2 | ▶ | no indirect; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 2 | ▶ | use Net::Async::Blockchain::Client::Websocket; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 2 | ▶ | use curry; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 2 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 2 | ▶ | use parent qw(Net::Async::Blockchain); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 2 | my %subscription_dictionary = ('blocks' => 'newHeads'); | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =head2 subscription_id | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | Actual subscription ID, this ID is received every time when a subscription | ||||||||||||||||||||||||||||
| 48 | is created. | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | =over 4 | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | =back | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | An hexadecimal string | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =cut | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 2 | ▶ | sub subscription_id { shift->{subscription_id} } | ||||||||||||||||||||||||||
|
Subroutine
subscription_id: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =head2 websocket_client | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | Returns the current instance for L<Net::Async::Blockchain::Client::Websocket> if not created | ||||||||||||||||||||||||||||
| 63 | create a new one. | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =over 4 | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =back | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | L<Net::Async::Blockchain::Client::Websocket> | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =cut | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | sub websocket_client { | ||||||||||||||||||||||||||||
| 74 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
websocket_client: called CC=2 SCAR=8.5
Pod
documented
| |||||||||||||||||||||||||||||
| 75 | 1 | ▶ | return $self->{websocket_client} //= do { | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->{"websocket_client"} //= do {
MC/DC0%0/2
$self->{"websocket_client"} //= do {
$self->add_child(my $websocket_client = "Net::Async::Blockchain::Client::Websocket"->new("endpoint", $self->subscription_url, "on_shutdown", sub {
my($error) = @_;
warn $error;
}
));
$websocket_client
}
$self->{"websocket_client"}
do {
$self->add_child(my $websocket_client = "Net::Async::Blockchain::Client::Websocket"->new("endpoint", $self->subscription_url, "on_shutdown", sub {
my($error) = @_;
warn $error;
}
));
$websocket_client
}
Truth table33%1/3
A //= B
A= $self->{"websocket_client"}B= do {
$self->add_child(my $websocket_client = "Net::Async::Blockchain::Client::Websocket"->new("endpoint", $self->subscription_url, "on_shutdown", sub {
my($error) = @_;
warn $error;
}
));
$websocket_client
}
| |||||||||||||||||||||||||||||
| 76 | $self->add_child( | ||||||||||||||||||||||||||||
| 77 | my $websocket_client = Net::Async::Blockchain::Client::Websocket->new( | ||||||||||||||||||||||||||||
| 78 | endpoint => $self->subscription_url, | ||||||||||||||||||||||||||||
| 79 | on_shutdown => sub { | ||||||||||||||||||||||||||||
| 80 | 0 | ▶ | my ($error) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 81 | 0 | warn $error; | |||||||||||||||||||||||||||
| 82 | }, | ||||||||||||||||||||||||||||
| 83 | 1 | )); | |||||||||||||||||||||||||||
| 84 | 1 | $websocket_client; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =head2 subscribe | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | Connect to the websocket port and subscribe to the implemented subscription: | ||||||||||||||||||||||||||||
| 91 | - https://github.com/ethereum/go-ethereum/wiki/RPC-PUB-SUB#create-subscription | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =over 4 | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =item * C<subscription> string subscription name | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =back | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | L<Ryu::Source> | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | =cut | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 2 | ▶ | async sub subscribe { | ||||||||||||||||||||||||||
|
Subroutine
subscribe: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 104 | 2 | my ($self, $subscription) = @_; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 2 | ▶ | my $subscription_event = $subscription_dictionary{$subscription} or die "Invalid or not implemented subscription"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 1 | my $source = await $self->websocket_client()->eth_subscribe($subscription_event); | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | # the first response from the node is the subscription id | ||||||||||||||||||||||||||||
| 110 | # once we received it we can start to listening the subscription. | ||||||||||||||||||||||||||||
| 111 | return $source->skip_until( | ||||||||||||||||||||||||||||
| 112 | $self->$curry::weak( | ||||||||||||||||||||||||||||
| 113 | sub { | ||||||||||||||||||||||||||||
| 114 | 2 | ▶ | my ($self, $response) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 115 | 2 | ▶ | return 1 unless $response->{result}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 1 | ▶ | $self->{subscription_id} = $response->{result} unless $self->{subscription_id}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 1 | return 0; | |||||||||||||||||||||||||||
| 118 | })) | ||||||||||||||||||||||||||||
| 119 | # we use the subscription id received as the first response to filter | ||||||||||||||||||||||||||||
| 120 | # all incoming subscription responses. | ||||||||||||||||||||||||||||
| 121 | ->filter( | ||||||||||||||||||||||||||||
| 122 | $self->$curry::weak( | ||||||||||||||||||||||||||||
| 123 | sub { | ||||||||||||||||||||||||||||
| 124 | 2 | ▶ | my ($self, $response) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=12.5
| |||||||||||||||||||||||||||||
| 125 | 2 | ▶ | return undef unless $response->{params} && $response->{params}->{subscription}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$response->{"params"} and $response->{"params"}{"subscription"}
MC/DC0%0/2
$response->{"params"} and $response->{"params"}{"subscription"}
$response->{"params"}
$response->{"params"}{"subscription"}
Truth table33%1/3
A and B
A= $response->{"params"}B= $response->{"params"}{"subscription"}
| |||||||||||||||||||||||||||||
| 126 | 2 | return $response->{params}->{subscription} eq $self->subscription_id; | |||||||||||||||||||||||||||
| 127 | }) | ||||||||||||||||||||||||||||
| 128 | )->map( | ||||||||||||||||||||||||||||
| 129 | $self->$curry::weak( | ||||||||||||||||||||||||||||
| 130 | sub { | ||||||||||||||||||||||||||||
| 131 | 1 | ▶ | my ($self, $response) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 132 | 1 | return $self->subscription_response($subscription, $response); | |||||||||||||||||||||||||||
| 133 | 1 | })); | |||||||||||||||||||||||||||
| 134 | 2 | } | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 2 | 1; | |||||||||||||||||||||||||||