コード例 #1
0
    def test_basic_rates(self):
        token_1, token_2 = tokens[:2]

        # set token base rates
        token_addresses = [token_1.address, token_2.address]
        buy_rates = [token_wei(500, 18), token_wei(400, 18)]
        sell_rates = [token_wei(0.00182, 18), token_wei(0.00232, 18)]
        self.contract.set_rates(token_addresses, buy_rates, sell_rates)

        self.assertEqual(
            self.contract.get_basic_rate(token_1.address, buy=True),
            buy_rates[0])
        self.assertEqual(
            self.contract.get_basic_rate(token_2.address, buy=True),
            buy_rates[1])
        self.assertEqual(
            self.contract.get_basic_rate(token_1.address, buy=False),
            sell_rates[0])
        self.assertEqual(
            self.contract.get_basic_rate(token_2.address, buy=False),
            sell_rates[1])

        # set pricing step function
        self.contract.set_qty_step_function(token_1.address, [0], [0], [0],
                                            [0])
        self.contract.set_imbalance_step_function(token_1.address, [0], [0],
                                                  [0], [0])

        self.assertEqual(self.contract.get_buy_rate(token_1.address, qty=1),
                         buy_rates[0])
        self.assertEqual(self.contract.get_sell_rate(token_1.address, qty=1),
                         sell_rates[0])
コード例 #2
0
    def test_rate_with_qty_step_function(self):
        token = tokens[0]

        self.contract.set_rates(token_addresses=[token.address],
                                buy_rates=[token_wei(500, 18)],
                                sell_rates=[token_wei(0.00182, 18)])
        self.contract.set_qty_step_function(
            token=token.address,
            x_buy=[100 * 10**18, 200 * 10**18, 300 * 10**18, 500 * 10**18],
            y_buy=[0, -30, -60, -80],
            x_sell=[100 * 10**18, 200 * 10**18, 300 * 10**18, 500 * 10**18],
            y_sell=[0, -30, -60, -80],
        )
        self.contract.set_imbalance_step_function(token.address, [0], [0], [0],
                                                  [0])

        # get sell rate, need to pass token qty
        self.assertEqual(
            self.contract.get_sell_rate(token.address, qty=(90 * 10**18)),
            token_wei(0.00182, 18))
        self.assertEqual(
            self.contract.get_sell_rate(token.address, qty=(150 * 10**18)),
            token_wei(0.00182, 18) * (1 - 30 * 0.01 / 100))

        # get sell rate, need to pass eth qty
        self.assertEqual(
            self.contract.get_buy_rate(token.address, qty=int(0.1 * 10**18)),
            token_wei(500, 18))
        self.assertEqual(
            self.contract.get_buy_rate(token.address, qty=int(0.3 * 10**18)),
            token_wei(500, 18) * (1 - 30 * 0.01 / 100))
コード例 #3
0
    def setUpClass(cls):
        reserve.pricing.add_operator(operator.address)
        reserve.pricing.set_valid_rate_duration_in_blocks(60)
        reserve.pricing.add_new_token(
            token=tokens[0].address,
            minimal_record_resolution=token_wei(0.0001, 18),
            max_per_block_imbalance=token_wei(439.79, 18),
            max_total_imbalance=token_wei(922.36, 18))

        reserve.pricing.add_new_token(
            token=tokens[1].address,
            minimal_record_resolution=token_wei(0.0001, 18),
            max_per_block_imbalance=token_wei(439.79, 18),
            max_total_imbalance=token_wei(922.36, 18))
コード例 #4
0
    def test_set_new_rate_with_small_and_big_changes(self):
        """
        Scenario:
            - Set base_rate for 2 tokens
            - Set new_rate:
                For the first token, the changes can not fit compact data.
                For the second token, the new_rate has changes which fit compact
                data.

        Expect:
            - For token 1:
                compact_data = 0
                basic_rate = new_rate
            - For token 2:
                compact_data = changes
                basic_rate = base_rate
        """

        # Set base rates for 2 token
        token_addresses = [token.address for token in tokens[:2]]

        # set rates -> consider as base rates
        base_buy_rates = [token_wei(500, 18), token_wei(400, 18)]
        base_sell_rates = [token_wei(0.00182, 18), token_wei(0.00232, 18)]

        self.contract.set_rates(token_addresses, base_buy_rates,
                                base_sell_rates)

        # Generate rate changes and set new rates
        buy_changes = [128, random.randint(-128, 127)]
        sell_changes = [-129, random.randint(-128, 127)]

        new_buy_rates = [
            int(rate * (1 + changes / 1000))
            for rate, changes in zip(base_buy_rates, buy_changes)
        ]
        new_sell_rates = [
            int(rate * (1 + changes / 1000))
            for rate, changes in zip(base_sell_rates, sell_changes)
        ]

        self.contract.set_rates(token_addresses, new_buy_rates, new_sell_rates)

        # Check base rates
        self.assertEqual(
            self.contract.get_basic_rate(token_addresses[0], buy=True),
            new_buy_rates[0])
        self.assertEqual(
            self.contract.get_basic_rate(token_addresses[0], buy=False),
            new_sell_rates[0])

        # Check compact rates first token
        _, _, compact_buy, compact_sell = self.contract.get_compact_data(
            token_addresses[0])

        compact_buy = int.from_bytes(compact_buy,
                                     byteorder='little',
                                     signed=True)
        compact_sell = int.from_bytes(compact_sell,
                                      byteorder='little',
                                      signed=True)

        self.assertEqual(compact_buy, 0)
        self.assertEqual(compact_sell, 0)

        # Check compact rates second token
        _, _, compact_buy, compact_sell = self.contract.get_compact_data(
            token_addresses[1])

        compact_buy = int.from_bytes(compact_buy,
                                     byteorder='little',
                                     signed=True)
        compact_sell = int.from_bytes(compact_sell,
                                      byteorder='little',
                                      signed=True)

        self.assertLessEqual(abs(compact_buy - buy_changes[1]), 1)
        self.assertLessEqual(abs(compact_sell - sell_changes[1]), 1)
コード例 #5
0
    def test_compact_data(self):
        """
        Set rate
        Set new_rate = rate * (1 + changes / 1000)
        (changes is in range -128...127 bps, 1 bps = 0.01%)

        Expect:
            compact_data = changes
            basic_rate = previous_rate
            rate = new_rate
        """
        token = tokens[0]

        # set rates -> consider as base rates
        token_addresses = [token.address]
        base_buy_rates = [token_wei(500, 18)]
        base_sell_rates = [token_wei(0.00182, 18)]

        self.contract.set_rates(token_addresses, base_buy_rates,
                                base_sell_rates)

        buy_changes = [random.randint(-128, 127) for _ in base_buy_rates]
        sell_changes = [random.randint(-128, 127) for _ in base_sell_rates]
        new_buy_rates = [
            int(rate * (1 + changes / 1000))
            for rate, changes in zip(base_buy_rates, buy_changes)
        ]
        new_sell_rates = [
            int(rate * (1 + changes / 1000))
            for rate, changes in zip(base_sell_rates, sell_changes)
        ]

        self.contract.set_rates(token_addresses, new_buy_rates, new_sell_rates)
        """Check base rates"""
        self.assertEqual(self.contract.get_basic_rate(token.address, buy=True),
                         base_buy_rates[0])
        self.assertEqual(
            self.contract.get_basic_rate(token.address, buy=False),
            base_sell_rates[0])
        """Check compact rates"""
        _, _, compact_buy, compact_sell = self.contract.get_compact_data(
            token.address)

        compact_buy = int.from_bytes(compact_buy,
                                     byteorder='little',
                                     signed=True)
        compact_sell = int.from_bytes(compact_sell,
                                      byteorder='little',
                                      signed=True)

        self.assertLessEqual(abs(compact_buy - buy_changes[0]), 1)
        self.assertLessEqual(abs(compact_sell - sell_changes[0]), 1)
        """Check rates"""
        sell = self.contract.get_sell_rate(token.address, 1)
        buy = self.contract.get_buy_rate(token.address, 1)

        bps = 0.0001 * 10**18  # 1 bps = 0.01%

        self.assertLess(abs((sell - new_sell_rates[0]) / new_sell_rates[0]),
                        bps)
        self.assertLess(abs((buy - new_buy_rates[0]) / new_buy_rates[0]), bps)