# cited from: https://www.r-bloggers.com/querying-the-bitcoin-blockchain-with-r/
# install.packages("Rbitcoin")
library(Rbitcoin)
Bitcoin屬於permissionless的blockchain,任何人都可以使用Public API下載匯率資料:
wait <- antiddos(market = 'kraken', antispam_interval = 5, verbose = 1)
market.api.process('kraken',c('BTC','EUR'),'ticker')
## market base quote timestamp market_timestamp last vwap
## 1: kraken BTC EUR 2017-11-18 21:30:06 <NA> 6570 6579.426
## volume ask bid
## 1: 6089.515 6570 6562.8
trades <- market.api.process('kraken',c('BTC','EUR'),'trades')
Rbitcoin.plot(trades, col='blue')
要使用bitcoin做交易,就要先建立自己的帳號,然後才可以使用錢包做交易:
wallet <- blockchain.api.process('15Mb2QcgF3XDMeVn6M7oCG6CQLw4mkedDi')
seed <- '1NfRMkhm5vjizzqkp2Qb28N7geRQCa4XqC'
genesis <- '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa'
singleaddress <- blockchain.api.query(method = 'Single Address',
bitcoin_address = seed, limit=100)
txs <- singleaddress$txs
bitcoin的好處之一,是容易追蹤交易記錄。我們可以下載帳戶的記錄 …
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))
}}}
交易記錄經過整理,就可以畫出與其(有過交易的)往來帳戶的網路圖:
library(plyr)
btc <- ddply(bc, c("from", "to"), summarize, value=sum(value))
library(igraph)
##
## Attaching package: 'igraph'
## The following objects are masked from 'package:stats':
##
## decompose, spectrum
## The following object is masked from 'package:base':
##
## union
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))