def test_deposit_transaction_to_dict_for_withdraw_action(self):
     # transaction instance for withdraw action
     tx_list = [
         DepositTransactionBuilder() \
             .from_(self.transaction_as_setting["from_"]) \
             .to(self.transaction_as_setting["to"]) \
             .step_limit(self.transaction_as_setting["step_limit"]) \
             .nid(self.transaction_as_setting["nid"]) \
             .nonce(self.transaction_as_setting["nonce"]) \
             .version(self.transaction_as_setting["version"]) \
             .timestamp(self.transaction_as_setting["timestamp"]) \
             .id(self.transaction_as_setting["id"]) \
             .amount(None)
             .action("withdraw") \
             .build(),
         DepositTransactionBuilder() \
             .from_(self.transaction_as_setting["from_"]) \
             .to(self.transaction_as_setting["to"]) \
             .step_limit(self.transaction_as_setting["step_limit"]) \
             .nid(self.transaction_as_setting["nid"]) \
             .nonce(self.transaction_as_setting["nonce"]) \
             .version(self.transaction_as_setting["version"]) \
             .timestamp(self.transaction_as_setting["timestamp"]) \
             .id(None) \
             .amount(self.transaction_as_setting["amount"]) \
             .action("withdraw") \
             .build(),
     ]
     for tx in tx_list:
         self._check_deposit_withdraw_transaction(tx)
    def test_add_deposit(self, _make_id):
        # transaction instance for add action
        action = "add"
        deposit_transaction = DepositTransactionBuilder() \
            .from_(self.setting["from"]) \
            .to(self.setting["to"]) \
            .value(self.setting["value"]) \
            .timestamp(self.setting["timestamp"]) \
            .step_limit(self.setting["step_limit"]) \
            .nid(self.setting["nid"]) \
            .nonce(self.setting["nonce"]) \
            .action(action) \
            .build()
        signed_transaction = SignedTransaction(deposit_transaction,
                                               self.wallet)

        with requests_mock.Mocker() as m:
            tx_hash = "0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238"
            expected_request = {
                'id': 1234,
                'jsonrpc': '2.0',
                'method': 'icx_sendTransaction',
                'params': {
                    'data': {
                        'action': action
                    },
                    'dataType':
                    'deposit',
                    'from':
                    self.setting["from"],
                    'nid':
                    hex(self.setting["nid"]),
                    'nonce':
                    hex(self.setting["nonce"]),
                    'signature':
                    signed_transaction.signed_transaction_dict["signature"],
                    'stepLimit':
                    hex(self.setting["step_limit"]),
                    'timestamp':
                    hex(self.setting["timestamp"]),
                    'to':
                    self.setting["to"],
                    'value':
                    hex(self.setting["value"]),
                    'version':
                    hex(3)
                }
            }

            response_json = {"jsonrpc": "2.0", "result": tx_hash, "id": 1234}
            m.post(self.matcher, json=response_json)
            result_dict = self.icon_service.send_transaction(
                signed_transaction, full_response=True)
            actual_request = json.loads(m._adapter.last_request.text)
            result_content = result_dict['result']

            self.assertEqual(expected_request, actual_request)
            self.assertEqual(result_success_v3.keys(), result_dict.keys())
            self.assertTrue(is_T_HASH(result_content))
Beispiel #3
0
    def test_deposit_transaction_to_dict_for_withdraw_action(self):

        # transaction instance for withdraw action
        deposit_transaction_of_withdraw_0: DepositTransaction = DepositTransactionBuilder() \
            .from_(self.transaction_as_setting["from_"]) \
            .to(self.transaction_as_setting["to"]) \
            .step_limit(self.transaction_as_setting["step_limit"]) \
            .nid(self.transaction_as_setting["nid"]) \
            .nonce(self.transaction_as_setting["nonce"]) \
            .version(self.transaction_as_setting["version"]) \
            .timestamp(self.transaction_as_setting["timestamp"]) \
            .id(self.transaction_as_setting["id"]) \
            .action("withdraw") \
            .build()

        # convert transaction into dict correctly
        # check 0 : check if it is deposit transaction
        deposit_transaction_of_withdraw_as_dict = deposit_transaction_of_withdraw_0.to_dict(
        )
        self.assertTrue(
            self._is_deposit_transaction(
                deposit_transaction_of_withdraw_as_dict))

        # check 1 : check if converted deposit transaction from dict is same as an origin deposit transaction
        deposit_transaction_of_withdraw_as_dict = deposit_transaction_of_withdraw_0.to_dict(
        )
        deposit_transaction_of_withdraw_1: DepositTransaction = DepositTransactionBuilder \
            .from_dict(deposit_transaction_of_withdraw_as_dict) \
            .build()
        self.assertEqual(deposit_transaction_of_withdraw_0.to_dict(),
                         deposit_transaction_of_withdraw_1.to_dict())

        # case 0 when action is 'withdraw' : without id
        try:
            deposit_transaction_of_add_with_id: DepositTransaction = DepositTransactionBuilder() \
                .from_(self.transaction_as_setting["from_"]) \
                .to(self.transaction_as_setting["to"]) \
                .step_limit(self.transaction_as_setting["step_limit"]) \
                .nid(self.transaction_as_setting["nid"]) \
                .nonce(self.transaction_as_setting["nonce"]) \
                .version(self.transaction_as_setting["version"]) \
                .timestamp(self.transaction_as_setting["timestamp"]) \
                .action("withdraw") \
                .build()
        except DataTypeException:
            self.assertTrue(True)
 def test_convert_tx_to_jsonrpc_request_for_add_deposit_transaction(self):
     # Add deposit
     add_deposit_transaction: DepositTransaction = DepositTransactionBuilder() \
         .from_(self.setting["from"]) \
         .to(self.setting["to"]) \
         .value(self.setting["value"]) \
         .step_limit(self.setting["step_limit"]) \
         .nid(self.setting["nid"]) \
         .nonce(self.setting["nonce"]) \
         .action("add") \
         .build()
     tx_dict = SignedTransaction.convert_tx_to_jsonrpc_request(
         add_deposit_transaction)
     self.assertTrue(is_deposit_transaction(tx_dict, "add"))
    def test_signed_transaction_with_deposit_transaction_of_deposit_action_without_step_limit(
            self):
        deposit_transaction_without_step_limit: DepositTransaction = DepositTransactionBuilder() \
            .from_(self.setting["from"]) \
            .to(self.setting["to"]) \
            .nid(self.setting["nid"]) \
            .nonce(self.setting["nonce"]) \
            .action("withdraw") \
            .id(self.setting["id"]) \
            .build()

        # fail without step limit
        self.assertRaises(DataTypeException, SignedTransaction,
                          deposit_transaction_without_step_limit, self.wallet)
 def test_convert_tx_to_jsonrpc_request_for_withdraw_deposit_transaction(
         self):
     # Withdraw deposit
     withdraw_deposit_transaction: DepositTransaction = DepositTransactionBuilder() \
         .from_(self.setting["from"]) \
         .to(self.setting["to"]) \
         .step_limit(self.setting["step_limit"]) \
         .nid(self.setting["nid"]) \
         .nonce(self.setting["nonce"]) \
         .id(self.setting["id"]) \
         .action("withdraw") \
         .build()
     tx_dict = SignedTransaction.convert_tx_to_jsonrpc_request(
         withdraw_deposit_transaction)
     self.assertTrue(is_deposit_transaction(tx_dict, "withdraw"))
Beispiel #7
0
    def test_add_deposit(self):
        # transaction instance for add action
        deposit_transaction_of_add_0: DepositTransaction = DepositTransactionBuilder() \
            .from_(self.setting["from"]) \
            .to(self.setting["to"]) \
            .value(self.setting["value"]) \
            .step_limit(self.setting["step_limit"]) \
            .nid(self.setting["nid"]) \
            .nonce(self.setting["nonce"]) \
            .action("add") \
            .build()

        # Checks if sending transaction correctly
        signed_transaction_dict = SignedTransaction(
            deposit_transaction_of_add_0, self.wallet)
        result = self.icon_service.send_transaction(signed_transaction_dict)
        self.assertTrue(is_T_HASH(result))
Beispiel #8
0
    def get_transaction(conf: dict, params: dict):
        data_type = params.get('dataType')
        params_data = params.get('data', {})

        try:
            transaction_params = {
                "from_": params['from'],
                "to": params['to'],
                "nid": convert_hex_str_to_int(conf['nid']),
                "value": convert_hex_str_to_int(params.get('value', "0x0"))
            }

            if data_type is None:
                transaction_builder = TransactionBuilder(**transaction_params)
            elif data_type == "call":
                transaction_params['method'] = params_data.get('method')
                transaction_params['params'] = params_data.get('params')
                transaction_builder = CallTransactionBuilder(
                    **transaction_params)
            elif data_type == "deploy":
                transaction_params['content'] = params_data.get('content')
                transaction_params['content_type'] = params_data.get(
                    'contentType')
                transaction_params['params'] = params_data.get('params')
                transaction_builder = DeployTransactionBuilder(**params)
            elif data_type == "message":
                transaction_params['data'] = params_data
                transaction_builder = MessageTransactionBuilder(
                    **transaction_params)
            elif data_type == "deposit":
                transaction_params['action'] = params_data.get('action')
                transaction_params['id'] = params_data.get('id')
                transaction_builder = DepositTransactionBuilder(
                    **transaction_params)
            else:
                raise JsonContentsException("Invalid dataType")
            transaction = transaction_builder.build()
        except KeyError:
            raise JsonContentsException(
                "Invalid json content. check json contents")
        except TypeError:
            raise JsonContentsException("Invalid json content. check keys")
        except DataTypeException:
            raise JsonContentsException("Invalid json content. check values")
        else:
            return transaction
 def test_deposit_transaction_id_and_amount_at_same_time_for_withdraw_action(
         self):
     try:
         deposit_transaction_of_add_with_id: DepositTransaction = DepositTransactionBuilder() \
             .from_(self.transaction_as_setting["from_"]) \
             .to(self.transaction_as_setting["to"]) \
             .step_limit(self.transaction_as_setting["step_limit"]) \
             .nid(self.transaction_as_setting["nid"]) \
             .nonce(self.transaction_as_setting["nonce"]) \
             .version(self.transaction_as_setting["version"]) \
             .timestamp(self.transaction_as_setting["timestamp"]) \
             .action("withdraw") \
             .id(self.transaction_as_setting["id"]) \
             .amount(self.transaction_as_setting["amount"]) \
             .build()
     except DataTypeException:
         self.assertTrue(True)
    def test_signed_transaction_with_deposit_transaction_of_deposit_action_without_step_limit(
            self):
        deposit_transaction_without_step_limit: DepositTransaction = DepositTransactionBuilder() \
            .from_(self.setting["from"]) \
            .to(self.setting["to"]) \
            .nid(self.setting["nid"]) \
            .nonce(self.setting["nonce"]) \
            .action("withdraw") \
            .id(self.setting["id"]) \
            .build()

        # fail without step limit
        self.assertRaises(DataTypeException, SignedTransaction,
                          deposit_transaction_without_step_limit, self.wallet)

        # success with param of step limit
        signed_transaction_dict = SignedTransaction(
            deposit_transaction_without_step_limit, self.wallet,
            self.setting["step_limit"])
        result = self.icon_service.send_transaction(signed_transaction_dict)
        self.assertTrue(is_T_HASH(result))
Beispiel #11
0
    def test_deposit_transaction_to_dict_for_add_action(self):
        """Test for converting deposit transaction object to dict by using 'to_dict' method"""

        # transaction instance for add action
        deposit_transaction_of_add_0: DepositTransaction = DepositTransactionBuilder() \
            .from_(self.transaction_as_setting["from_"]) \
            .to(self.transaction_as_setting["to"]) \
            .value(self.transaction_as_setting["value"]) \
            .step_limit(self.transaction_as_setting["step_limit"]) \
            .nid(self.transaction_as_setting["nid"]) \
            .nonce(self.transaction_as_setting["nonce"]) \
            .version(self.transaction_as_setting["version"]) \
            .timestamp(self.transaction_as_setting["timestamp"]) \
            .action("add") \
            .build()

        # convert transaction into dict correctly
        # check 0 : check if it is deposit transaction
        deposit_transaction_of_add_as_dict = deposit_transaction_of_add_0.to_dict(
        )
        self.assertTrue(
            self._is_deposit_transaction(deposit_transaction_of_add_as_dict))

        # check 1 : check if converted deposit transaction from dict is same as an origin deposit transaction
        deposit_transaction_of_add_as_dict = deposit_transaction_of_add_0.to_dict(
        )
        deposit_transaction_of_add_1: DepositTransaction = DepositTransactionBuilder \
            .from_dict(deposit_transaction_of_add_as_dict) \
            .build()
        self.assertEqual(deposit_transaction_of_add_0.to_dict(),
                         deposit_transaction_of_add_1.to_dict())

        # case 0 when action is 'add' : without action
        tmp_deposit_transaction_of_add_as_dict = deepcopy(
            deposit_transaction_of_add_as_dict)
        del tmp_deposit_transaction_of_add_as_dict["action"]
        self.assertFalse(
            self._is_deposit_transaction(
                tmp_deposit_transaction_of_add_as_dict))

        # case 1 when action is 'add' : without value
        tmp_deposit_transaction_of_add_as_dict = deepcopy(
            deposit_transaction_of_add_as_dict)
        del tmp_deposit_transaction_of_add_as_dict["value"]
        self.assertFalse(
            self._is_deposit_transaction(
                tmp_deposit_transaction_of_add_as_dict))

        # case 2 when action is 'add' : without value
        try:
            deposit_transaction_of_add_with_id: DepositTransaction = DepositTransactionBuilder() \
                .from_(self.transaction_as_setting["from_"]) \
                .to(self.transaction_as_setting["to"]) \
                .step_limit(self.transaction_as_setting["step_limit"]) \
                .nid(self.transaction_as_setting["nid"]) \
                .nonce(self.transaction_as_setting["nonce"]) \
                .version(self.transaction_as_setting["version"]) \
                .timestamp(self.transaction_as_setting["timestamp"]) \
                .action("add") \
                .id(self.transaction_as_setting["id"]) \
                .build()
        except DataTypeException:
            self.assertTrue(True)
Beispiel #12
0
    def test_deposit_add_and_withdraw(self):
        # Test install SCORE : Checks if making an instance of deploy transaction correctly
        param = {"init_supply": 10000}
        deploy_transaction = DeployTransactionBuilder() \
            .from_(self.setting["from"]) \
            .to(self.setting["to_install"]) \
            .step_limit(self.setting["step_limit"]) \
            .nid(self.setting["nid"]) \
            .nonce(self.setting["nonce"]) \
            .content_type(self.setting["content_type"]) \
            .content(self.setting["content_install"]) \
            .params(param) \
            .version(3) \
            .build()
        tx_dict = SignedTransaction.convert_tx_to_jsonrpc_request(
            deploy_transaction)
        self.assertTrue(is_deploy_transaction(tx_dict))

        # Test install SCORE : Sends transaction which makes the SCORE install correctly
        signed_transaction_dict = SignedTransaction(deploy_transaction,
                                                    self.wallet)
        result_install = self.icon_service.send_transaction(
            signed_transaction_dict)
        self.assertTrue(is_T_HASH(result_install))

        sleep(2)
        installed_score_address = self.icon_service.get_transaction_result(
            result_install)["scoreAddress"]

        _DEPOSIT_AMOUNT = 5000 * (10**18)
        deposit_transaction_of_add_0: DepositTransaction = DepositTransactionBuilder() \
            .from_(self.setting["from"]) \
            .to(installed_score_address) \
            .value(_DEPOSIT_AMOUNT) \
            .step_limit(self.setting["step_limit"]) \
            .nid(self.setting["nid"]) \
            .nonce(self.setting["nonce"]) \
            .action("add") \
            .build()

        # Checks if sending transaction correctly
        signed_transaction_dict = SignedTransaction(
            deposit_transaction_of_add_0, self.wallet)
        result = self.icon_service.send_transaction(signed_transaction_dict)
        self.assertTrue(is_T_HASH(result))

        sleep(2)
        self.assertEqual(
            self.icon_service.get_transaction_result(result)["status"], 1)

        # transaction instance for withdraw action
        deposit_transaction_of_withdraw: DepositTransaction = DepositTransactionBuilder() \
            .from_(self.setting["from"]) \
            .to(installed_score_address) \
            .step_limit(self.setting["step_limit"]) \
            .nid(self.setting["nid"]) \
            .nonce(self.setting["nonce"]) \
            .id(result) \
            .action("withdraw") \
            .build()

        signed_transaction_dict = SignedTransaction(
            deposit_transaction_of_withdraw, self.wallet)
        result = self.icon_service.send_transaction(signed_transaction_dict)
        self.assertTrue(is_T_HASH(result))

        sleep(2)
        self.assertEqual(
            self.icon_service.get_transaction_result(result)["status"], 1)