How to use iconservice - 10 common examples

To help you get started, we’ve selected a few iconservice examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github icon-project / icon-service / tests / integrate_test / test_integrate_base_transaction_validation.py View on Github external
def setUp(self):
        _PREPS_LEN = 30
        _MAIN_PREPS_LEN = 22
        _AMOUNT_DELEGATE = 10000
        _MINIMUM_DELEGATE_AMOUNT = 10 ** 18
        # same as fee treasury address constant value
        self._fee_treasury = Address.from_prefix_and_int(AddressPrefix.CONTRACT, 1)
        default_icon_config[ConfigKey.IISS_CALCULATE_PERIOD] = 10
        default_icon_config[ConfigKey.TERM_PERIOD] = 10
        super().setUp()
        self._update_governance()
        self._set_revision(REV_IISS)

        addr_array = [create_address() for _ in range(_PREPS_LEN)]

        total_supply = 800_460_000 * ICX_IN_LOOP

        # Minimum_delegate_amount is 0.02 * total_supply
        # In this test delegate 0.03*total_supply because `Issue transaction` exists since REV_IISS
        delegate_amount = total_supply * 2 // 1000

        # generate preps
        self._decentralize(addr_array, delegate_amount)

        response = self._get_prep_list()
github icon-project / icon-service / tests / integrate_test / iiss / decentralized / test_base_transaction_validation.py View on Github external
def _init_decentralized(self):
        # decentralized
        self.update_governance()

        # set Revision REV_IISS
        self.set_revision(Revision.IISS.value)

        total_supply = TOTAL_SUPPLY * ICX_IN_LOOP
        # Minimum_delegate_amount is 0.02 * total_supply
        # In this test delegate 0.03*total_supply because `Issue transaction` exists since REV_IISS
        minimum_delegate_amount_for_decentralization: int = total_supply * 2 // 1000 + 1
        init_balance: int = minimum_delegate_amount_for_decentralization * 2

        # distribute icx PREP_MAIN_PREPS ~ PREP_MAIN_PREPS + PREP_MAIN_PREPS - 1
        self.distribute_icx(accounts=self._accounts[PREP_MAIN_PREPS:PREP_MAIN_AND_SUB_PREPS],
                            init_balance=init_balance)

        # stake PREP_MAIN_PREPS ~ PREP_MAIN_PREPS + PREP_MAIN_PREPS - 1
        stake_amount: int = minimum_delegate_amount_for_decentralization
        tx_list: list = []
        for i in range(PREP_MAIN_PREPS):
            tx: dict = self.create_set_stake_tx(from_=self._accounts[PREP_MAIN_PREPS + i],
github icon-project / icon-service / tests / unit_test / iconscore / test_icon_score_api.py View on Github external
TX_V3 = {
    'version': '0x3',
    'nid': '0x1',
    'from': 'hx522bff55a62e0c75a1b51855b0802cfec6a92e84',
    'to': 'hx11de4e28be4845de3ea392fd8d758655bf766ca7',
    'value': '0x71afd498d0000',
    'stepLimit': '0xf4240',
    'timestamp': '0x57a4e5556cc03',
    'signature': 'fcEMXqEGlqEivXXr7YtD/F1RXgxSXF+R4gVrGKxT1zxi3HukX4NzkSl9/Es1G+nyZx+kviTAtQFUrA+/T0NrfAA=',
    'txHash': '6c71ac77b2d130a1f81d234e814974e85cabb0a3ec462c66ff3f820502d0ded2'
}
STEP_LIMIT = 1_000_000_000
STEP_COSTS = {
    StepType.DEFAULT: 0,
    StepType.CONTRACT_CALL: 25_000,
    StepType.CONTRACT_CREATE: 1_000_000_000,
    StepType.CONTRACT_UPDATE: 1_600_000_000,
    StepType.CONTRACT_DESTRUCT: -70000,
    StepType.CONTRACT_SET: 30_000,
    StepType.GET: 0,
    StepType.SET: 320,
    StepType.REPLACE: 80,
    StepType.DELETE: -240,
    StepType.INPUT: 200,
    StepType.EVENT_LOG: 100,
    StepType.API_CALL: 10_000
}


def _calc_step_cost(ratio: ScoreApiStepRatio) -> int:
    step_cost: int = STEP_COSTS[StepType.API_CALL] * ratio // ScoreApiStepRatio.SHA3_256
    return step_cost
github icon-project / icon-service / tests / integrate_test / iiss / test_integrate_decentralization_trigger.py View on Github external
tx2 = self._stake(self._addr_array[23], stake_amount)
        tx3 = self._stake(self._addr_array[24], stake_amount)
        prev_block, tx_results, main_prep_list = self._make_and_req_block_for_prep_test([tx1, tx2, tx3])
        self.assertIsNone(main_prep_list)
        self._write_precommit_state(prev_block)

        self._set_revision(REV_DECENTRALIZATION)

        # register preps
        reg_prep_tx_list = []
        for i, address in enumerate(self._main_preps):
            data: dict = {
                ConstantKeys.NAME: "name",
                ConstantKeys.EMAIL: "email",
                ConstantKeys.WEBSITE: "website",
                ConstantKeys.DETAILS: "json",
                ConstantKeys.P2P_END_POINT: "ip",
                ConstantKeys.PUBLIC_KEY: f'publicKey{i}'.encode(),
                ConstantKeys.IREP: IISS_MIN_IREP
            }
            reg_prep_tx_list.append(self._reg_prep(address, data))

        prev_block, tx_results, main_prep_list = self._make_and_req_block_for_prep_test(reg_prep_tx_list)
        self.assertIsNone(main_prep_list)
        self._write_precommit_state(prev_block)

        # delegate
        data: list = [
            {
                "address": str(address),
                "value": hex(_DELEGATE_AMOUNT)
            }for address in self._addr_array[:10]
github icon-project / icon-service / tests / integrate_test / test_integrate_existent_scores_audit.py View on Github external
def _setUp_audit(self):
        self.config.update_conf(
            {
                ConfigKey.SERVICE: {
                    ConfigKey.SERVICE_AUDIT: True,
                    ConfigKey.SERVICE_FEE: False,
                    ConfigKey.SERVICE_SCORE_PACKAGE_VALIDATOR: False,
                }
            }
        )
        self.icon_service_engine = IconServiceEngine()
        self.icon_service_engine.open(self.config)
        self._genesis_invoke()
        self.token_initial_params = {"init_supply": hex(1000), "decimal": "0x12"}
github icon-project / icon-service / tests / legacy_unittest / fee / test_deposit.py View on Github external
def test_to_bytes_from_bytes(self):
        """
        - Checks if Deposit object in bytes is converted into Deposit object correctly.
        - Checks if Deposit object is converted into Deposit object in bytes correctly.
        """
        deposit = Deposit()
        deposit.id = create_tx_hash()
        deposit.score_address = create_address(AddressPrefix.CONTRACT)
        deposit.sender = create_address(AddressPrefix.EOA)
        deposit.deposit_amount = 10000
        deposit.deposit_used = 10000
        deposit.created = 10
        deposit.expires = 1000000
        deposit.virtual_step_issued = 100000000000
        deposit.virtual_step_used = 200000000000
        deposit.prev_id = create_tx_hash()
        deposit.next_id = create_tx_hash()

        deposit_in_bytes = deposit.to_bytes()
        self.assertIsInstance(deposit_in_bytes, bytes)
        deposit2 = Deposit.from_bytes(deposit_in_bytes)
        self.assertIsInstance(deposit2, Deposit)
        self.assertEqual(deposit, deposit2)
github icon-project / icon-service / tests / unittest / deploy / test_icon_score_deploy_engine.py View on Github external
def test_write_score_to_score_deploy_path_revision_ge3(
        self, mock_engine, context, mocker, revision
    ):
        self.set_test(mocker, self.score_path, self.score_deploy_path, revision)

        mock_engine._write_score_to_score_deploy_path(
            context, GOVERNANCE_SCORE_ADDRESS, context.tx.hash, None
        )

        isde.get_score_deploy_path.assert_called_with(
            context.score_root_path, GOVERNANCE_SCORE_ADDRESS, context.tx.hash
        )
        os.path.join.assert_called_with(
            context.score_root_path,
            GOVERNANCE_SCORE_ADDRESS.to_bytes().hex(),
            f"0x{context.tx.hash.hex()}",
        )
        isde.remove_path.assert_called_with(self.score_path)
        IconScoreDeployer.deploy.assert_called_with(
            self.score_deploy_path, None, revision
        )
        mocker.stopall()
github icon-project / icon-service / tests / unittest / deploy / test_icon_score_deploy_engine.py View on Github external
def test_deploy_case_tx_param_none(self, mock_engine, context, mocker):
        """case when tx_param is None"""
        self.set_test(mocker, get_deploy_tx_param_return_value=None)

        with pytest.raises(InvalidParamsException) as e:
            mock_engine.deploy(context, context.tx.hash)

        context.storage.deploy.get_deploy_tx_params.assert_called_with(
            context, context.tx.hash
        )
        assert e.value.code == ExceptionCode.INVALID_PARAMETER
        mock_engine._score_deploy.assert_not_called()
        context.storage.deploy.update_score_info.assert_not_called()
        mocker.stopall()
github icon-project / icon-service / tests / integrate_test / test_integrate_base_transaction_validation.py View on Github external
]
        prev_block, tx_results = self._make_and_req_block_for_issue_test(tx_list, is_block_editable=False)
        self._write_precommit_state(prev_block)
        expected_tx_status = 1
        expected_failure = None
        expected_trace = []
        self.assertEqual(expected_tx_status, tx_results[0].status)
        self.assertEqual(expected_failure, tx_results[0].failure)
        self.assertEqual(expected_step_price, tx_results[0].step_price)
        self.assertEqual(expected_step_used, tx_results[0].step_used)
        self.assertEqual(expected_trace, tx_results[0].traces)

        for index, group_key in enumerate(ISSUE_CALCULATE_ORDER):
            if group_key not in self.issue_data:
                continue
            expected_score_address = ZERO_SCORE_ADDRESS
            expected_indexed: list = [ISSUE_EVENT_LOG_MAPPER[group_key]['event_signature']]
            expected_data: list = [self.issue_data[group_key][key] for key in ISSUE_EVENT_LOG_MAPPER[group_key]['data']]
            self.assertEqual(expected_score_address, tx_results[0].event_logs[index].score_address)
            self.assertEqual(expected_indexed, tx_results[0].event_logs[index].indexed)
            self.assertEqual(expected_data, tx_results[0].event_logs[index].data)

        # event log about correction
        self.assertEqual(0, tx_results[0].event_logs[1].data[0])
        self.assertEqual(0, tx_results[0].event_logs[1].data[1])
        self.assertEqual(self.total_issue_amount, tx_results[0].event_logs[1].data[2])
        self.assertEqual(0, tx_results[0].event_logs[1].data[3])

        after_total_supply = self._query({}, "icx_getTotalSupply")
        after_treasury_icx_amount = self._query({"address": self._fee_treasury}, 'icx_getBalance')

        self.assertEqual(before_total_supply + self.total_issue_amount, after_total_supply)
github icon-project / icon-service / tests / unittest / deploy / test_icon_score_deploy_engine.py View on Github external
def test_initialize_score_on_install(self, mock_engine, mocker):
        """case on_install"""
        deploy_type = DeployType.INSTALL

        self.set_test(mocker)

        mock_engine._initialize_score(deploy_type, self.mock_score, self.params)
        TypeConverter.adjust_params_to_method.assert_called_with(
            self.on_install, self.params
        )
        self.on_install.assert_called_with(**self.params)
        self.on_update.assert_not_called()
        self.on_invalid.assert_not_called()
        mocker.stopall()