Skip to content

Instantly share code, notes, and snippets.

@Nikolaj-K
Created February 14, 2018 01:32
Show Gist options
  • Save Nikolaj-K/1034d2e33cc587d70d5dbd16c6e7c7ab to your computer and use it in GitHub Desktop.
Save Nikolaj-K/1034d2e33cc587d70d5dbd16c6e7c7ab to your computer and use it in GitHub Desktop.
imusify reward model 2.0 dummy reference implementation
import math
ICO_SUPPLY = 10**9
DECIMALS = 8
FLOAT_FACTOR = 10**DECIMALS
IMUSIFY_OPERATION_STACK = (3-1) * ICO_SUPPLY * FLOAT_FACTOR
SOME_USER_ADDRESS = 'ASsudg...poor soul'
IMUSIFY_MAIN_IMU_ADDRESS = "AXjsdb...much IMU here"
IMUSIFY_OPERATION_IMU_ADDRESS = "AKjsdb...less IMU here"
class ImusifyConfig:
def __init__(self):
self.marketFactor = 1 ## we tune this down when IMU goes up in $ price to regulate $ output
self.bonusMaximum = 3 ## +300 % in addition to the default gain
self.levelHalf = 150 ## level at which the bonus function gives the bonus of bonusMaximum/2
self.levelPlusForUpvote = 5 ## valueOfSomething is a number between 1 and 100
self.levelPlusForSongUpload = 60
# self.levelPlusForOtherThingsThatTriggerLevelUpsOnTheSite = ...
def rewardModel(self, config, userLevel, levelAccumulations):
bonusTerm = 1 - config.levelHalf / float(config.levelHalf + userLevel)
bonusFactor = 1 + config.bonusMaximum * bonusTerm
return config.marketFactor * bonusFactor * levelAccumulations / float(100)
def toBlockchainFormat(balance):
balanceShiftedComma = balance * FLOAT_FACTOR
balanceInteger = int(math.floor(balanceShiftedComma))
return balanceInteger
class DummyBlockchain:
def __init__(self):
self.ledger = {IMUSIFY_OPERATION_IMU_ADDRESS : IMUSIFY_OPERATION_STACK}
def transfer(self, from_, to_, amount):
self.ledger[from_] -= amount
self.ledger[to_] += amount
return True
class DummyBackend:
def __init__(self):
self.config = ImusifyConfig()
## data base
self.levelAccumulations = {}
self.levels = {} ## this was on-chain in the last contract
def registerUser(self, blockchain, userAddress):
self.levelAccumulations[userAddress] = 0
self.levels[userAddress] = 0
blockchain.ledger[userAddress] = 0
def updateLevelAccumulationsUponUserAction(self, userAddress, action):
if action=="upvote":
levelPlus = self.config.levelPlusForUpvote
if action=="songUpload":
levelPlus = self.config.levelPlusForSongUpload
# etc.
self.levelAccumulations[userAddress] += levelPlus
return True
def _computeReward(self, userAddress): ## a version of this way on-chain in the last imusify contact, this code
self.levels[userAddress] += self.levelAccumulations[userAddress]
userLevel = self.levels[userAddress]
userLevelAccumulations = self.levelAccumulations[userAddress]
reward = rewardModel(self, self.config, userLevel, userLevelAccumulations) ## all arguments except this one can in principle go on-chain
self.levelAccumulations[userAddress] = 0
return reward
def payoutReward(self, blockchain, userAddress):
rewardInBlockchainFormat = toBlockchainFormat(self._computeReward(userAddress))
success = blockchain.transfer(IMUSIFY_OPERATION_IMU_ADDRESS, userAddress, rewardInBlockchainFormat) ## communication with the blockchain
return success
if __name__ == '__main__':
blockchain = DummyBlockchain()
backend = DummyBackend()
print('')
print('* blockchain.ledger:\n ' + str(blockchain.ledger))
print('* calling registerUser with SOME_USER_ADDRESS')
backend.registerUser(blockchain, SOME_USER_ADDRESS)
print('* blockchain.ledger:\n ' + str(blockchain.ledger))
print('* calling updateLevelAccumulationsUponUserAction with SOME_USER_ADDRESS and "upvote"')
backend.updateLevelAccumulationsUponUserAction(SOME_USER_ADDRESS, 'upvote')
print('* calling updateLevelAccumulationsUponUserAction with SOME_USER_ADDRESS and "songUpload"')
backend.updateLevelAccumulationsUponUserAction(SOME_USER_ADDRESS, 'songUpload')
print('* calling payoutReward with SOME_USER_ADDRESS')
backend.payoutReward(blockchain, SOME_USER_ADDRESS)
print('* blockchain.ledger:\n ' + str(blockchain.ledger))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment