123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- import pandas as pd
- from constants import *
- def weight_to_fee(weight, coeff):
- return coeff * weight
- def length_to_fee(length, coeff):
- return coeff * length
- def token_to_price(token, market_cap, issuance):
- return (market_cap / issuance) * token
- def price_weight_function(x, weight_coefficient, market_cap, issuance):
- return token_to_price(weight_to_fee(x, weight_coefficient), market_cap, issuance)
- def price_length_function(x, length_coefficient, market_cap, issuance):
- return token_to_price(length_to_fee(x, length_coefficient), market_cap, issuance)
- def print_var_err(var, extrn):
- print("WARNING: the parameter {} isn't defined in the calculation for extrinsic: {}".format(
- var[0], extrn))
- def calc_vars_weight(weight, extrinsic, params):
- total = 0
- if extrinsic in params:
- for var in weight[VARS]:
- if var[0] in params[extrinsic]:
- total += params[extrinsic][var[0]] * var[1]
- else:
- print_var_err(var, extrinsic)
- for var in weight[DB_READS][DB_VARS]:
- if var[0] in params[extrinsic]:
- total += params[extrinsic][var[0]] * var[1] * READ_WEIGHT
- else:
- print_var_err(var, extrinsic)
- for var in weight[DB_WRITES][DB_VARS]:
- if var[0] in params[extrinsic]:
- total += params[extrinsic][var] * WRITE_WEIGHT
- else:
- print_var_err(var, extrinsic)
- return total
- def calc_weight(weight, extrinsic, params):
- vars_weight = calc_vars_weight(weight, extrinsic, params)
- return vars_weight + \
- weight[BASE_WEIGHT] + \
- weight[DB_READS][BASE_DB] * READ_WEIGHT + \
- weight[DB_WRITES][BASE_DB] * WRITE_WEIGHT + EXTRINSIC_BASE_WEIGHT
- def calc_total_price_given_params(extrinsic, weight_coeff, market_cap, issuance, length_coeff, params, lengths, weights):
- return price_weight_function(calc_weight(weights[extrinsic], extrinsic, params), weight_coeff, market_cap, issuance) + \
- price_length_function(lengths.get(extrinsic, 0),
- length_coeff, market_cap, issuance)
- def calc_total_fee(extrinsic, weight_coeff, length_coeff, params, lengths, weights):
- return weight_to_fee(calc_weight(weights[extrinsic], extrinsic, params), weight_coeff) + \
- length_to_fee(lengths.get(extrinsic, 0), length_coeff)
- def get_computed_values(
- extrinsic,
- weight_model,
- weight_coeff,
- min_market_cap,
- max_market_cap,
- issuance,
- length_coeff,
- params,
- lengths,
- weights
- ):
- weight = calc_weight(weight_model, extrinsic, params)
- tokens = calc_total_fee(extrinsic, weight_coeff,
- length_coeff, params, lengths, weights)
- min_price = calc_total_price_given_params(
- extrinsic,
- weight_coeff,
- min_market_cap,
- issuance,
- length_coeff,
- params,
- lengths,
- weights
- )
- max_price = calc_total_price_given_params(
- extrinsic,
- weight_coeff,
- max_market_cap,
- issuance,
- length_coeff,
- params,
- lengths,
- weights
- )
- return weight, tokens, min_price, max_price
- def calc_all_price(weight_coeff, issuance, length_coeff, min_market_cap, max_market_cap, weights, params, lengths):
- names = []
- computed_weights = []
- computed_tokens = []
- min_prices = []
- max_prices = []
- for (key, val) in weights.items():
- weight, tokens, min_price, max_price = get_computed_values(
- key,
- val,
- weight_coeff,
- min_market_cap,
- max_market_cap,
- issuance,
- length_coeff,
- params,
- lengths,
- weights
- )
- names.append(key)
- computed_weights.append(weight)
- min_prices.append(min_price)
- max_prices.append(max_price)
- computed_tokens.append(tokens)
- weight_table = {
- "Extrinsic": names,
- "Weight": computed_weights,
- "Tokens(JOY)": computed_tokens,
- "Min Price(¢)": min_prices,
- "Max Price(¢)": max_prices
- }
- df = pd.DataFrame(weight_table)
- return df, min_prices, max_prices
- def get_weight_info(weights, weight_coeff=1, issuance=1, length_coeff=1, min_market_cap=1, max_market_cap=1, params={},
- lengths={}):
- weights[RUNTIME_UPGRADE] = {
- BASE_WEIGHT: MAX_BLOCK_WEIGHT,
- DB_READS: {
- BASE_DB: 0,
- DB_VARS: []
- },
- DB_WRITES: {
- BASE_DB: 0,
- DB_VARS: []
- },
- VARS: []
- }
- df, _, _ = calc_all_price(
- weight_coeff,
- issuance,
- length_coeff,
- min_market_cap,
- max_market_cap,
- weights,
- params,
- lengths,
- )
- return df
|