strategy.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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_APY
  8. self.type = 'base'
  9. def set_ratio(self, slot=0, apy=0):
  10. pass
  11. def staked_value(self, stake):
  12. #assert(self.staked_tokens_ratio[-1]>=0 and self.staked_tokens_ratio[-1]<=1)
  13. return Num(self.staked_tokens_ratio[-1])*Num(stake)
  14. class RandomStrategy(Strategy):
  15. def __init__(self, epoch_len):
  16. Strategy.__init__(self, epoch_len)
  17. self.type = 'random'
  18. def set_ratio(self, slot, apy=0):
  19. if slot%self.epoch_len==0 and slot>EPOCH_LENGTH:
  20. self.staked_tokens_ratio += [random.random()]
  21. class LinearStrategy(Strategy):
  22. '''
  23. linear staking strategy wrt apy.
  24. assume optimal is 20% APY!
  25. '''
  26. def __init__(self, epoch_len=0):
  27. Strategy.__init__(self, epoch_len)
  28. self.type = 'linear'
  29. def set_ratio(self, slot, apy):
  30. if slot%self.epoch_len==0 and slot>EPOCH_LENGTH:
  31. self.staked_tokens_ratio += [Num(apy)/Num(self.target_apy)]
  32. class LogarithmicStrategy(Strategy):
  33. '''
  34. logarithmic staking strategy wrt apy.
  35. assume optimal is 20% APY!
  36. '''
  37. def __init__(self, epoch_len=0):
  38. Strategy.__init__(self, epoch_len)
  39. self.type = 'logarithmic'
  40. def set_ratio(self, slot, apy):
  41. if slot%self.epoch_len==0 and slot>EPOCH_LENGTH:
  42. apy_ratio = math.fabs(apy/self.target_apy)
  43. fn = lambda x: (math.log(x, 10)+1)/2 * 0.95 + 0.05
  44. print('apy_ratio: {}, output: {}'.format(apy_ratio, fn(apy_ratio)))
  45. self.staked_tokens_ratio += [Num(fn(apy_ratio) if apy_ratio != 0 else 0)]
  46. class SigmoidStrategy(Strategy):
  47. '''
  48. logarithmic staking strategy wrt apy.
  49. assume optimal is 20% APY!
  50. '''
  51. def __init__(self, epoch_len=0):
  52. Strategy.__init__(self, epoch_len)
  53. self.type = 'sigmoid'
  54. def set_ratio(self, slot, apy):
  55. if slot%self.epoch_len==0 and slot>self.epoch_len:
  56. apy_ratio = math.fabs(apy/self.target_apy)
  57. self.staked_tokens_ratio += [Num(2/(1+math.pow(math.e, -4*apy_ratio))-1)]
  58. def random_strategy(epoch_length):
  59. rnd = random.random()
  60. if rnd < 0.25:
  61. return RandomStrategy(epoch_length)
  62. elif rnd < 0.5 and rnd >=0.25:
  63. return LinearStrategy(epoch_length)
  64. else:
  65. return SigmoidStrategy(epoch_length)