strategy.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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_APR
  8. self.annual_return = [0]
  9. self.type = 'base'
  10. def set_ratio(self, slot, apr):
  11. return
  12. def staked_value(self, stake):
  13. return Num(self.staked_tokens_ratio[-1])*Num(stake)
  14. class Hodler(Strategy):
  15. def __init__(self, epoch_len):
  16. super().__init__(epoch_len)
  17. self.type = 'hodler'
  18. def set_ratio(self, slot, apr):
  19. if slot%self.epoch_len==0:
  20. self.staked_tokens_ratio += [1]
  21. self.annual_return +=[apr]
  22. class LinearStrategy(Strategy):
  23. def __init__(self, epoch_len=0):
  24. super().__init__(epoch_len)
  25. self.type = 'linear'
  26. def set_ratio(self, slot, apr):
  27. if slot%self.epoch_len==0:
  28. sr = Num(apr)/Num(self.target_apy)
  29. if sr>1:
  30. sr = 1
  31. elif sr<0:
  32. sr = 0
  33. self.staked_tokens_ratio += [sr]
  34. self.annual_return += [apr]
  35. class LogarithmicStrategy(Strategy):
  36. def __init__(self, epoch_len=0):
  37. super().__init__(epoch_len)
  38. self.type = 'logarithmic'
  39. def set_ratio(self, slot, apr):
  40. if slot%self.epoch_len==0:
  41. apr_ratio = math.fabs(apr/self.target_apy)
  42. fn = lambda x: (math.log(x, 10)+1)/2 * 0.95 + 0.05
  43. sr = Num(fn(apr_ratio) if apr_ratio != 0 else 0)
  44. if sr>1:
  45. sr = 1
  46. elif sr<0:
  47. sr = 0
  48. self.staked_tokens_ratio += [sr]
  49. self.annual_return += [apr]
  50. class SigmoidStrategy(Strategy):
  51. def __init__(self, epoch_len=0):
  52. super().__init__(epoch_len)
  53. self.type = 'sigmoid'
  54. def set_ratio(self, slot, apr):
  55. if slot%self.epoch_len==0:
  56. apr_ratio = apr/self.target_apy
  57. sr = Num(2/(1+math.pow(math.e, -4*apr_ratio))-1)
  58. if sr>1:
  59. sr = 1
  60. elif sr<0:
  61. sr = 0
  62. self.staked_tokens_ratio += [sr]
  63. self.annual_return += [apr]
  64. def random_strategy(epoch_length=EPOCH_LENGTH):
  65. rnd = random.random()
  66. if rnd < 0.25:
  67. return Hodler(epoch_length)
  68. elif rnd < 0.5 and rnd >= 0.25:
  69. return LinearStrategy(epoch_length)
  70. elif rnd < 0.75 and rnd >= 0.5:
  71. return LogarithmicStrategy(epoch_length)
  72. else:
  73. return SigmoidStrategy(epoch_length)