In [1]:
%load_ext autoreload
%autoreload 2
It looks like the Colu explorer doesn't have a way to get all assets currently issued. Options are:
Setting up your own colored coins explorer (basically a full bitcoin node)
Using the API to crawl from asset
--> holders
--> assets
--> holders
With (2) we're essentially building an asset tree, which is a good entry point for further analysis.
In [2]:
import pandas as pd
from colupy import Asset, Holder, Colu
In [3]:
romanian_votecoinId = 'Ua7qAZa2UQioKf4Z4KKFwgpzUug3Ztczm7YgE4' # Romanian votecoin from bitnation
romanian_votecoin = Asset(romanian_votecoinId)
metadata = romanian_votecoin.get_metadata(use_utxo=True) #returns a dict
In [4]:
pd.DataFrame(metadata).T
Out[4]:
In [5]:
pd.DataFrame.from_dict(metadata['metadataOfIssuence'])
Out[5]:
In [6]:
pd.DataFrame(metadata['metadataOfIssuence']['data']['userData']['meta'])
Out[6]:
In [7]:
print romanian_votecoin.as_dict().keys()
In [8]:
romanian_votecoin_dict = romanian_votecoin.as_dict()
In [9]:
loaded_asset = Asset(asset_dict=romanian_votecoin_dict)
loaded_asset_dict = loaded_asset.as_dict()
for k,v in romanian_votecoin_dict.items():
assert(v == loaded_asset_dict[k])
pd.DataFrame(loaded_asset.get_stakeholders())
Out[9]:
In [11]:
romanian_votecoin.save('testdata/'+ romanian_votecoinId + '.json')
In [12]:
loaded_asset = Asset(asset_file='testdata/' + romanian_votecoinId + '.json')
In [13]:
pd.DataFrame(loaded_asset.get_stakeholders())
Out[13]:
In [14]:
holders = pd.DataFrame(romanian_votecoin.get_stakeholders())
In [15]:
holders.address[2]
Out[15]:
In [16]:
some_romanian_votecoin_holder = Holder(holders.address[2])
assets = some_romanian_votecoin_holder.get_assets()
assets
Out[16]:
In [17]:
assets.assetId.unique()
Out[17]:
In [18]:
print some_romanian_votecoin_holder.as_json(indent=2)[:400]
In [19]:
rv_holder_dict = some_romanian_votecoin_holder.as_dict()
load the holder from dict
In [20]:
loaded_holder = Holder(holder_dict = rv_holder_dict)
loaded_holder_dict = loaded_holder.as_dict()
In [21]:
loaded_holder.address
Out[21]:
In [22]:
loaded_holder.save('testdata/' + loaded_holder.address + '.json', indent=2)
In [23]:
holder_dict = Holder(holder_file = 'testdata/' + loaded_holder.address + '.json').as_dict()
In [24]:
colu = Colu()
In [25]:
romanian_votecoin = colu.get_asset(romanian_votecoinId, verbose = True)
Cached assets and holders can be retrieved from Colu object. Use force
to force download and refresh of asset/holder, creating a new instance.
In [26]:
assert(colu.get_asset(romanian_votecoinId, force = True, verbose = True) != romanian_votecoin)
Get assets held by some romanian_votecoin stakeholder
In [37]:
romanian_votecoin_stakeholders = pd.DataFrame(romanian_votecoin.get_stakeholders())
romanian_votecoin_stakeholders
Out[37]:
In [38]:
romanian_votecoin_stakeholders_0 = colu.get_holder(romanian_votecoin_stakeholders.address[0])
In [39]:
romanian_votecoin_stakeholders_0.get_assets()
Out[39]:
Iterate through manager's assets and holders
In [40]:
mycityId = u'La8Hvi9QSWxH7gr6ofdi6JefUSjm2djoBxv7VT'
In [41]:
colu.get_asset(mycityId)
colu.assets.keys()
Out[41]:
In [42]:
colu_dict = colu.as_dict()
In [43]:
loaded_colu = Colu(colu_dict = colu_dict)
In [44]:
colu.save('testdata/colu_save.json')
In [45]:
cat testdata/colu_save.json
In [55]:
colu = Colu(colu_file = 'testdata/colu_save.json')
In [52]:
print colu.as_json(indent = 2)[:400]
In [57]:
colu = Colu()
In [58]:
found_assets = colu.crawl_assets([mycityId], wait = 0, verbose = True)
found_assets
Out[58]:
In [59]:
pd.Series(list(colu._loaded))
Out[59]:
In [60]:
existing = pd.DataFrame.from_dict(colu.assets, orient='index')
existing = existing.assign(loaded = False)
existing.loc[list(colu._loaded), 'loaded'] = True
existing
Out[60]:
In [61]:
existing = existing.assign(search_set = False)
existing.loc[list(colu._search_set), 'search_set'] = True
existing
Out[61]:
We should know the holders of the loaded assets. The holders of the search set assets should be unknown. Assets of the holders should be known.
In [62]:
for assetId, asset in colu.assets.items():
print assetId
metadata = asset.get_metadata(use_utxo=True, force = False)
print metadata['metadataOfIssuence']['data']['description']
print pd.Series(metadata)
print pd.DataFrame.from_dict(metadata['metadataOfIssuence']), '\n'
In [63]:
holder0 = colu.holders['17cXQREyFVBiKoh4TdPozWDn5D4ounheK8']
In [64]:
len(holder0.get_assets())
Out[64]:
In [65]:
# mycity_search.save('mycity_search.json')
# colu = Colu(colu_file='mycity_search.json')
In [66]:
colu.get_asset_graph(min_assets=2)
In [67]:
colu.weight_assets(min_assets=2)
first add all the known assets
Connect all the assets to their respective holders
Add edges connecting assets with their holders, weighted by the amount held? This will draw big holders relatively closer to their assets, but there's some kind of pareto distribution that makes this ugly.
In [69]:
mycity_asset = colu.get_asset(mycityId)
# (<br>), bold (<b></b>), italics (<i></i>), hyperlinks (<a href='...'></a>). Tags <em>, <sup>, <sub> <span>
def get_issuance_HTML(asset):
issuance = asset.get_metadata()['metadataOfIssuence']['data']
asset_name = issuance['assetName']
asset_url = issuance['userData']['meta'][0]['value']
# asset_value = issuance['userData']['meta'][0]
return dict(name = '<b>' + asset_name + '</b>',
issuance = issuance)
get_issuance_HTML(mycity_asset)['name']
Out[69]:
In [70]:
# Assign weights by holder
In [71]:
import plotly
plotly.offline.init_notebook_mode() # run at the start of every ipython notebook
In [72]:
import networkx as nx
import plotly
from plotly.offline import iplot, plot
from plotly.graph_objs import Scatter, Line, Marker, Figure, Data, Layout, XAxis, YAxis, Scattergl
from textwrap import wrap
def graph_assets(self, title_assetId, layout_fn = nx.spring_layout, layout_kwargs = {}):
title_asset = self.get_asset(title_assetId)
asset_list = []
for node in self.graph.nodes():
if self.graph.node[node]['isasset']:
asset_list.append(node)
pos=nx.spring_layout(self.graph,weight=None, scale=.002)
pos=nx.spring_layout(self.graph,pos = pos, fixed = asset_list, scale = .002)
edge_trace = Scatter(
x=[],
y=[],
name = 'ownership',
line=Line(width=0.5,color='#888'),
hoverinfo='none',
mode='lines')
for edge in self.graph.edges():
x0, y0 = pos[edge[0]]
x1, y1 = pos[edge[1]]
edge_trace['x'] += [x0, x1, None]
edge_trace['y'] += [y0, y1, None]
asset_trace = Scatter(
x=[],
y=[],
text=[],
name = 'Assets',
mode='markers',
hoverinfo='text',
marker=Marker(
# showscale=True,
symbol = 'square',
colorscale='YIGnBu',
reversescale=True,
color=[],
size=25,
line=dict(width=2)
)
)
holder_trace = Scatter(
x=[],
y=[],
text=[],
name = 'Holders',
mode='markers',
hoverinfo='text',
marker=Marker(
# symbol = 'circle',
colorscale='YIGnBu',
reversescale=True,
color=[],
size=15,
line=dict(width=1)))
for node in self.graph.nodes():
x, y = pos[node]
if self.graph.node[node]['isasset']:
asset_trace['x'].append(x)
asset_trace['y'].append(y)
else:
holder_trace['x'].append(x)
holder_trace['y'].append(y)
for node_number, adjacencies in enumerate(self.graph.adjacency_list()):
nodeId = self.graph.nodes()[node_number]
node_info = '<b>' + nodeId + '</b><br>'
if self.graph.node[nodeId]['isasset']:
asset_trace['marker']['color'].append(len(adjacencies))
node_info = node_info + '# of holders: '+str(len(adjacencies)) + '<br>'
metadata = self.assets[nodeId].get_metadata()
if metadata.has_key('metadataOfIssuence'):
description = metadata['metadataOfIssuence']['data']['description']
node_info = node_info + '<br>'.join(wrap(description, 50))
asset_trace['text'].append(node_info)
else:
num_assets = len(self.holders[nodeId].get_assets())
node_info = node_info + '# of assets: '+ str(num_assets)
holder_trace['text'].append(node_info)
asset_trace['marker']['color'].append(num_assets)
fig = Figure(data=Data([edge_trace, asset_trace, holder_trace]),
layout=Layout(
title='Colored Coins Asset Graph for: ' +
get_issuance_HTML(title_asset)['name'] +
'<br>' + title_asset.assetId,
titlefont=dict(size=16),
showlegend=True,
hovermode='closest',
margin=dict(b=20,l=5,r=5,t=40),
xaxis=XAxis(showgrid=False, zeroline=False, showticklabels=False),
yaxis=YAxis(showgrid=False, zeroline=False, showticklabels=False)))
return fig
In [74]:
plot(graph_assets(colu,mycityId, layout_fn=nx.spring_layout))
Out[74]: