<jupyter_start><jupyter_text>ThorViz Notebook<jupyter_code>from typing import Dict
import numpy as np
import requests
import pandas as pd<jupyter_output><empty_output><jupyter_text>Resource: https://testnet.midgard.thorchain.info/v2/docoperation/GetPools<jupyter_code>my_request = 'https://midgard.thorchain.info/v2/pools'
    
r = requests.get(my_request)
response_dict = r.json()
response_dict.keys()
pool_df = pd.DataFrame(response_dict)
pool_df.head(2)
pool_df['assetDepth'] = pool_df['assetDepth'].astype(float) / 1e8 

pool_df['assetPriceUSD'] = pool_df['assetPriceUSD'].astype(float)
pool_df[pool_df['asset'] == 'BTC.BTC']['assetDepth'] #.astype(float) / 1e8
pool_df[pool_df['asset'] == 'BTC.BTC']['tv_usd'].round(2)
pool_df[pool_df['asset'] == 'BNB.BNB']['assetDepth']# .astype(float) / 1e8
# Calculate total value Locked for NON rune 

# Locked Value
pool_df['tv_usd'] = pool_df['assetPriceUSD'] * pool_df['assetDepth']

pool_df['runeDepth'] = pool_df['runeDepth'].astype(float) / 1e8
pool_df['rune_vt_usd'] = pool_df['runeDepth'] * 
# Sum of locked value
pool_df['tv_usd'].sum()
pool_df['runeDepth'].astype(float).sum() / 100000000
pool_df['adj_runeDepth'] = pool_df['runeDepth'].apply(lambda x: int(x) / 100_000_000)
pool_df['adj_runeDepth'].sum()<jupyter_output><empty_output><jupyter_text>Note: can also get the value from: https://midgard.thorchain.info/v2/stats`runeDepth column`<jupyter_code>50518348515320 
pool_df[pool_df['asset'] == 'BNB.BNB']['adj_runeDepth'].values #.value / 1000000<jupyter_output><empty_output><jupyter_text>Now lets get Bonded Rune<jupyter_code>network_data_request = 'https://midgard.thorchain.info/v2/network'
    
rn = requests.get(network_data_request)
net_response_dict = rn.json()
net_response_dict['bondMetrics']<jupyter_output><empty_output><jupyter_text>SLAW method<jupyter_code>import ftx


def get_market_price() -> float:
    
    ftx_client = ftx.FtxClient()
    
    result = ftx_client.get_market('RUNE/USD')
    
    market_price = result['price']
    
    return market_price
    
get_market_price()
!pip list | grep c
pd.set_option('display.float_format', lambda x: '%.5f' % x)
import pandas as pd
import ftx
import numpy 
import requests


def get_market_price() -> float:
    
    ftx_client = ftx.FtxClient()
    
    result = ftx_client.get_market('RUNE/USD')
    
    market_price = result['price']
    
    return market_price


def get_rune_stats() -> pd.DataFrame:
    
    '''
    Slaw's method.
    '''
    
    market_price = get_market_price()
    
    # MCCN
    mccn = requests.get('https://midgard.thorchain.info/v2/network')
    mccn_dict = mccn.json()
    
    mccn_total_pooled_rune = float(mccn_dict['totalPooledRune']) / 1e7
    mccn_total_active_bond = float(mccn_dict['bondMetrics']['totalActiveBond']) / 1e7 
    
    # ---
    
    # SCCN
    sccn = requests.get('http://thorb.sccn.nexain.com:8080/v1/network')
    sccn_dict = sccn.json()
    
    sccn_total_staked_rune = float(sccn_dict['totalStaked']) / 1e7
    sccn_total_active_bond = float(sccn_dict['bondMetrics']['totalActiveBond']) / 1e7 
    
    # calculations
    
    rune_in_lp_count = mccn_total_pooled_rune + sccn_total_staked_rune
    rune_bonded_count = mccn_total_active_bond + sccn_total_active_bond
    
    total_in_network_count = rune_in_lp_count + rune_bonded_count
    
    deterministic_value = rune_in_lp_count * market_price * 3 # In USD
    
    determined_price = deterministic_value / total_in_network_count # In USD
    
    speculation = market_price - determined_price # USD
    
    speculation_pct = speculation / market_price
    
    # Collect Results
    result_dict = {
        'Rune_in_LP_count': rune_in_lp_count,
        'Rune_bonded_count': rune_bonded_count,
        'total_in_network_count': total_in_network_count,
        'deterministic_value_usd': deterministic_value,
        'determined_price': determined_price,
        'market_price_usd': market_price,
        'speculation_premium_usd': speculation,
        'speculation_pct_of_market': speculation_pct,
    }
    
    return pd.DataFrame(result_dict, index=[0]).T.round(2)
get_rune_stats()
RUNE_in_LP_count = totalStaked_SCCN + totalPooleRune_MCCN
RUNE_bonded_count = totalActiveBond_SCCN + totalActiveBond_MCCN

total_in_network_count = RUNE_in_LP_count + RUNE_bonded_count

deterministic_value = RUNE_in_LP_count * market_price * 3
determined_price = deterministic_value / total_in_network_count
speculation = market_price - determined_price<jupyter_output><empty_output><jupyter_text>In-Network Rune CalculationFor the in-network rune calc we need the following values: 1. `runeDepth`, depth of rune in LP. source: https://midgard.thorchain.info/v2/stats2. `totalActiveBond`, source: https://midgard.thorchain.info/v2/network3. `totalStandbyBond`, source: same as (2)4. `totalReserve`, source: same as (2) 5. `totalPooledRune`, source: same as (2)  <-  Ask if this is same as `runeDepth`?<jupyter_code>my_request = 'https://midgard.thorchain.info/v2/pools'
    
r = requests.get(my_request)
response_dict = r.json()
#    response_dict.keys()
pd.DataFrame(response_dict)
def get_multichain_pool_data():
    
    my_request = 'https://midgard.thorchain.info/v2/pools'
    
    r = requests.get(my_request)
    response_dict = r.json()
    response_dict.keys()
    
def get_singlechain_stats():
    
    '''
    Returns Datafraome containing global stats for all books and all transaction from BEPSwap.
    
    Documentation: https://testnet.midgard.thorchain.info/v2/doc#operation/GetStats
    '''
    
    _url = "https://midgard.thorchain.info/v2/stats"
    rn = requests.get(_url)
    response_dict = rn.json()
    
    return pd.DataFrame([response_dict]).astype(float)
import requests
import pandas as pd

def get_non_rune_TVL():
    
    my_request = 'https://midgard.thorchain.info/v2/pools'
    
    r = requests.get(my_request)
    response_dict = r.json()
    
    
    return pd.DataFrame(response_dict)

    
df = get_non_rune_TVL()
df.to_csv('pool-data-2021-05-05.csv')
df.head(2)
def calculate_non_rune_TVL(df) -> float:
    '''
    Calulates total non Rune value locked in the Network by summing
    all Pool USD value.
    '''
    df = df.copy()
    
    df['total_value_USD'] = df['assetPriceUSD'].astype(float) * (df['assetDepth'].astype(float) / 1e7 )
    
    total_non_rune_tvl = df['total_value_USD'].sum()
    
    return total_non_rune_tvl

tvl = calculate_non_rune_TVL(df)
tvl
v2_stats_df = get_v2_stats()
v2_stats_df
v2_stats_df['runeDepth'].astype(float)
v2_stats_df.columns.tolist()
def get_NetworkRuneMetrics_stats():
    
    network_data_request = 'https://midgard.thorchain.info/v2/network'
    rn = requests.get(network_data_request)
    net_response_dict = rn.json()
    
    _df = pd.DataFrame([net_response_dict['bondMetrics']]).astype(float)
    
    _df['totalPooledRune'] = float(net_response_dict['totalPooledRune'])
    _df['totalReserve'] = float(net_response_dict['totalReserve'])
    
    return _df
    
networkrune_df = get_NetworkRuneMetrics_stats()
networkrune_df.to_dict()<jupyter_output><empty_output><jupyter_text>Calculate In-Network Rune<jupyter_code>def calc_in_network_rune(network_rune_df: pd.DataFrame, stats_df: pd.DataFrame):
    
    assert network_rune_df.shape[0] == 1, "`network_rune_df` is wrong shape, should be single row"
    # TODO add assert for stats_df
    
    total_bond = network_rune_df['totalActiveBond'][0] + network_rune_df['totalStandbyBond'][0]
    
    # TODO: Verify that `runeDepth`
    total_pooled = network_rune_df['totalPooledRune'][0] + stats_df['runeDepth'][0]
    
    print((network_rune_df['totalPooledRune'][0] - stats_df['runeDepth'][0])/ 1e7)
    
    total_reserve = network_rune_df['totalReserve'][0]
    
    return np.round((total_bond + total_pooled + total_reserve) / 1e7, 2)
calc_in_network_rune(network_rune_df=networkrune_df, stats_df=v2_stats_df)
bond_df['totalActiveBond'].values + bond_df['totalStandbyBond'].values 
#bond_df = pd.DataFrame([net_response_dict['bondMetrics']])
#bond_df = bond_df.astype(int)
#total_bond_rune = bond_df['totalActiveBond'] + bond_df['totalStandbyBond']
total_bond_rune[0] / 100_000_000
17 + 6 + 
"totalPooledRune": "50518030721572",
  "totalReserve": "484395137229"
50518030721572 + 484395137229 + 239860003651453
34 - (29 + 4.8) # Total Reserve, total pooled + total bonded (both active and standby)
34 - 29
"totalPooledRune": "50518030721572",
"totalReserve": "484395137229",
"totalActiveBond": "179864730344791",
"totalStandbyBond": "60002442000004",
(50518030721572
+ 484395137229
+ 179864730344791
+ 60002442000004)
# from: https://midgard.thorchain.info/v2/stats
50449426289833  + 290869598203596
 (   390000000
    + 392000000
    + 486000000
    + 490000002
    + 30000292000001
    + 30000392000001) / 100_000_000
60002442000004 / 100_000_000

"totalActiveBond": "179864730344791",
    "totalStandbyBond": "60002442000004"
pool_df[pool_df['asset']]
def get_data():
    
    
    my_request = 'https://midgard.thorchain.info/v2/pools'
    
    r = requests.get(my_request)
    response_dict = r.json()
    response_dict.keys()
So we need to get all the pool data for pool rune
and we need to get bonded rune and sum
value_dict = {
    'non_rune_TVL': 91, # in Million USD
    'in_network_rune': 33.6, # in Million USD 
    'market_price': 13.45,
    
}
def new_det_price_calc(non_rune_tvl, circ_supply):
    
    '''
    source: https://thorchain.org/rune#what-influences-it
    '''
    
    det_price = (3 * non_rune_tvl) / circ_supply
    
    return det_price
new_det_price_calc(non_rune_tvl=13_000_000, circ_supply=235_000_000)
def calculate_deterministic_price(value_dict: Dict[str, float]):
  
 NEED TO REVISE THIS!!!!!
#     det_value = value_dict.get('non_rune_TVL') * 3
    
#     # Deterministic price per Rune, `baseline` from @Slawteh
#     det_price = det_value / value_dict.get('in_network_rune')
    
#     # Speculation Premium 
#     spec_prem = value_dict.get('market_price') - det_price
    

#     print(f"Deterministic Price, USD: ${np.round(det_price ,2)}")
#     print(f"Speculative Premium, USD: ${np.round(spec_prem ,2)}")
#     #return 
    
calculate_deterministic_price(value_dict)
def calculate_rune_elasticity(value_dict: Dict[str, float], det_price: float):
    
    # Rune Cap, Deterministic Price elasticity
    new_det_value = (1 + value_dict.get('non_rune_TVL')) * 3
    
    new_det_price = new_det_value / (1 + value_dict.get('in_network_rune'))
    # increase of 1M rune to det price
    #inc_det_price = det_value / ( 1 + value_dict.get('in_network_rune'))
    # Rune Cap Elasticity, +1M Rune -> +x price increase in Rune price.
    cap_elasticity = new_det_price - det_price 
    
    
    print(new_det_price)
    print(f"Rune Cap Elasticity, USD: ${np.round(cap_elasticity ,2)}")
    

calculate_rune_elasticity(value_dict, det_price=8.12)<jupyter_output>7.976878612716763
Rune Cap Elasticity, USD: $-0.14
{'fraction_non_alphanumeric': 0.12026705688677519, 'fraction_numerical': 0.04883848545820377, 'mean_word_length': 4.386699507389163, 'pattern_counts': {'":': 8, '<': 29, '<?xml version=': 0, '>': 33, 'https://': 16, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '292780', 'license': 'MIT', 'n_tokens_mistral': 4541, 'n_tokens_neox': 4101, 'n_words': 659, 'path': 'ThorVizNotebook.ipynb', 'repo_name': 'Cattleman/thor-viz', 'size': '10934'}