darkie.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. from core.utils import *
  2. from core.strategy import *
  3. class Darkie():
  4. def __init__(self, airdrop, initial_stake=None, vesting=[], hp=False, commit=True, epoch_len=EPOCH_LENGTH, strategy=random_strategy(EPOCH_LENGTH)):
  5. self.vesting = vesting
  6. self.stake = (Num(airdrop) if hp else airdrop)
  7. self.initial_stake = [self.stake]
  8. self.Sigma = None
  9. self.feedback = None
  10. self.f = None
  11. self.epoch_len=epoch_len # epoch length during which the stake is static
  12. self.strategy = strategy
  13. self.slot = 0
  14. self.won_hist = [] # winning history boolean
  15. def clone(self):
  16. return Darkie(self.stake)
  17. def apy_scaled_to_runningtime(self, rewards):
  18. avg_apy = 0
  19. for idx, reward in enumerate(rewards):
  20. current_epoch_staked_tokens = Num(self.strategy.staked_tokens_ratio[idx-1]) * Num(self.initial_stake[idx-1])
  21. avg_apy += (Num(reward) / current_epoch_staked_tokens) if current_epoch_staked_tokens!=0 else 0
  22. return avg_apy * Num(ONE_YEAR/(self.slot/EPOCH_LENGTH)) if self.slot and self.initial_stake[0]>0 >0 else 0
  23. def vesting_wrapped_initial_stake(self):
  24. #print('initial stake: {}, corresponding vesting: {}'.format(self.initial_stake[0], self.vesting[int((self.slot)/VESTING_PERIOD)]))
  25. # note index is previous slot since update_vesting is called after background execution.
  26. return self.current_vesting() if self.slot>0 else self.initial_stake[0]
  27. def apr_scaled_to_runningtime(self):
  28. initial_stake = self.vesting_wrapped_initial_stake()
  29. #print('stake: {}, initial_stake: {}'.format(self.stake, initial_stake))
  30. assert self.stake >= initial_stake, 'stake: {}, initial_stake: {}, slot: {}, current: {}, previous: {} vesting'.format(self.stake, initial_stake, self.slot, self.current_vesting(), self.prev_vesting())
  31. return Num(self.stake - initial_stake) / Num(initial_stake) * Num(ONE_YEAR/(self.slot/EPOCH_LENGTH)) if self.slot> 0 and self.initial_stake[0]>0 else 0
  32. def staked_tokens(self):
  33. '''
  34. the ratio of the staked tokens during the epochs
  35. of the total running time
  36. '''
  37. return Num(self.initial_stake[0])*self.staked_tokens_ratio()
  38. def staked_tokens_ratio(self):
  39. staked_ratio = Num(sum(self.strategy.staked_tokens_ratio)/len(self.strategy.staked_tokens_ratio))
  40. #print('type: {}, ratio: {}'.format(self.strategy.type, staked_ratio))
  41. assert staked_ratio <= 1 and staked_ratio >=0, 'staked_ratio: {}'.format(staked_ratio)
  42. return staked_ratio
  43. def set_sigma_feedback(self, sigma, feedback, f, count, hp=True):
  44. self.Sigma = (Num(sigma) if hp else sigma)
  45. self.feedback = (Num(feedback) if hp else feedback)
  46. self.f = (Num(f) if hp else f)
  47. self.slot = count
  48. def run(self, hp=True):
  49. k=N_TERM
  50. def target(tune_parameter, stake):
  51. x = (Num(1) if hp else 1) - (Num(tune_parameter) if hp else tune_parameter)
  52. c = (x.ln() if type(x)==Num else math.log(x))
  53. sigmas = [ c/((self.Sigma+EPSILON)**i) * ( ((L_HP if hp else L)/fact(i)) ) for i in range(1, k+1) ]
  54. scaled_target = approx_target_in_zk(sigmas, Num(stake)) #+ (BASE_L_HP if hp else BASE_L)
  55. return scaled_target
  56. if self.slot % EPOCH_LENGTH ==0 and self.slot > 0:
  57. apr = self.apr_scaled_to_runningtime()
  58. # staked ratio is added in strategy
  59. self.strategy.set_ratio(self.slot, apr)
  60. # epoch stake is added
  61. self.initial_stake +=[self.stake]
  62. T = target(self.f, self.strategy.staked_value(self.stake))
  63. won = lottery(T, hp)
  64. self.won_hist += [won]
  65. def update_vesting(self):
  66. self.stake+= self.vesting_differential()
  67. def current_vesting(self):
  68. '''
  69. current corresponding slot vesting
  70. '''
  71. return self.vesting[int(self.slot/VESTING_PERIOD)]
  72. def prev_vesting(self):
  73. '''
  74. previous corresponding slot vesting
  75. '''
  76. return self.vesting[int((self.slot-1)/VESTING_PERIOD)] if self.slot>0 else self.current_vesting()
  77. def vesting_differential(self):
  78. vesting_value = self.current_vesting() - self.prev_vesting()
  79. return vesting_value
  80. def update_stake(self, reward):
  81. if self.won_hist[-1]:
  82. self.stake+=reward
  83. #print('updating stake, stake: {}, last: {}'.format(self.stake, self.initial_stake[-1]))
  84. def resync_stake(self, reward):
  85. '''
  86. add resync stake
  87. '''
  88. self.stake += reward
  89. def write(self, idx):
  90. with open('log/darkie'+str(idx)+'.log', 'w+') as f:
  91. buf = 'initial stake:'+','.join([str(i) for i in self.initial_stake])
  92. buf += '\r\n'
  93. buf += '(apr,staked ratio,{}):'.format(self.strategy.type)+','.join(['('+str(apr)+','+str(sr)+')' for sr, apr in zip(self.strategy.staked_tokens_ratio, self.strategy.annual_return)])
  94. buf+='\r\n'
  95. buf += 'apr: {}'.format(self.apr_scaled_to_runningtime())
  96. f.write(buf)