polkascan / py-substrate-interface

Python Substrate Interface
https://polkascan.github.io/py-substrate-interface/
Apache License 2.0
239 stars 111 forks source link

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

Closed cambriadaniele closed 1 year ago

cambriadaniele commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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

arjanz commented 1 year ago

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