strategy.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import math
  2. from core.utils import *
  3. class Strategy(object):
  4. '''
  5. @type epoch_len: int
  6. @epoch_len: epoch length
  7. @type airdrop_period: int
  8. @param airdrop_period: strategy grace period, during which strategy is HODL only
  9. '''
  10. def __init__(self, epoch_len=0, airdrop_period=HEADSTART_AIRDROP):
  11. self.epoch_len = epoch_len
  12. self.airdrop_period=HEADSTART_AIRDROP
  13. self.staked_tokens_ratio = [1]
  14. self.target_apy = TARGET_APR
  15. self.annual_return = [0]
  16. self.type = 'base'
  17. def set_ratio(self, slot, apr):
  18. return
  19. def staked_value(self, stake):
  20. return Num(self.staked_tokens_ratio[-1])*Num(stake)
  21. class Hodler(Strategy):
  22. def __init__(self, epoch_len):
  23. super().__init__(epoch_len)
  24. self.type = 'hodler'
  25. def set_ratio(self, slot, apr):
  26. if slot%self.epoch_len==0:
  27. self.staked_tokens_ratio += [1]
  28. self.annual_return +=[apr]
  29. class LinearStrategy(Strategy):
  30. def __init__(self, epoch_len=0):
  31. super().__init__(epoch_len)
  32. self.type = 'linear'
  33. def set_ratio(self, slot, apr):
  34. if slot%self.epoch_len==0:
  35. if slot < self.airdrop_period:
  36. self.staked_tokens_ratio += [1]
  37. self.annual_return +=[apr]
  38. return
  39. sr = Num(apr)/Num(self.target_apy)
  40. if sr>1:
  41. sr = 1
  42. elif sr<0:
  43. sr = 0
  44. self.staked_tokens_ratio += [sr]
  45. self.annual_return += [apr]
  46. class LogarithmicStrategy(Strategy):
  47. def __init__(self, epoch_len=0):
  48. super().__init__(epoch_len)
  49. self.type = 'logarithmic'
  50. def set_ratio(self, slot, apr):
  51. if slot%self.epoch_len==0:
  52. if slot < self.airdrop_period:
  53. self.staked_tokens_ratio += [1]
  54. self.annual_return +=[apr]
  55. return
  56. apr_ratio = math.fabs(apr/self.target_apy)
  57. fn = lambda x: (math.log(x, 10)+1)/2 * 0.95 + 0.05
  58. sr = Num(fn(apr_ratio) if apr_ratio != 0 else 0)
  59. if sr>1:
  60. sr = 1
  61. elif sr<0:
  62. sr = 0
  63. self.staked_tokens_ratio += [sr]
  64. self.annual_return += [apr]
  65. class SigmoidStrategy(Strategy):
  66. def __init__(self, epoch_len=0):
  67. super().__init__(epoch_len)
  68. self.type = 'sigmoid'
  69. def set_ratio(self, slot, apr):
  70. if slot%self.epoch_len==0:
  71. if slot < self.airdrop_period:
  72. self.staked_tokens_ratio += [1]
  73. self.annual_return +=[apr]
  74. return
  75. apr_ratio = apr/self.target_apy
  76. sr = Num(2/(1+math.pow(math.e, -4*apr_ratio))-1)
  77. if sr>1:
  78. sr = 1
  79. elif sr<0:
  80. sr = 0
  81. self.staked_tokens_ratio += [sr]
  82. self.annual_return += [apr]
  83. def random_strategy(epoch_length=EPOCH_LENGTH):
  84. rnd = random.random()
  85. if rnd < 0.25:
  86. return Hodler(epoch_length)
  87. elif rnd < 0.5 and rnd >= 0.25:
  88. return LinearStrategy(epoch_length)
  89. elif rnd < 0.75 and rnd >= 0.5:
  90. return LogarithmicStrategy(epoch_length)
  91. else:
  92. return SigmoidStrategy(epoch_length)