Beispiel #1
0
    def test_sell(self):
        connector_token2 = Address.from_string("cx" + os.urandom(20).hex())
        connector_token2_weight = 500000
        self.score.addConnector(connector_token2, connector_token2_weight,
                                False)

        return_amount = 100
        return_fee = 1
        connector_balance = 1000
        trader = Address.from_string("cx" + os.urandom(20).hex())
        amount = 100
        min_return = 100

        sale_return = {'amount': return_amount, 'fee': return_fee}
        self.score.get_sale_return = Mock(return_value=sale_return)
        self.score.getConnectorBalance = Mock(return_value=connector_balance)
        inter_call_return = Mock()

        with patch.object(InternalCall,
                          'other_external_call',
                          return_value=inter_call_return):
            result = self.score._sell(trader, connector_token2, amount,
                                      min_return)
            assert_inter_call(self, self.score.address,
                              self.score._token.get(), 'destroy',
                              [self.score.address, return_amount])
            self.score.Conversion.assert_called_with(self.score._token.get(),
                                                     connector_token2, trader,
                                                     amount, return_amount,
                                                     return_fee)
            self.score.PriceDataUpdate.assert_called_with(
                connector_token2, inter_call_return, connector_balance,
                connector_token2_weight)
            self.assertEqual(return_amount, result)
Beispiel #2
0
    def test_tokenFallback_cross_convert_called(self):
        # Mocks parent functions
        self.score.getOwner.return_value = self.owner
        self.score._is_active.return_value = True
        self.score._convert_cross_connector = Mock()

        network_address = Address.from_string("cx" + os.urandom(20).hex())

        to_token = Address.from_string("cx" + os.urandom(20).hex())
        to_token_weight = 500000
        self.score.addConnector(to_token, to_token_weight, False)

        min_return = 10

        data = {'toToken': str(to_token), 'minReturn': min_return}

        # success case
        token = self.initial_connector_token
        value = 100
        with MultiPatch([
                patch_property(IconScoreBase, 'msg', Message(token)),
                patch.object(InternalCall,
                             'other_external_call',
                             return_value=network_address)
        ]):
            self.score.tokenFallback(network_address, value,
                                     json_dumps(data).encode())
            assert_inter_call(self, self.score.address,
                              self.score._registry.get(), 'getAddress',
                              [ScoreRegistry.NETWORK])
            self.score._convert_cross_connector.assert_called_with(
                network_address, token, to_token, value, min_return)
Beispiel #3
0
    def test_convert_cross_connector(self):
        connector_token2 = Address.from_string("cx" + os.urandom(20).hex())
        connector_token2_weight = 500000
        self.score.addConnector(connector_token2, connector_token2_weight,
                                False)

        return_amount = 100
        return_fee = 1
        connector_balance = 1000
        trader = Address.from_string("cx" + os.urandom(20).hex())
        amount = 100
        min_return = 100

        convert_return = {'amount': return_amount, 'fee': return_fee}
        self.score.get_cross_connector_return = Mock(
            return_value=convert_return)
        self.score.getConnectorBalance = Mock(return_value=connector_balance)
        inter_call_return = Mock()

        with patch.object(InternalCall,
                          'other_external_call',
                          return_value=inter_call_return):
            self.score._convert_cross_connector(trader,
                                                self.initial_connector_token,
                                                connector_token2, amount,
                                                min_return)
            assert_inter_call(self, self.score.address, connector_token2,
                              'transfer',
                              [trader, return_amount, TRANSFER_DATA])

            self.score.Conversion.assert_called_with(
                self.initial_connector_token, connector_token2, trader, amount,
                return_amount, return_fee)

            first_price_update_event = self.score.PriceDataUpdate.call_args_list[
                0]
            self.assertEqual(first_price_update_event[0][0],
                             self.initial_connector_token)
            self.assertEqual(first_price_update_event[0][1], inter_call_return)
            self.assertEqual(first_price_update_event[0][2], connector_balance)
            self.assertEqual(first_price_update_event[0][3],
                             self.initial_connector_weight)

            second_price_update_event = self.score.PriceDataUpdate.call_args_list[
                1]
            self.assertEqual(second_price_update_event[0][0], connector_token2)
            self.assertEqual(second_price_update_event[0][1],
                             inter_call_return)
            self.assertEqual(second_price_update_event[0][2],
                             connector_balance)
            self.assertEqual(second_price_update_event[0][3],
                             connector_token2_weight)
    def test_acceptTokenOwnership(self):
        with MultiPatch([
            patch_property(IconScoreBase, 'msg', Message(self.owner)),
            patch.object(InternalCall, 'other_external_call'),
        ]):
            self.score.acceptTokenOwnership()
            self.score.require_owner_only.assert_called()

            assert_inter_call(
                self,
                self.score.address,
                self.score._token.get(),
                'acceptOwnerShip',
                []
            )
    def test_disableTokenTransfers(self):
        disable = True
        with MultiPatch([
            patch_property(IconScoreBase, 'msg', Message(self.owner)),
            patch.object(InternalCall, 'other_external_call'),
        ]):
            self.score.disableTokenTransfers(disable)
            self.score.require_owner_only.assert_called()

            assert_inter_call(
                self,
                self.score.address,
                self.score._token.get(),
                'disableTransfer',
                [disable]
            )
    def test_transferTokenOwnership(self):
        new_owner = Address.from_string("hx" + "3" * 40)

        with MultiPatch([
            patch_property(IconScoreBase, 'msg', Message(self.owner)),
            patch.object(InternalCall, 'other_external_call'),
        ]):
            self.score.transferTokenOwnership(new_owner)
            self.score.require_owner_only.assert_called()

            assert_inter_call(
                self,
                self.score.address,
                self.score._token.get(),
                'transferOwnerShip',
                [new_owner])
Beispiel #7
0
    def test_getConnectorBalance(self):
        balance = 100

        with MultiPatch([
                patch_property(IconScoreBase, 'msg', Message(self.owner)),
                patch.object(InternalCall,
                             'other_external_call',
                             return_value=balance)
        ]):
            result_balance = self.score.getConnectorBalance(
                self.initial_connector_token)

            assert_inter_call(self, self.score.address,
                              self.initial_connector_token, 'balanceOf',
                              [self.score.address])

            self.assertEqual(result_balance, balance)
    def test_withdrawFromToken(self):
        token = Address.from_string("cx" + "3" * 40)
        to = Address.from_string("hx" + "3" * 40)
        amount = 100
        with MultiPatch([
            patch_property(IconScoreBase, 'msg', Message(self.owner)),
            patch.object(InternalCall, 'other_external_call'),
        ]):
            self.score.withdrawFromToken(token, to, amount)
            self.score.require_owner_only.assert_called()

            assert_inter_call(
                self,
                self.score.address,
                self.score._token.get(),
                'withdrawTokens',
                [token, to, amount]
            )
    def test_proxy_call(self):
        # tests the proxy call

        self_address = Mock(Address)
        score_address = Mock(Address)
        to_address = Mock(Address)

        value = 1

        icon_score_base = Mock(IconScoreBase)
        icon_score_base.address = self_address

        score = ScoreInterface(score_address, icon_score_base)

        with patch.object(InternalCall, 'other_external_call'):
            score.transfer(to_address, value)

            assert_inter_call(self, self_address, score_address, 'transfer',
                              [to_address, 1])
Beispiel #10
0
    def test_updateRegistry(self):
        self.score._allow_registry_update.set(True)

        old_registry = self.score._registry.get()
        new_registry = Address.from_string("cx" + os.urandom(20).hex())

        with MultiPatch([
                patch_property(IconScoreBase, 'msg', Message(self.owner)),
                patch.object(InternalCall,
                             'other_external_call',
                             return_value=new_registry)
        ]):
            self.score.updateRegistry()

            assert_inter_call(self, self.score.address, old_registry,
                              'getAddress', [ScoreRegistry.SCORE_REGISTRY])

            self.assertEqual(old_registry, self.score._prev_registry.get())
            self.assertEqual(new_registry, self.score._registry.get())