Skip to content

Instantly share code, notes, and snippets.

@maesoser
Created November 3, 2017 22:23
Show Gist options
  • Save maesoser/a34969cf1d664820240dfe9dac915e08 to your computer and use it in GitHub Desktop.
Save maesoser/a34969cf1d664820240dfe9dac915e08 to your computer and use it in GitHub Desktop.
Processing script which paints a sql database containing bitcoin transactions.
class Transaction{
String addr;
double amount;
String timestamp;
Transaction(String addr, double amount,String ts){
this.addr = addr;
this.amount = amount;
this.timestamp = ts;
}
}
class Account{
String addr;
float amount;
PVector pos = new PVector(0,0);
boolean isSrc;
boolean isDst;
ArrayList<Transaction> transactions;
Account(String addr){
this.addr = addr;
this.amount = 0.0;
pos.x = random(width) - 1;
pos.y = random(height) - 1;
transactions = new ArrayList<Transaction>();
isSrc = false;
isDst = false;
}
boolean isEqual(String addr){
if(addr.equals(this.addr)) return true;
return false;
}
boolean alreadyExists(ArrayList<Account> accounts, boolean isSrc, boolean isDst){
for (Account a : accounts) {
if(a.isEqual(this.addr)) {
if(isSrc) a.isSrc = true;
if(isDst) a.isDst = true;
return true;
}
}
return false;
}
void drawCircle(){
stroke(color(0,0,0,20));
fill(color(0,0,0,5));
ellipseMode(CENTER);
ellipse(pos.x,pos.y,1+(floor(amount)/2),1+(floor(amount)/2));
}
void display(){
if(isSrc){
stroke(color(255,0,0));
}
if(isDst){
stroke(color(255,0,255));
}
if(isSrc && isDst){
stroke(color(0,255,0));
}
point(pos.x,pos.y);
//text(addr,pos.x,pos.y);
}
void displayTransactions(ArrayList<Account> accounts){
for (Transaction a : transactions) {
PVector apos = getPos(accounts,a.addr);
stroke(color(0,0,0,3));
fill(color(0,0,0,3));
line(pos.x,pos.y, apos.x,apos.y);
point(pos.x,pos.y);
point(apos.x,apos.y);
}
}
}
import de.bezier.data.sql.*;
import java.util.Date;
import java.util.Collections;
import java.util.List;
PVector getPos(ArrayList<Account> accounts,String addr){
for (Account a : accounts) {
if(a.isEqual(addr)) return a.pos;
}
return new PVector(0,0);
}
ArrayList<Integer> getShuffledList(int maxn){
ArrayList<Integer> dataList = new ArrayList<Integer>();
for (int i = 0; i < maxn; i++) {
dataList.add(i);
}
Collections.shuffle(dataList);
return dataList;
}
SQLite db;
ArrayList<Account> accounts = new ArrayList<Account>();
int i = 0;
ArrayList<Integer> slist = new ArrayList<Integer>();
double total = 0;
void setup()
{
size( 1080, 1080 );
background(255);
smooth();
db = new SQLite( this, "ttr.db" ); // open database file
if ( db.connect() ){
db.query( "SELECT name as \"Name\" FROM SQLITE_MASTER where type=\"table\"" );
while (db.next()){
println( db.getString("Name") );
}
db.query( "SELECT DISTINCT SRC.ADDR as addr FROM SRC_ADDRESSES SRC");
while (db.next()){
Account acc = new Account(db.getString("addr"));
if(!acc.alreadyExists(accounts, true, false))
acc.isSrc = true;
accounts.add(acc);
}
db.query( "SELECT DISTINCT DST.ADDR as addr FROM DST_ADDRESSES DST");
while (db.next()){
Account acc = new Account(db.getString("addr"));
if(!acc.alreadyExists(accounts,false,true)){
acc.isDst = true;
accounts.add(acc);
}
}
println(accounts.size());
int linesize = floor(sqrt(accounts.size()));
int space =floor(width/linesize);
for (int i = 0; i < accounts.size(); i++) {
int x = i%linesize;
int y = (i - i%linesize)/linesize;
accounts.get(i).pos.x = space*x;
accounts.get(i).pos.y = space*y;
}
db.query( "SELECT TR.TIMESTAMP as ts, SRC.ADDR as srcaddr, DST.ADDR as dstaddr, SRC.AMOUNT as amount FROM SRC_ADDRESSES SRC INNER JOIN DST_ADDRESSES DST ON SRC.TID=DST.TID JOIN TRANSACTIONS TR ON TR.ROWID=SRC.TID");
while (db.next()){
String srcaddr = db.getString("srcaddr");
String dstaddr = db.getString("dstaddr");
for (Account a : accounts) {
if(a.isEqual(srcaddr)){
total += db.getDouble("amount")/100000000.0;
Transaction tr = new Transaction(dstaddr,db.getDouble("amount"),db.getString("ts"));
a.transactions.add(tr);
}
}
for (Account a : accounts) {
if(a.isEqual(dstaddr)){
a.amount += db.getDouble("amount")/100000000.0;
}
}
}
}
slist = getShuffledList(accounts.size());
i=0;
println(total);
for (Account a : accounts) a.drawCircle();
}
void draw(){
if(i==(accounts.size()-1)){
Date d = new Date();
long current= d.getTime()/1000;
saveFrame(current+".png");
exit();
}
String txt_fps = String.format(getClass().getSimpleName()+ " [size %d/%d] [frame %d] [fps %6.2f] [TR %d]", width, height, frameCount, frameRate,accounts.size());
surface.setTitle(txt_fps);
accounts.get(slist.get(i)).displayTransactions(accounts);
i++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment