Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValueError: Index '174' not present in Enum type mapping #335

Closed
cambriadaniele opened this issue May 2, 2023 · 4 comments
Closed

ValueError: Index '174' not present in Enum type mapping #335

cambriadaniele opened this issue May 2, 2023 · 4 comments

Comments

@cambriadaniele
Copy link

Hello,
I am trying to read and call a simple flipper smart contract deployed on the Aleph Zero testnet. I have no problems interacting with the blockchain, I get an error just while calling contract.read()

After writing my own code and smart contract, and not being able to overcome the error, i started using the flipper contract and the exact same code from the examples. I still get this error:

2023-05-02 17:46:35 Traceback (most recent call last):
2023-05-02 17:46:35   File "/usr/local/lib/python3.8/site-packages/scalecodec/types.py", line 1232, in process
2023-05-02 17:46:35     enum_type_mapping = self.type_mapping[self.index]
2023-05-02 17:46:35 IndexError: list index out of range
2023-05-02 17:46:35 
2023-05-02 17:46:35 During handling of the above exception, another exception occurred:
2023-05-02 17:46:35 
2023-05-02 17:46:35 Traceback (most recent call last):
2023-05-02 17:46:35   File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 2528, in wsgi_app
2023-05-02 17:46:35     response = self.full_dispatch_request()
2023-05-02 17:46:35   File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1825, in full_dispatch_request
2023-05-02 17:46:35     rv = self.handle_user_exception(e)
2023-05-02 17:46:35   File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1823, in full_dispatch_request
2023-05-02 17:46:35     rv = self.dispatch_request()
2023-05-02 17:46:35   File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1799, in dispatch_request
2023-05-02 17:46:35     return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
2023-05-02 17:46:35   File "/app/app.py", line 39, in deploy_certificates
2023-05-02 17:46:35     return blockchainService.initialize_substrate_interface()
2023-05-02 17:46:35   File "/app/blockchain_service.py", line 41, in initialize_substrate_interface
2023-05-02 17:46:35     gas_predit_result = contract.read(keypair, 'flip')
2023-05-02 17:46:35   File "/usr/local/lib/python3.8/site-packages/substrateinterface/contracts.py", line 795, in read
2023-05-02 17:46:35     call_result = self.substrate.runtime_call("ContractsApi", "call", {
2023-05-02 17:46:35   File "/usr/local/lib/python3.8/site-packages/substrateinterface/base.py", line 1258, in runtime_call
2023-05-02 17:46:31  * Debug mode: off
2023-05-02 17:46:35     result_obj.decode(ScaleBytes(result_data['result']))
2023-05-02 17:46:35   File "/usr/local/lib/python3.8/site-packages/scalecodec/base.py", line 874, in decode
2023-05-02 17:46:35     self.value_serialized = self.process()
2023-05-02 17:46:35   File "/usr/local/lib/python3.8/site-packages/scalecodec/types.py", line 1755, in process
2023-05-02 17:46:35     value = super().process()
2023-05-02 17:46:35   File "/usr/local/lib/python3.8/site-packages/scalecodec/types.py", line 1245, in process
2023-05-02 17:46:35     raise ValueError("Index '{}' not present in Enum type mapping".format(self.index))
2023-05-02 17:46:35 ValueError: Index '174' not present in Enum type mapping

this is my code:

        substrate = substrateinterface.SubstrateInterface(
            url="wss://ws.test.azero.dev",
            ss58_format=42,  # Change according to the network
            type_registry_preset="substrate-node-template",
        )

        # Create a Keypair from the seed phrase
        keypair = substrateinterface.Keypair.create_from_mnemonic(seed_phrase)
        # Set the sender account as the keypair account
        sending_address = keypair.ss58_address


        contract_address = "5EMmoPRx83Umuq6KpivRuhFKNe9vJVct4GfoFRQC1UFPRwa7"
        

        contract = substrateinterface.ContractInstance.create_from_address(
            contract_address=contract_address,
            metadata_file=os.path.join(os.path.dirname(__file__), 'assets', 'flipper.json'),
            substrate=substrate
        )

        """result = contract.read(keypair, 'get')
        print('Current value of "get":', result.contract_result_data)"""

        # Do a gas estimation of the message
        gas_predit_result = contract.read(keypair, 'flip')

        print('Result of dry-run: ', gas_predit_result.value)
        print('Gas estimate: ', gas_predit_result.gas_required)

        # Do the actual call
        print('Executing contract call...')
        contract_receipt = contract.exec(keypair, 'flip', args={

        }, gas_limit=gas_predit_result.gas_required)

        if contract_receipt.is_success:
            print(f'Events triggered in contract: {contract_receipt.contract_events}')
        else:
            print(f'Error message: {contract_receipt.error_message}')

I am using a docker image, python 3.8
please let me know if I'm the only one having this issue

@arjanz
Copy link
Member

arjanz commented May 3, 2023

There are some known issues atm with the contract interface, seems not up to date with latest version of the Substrate contract pallet. Also see #334, I will open a PR soon where you can track the status of this issue.

@arjanz
Copy link
Member

arjanz commented May 3, 2023

I'm working on a PR, for now I have a workaround to read and exec contract methods, until the new release. Add the following type registry override to the init:

substrate = SubstrateInterface(
        url="wss://ws.test.azero.dev",
        type_registry={
            "types": {
                "ContractExecResult": "ContractExecResultTo267"
            }
        }
    )

@cambriadaniele
Copy link
Author

Thanks a lot for looking into it, I know it's a workaround, but everything works perfectly.

@arjanz
Copy link
Member

arjanz commented May 8, 2023

Fix released: https://github.com/polkascan/py-substrate-interface/releases/tag/v1.7.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants