| 1 | package Net::Async::Blockchain::BTC; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | our $VERSION = '0.002'; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | Net::Async::Blockchain::BTC - Bitcoin based subscription. | ||
| 11 | |||
| 12 | =head1 SYNOPSIS | ||
| 13 | |||
| 14 | my $loop = IO::Async::Loop->new; | ||
| 15 | |||
| 16 | $loop->add( | ||
| 17 | my $btc_client = Net::Async::Blockchain::BTC->new( | ||
| 18 | subscription_url => "tcp://127.0.0.1:28332", | ||
| 19 | blockchain_code => 'Bitcoin', | ||
| 20 | ) | ||
| 21 | ); | ||
| 22 | |||
| 23 | $btc_client->subscribe("blocks")->each(sub { print shift->{hash} })->get; | ||
| 24 | |||
| 25 | =head1 DESCRIPTION | ||
| 26 | |||
| 27 | Bitcoin subscription using ZMQ from the bitcoin based blockchain nodes | ||
| 28 | |||
| 29 | =over 4 | ||
| 30 | |||
| 31 | =back | ||
| 32 | |||
| 33 | =cut | ||
| 34 | |||
| 35 | 1 | ▶ | no indirect; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 36 | |||
| 37 | 1 | ▶ | use Net::Async::Blockchain::Client::ZMQ; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 38 | use Future::AsyncAwait; | ||
| 39 | use curry; | ||
| 40 | |||
| 41 | use parent qw(Net::Async::Blockchain); | ||
| 42 | |||
| 43 | my %subscription_dictionary = ( | ||
| 44 | 'blocks' => 'hashblock', | ||
| 45 | ); | ||
| 46 | |||
| 47 | =head2 zmq_client | ||
| 48 | |||
| 49 | Returns the current instance for L<Net::Async::Blockchain::Client::ZMQ> if not created | ||
| 50 | create a new one. | ||
| 51 | |||
| 52 | =over 4 | ||
| 53 | |||
| 54 | =back | ||
| 55 | |||
| 56 | L<Net::Async::Blockchain::Client::ZMQ> | ||
| 57 | |||
| 58 | =cut | ||
| 59 | |||
| 60 | sub zmq_client { | ||
| 61 | my ($self) = @_; | ||
| 62 | return $self->{zmq_client} //= do { | ||
| 63 | $self->add_child( | ||
| 64 | my $zmq_client = Net::Async::Blockchain::Client::ZMQ->new( | ||
| 65 | endpoint => $self->subscription_url, | ||
| 66 | timeout => $self->subscription_timeout, | ||
| 67 | msg_timeout => $self->subscription_msg_timeout, | ||
| 68 | on_shutdown => sub { | ||
| 69 | my ($error) = @_; | ||
| 70 | warn $error; | ||
| 71 | }, | ||
| 72 | )); | ||
| 73 | $zmq_client; | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | =head2 subscribe | ||
| 78 | |||
| 79 | Connect to the ZMQ port and subscribe to the implemented subscription: https://github.com/bitcoin/bitcoin/blob/master/doc/zmq.md#usage | ||
| 80 | |||
| 81 | =over 4 | ||
| 82 | |||
| 83 | =item * C<subscription> string subscription name | ||
| 84 | |||
| 85 | =back | ||
| 86 | |||
| 87 | L<Ryu::Source> | ||
| 88 | |||
| 89 | =cut | ||
| 90 | |||
| 91 | async sub subscribe { | ||
| 92 | my ($self, $subscription) = @_; | ||
| 93 | my $subscription_event = $subscription_dictionary{$subscription} or die "Invalid or not implemented subscription"; | ||
| 94 | return $self->zmq_client->subscribe($subscription_event)->map( | ||
| 95 | $self->$curry::weak( | ||
| 96 | sub { | ||
| 97 | my ($self, $message) = @_; | ||
| 98 | return $self->subscription_response($subscription, $message); | ||
| 99 | })); | ||
| 100 | } | ||
| 101 | |||
| 102 | 1; | ||