strategy.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. sr = Num(apr)/Num(self.target_apy)
  36. if sr>1:
  37. sr = 1
  38. elif sr<0:
  39. sr = 0
  40. self.staked_tokens_ratio += [sr]
  41. self.annual_return += [apr]
  42. class LogarithmicStrategy(Strategy):
  43. def __init__(self, epoch_len=0):
  44. super().__init__(epoch_len)
  45. self.type = 'logarithmic'
  46. def set_ratio(self, slot, apr):
  47. if slot%self.epoch_len==0:
  48. apr_ratio = math.fabs(apr/self.target_apy)
  49. fn = lambda x: (math.log(x, 10)+1)/2 * 0.95 + 0.05
  50. sr = Num(fn(apr_ratio) if apr_ratio != 0 else 0)
  51. if sr>1:
  52. sr = 1
  53. elif sr<0:
  54. sr = 0
  55. self.staked_tokens_ratio += [sr]
  56. self.annual_return += [apr]
  57. class SigmoidStrategy(Strategy):
  58. def __init__(self, epoch_len=0):
  59. super().__init__(epoch_len)
  60. self.type = 'sigmoid'
  61. def set_ratio(self, slot, apr):
  62. if slot%self.epoch_len==0:
  63. apr_ratio = apr/self.target_apy
  64. sr = Num(2/(1+math.pow(math.e, -4*apr_ratio))-1)
  65. if sr>1:
  66. sr = 1
  67. elif sr<0:
  68. sr = 0
  69. self.staked_tokens_ratio += [sr]
  70. self.annual_return += [apr]
  71. def random_strategy(epoch_length=EPOCH_LENGTH):
  72. rnd = random.random()
  73. if rnd < 0.25:
  74. return Hodler(epoch_length)
  75. elif rnd < 0.5 and rnd >= 0.25:
  76. return LinearStrategy(epoch_length)
  77. elif rnd < 0.75 and rnd >= 0.5:
  78. return LogarithmicStrategy(epoch_length)
  79. else:
  80. return SigmoidStrategy(epoch_length)