strategy.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 = 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 (self.staked_tokens_ratio[-1])*(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 < HEADSTART_AIRDROP:
  27. self.staked_tokens_ratio += [1]
  28. self.annual_return +=[apr]
  29. return
  30. if slot%self.epoch_len==0:
  31. self.staked_tokens_ratio += [1]
  32. self.annual_return +=[apr]
  33. class LinearStrategy(Strategy):
  34. def __init__(self, epoch_len=0):
  35. super().__init__(epoch_len)
  36. self.type = 'linear'
  37. def set_ratio(self, slot, apr):
  38. if slot < HEADSTART_AIRDROP:
  39. self.staked_tokens_ratio += [1]
  40. self.annual_return +=[apr]
  41. return
  42. if slot%self.epoch_len==0:
  43. sr = (apr)/(self.target)
  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 LogarithmicStrategy(Strategy):
  51. def __init__(self, epoch_len=0):
  52. super().__init__(epoch_len)
  53. self.type = 'logarithmic'
  54. def set_ratio(self, slot, apr):
  55. if slot < HEADSTART_AIRDROP:
  56. self.staked_tokens_ratio += [1]
  57. self.annual_return +=[apr]
  58. return
  59. if slot%self.epoch_len==0:
  60. apr_ratio = math.fabs(apr/self.target)
  61. fn = lambda x: (math.log(x, 10)+1)/2 * 0.95 + 0.05
  62. sr = (fn(apr_ratio) if apr_ratio != 0 else 0)
  63. if sr>1:
  64. sr = 1
  65. elif sr<0:
  66. sr = 0
  67. self.staked_tokens_ratio += [sr]
  68. self.annual_return += [apr]
  69. class SigmoidStrategy(Strategy):
  70. def __init__(self, epoch_len=0):
  71. super().__init__(epoch_len)
  72. self.type = 'sigmoid'
  73. def set_ratio(self, slot, apr):
  74. if slot < HEADSTART_AIRDROP:
  75. self.staked_tokens_ratio += [1]
  76. self.annual_return +=[apr]
  77. return
  78. if slot%self.epoch_len==0:
  79. apr_ratio = apr/self.target
  80. apr_ratio = max(apr_ratio, 0)
  81. sr = (2/(1+math.pow(math.e, -4*apr_ratio))-1)
  82. if sr>1:
  83. sr = 1
  84. elif sr<0:
  85. sr = 0
  86. self.staked_tokens_ratio += [sr]
  87. self.annual_return += [apr]
  88. def random_strategy(epoch_length=EPOCH_LENGTH):
  89. rnd = random.random()
  90. if rnd < 0.25:
  91. return Hodler(epoch_length)
  92. elif rnd < 0.5 and rnd >= 0.25:
  93. return LinearStrategy(epoch_length)
  94. elif rnd < 0.75 and rnd >= 0.5:
  95. return LogarithmicStrategy(epoch_length)
  96. else:
  97. return SigmoidStrategy(epoch_length)
  98. class Tip(object):
  99. def __init__(self):
  100. self.type = 'tip'
  101. def get_tip(self, last_reward, apr, size, last_tip):
  102. return 0
  103. class ZeroTip(Tip):
  104. def __init__(self):
  105. super().__init__()
  106. self.type = 'zero'
  107. def get_tip(self, last_reward, apr, size, last_tip):
  108. return 0
  109. class MilthOfReward(Tip):
  110. def __init__(self):
  111. super().__init__()
  112. self.type = '1000th'
  113. def get_tip(self, last_reward, apr, size, last_tip):
  114. return last_reward/1000
  115. class RewardApr(Tip):
  116. def __init__(self):
  117. super().__init__()
  118. self.type = 'reward_apr'
  119. def get_tip(self, last_reward, apr, size, last_tip):
  120. apr_relu = max(apr, 0)
  121. apr_relu = min(apr_relu, 1)
  122. return last_reward*apr_relu
  123. class MilthCCApr(Tip):
  124. def __init__(self):
  125. super().__init__()
  126. self.type = "cc_apr_1000"
  127. def get_tip(self, last_reward, apr, size, last_tip):
  128. return size/MAX_BLOCK_SIZE/1000
  129. class Conservative(Tip):
  130. def __init__(self):
  131. super().__init__()
  132. self.type = "cc_apr_1000"
  133. def get_tip(self, last_reward, apr, size, last_tip):
  134. return last_tip
  135. class Generous(Tip):
  136. def __init__(self):
  137. super().__init__()
  138. self.type = "cc_apr_1000"
  139. def get_tip(self, last_reward, apr, size, last_tip):
  140. return last_tip*2
  141. def random_tip_strategy():
  142. return random.choice([ZeroTip(), RewardApr(), MilthOfReward(), MilthCCApr(), Conservative(), Generous()])