analysis.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import pandas as pd
  2. from constants import *
  3. def weight_to_fee(weight, coeff):
  4. return coeff * weight
  5. def length_to_fee(length, coeff):
  6. return coeff * length
  7. def token_to_price(token, market_cap, issuance):
  8. return (market_cap / issuance) * token
  9. def price_weight_function(x, weight_coefficient, market_cap, issuance):
  10. return token_to_price(weight_to_fee(x, weight_coefficient), market_cap, issuance)
  11. def price_length_function(x, length_coefficient, market_cap, issuance):
  12. return token_to_price(length_to_fee(x, length_coefficient), market_cap, issuance)
  13. def print_var_err(var, extrn):
  14. print("WARNING: the parameter {} isn't defined in the calculation for extrinsic: {}".format(
  15. var[0], extrn))
  16. def calc_vars_weight(weight, extrinsic, params):
  17. total = 0
  18. if extrinsic in params:
  19. for var in weight[VARS]:
  20. if var[0] in params[extrinsic]:
  21. total += params[extrinsic][var[0]] * var[1]
  22. else:
  23. print_var_err(var, extrinsic)
  24. for var in weight[DB_READS][DB_VARS]:
  25. if var[0] in params[extrinsic]:
  26. total += params[extrinsic][var[0]] * var[1] * READ_WEIGHT
  27. else:
  28. print_var_err(var, extrinsic)
  29. for var in weight[DB_WRITES][DB_VARS]:
  30. if var[0] in params[extrinsic]:
  31. total += params[extrinsic][var] * WRITE_WEIGHT
  32. else:
  33. print_var_err(var, extrinsic)
  34. return total
  35. def calc_weight(weight, extrinsic, params):
  36. vars_weight = calc_vars_weight(weight, extrinsic, params)
  37. return vars_weight + \
  38. weight[BASE_WEIGHT] + \
  39. weight[DB_READS][BASE_DB] * READ_WEIGHT + \
  40. weight[DB_WRITES][BASE_DB] * WRITE_WEIGHT + EXTRINSIC_BASE_WEIGHT
  41. def calc_total_price_given_params(extrinsic, weight_coeff, market_cap, issuance, length_coeff, params, lengths, weights):
  42. return price_weight_function(calc_weight(weights[extrinsic], extrinsic, params), weight_coeff, market_cap, issuance) + \
  43. price_length_function(lengths.get(extrinsic, 0),
  44. length_coeff, market_cap, issuance)
  45. def calc_total_fee(extrinsic, weight_coeff, length_coeff, params, lengths, weights):
  46. return weight_to_fee(calc_weight(weights[extrinsic], extrinsic, params), weight_coeff) + \
  47. length_to_fee(lengths.get(extrinsic, 0), length_coeff)
  48. def get_computed_values(
  49. extrinsic,
  50. weight_model,
  51. weight_coeff,
  52. min_market_cap,
  53. max_market_cap,
  54. issuance,
  55. length_coeff,
  56. params,
  57. lengths,
  58. weights
  59. ):
  60. weight = calc_weight(weight_model, extrinsic, params)
  61. tokens = calc_total_fee(extrinsic, weight_coeff,
  62. length_coeff, params, lengths, weights)
  63. min_price = calc_total_price_given_params(
  64. extrinsic,
  65. weight_coeff,
  66. min_market_cap,
  67. issuance,
  68. length_coeff,
  69. params,
  70. lengths,
  71. weights
  72. )
  73. max_price = calc_total_price_given_params(
  74. extrinsic,
  75. weight_coeff,
  76. max_market_cap,
  77. issuance,
  78. length_coeff,
  79. params,
  80. lengths,
  81. weights
  82. )
  83. return weight, tokens, min_price, max_price
  84. def calc_all_price(weight_coeff, issuance, length_coeff, min_market_cap, max_market_cap, weights, params, lengths):
  85. names = []
  86. computed_weights = []
  87. computed_tokens = []
  88. min_prices = []
  89. max_prices = []
  90. for (key, val) in weights.items():
  91. weight, tokens, min_price, max_price = get_computed_values(
  92. key,
  93. val,
  94. weight_coeff,
  95. min_market_cap,
  96. max_market_cap,
  97. issuance,
  98. length_coeff,
  99. params,
  100. lengths,
  101. weights
  102. )
  103. names.append(key)
  104. computed_weights.append(weight)
  105. min_prices.append(min_price)
  106. max_prices.append(max_price)
  107. computed_tokens.append(tokens)
  108. weight_table = {
  109. "Extrinsic": names,
  110. "Weight": computed_weights,
  111. "Tokens(JOY)": computed_tokens,
  112. "Min Price(¢)": min_prices,
  113. "Max Price(¢)": max_prices
  114. }
  115. df = pd.DataFrame(weight_table)
  116. return df, min_prices, max_prices
  117. def get_weight_info(weights, weight_coeff=1, issuance=1, length_coeff=1, min_market_cap=1, max_market_cap=1, params={},
  118. lengths={}):
  119. weights[RUNTIME_UPGRADE] = {
  120. BASE_WEIGHT: MAX_BLOCK_WEIGHT,
  121. DB_READS: {
  122. BASE_DB: 0,
  123. DB_VARS: []
  124. },
  125. DB_WRITES: {
  126. BASE_DB: 0,
  127. DB_VARS: []
  128. },
  129. VARS: []
  130. }
  131. df, _, _ = calc_all_price(
  132. weight_coeff,
  133. issuance,
  134. length_coeff,
  135. min_market_cap,
  136. max_market_cap,
  137. weights,
  138. params,
  139. lengths,
  140. )
  141. return df