File Coverage

blib/lib/Web3/Tiny.pm
Criterion Covered Total %
statement 18 40 45.0
branch 0 2 0.0
condition n/a
subroutine 6 17 35.2
pod 0 11 0.0
total 24 70 34.2


line stmt bran cond sub pod time code
1             package Web3::Tiny;
2              
3 1     1   200108 use strict;
  1         1  
  1         28  
4 1     1   3 use warnings;
  1         3  
  1         37  
5              
6 1     1   440 use Web3::Tiny::RPC;
  1         4  
  1         30  
7 1     1   420 use Web3::Tiny::Wallet;
  1         3  
  1         31  
8 1     1   407 use Web3::Tiny::Contract;
  1         2  
  1         30  
9 1     1   469 use Web3::Tiny::Util qw(hex_to_bigint);
  1         3  
  1         358  
10              
11             our $VERSION = '0.01';
12              
13             # Web3::Tiny->new(rpc_url => 'https://...')
14             sub new {
15 0     0 0   my ($class, %opts) = @_;
16 0 0         die "Web3::Tiny: 'rpc_url' is required\n" unless $opts{rpc_url};
17              
18             return bless {
19 0           rpc => Web3::Tiny::RPC->new(url => $opts{rpc_url}, timeout => $opts{timeout}),
20             }, $class;
21             }
22              
23 0     0 0   sub rpc { return $_[0]->{rpc} }
24              
25             # wallet(private_key => '0x...') -> Web3::Tiny::Wallet
26             sub wallet {
27 0     0 0   my ($self, %opts) = @_;
28 0           return Web3::Tiny::Wallet->new(%opts);
29             }
30              
31             # contract(address => '0x...', abi => { ... }) -> Web3::Tiny::Contract
32             sub contract {
33 0     0 0   my ($self, %opts) = @_;
34 0           return Web3::Tiny::Contract->new(%opts, rpc => $self->{rpc});
35             }
36              
37             # get_balance('0x...') -> Math::BigInt (wei)
38             sub get_balance {
39 0     0 0   my ($self, $address) = @_;
40 0           return hex_to_bigint($self->{rpc}->call('eth_getBalance', $address, 'latest'));
41             }
42              
43             # transaction_count('0x...') -> Math::BigInt (nonce)
44             sub transaction_count {
45 0     0 0   my ($self, $address) = @_;
46 0           return hex_to_bigint($self->{rpc}->call('eth_getTransactionCount', $address, 'latest'));
47             }
48              
49             # gas_price() -> Math::BigInt (wei)
50             sub gas_price {
51 0     0 0   my ($self) = @_;
52 0           return hex_to_bigint($self->{rpc}->call('eth_gasPrice'));
53             }
54              
55             # chain_id() -> Math::BigInt
56             sub chain_id {
57 0     0 0   my ($self) = @_;
58 0           return hex_to_bigint($self->{rpc}->call('eth_chainId'));
59             }
60              
61             # block_number() -> Math::BigInt
62             sub block_number {
63 0     0 0   my ($self) = @_;
64 0           return hex_to_bigint($self->{rpc}->call('eth_blockNumber'));
65             }
66              
67             # send_raw_transaction('0x...') -> '0x...' transaction hash
68             sub send_raw_transaction {
69 0     0 0   my ($self, $raw_tx) = @_;
70 0           return $self->{rpc}->call('eth_sendRawTransaction', $raw_tx);
71             }
72              
73             # transaction_receipt('0x...') -> hashref or undef if not yet mined
74             sub transaction_receipt {
75 0     0 0   my ($self, $tx_hash) = @_;
76 0           return $self->{rpc}->call('eth_getTransactionReceipt', $tx_hash);
77             }
78              
79             1;
80              
81             __END__