Bitcoin Forum
July 07, 2024, 03:11:27 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Simple PHP script to summarize listtransactions output.  (Read 2330 times)
btc4ever (OP)
Sr. Member
****
Offline Offline

Activity: 321
Merit: 250


View Profile
October 16, 2013, 03:26:26 AM
 #1

I figured I would share this simple script I wrote that summarizes the output of listtransactions.  This enables me to quickly see the count and sum of each category of receive transaction.  It also displays the total revenue (generate+immature) and total received including orphans.  Here is sample usage/output:

Quote
$ ./bitcoind listtransactions '' 10000 | sum_transactions.php

Array
(   
    [generate] => 0.35260452
    [generate_count] => 8
    [immature] => 0.17373111
    [immature_count] => 2
)
Array
(   
    [all_count] => 10
    [all] => 0.52633563
    [revenue_count] => 10
    [revenue] => 0.52633563
)

Note, you should pass a high number as the 2nd param to listtransactions in order to get all transactions for all time.  The default is only 10.

I don't have any orphans at present, so those are not displayed.  I see them when mining alt-coins.

We can check the results against getbalance.

Quote
$ ./bitcoind getbalance
0.35260452

getbalance matches "generate".

Here is the script.

Code:
#!/usr/bin/env php

<?php

$fh 
STDIN;

$buf stream_get_contents$fh );

$data json_decode$buf );

$amounts = array();
foreach( 
$data as $trans ) {
        if( @
$trans->amount ) {
                if( @
$amounts[$trans->category] ) {
                        
$amounts[$trans->category] += $trans->amount;
                        
$amounts[$trans->category '_count'] += 1;
                }
                else { 
                        
$amounts[$trans->category] = $trans->amount;
                        
$amounts[$trans->category '_count'] = 1;
                }
        }
}

$summary = array();
$summary['all_count'] = @$amounts['generate_count'] + @$amounts['orphan_count'] + @$amounts['immature_count'];
$summary['all'] = @$amounts['generate'] + @$amounts['orphan'] + @$amounts['immature'];

$summary['revenue_count'] = @$amounts['generate_count'] + @$amounts['immature_count'];
$summary['revenue'] = @$amounts['generate'] + @$amounts['immature'];

print_r$amounts );
print_r$summary );


As a bonus, here is another script that will display the date/time for each transaction ( instead of a timestamp ).

Code:
#!/usr/bin/env php

<?php

$fh 
STDIN;

$buf stream_get_contents$fh );

$data json_decode$buf );

foreach( 
$data as $trans ) {
        if( @
$trans->time ) {
                
$trans->time fix_time$trans->time );
        }
        if( @
$trans->timereceived ) {
                
$trans->timereceived fix_time$trans->timereceived );
        }
        if( @
$trans->blocktime ) {
                
$trans->blocktime fix_time$trans->blocktime );
        }
}

print_r$data );

function 
fix_time$timestamp ) {
        return 
date('c'$timestamp );
}

If anyone improves on either of these, please post your changes here.  thanks.


Psst!!  Wanna make bitcoin unstoppable? Why the Only Real Way to Buy Bitcoins Is on the Streets. Avoid banks and centralized exchanges.   Buy/Sell coins locally.  Meet other bitcoiners and develop your network.   Try localbitcoins.com or find or start a buttonwood / satoshi square in your area.  Pass it on!
btc4ever (OP)
Sr. Member
****
Offline Offline

Activity: 321
Merit: 250


View Profile
October 16, 2013, 04:47:51 PM
 #2

Here is an updated version of the script that also prints a summary by date, and can print USD values if exchange rate is supplied as an argument. Also it displays output using json_encode() which is a bit more compact and readable.

Usage:    ( Here I supply an exchange rate of 140 )

Quote
./bitcoind listtransactions '' 1000 | sum_transactions.php 140

{
    "2013-10-09": {
        "generate": 0.0533301,
        "generate_count": 1,
        "generate_usd": 7.466214
    }, 
    "2013-10-11": {
        "generate": 0.01159667,
        "generate_count": 1,
        "generate_usd": 1.6235338
    }, 
    "2013-10-13": {
        "generate": 0.04773484,
        "generate_count": 3,
        "generate_usd": 6.6828776
    }, 
    "2013-10-14": {
        "generate": 0.14187266,
        "generate_count": 2,
        "generate_usd": 19.8621724
    },
    "2013-10-15": {
        "generate": 0.27180136,
        "generate_count": 3,
        "generate_usd": 38.0521904
    },
    "2013-10-16": {
        "immature": 0.23548219,
        "immature_count": 3,
        "immature_usd": 32.9675066
    }
}{
    "generate": 0.52633563,
    "generate_count": 10,   
    "immature": 0.23548219,
    "immature_count": 3
}{
    "all_count": 13,
    "all": 0.76181782,
    "revenue_count": 13,
    "revenue": 0.76181782, 
    "revenue_usd": 106.6544948
}

Code:
#!/usr/bin/env php

<?php

$fh 
STDIN;

$buf stream_get_contents$fh );

$usd_rate = @$argv[1];

$data json_decode$buf );

$amounts = array();
$daily = array();
foreach( 
$data as $trans ) {
        if( @
$trans->amount ) {
                if( @
$amounts[$trans->category] ) {
                        
$amounts[$trans->category] += $trans->amount;
                        
$amounts[$trans->category '_count'] += 1;
                }
                else { 
                        
$amounts[$trans->category] = $trans->amount;
                        
$amounts[$trans->category '_count'] = 1;
                }
               
$date date('Y-m-d'$trans->time );
                if( @
$daily[$date][$trans->category] ) {
                        
$daily[$date][$trans->category] += $trans->amount;
                        
$daily[$date][$trans->category '_count'] += 1;
                }
                else {
                        
$daily[$date][$trans->category] = $trans->amount;
                        
$daily[$date][$trans->category '_count'] = 1;
                }
                if( 
$usd_rate ) {
                        
$daily[$date][$trans->category '_usd'] = $daily[$date][$trans->category] * $usd_rate;
                }
        }
}

$summary = array();
$summary['all_count'] = @$amounts['generate_count'] + @$amounts['orphan_count'] + @$amounts['immature_count'];
$summary['all'] = @$amounts['generate'] + @$amounts['orphan'] + @$amounts['immature'];

$summary['revenue_count'] = @$amounts['generate_count'] + @$amounts['immature_count'];
$summary['revenue'] = @$amounts['generate'] + @$amounts['immature'];
if( 
$usd_rate ) {
        
$summary['revenue_usd'] = $summary['revenue'] * $usd_rate;
}

echo 
json_encode($dailyJSON_PRETTY_PRINT);
echo 
json_encode($amountsJSON_PRETTY_PRINT);
echo 
json_encode($summaryJSON_PRETTY_PRINT);

Psst!!  Wanna make bitcoin unstoppable? Why the Only Real Way to Buy Bitcoins Is on the Streets. Avoid banks and centralized exchanges.   Buy/Sell coins locally.  Meet other bitcoiners and develop your network.   Try localbitcoins.com or find or start a buttonwood / satoshi square in your area.  Pass it on!
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!