def test_calculate(self):
        rewards = []
        ratios = [0.25, 0.05, 0.3, 0.15, 0.25]
        total_reward = 1000

        for i, ratio in enumerate(ratios, start=1):
            rl0 = RewardLog(
                address="addr" + str(i),
                type="D",
                staking_balance=total_reward * ratio,
                current_balance=0,
            )
            rl0.ratio = ratio
            rl0.ratio3 = ratio
            rewards.append(rl0)

        rewards[0].type = TYPE_OWNERS_PARENT
        rewards[1].type = TYPE_FOUNDERS_PARENT

        rewards.append(
            RewardLog("addrdummy", "D", 0, 0).skip("skipped for testing", 3))

        founders_map = {"addr1": 0.4, "addr2": 0.6}
        owners_map = {"addr1": 0.6, "addr2": 0.4}

        phase4 = CalculatePhase4(founders_map, owners_map)
        new_rewards, new_total_reward = phase4.calculate(rewards, total_reward)

        # new_total_reward = total_reward
        self.assertEqual(total_reward, new_total_reward)

        # check new ratios sum up to 1
        # old and new reward amount is the same
        ratio_sum = 0.0

        # filter out skipped records
        new_rewards = list(rl for rl in new_rewards if not rl.skipped)

        # 2 owner, 2 founders and 3 delegators
        self.assertEqual(7, len(new_rewards))

        founder_ratio = 0.0
        owner_ratio = 0.0
        for rl4 in new_rewards:
            if rl4.skipped:
                continue

            if rl4.type == TYPE_FOUNDER:
                founder_ratio += rl4.ratio4
            if rl4.type == TYPE_OWNER:
                owner_ratio += rl4.ratio4

            ratio_sum += rl4.ratio4

        self.assertAlmostEqual(1.0, ratio_sum, delta=1e-6)
        self.assertAlmostEqual(0.25, owner_ratio, delta=1e-6)
        self.assertAlmostEqual(0.05, founder_ratio, delta=1e-6)
Ejemplo n.º 2
0
    def calculate(self, reward_data2, total_amount):

        new_rewards = []
        total_excluded_ratio = 0.0

        for rl2 in self.iterateskipped(reward_data2):
            # move skipped records to next phase
            new_rewards.append(rl2)

        # exclude requested items
        for rl2 in self.filterskipped(reward_data2):
            if rl2.address in self.excluded_set:
                rl2.skip(desc=BY_CONFIGURATION, phase=self.phase)
                new_rewards.append(rl2)
                total_excluded_ratio += rl2.ratio
            elif (
                MIN_DELEGATION_KEY in self.excluded_set
                and rl2.staking_balance < self.min_delegation_amount
            ):
                rl2.skip(desc=BY_MIN_DELEGATION, phase=self.phase)
                new_rewards.append(rl2)
                total_excluded_ratio += rl2.ratio
            else:
                new_rewards.append(rl2)

        total_service_fee_ratio = total_excluded_ratio

        # set fee rates and ratios
        for rl in self.filterskipped(new_rewards):
            rl.service_fee_rate = self.fee_calc.calculate(rl.originaladdress)
            rl.service_fee_ratio = rl.service_fee_rate * rl.ratio
            rl.ratio = rl.ratio - rl.service_fee_ratio
            rl.ratio3 = rl.ratio

            total_service_fee_ratio += rl.service_fee_ratio

        # create founders parent record
        if total_service_fee_ratio > ALMOST_ZERO:
            rl = RewardLog(
                address=TYPE_FOUNDERS_PARENT,
                type=TYPE_FOUNDERS_PARENT,
                staking_balance=0,
                current_balance=0,
            )
            rl.service_fee_rate = 0
            rl.service_fee_ratio = 0
            rl.ratio = total_service_fee_ratio
            rl.ratio3 = rl.ratio

            new_rewards.append(rl)

        return new_rewards, int(total_amount)