Skip to content

Commit

Permalink
Merge pull request tezos-reward-distributor-organization#22 from utdr…
Browse files Browse the repository at this point in the history
…mac/nofounderowner

Support for no founders/no owners
  • Loading branch information
habanoz authored Jan 31, 2019
2 parents eb90c5b + 98235b7 commit 82e1fc4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
49 changes: 34 additions & 15 deletions src/calc/payment_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ def __init__(self, founders_map, owners_map, reward_list, total_rewards, service
self.fee_calc = service_fee_calculator
self.reward_list = reward_list
self.founders_map = founders_map
self.total_service_fee = 0

#
# calculation details
Expand Down Expand Up @@ -46,21 +45,34 @@ def calculate(self):
delegators_total_fee = delegators_total_fee + fee

# 2- calculate deposit owners payments. They share the remaining rewards according to their ratio (check config)
owners_total_payment = 0
owners_total_pymnt = 0
owners_total_reward = self.total_rewards - (delegators_total_pymnt + delegators_total_fee)
for address, ratio in self.owners_map.items():
owner_pymnt_amnt = floorf(ratio * owners_total_reward, 3)
owners_total_payment = owners_total_payment + owner_pymnt_amnt

pymnts.append(PaymentRecord.OwnerInstance(self.cycle, address, ratio, owner_pymnt_amnt, owner_pymnt_amnt))
no_owners = True

# Skip if no owners defined
if len(self.owners_map) > 0:
no_owners = False
for address, ratio in self.owners_map.items():
owner_pymnt_amnt = floorf(ratio * owners_total_reward, 3)
owners_total_pymnt = owners_total_pymnt + owner_pymnt_amnt
pymnts.append(PaymentRecord.OwnerInstance(self.cycle, address, ratio, owner_pymnt_amnt, owner_pymnt_amnt))

# move remaining rewards to service fee bucket
self.total_service_fee = self.total_rewards - delegators_total_pymnt - owners_total_payment

# 3- service fee is shared among founders according to founders_map ratios
for address, ratio in self.founders_map.items():
pymnt_amnt = floorf(ratio * self.total_service_fee, 6)
pymnts.append(PaymentRecord.FounderInstance(self.cycle, address, ratio, pymnt_amnt))
founders_total_pymnt = 0
founders_total_reward = self.total_rewards - delegators_total_pymnt - owners_total_pymnt
no_founders = True

# If no owners paid, must include their would-be reward in this calculation
if no_owners:
founders_total_reward = founders_total_reward - owners_total_reward

# Skip if no founders defined
if len(self.founders_map) > 0:
no_founders = False
for address, ratio in self.founders_map.items():
founder_pymnt_amnt = floorf(ratio * founders_total_reward, 6)
pymnts.append(PaymentRecord.FounderInstance(self.cycle, address, ratio, founder_pymnt_amnt))

###
# sanity check
Expand All @@ -69,13 +81,20 @@ def calculate(self):
for payment_log in pymnts:
total_sum = total_sum + payment_log.payment

# if no owners/no founders, add that would-be amount to total_sum
# the baker will keep (owners_total_reward + founders_total_reward) automatically when unfrozen
if no_owners:
total_sum = total_sum + owners_total_reward
if no_founders:
total_sum = total_sum + founders_total_reward

# if there is a minor difference due to floor function; it is added to last payment
if self.total_rewards - total_sum > 1e-6:
last_payment_log = pymnts[-1] # last payment, probably one of the founders
last_payment_log.payment = last_payment_log.payment + (self.total_rewards - total_sum)
last_pymnt_log = pymnts[-1] # last payment, probably one of the founders
last_pymnt_log.payment = last_pymnt_log.payment + (self.total_rewards - total_sum)

# this must never return true
if abs(total_sum - self.total_rewards) > 5e-6:
raise Exception("Calculated reward {} is not equal total reward {}".format(total_sum, self.total_rewards))
raise Exception("Calculated reward {} is not equal to total reward {}".format(total_sum, self.total_rewards))

return pymnts
5 changes: 3 additions & 2 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,9 @@ def retry_failed_payments(self):

# all shares in the map must sum up to 1
def validate_map_share_sum(share_map, map_name):
if abs(1 - sum(share_map.values()) > 1e-4): # a zero check actually
raise Exception("Map '{}' shares does not sum up to 1!".format(map_name))
if len(share_map) > 0:
if abs(1 - sum(share_map.values()) > 1e-4): # a zero check actually
raise Exception("Map '{}' shares does not sum up to 1!".format(map_name))


def main(config):
Expand Down

0 comments on commit 82e1fc4

Please sign in to comment.