Guest User

Untitled

a guest
Feb 25th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. import math
  2.  
  3. ICO_SUPPLY = 10**9
  4. DECIMALS = 8
  5. FLOAT_FACTOR = 10**DECIMALS
  6.  
  7. IMUSIFY_OPERATION_STACK = (3-1) * ICO_SUPPLY * FLOAT_FACTOR
  8. SOME_USER_ADDRESS = 'ASsudg...poor soul'
  9. IMUSIFY_MAIN_IMU_ADDRESS = "AXjsdb...much IMU here"
  10. IMUSIFY_OPERATION_IMU_ADDRESS = "AKjsdb...less IMU here"
  11.  
  12. class ImusifyConfig:
  13. def __init__(self):
  14. self.marketFactor = 1 ## we tune this down when IMU goes up in $ price to regulate $ output
  15. self.bonusMaximum = 3 ## +300 % in addition to the default gain
  16. self.levelHalf = 150 ## level at which the bonus function gives the bonus of bonusMaximum/2
  17.  
  18. self.levelPlusForUpvote = 5 ## valueOfSomething is a number between 1 and 100
  19. self.levelPlusForSongUpload = 60
  20. # self.levelPlusForOtherThingsThatTriggerLevelUpsOnTheSite = ...
  21.  
  22. def rewardModel(self, config, userLevel, levelAccumulations):
  23. bonusTerm = 1 - config.levelHalf / float(config.levelHalf + userLevel)
  24. bonusFactor = 1 + config.bonusMaximum * bonusTerm
  25. return config.marketFactor * bonusFactor * levelAccumulations / float(100)
  26.  
  27. def toBlockchainFormat(balance):
  28. balanceShiftedComma = balance * FLOAT_FACTOR
  29. balanceInteger = int(math.floor(balanceShiftedComma))
  30. return balanceInteger
  31.  
  32. class DummyBlockchain:
  33. def __init__(self):
  34. self.ledger = {IMUSIFY_OPERATION_IMU_ADDRESS : IMUSIFY_OPERATION_STACK}
  35.  
  36. def transfer(self, from_, to_, amount):
  37. self.ledger[from_] -= amount
  38. self.ledger[to_] += amount
  39. return True
  40.  
  41. class DummyBackend:
  42. def __init__(self):
  43. self.config = ImusifyConfig()
  44.  
  45. ## data base
  46. self.levelAccumulations = {}
  47. self.levels = {} ## this was on-chain in the last contract
  48.  
  49. def registerUser(self, blockchain, userAddress):
  50. self.levelAccumulations[userAddress] = 0
  51. self.levels[userAddress] = 0
  52. blockchain.ledger[userAddress] = 0
  53.  
  54. def updateLevelAccumulationsUponUserAction(self, userAddress, action):
  55. if action=="upvote":
  56. levelPlus = self.config.levelPlusForUpvote
  57. if action=="songUpload":
  58. levelPlus = self.config.levelPlusForSongUpload
  59. # etc.
  60. self.levelAccumulations[userAddress] += levelPlus
  61. return True
  62.  
  63. def _computeReward(self, userAddress): ## a version of this way on-chain in the last imusify contact, this code
  64. self.levels[userAddress] += self.levelAccumulations[userAddress]
  65.  
  66. userLevel = self.levels[userAddress]
  67. userLevelAccumulations = self.levelAccumulations[userAddress]
  68. reward = rewardModel(self, self.config, userLevel, userLevelAccumulations) ## all arguments except this one can in principle go on-chain
  69.  
  70. self.levelAccumulations[userAddress] = 0
  71.  
  72. return reward
  73.  
  74. def payoutReward(self, blockchain, userAddress):
  75. rewardInBlockchainFormat = toBlockchainFormat(self._computeReward(userAddress))
  76.  
  77. success = blockchain.transfer(IMUSIFY_OPERATION_IMU_ADDRESS, userAddress, rewardInBlockchainFormat) ## communication with the blockchain
  78.  
  79. return success
  80.  
  81.  
  82.  
  83. if __name__ == '__main__':
  84.  
  85. blockchain = DummyBlockchain()
  86. backend = DummyBackend()
  87.  
  88. print('')
  89. print('* blockchain.ledger:\n ' + str(blockchain.ledger))
  90. print('* calling registerUser with SOME_USER_ADDRESS')
  91. backend.registerUser(blockchain, SOME_USER_ADDRESS)
  92. print('* blockchain.ledger:\n ' + str(blockchain.ledger))
  93. print('* calling updateLevelAccumulationsUponUserAction with SOME_USER_ADDRESS and "upvote"')
  94. backend.updateLevelAccumulationsUponUserAction(SOME_USER_ADDRESS, 'upvote')
  95. print('* calling updateLevelAccumulationsUponUserAction with SOME_USER_ADDRESS and "songUpload"')
  96. backend.updateLevelAccumulationsUponUserAction(SOME_USER_ADDRESS, 'songUpload')
  97. print('* calling payoutReward with SOME_USER_ADDRESS')
  98. backend.payoutReward(blockchain, SOME_USER_ADDRESS)
  99. print('* blockchain.ledger:\n ' + str(blockchain.ledger))
Add Comment
Please, Sign In to add comment