Skip to content

Commit

Permalink
Updated examples to be more current and more simple.
Browse files Browse the repository at this point in the history
  • Loading branch information
whindsx committed Mar 10, 2023
1 parent be61253 commit 2680472
Showing 1 changed file with 36 additions and 69 deletions.
105 changes: 36 additions & 69 deletions ex/example.pl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/perl
#!/usr/bin/perl

use Bitcoin::RPC::Client;

Expand All @@ -20,97 +20,64 @@
password => $RPCPASSWORD,
);

# https://bitcoin.org/en/developer-reference#getinfo
$info = $btc->getinfo;
$blocks = $info->{blocks};
print $blocks;

# https://bitcoin.org/en/developer-reference#getbalance
$balance = $btc->getbalance("root", 1, JSON::true);
# When a scalar value is returned
# https://developer.bitcoin.org/reference/rpc/getbalance.html
$balance = $btc->getbalance("*", 1, JSON::true);
print $balance;
print "\n";

# https://bitcoin.org/en/developer-reference#getblockchaininfo
$info = $btc->getblockchaininfo;
print $info->{softforks}[0]->{id};


###
# More on handling JSON data
###

# Getting Data when JSON/hash is returned from getinfo
# https://bitcoin.org/en/developer-reference#getinfo
# Getting data when JSON/hash is returned from getblockchaininfo
# https://developer.bitcoin.org/reference/rpc/getblockchaininfo.html
#
#{
# "version": 130000,
# "protocolversion": 70014,
# "walletversion": 130000,
# "balance": 0.00000000,
# "blocks": 584240,
# "proxy": "",
# "difficulty": 1,
# "paytxfee": 0.00500000,
# "relayfee": 0.00001000,
# "errors": ""
# "paytxfee": 0.00500000
#}
$info = $btc->getinfo;
$balance = $info->{balance};
print $balance;
# 0.0

$info = $btc->getblockchaininfo;
$blocks = $info->{blocks};
print $blocks;
print "\n";
# 584240

# JSON Objects
# Let's say we want the timeframe value from getnetttotals
# https://bitcoin.org/en/developer-reference#getnettotals
# https://developer.bitcoin.org/reference/rpc/getnettotals.html
#
#{
# "totalbytesrecv": 7137052851,
# "totalbytessent": 211648636140,
# "uploadtarget": {
# "timeframe": 86400,
# "target": 0,
# "target_reached": false,
# "serve_historical_blocks": true,
# "bytes_left_in_cycle": 0,
# "time_left_in_cycle": 0
# "target": 0
# }
#}
$nettot = $btc->getnettotals;
$timeframe = $nettot->{uploadtarget}{timeframe};
$timeframe = $nettot->{uploadtarget}->{timeframe};
print $timeframe;
print "\n";
# 86400

# JSON arrays
# Let's say we want the softfork IDs from getblockchaininfo
# https://bitcoin.org/en/developer-reference#getblockchaininfo
#
# Let's say we want the feerate_percentiles from getblockstats
# https://developer.bitcoin.org/reference/rpc/getblockstats.html
#{
# "chain": "main",
# "blocks": 464562,
# "headers": 464562,
# "pruned": false,
# "softforks": [
# {
# "id": "bip34",
# "version": 2,
# "reject": {
# "status": true
# }
# },
# {
# "id": "bip66",
# "version": 3,
# "reject": {
# "status": true
# }
# }
$bchain = $btc->getblockchaininfo;
@forks = @{ $bchain->{softforks} };
foreach $f (@forks) {
print $f->{id};
# "avgfee": 8967,
# "avgfeerate": 28,
# "feerate_percentiles": [1,1,3,62,65],
# "height": 584240,
# "maxfee": 850011
#}
$bstats = $btc->getblockstats(584240);
@fps = @{ $bstats->{feerate_percentiles} };
foreach $fr (@fps) {
print $fr;
print "\n";
}
# bip34
# bip66

# 1
# 1
# 3
# 62
# 65

exit(0);

0 comments on commit 2680472

Please sign in to comment.