示例#1
0
    def convert_tx_to_jsonrpc_request(transaction, wallet: Wallet = None):
        """Converts an instance of the transaction into JSON RPC request in dict"""
        dict_tx = {
            "version":
            convert_int_to_hex_str(transaction.version)
            if transaction.version else "0x3",
            "from":
            transaction.from_ if transaction.from_ else wallet.get_address(),
            "to":
            transaction.to,
            "stepLimit":
            convert_int_to_hex_str(transaction.step_limit),
            "timestamp":
            convert_int_to_hex_str(transaction.timestamp)
            if transaction.timestamp else get_timestamp(),
            "nid":
            convert_int_to_hex_str(transaction.nid)
            if transaction.nid else "0x1"
        }

        if transaction.value is not None:
            dict_tx["value"] = convert_int_to_hex_str(transaction.value)

        if transaction.nonce is not None:
            dict_tx["nonce"] = convert_int_to_hex_str(transaction.nonce)

        if transaction.data_type is not None:
            dict_tx["dataType"] = transaction.data_type

        if transaction.data_type in ('deploy', 'call'):
            dict_tx["data"] = generate_data_value(transaction)
        elif transaction.data_type == 'message':
            dict_tx["data"] = transaction.data

        return dict_tx
    def to_dict(transaction, wallet: Wallet = None):
        """Converts an instance of the transaction into a dictionary"""
        dict_tx = {
            "version":
            convert_int_to_hex_str(transaction.version)
            if transaction.version else "0x3",
            "from":
            transaction.from_ if transaction.from_ else wallet.get_address(),
            "stepLimit":
            convert_int_to_hex_str(transaction.step_limit),
            "timestamp":
            convert_int_to_hex_str(transaction.timestamp)
            if transaction.timestamp else get_timestamp(),
            # Network ID ("0x1" for Main net, "0x2" for Test net, etc)
            "nid":
            convert_int_to_hex_str(transaction.nid)
            if transaction.nid else "0x1"
        }

        if transaction.to is not None:
            dict_tx["to"] = transaction.to

        # value can be 0
        if transaction.value is not None:
            dict_tx["value"] = convert_int_to_hex_str(transaction.value)

        if transaction.nonce is not None:
            dict_tx["nonce"] = convert_int_to_hex_str(transaction.nonce)

        if transaction.data_type is not None:
            dict_tx["dataType"] = transaction.data_type

        if transaction.data_type in ('deploy', 'call', 'message'):
            dict_tx["data"] = generate_data_value(transaction)
        return dict_tx
    def setUpClass(cls):
        """
        Sets needed data like an instance of a wallet and iconsdk
        and default values used to make 4 types of transactions. (transfer, call, deploy, message)
        """
        cls.wallet = KeyWallet.load(PRIVATE_KEY_FOR_TEST)

        install_content_bytes = b'install_test'
        update_content_bytes = b'update_test'

        cls.transaction_as_setting = {
            "from_": cls.wallet.get_address(),
            "to": "hx5bfdb090f43a808005ffc27c25b213145e80b7cd",
            "to_governance": "cx0000000000000000000000000000000000000001",
            "step_limit": 4000000000,
            "nid": 3,
            "nonce": 3,
            # It is used to send icx(transfer) only.
            "value": 1000000000000000000,
            # It is used to send call only.
            "method": "transfer",
            "params_call": {
                "to": "hxab2d8215eab14bc6bdd8bfb2c8151257032ecd8b",
                "value": "0x1"
            },
            # It is used to send SCORE install(deploy).
            # If SCORE's address is as follows, it means install SCORE.
            "to_install": "cx0000000000000000000000000000000000000000",
            "content_type": "application/zip",
            # Data type of content should be bytes.
            "content_install": install_content_bytes,
            "content_update": update_content_bytes,
            # It is used to deploy only.(install)
            "params_install": {
                "init_supply": 10000
            },
            # It is used to send message only.
            "data": "0x" + "test".encode().hex(),
            "version": 3,
            "timestamp": get_timestamp(),
            "id": "0x" + "1" * 64,
            "amount": 10000,
        }
示例#4
0
    def estimate_step(self, transaction: Transaction) -> int:
        """
        Returns an estimated step of how much step is necessary to allow the transaction to complete.

        :param transaction: Transaction
        :return: an estimated step
        """
        if not isinstance(transaction, Transaction):
            raise DataTypeException("Transaction object is unrecognized.")

        params = {
            "version":
            convert_int_to_hex_str(transaction.version)
            if transaction.version else "0x3",
            "from":
            transaction.from_,
            "to":
            transaction.to,
            "timestamp":
            convert_int_to_hex_str(transaction.timestamp)
            if transaction.timestamp else get_timestamp(),
            "nid":
            convert_int_to_hex_str(transaction.nid)
            if transaction.nid else "0x1"
        }

        if transaction.value is not None:
            params["value"] = convert_int_to_hex_str(transaction.value)

        if transaction.nonce is not None:
            params["nonce"] = convert_int_to_hex_str(transaction.nonce)

        if transaction.data_type is not None:
            params["dataType"] = transaction.data_type

        if transaction.data_type in ('deploy', 'call'):
            params["data"] = generate_data_value(transaction)
        elif transaction.data_type == 'message':
            params["data"] = transaction.data

        result = self.__provider.make_request('debug_estimateStep', params)
        return int(result, 16)