Rbitcoin

The function antiddos makes sure that you’re not overusing the Bitcoin API. A reasonable query interval should be one query every 10s.

The first two examples all were based on aggregated values. But the Blockchain API allows to read every single transaction in the history of Bitcoin.

The third example on how to query historical transactions for one address and then mapping the connections between all addresses in this strand of the Blockchain. The red dot is the address we were looking at (so you can change the value to one of your own Bitcoin addresses):

library(Rbitcoin)
## Loading required package: data.table
## You are currently using Rbitcoin 0.9.2, be aware of the changes coming in the next releases (0.9.3 - github, 0.9.4 - cran). Do not auto update Rbitcoin to 0.9.3 (or later) without testing. For details see github.com/jangorecki/Rbitcoin. This message will be removed in 0.9.5 (or later).
library(plyr)
library(igraph)
## 
## Attaching package: 'igraph'
## The following objects are masked from 'package:stats':
## 
##     decompose, spectrum
## The following object is masked from 'package:base':
## 
##     union
## Loading required package: data.table
## You are currently using Rbitcoin 0.9.2, be aware of the changes coming in the next releases (0.9.3 - github, 0.9.4 - cran). Do not auto update Rbitcoin to 0.9.3 (or later) without testing. For details see github.com/jangorecki/Rbitcoin. This message will be removed in 0.9.5 (or later).

wait <- antiddos(market = 'btce', antispam_interval = 5, verbose = 1)
market.api.process('btce',c('BTC','USD'),'ticker')
##    market base quote           timestamp    market_timestamp     last
## 1:   btce  BTC   USD 2017-03-02 09:38:03 2017-03-02 14:37:58 1223.046
##       vwap  volume      ask      bid
## 1: 1206.31 7798876 1224.998 1223.046
# market base quote           timestamp    market_timestamp     last
#1:   btce  BTC   USD 2017-03-02 09:11:44 2017-03-02 14:11:39 1222.998
#      vwap  volume      ask      bid
#1: 1205.81 8040112 1222.998 1220.101

trades <- market.api.process('btce',c('BTC','USD'),'trades')
Rbitcoin.plot(trades, col='blue')

Mapping Bitcon Transaction

You can also map the transaction, for example:

wallet <- blockchain.api.process('15Mb2QcgF3XDMeVn6M7oCG6CQLw4mkedDi')
seed <- '1NfRMkhm5vjizzqkp2Qb28N7geRQCa4XqC'
genesis <- '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa'
singleaddress <- blockchain.api.query(method = 'Single Address', bitcoin_address = seed, limit=100)
txs <- singleaddress$txs

bc <- data.frame()
for (t in txs) {
  hash <- t$hash
  for (inputs in t$inputs) {
    from <- inputs$prev_out$addr
    for (out in t$out) {
      to <- out$addr
      va <- out$value
      bc <- rbind(bc, data.frame(from=from,to=to,value=va, stringsAsFactors=F))
    }
  }
}


btc <- ddply(bc, c("from", "to"), summarize, value=sum(value))


btc.net <- graph.data.frame(btc, directed=T)
V(btc.net)$color <- "blue"
V(btc.net)$color[unlist(V(btc.net)$name) == seed] <- "red"
nodes <- unlist(V(btc.net)$name)
E(btc.net)$width <- log(E(btc.net)$value)/10
plot.igraph(btc.net, vertex.size=5, edge.arrow.size=0.1, vertex.label=NA, main=paste("BTC transaction network for\n", seed))