Exploring various blockchains using Python
We wrote a small script to query the block chains of Namecoin, Litecoin, Peercoin, Bitcoin and Dogecoin. It is meant as simple example for programmers new to blockchain technology. You do not need to download any full blockchain for this example. The script currently has this functionality:
- explore blocks
- explore transactions
- explore addresses
Screenshot of bkchain
The script is minimalistic, I made it in 20 minutes, and like all software it could be improved. I think it is simple enough to change to your own wishes. It relies on https://bkchain.org/ to collect the data, but could be modified for any webserver that has an JSON API interface. To see the list of API calls visit https://bkchain.org/static/api.txt Every function is basically dealing with JSON data, while on the bottom there are some calls to these functions.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
# query the Liteocin, Peercoin, Namecoin, Bitcoin and Dogecoin blockchain using the bkchain.org API.
# Full description of the API: https://bkchain.org/static/api.txt
#
# Support:
# - explore blocks
# - explore transactions
# - explore addresses
#
# I've implemented a simple sample code in Python to explore these blockchains.
# Feel free to copy, redistribute, modify, push to github etc etc.
#
# visit me @ talkera.org
import urllib2
import json
import numpy as np
import matplotlib.pyplot as plt
from pprint import pprint
def getBalance(currency, address):
response = urllib2.urlopen('https://bkchain.org/' + currency + '/api/v1/address/balance/' + address)
data = response.read()
data = json.loads(data)
return data[0]['balance']
def getTransactionOut(currency, tx_hash):
response = urllib2.urlopen('https://bkchain.org/' + currency + '/api/v1/tx/hash/' + tx_hash)
data = response.read()
data = json.loads(data)
return data['out']
def getTransactionConfirmations(currency, tx_hash):
response = urllib2.urlopen('https://bkchain.org/' + currency + '/api/v1/tx/hash/' + tx_hash)
data = response.read()
data = json.loads(data)
return data['confirmations']
def getTransactionJSON(currency, tx_hash):
response = urllib2.urlopen('https://bkchain.org/' + currency + '/api/v1/tx/hash/' + tx_hash)
data = response.read()
data = json.loads(data)
return data
def getBlockJSON(currency, blk_index):
response = urllib2.urlopen('https://bkchain.org/' + currency + '/api/v1/block/index/' + str(blk_index))
data = response.read()
data = json.loads(data)
return data
print "------------------------------------------"
print "Namecoin : "
print "------------------------------------------"
print "Balance: ",
print getBalance('nmc','NDGh9jR3DYxKK9jYB4HdkzYE5HGaHiZnTd')
print "Tx out: ",
print getTransactionOut('nmc','bc5da2a8b64e8eda9045592bf77ef14f00c25f37cb4d063141660cc052dc2777')
print "Tx conf: ",
print getTransactionConfirmations('nmc','bc5da2a8b64e8eda9045592bf77ef14f00c25f37cb4d063141660cc052dc2777')
print "Tx JSON out: ",
print getTransactionJSON('nmc', 'bc5da2a8b64e8eda9045592bf77ef14f00c25f37cb4d063141660cc052dc2777')['out']
print "Block reward: ",
print getBlockJSON('nmc',0)['reward']
|
Save the script as coin.py and run:
1
|
python coin.py
|
This will output the data for those addresses and blocks. To change currency simply change ‘nmc’ to the blockchain you want. The code does not have proper error checking yet. Have fun with it!
Recent Comments