Skip to content

Instantly share code, notes, and snippets.

@kangchihlun
Created January 9, 2023 08:25
Show Gist options
  • Save kangchihlun/4e096c9af4bb7e8bacb89f7f4407d96e to your computer and use it in GitHub Desktop.
Save kangchihlun/4e096c9af4bb7e8bacb89f7f4407d96e to your computer and use it in GitHub Desktop.
mock daily distributed rewards
# coding=utf-8
import sys,os
import secrets
from datetime import datetime
import time
import csv
import random
class dailyRecord():
def __init__(self,name):
self.name = name
self.equity = 0
self.deposit_records = {}
self.withdraw_records = {}
self.unclaimed_rewards = 0
def deposit(self,amt,t):
self.equity += amt
print(self.name + ' deposit '+str(amt) + ' @' + str(t))
if(not t in self.deposit_records):
self.deposit_records[t] = amt
def withdraw(self,amt,t):
self.equity -= amt
self.equity = max(0,self.equity)
print(self.name + ' withdraw '+str(amt) + ' curr equity total '+ str(self.equity) + ' @' + str(t) )
if(not t in self.withdraw_records):
self.withdraw_records[t] = amt
def rewardToday(self,rewardToday,t):
print(self.name + ' get reward distributed '+str(rewardToday) + ' @'+str(t))
self.unclaimed_rewards += rewardToday
# turn unclaimed_rewards into deposit
def reinvest(self,amt,t):
if(self.unclaimed_rewards>0):
self.deposit(max(self.unclaimed_rewards,amt),t)
self.unclaimed_rewards = min(self.unclaimed_rewards - amt,0)
def generate_serial_number():
serial_number = secrets.token_hex(8)
return serial_number
def generate_random_integer(min_value, max_value):
return random.randint(min_value, max_value)
class staking_vault():
def __init__(self):
self.users = [] # dailyRecord
self.pool_total = 0
def mock_add_user(self,num=100):
for i in range(num):
#_user_name = generate_serial_number()
ni = '00'+str(i)
if(i>10):
ni = '0'+str(i)
_user_name = 'user'+ ni
self.users.append(dailyRecord(_user_name))
def mock_deposit_withdraw(self,t):
for i in range(len(self.users)):
mode = generate_random_integer(1, 2)
amt = generate_random_integer(500,1000)
if(mode==1):
self.users[i].deposit(amt,t)
self.pool_total += amt
else:
self.users[i].withdraw(amt,t)
self.pool_total -= amt
self.pool_total = max(0,self.pool_total)
def mock_daily_reward(self,reward,t):
if(self.pool_total<0.001):
return
for i in range(len(self.users)):
_reward = reward * (self.users[i].equity / self.pool_total)
self.users[i].rewardToday(_reward,t)
ONE_DAY = 86400
_t = 1610174556
vault = staking_vault()
vault.mock_add_user()
for t in range(100):
_t += ONE_DAY
vault.mock_deposit_withdraw(_t)
reward_today = generate_random_integer(-50,100)
print(" ------ today's reward is "+str(reward_today) )
vault.mock_daily_reward(reward_today,_t)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment