strategy.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. from utils import *
  2. import math
  3. class Strategy(object):
  4. def __init__(self, epoch_len=0):
  5. self.epoch_len = epoch_len
  6. self.staked_tokens_ratio = [Num(1)]
  7. self.target_apy = TARGET_APY
  8. def set_ratio(self, slot=0, apy=0):
  9. pass
  10. def staked_value(self, stake):
  11. assert(self.staked_tokens_ratio[-1]>=0 and self.staked_tokens_ratio[-1]<=1)
  12. return self.staked_tokens_ratio[-1]*Num(stake)
  13. class RandomStrategy(Strategy):
  14. def __init__(self, epoch_len):
  15. Strategy.__init__(self, epoch_len)
  16. def set_ratio(self, slot, apy=0):
  17. if slot%self.epoch_len==0 and slot>EPOCH_LENGTH:
  18. self.staked_tokens_ratio += [random.random()]
  19. class LinearStrategy(Strategy):
  20. '''
  21. linear staking strategy wrt apy.
  22. assume optimal is 20% APY!
  23. '''
  24. def __init__(self, epoch_len=0):
  25. Strategy.__init__(self, epoch_len)
  26. def set_ratio(self, slot, apy):
  27. if slot%self.epoch_len==0 and slot>EPOCH_LENGTH:
  28. self.staked_tokens_ratio += [apy/Num(self.target_apy) * Num(0.9) + Num(0.1)]
  29. class LogarithmicStrategy(Strategy):
  30. '''
  31. logarithmic staking strategy wrt apy.
  32. assume optimal is 20% APY!
  33. '''
  34. def __init__(self, epoch_len=0):
  35. Strategy.__init__(self, epoch_len)
  36. def set_ratio(self, slot, apy):
  37. if slot%self.epoch_len==0 and slot>EPOCH_LENGTH:
  38. apy_ratio = math.fabs(apy/self.target_apy)
  39. self.staked_tokens_ratio += [Num((math.log(apy_ratio, 10)+1)/2 if apy_ratio != 0 else 0)]
  40. class SigmoidStrategy(Strategy):
  41. '''
  42. logarithmic staking strategy wrt apy.
  43. assume optimal is 20% APY!
  44. '''
  45. def __init__(self, epoch_len=0):
  46. Strategy.__init__(self, epoch_len)
  47. def set_ratio(self, slot, apy):
  48. if slot%self.epoch_len==0 and slot>self.epoch_len:
  49. apy_ratio = math.fabs(apy/self.target_apy)
  50. self.staked_tokens_ratio += [Num(2/(1+math.pow(math.e, -4*apy_ratio))-1)]