strategy.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import math
  2. from core.utils import *
  3. class Strategy(object):
  4. def __init__(self, epoch_len=0):
  5. self.epoch_len = epoch_len
  6. self.staked_tokens_ratio = [1]
  7. self.target_apy = TARGET_APY
  8. self.type = 'base'
  9. def set_ratio(self, slot, apy):
  10. return
  11. def staked_value(self, stake):
  12. return Num(self.staked_tokens_ratio[-1])*Num(stake)
  13. class RandomStrategy(Strategy):
  14. def __init__(self, epoch_len):
  15. super().__init__(epoch_len)
  16. self.type = 'random'
  17. def set_ratio(self, slot, apy):
  18. if slot%self.epoch_len==0:
  19. self.staked_tokens_ratio += [random.random()]
  20. class LinearStrategy(Strategy):
  21. def __init__(self, epoch_len=0):
  22. super().__init__(epoch_len)
  23. self.type = 'linear'
  24. def set_ratio(self, slot, apy):
  25. if slot%self.epoch_len==0:
  26. sr = Num(apy)/Num(self.target_apy)
  27. if sr>1:
  28. sr = 0
  29. elif sr<0:
  30. sr = 0
  31. self.staked_tokens_ratio += [sr]
  32. class LogarithmicStrategy(Strategy):
  33. def __init__(self, epoch_len=0):
  34. super().__init__(epoch_len)
  35. self.type = 'logarithmic'
  36. def set_ratio(self, slot, apy):
  37. if slot%self.epoch_len==0:
  38. apy_ratio = math.fabs(apy/self.target_apy)
  39. fn = lambda x: (math.log(x, 10)+1)/2 * 0.95 + 0.05
  40. sr = Num(fn(apy_ratio) if apy_ratio != 0 else 0)
  41. if sr>1:
  42. sr = 0
  43. elif sr<0:
  44. sr = 0
  45. self.staked_tokens_ratio += [sr]
  46. class SigmoidStrategy(Strategy):
  47. def __init__(self, epoch_len=0):
  48. super().__init__(epoch_len)
  49. self.type = 'sigmoid'
  50. def set_ratio(self, slot, apy):
  51. if slot%self.epoch_len==0:
  52. #TODO should be abs?
  53. apy_ratio = apy/self.target_apy
  54. sr = Num(2/(1+math.pow(math.e, -4*apy_ratio))-1)
  55. if sr>1:
  56. sr = 0
  57. elif sr<0:
  58. sr = 0
  59. self.staked_tokens_ratio += [sr]
  60. def random_strategy(epoch_length):
  61. rnd = random.random()
  62. if rnd < 0.25:
  63. return RandomStrategy(epoch_length)
  64. elif rnd < 0.5 and rnd >=0.25:
  65. return LinearStrategy(epoch_length)
  66. else:
  67. return SigmoidStrategy(epoch_length)