strategy.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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%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 = (apr)/(self.target)
  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)
  49. fn = lambda x: (math.log(x, 10)+1)/2 * 0.95 + 0.05
  50. sr = (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
  64. apr_ratio = max(apr_ratio, 0)
  65. sr = (2/(1+math.pow(math.e, -4*apr_ratio))-1)
  66. if sr>1:
  67. sr = 1
  68. elif sr<0:
  69. sr = 0
  70. self.staked_tokens_ratio += [sr]
  71. self.annual_return += [apr]
  72. def random_strategy(epoch_length=EPOCH_LENGTH):
  73. rnd = random.random()
  74. if rnd < 0.25:
  75. return Hodler(epoch_length)
  76. elif rnd < 0.5 and rnd >= 0.25:
  77. return LinearStrategy(epoch_length)
  78. elif rnd < 0.75 and rnd >= 0.5:
  79. return LogarithmicStrategy(epoch_length)
  80. else:
  81. return SigmoidStrategy(epoch_length)
  82. class Tip(object):
  83. def __init__(self):
  84. self.type = 'tip'
  85. def get_tip(self, last_reward, apr, size, last_tip):
  86. return 0
  87. class ZeroTip(Tip):
  88. def __init__(self):
  89. super().__init__()
  90. self.type = 'zero'
  91. def get_tip(self, last_reward, apr, size, last_tip):
  92. return 0
  93. class TenthOfReward(Tip):
  94. def __init__(self):
  95. super().__init__()
  96. self.type = '10th'
  97. def get_tip(self, last_reward, apr, size, last_tip):
  98. return last_reward/10
  99. class HundredthOfReward(Tip):
  100. def __init__(self):
  101. super().__init__()
  102. self.type = '100th'
  103. def get_tip(self, last_reward, apr, size, last_tip):
  104. return last_reward/100
  105. class MilthOfReward(Tip):
  106. def __init__(self):
  107. super().__init__()
  108. self.type = '1000th'
  109. def get_tip(self, last_reward, apr, size, last_tip):
  110. return last_reward/1000
  111. class RewardApr(Tip):
  112. def __init__(self):
  113. super().__init__()
  114. self.type = 'reward_apr'
  115. def get_tip(self, last_reward, apr, size, last_tip):
  116. apr_relu = max(apr, 0)
  117. return last_reward*apr_relu
  118. class TenthRewardApr(Tip):
  119. def __init__(self):
  120. super().__init__()
  121. self.type = 'reward_apr'
  122. def get_tip(self, last_reward, apr, size, last_tip):
  123. apr_relu = max(apr, 0)
  124. return last_reward*apr_relu/10
  125. class TenthCCApr(Tip):
  126. def __init__(self):
  127. super().__init__()
  128. self.type = "cc_apr_10"
  129. def get_tip(self, last_reward, apr, size, last_tip):
  130. return size/MAX_BLOCK_SIZE/10
  131. class HundredthCCApr(Tip):
  132. def __init__(self):
  133. super().__init__()
  134. self.type = "cc_apr_100"
  135. def get_tip(self, last_reward, apr, size, last_tip):
  136. return size/MAX_BLOCK_SIZE/100
  137. class MilthCCApr(Tip):
  138. def __init__(self):
  139. super().__init__()
  140. self.type = "cc_apr_1000"
  141. def get_tip(self, last_reward, apr, size, last_tip):
  142. return size/MAX_BLOCK_SIZE/1000
  143. class Conservative(Tip):
  144. def __init__(self):
  145. super().__init__()
  146. self.type = "cc_apr_1000"
  147. def get_tip(self, last_reward, apr, size, last_tip):
  148. return last_tip
  149. class Generous(Tip):
  150. def __init__(self):
  151. super().__init__()
  152. self.type = "cc_apr_1000"
  153. def get_tip(self, last_reward, apr, size, last_tip):
  154. return last_tip*2
  155. def random_tip_strategy():
  156. return random.choice([ZeroTip(), HundredthOfReward(), MilthOfReward(), RewardApr(), TenthCCApr(), HundredthCCApr(), MilthCCApr(), Conservative(), Generous()])