Skip to content

Commit

Permalink
Determine correct ContractExecResult by PalletVersion (#370)
Browse files Browse the repository at this point in the history
* Fixed generate PalletVersion storage key
* Determine correct `ContractExecResult` by PalletVersion
  • Loading branch information
arjanz authored Dec 29, 2023
1 parent 40f255c commit 43fcc8e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
4 changes: 2 additions & 2 deletions substrateinterface/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ def query_map(self, module: str, storage_function: str, params: Optional[list] =

# Generate storage key prefix
storage_key = StorageKey.create_from_storage_function(
module, storage_function, params, runtime_config=self.runtime_config, metadata=self.metadata
module, storage_item.value['name'], params, runtime_config=self.runtime_config, metadata=self.metadata
)
prefix = storage_key.to_hex()

Expand Down Expand Up @@ -1021,7 +1021,7 @@ def subscription_handler(obj, update_nr, subscription_id):
else:

storage_key = StorageKey.create_from_storage_function(
module, storage_function, params, runtime_config=self.runtime_config, metadata=self.metadata
module, storage_item.value['name'], params, runtime_config=self.runtime_config, metadata=self.metadata
)

if callable(subscription_handler):
Expand Down
20 changes: 19 additions & 1 deletion substrateinterface/contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from .utils import version_tuple

from substrateinterface.exceptions import ExtrinsicFailedException, DeployContractFailedException, \
ContractReadFailedException, ContractMetadataParseException
ContractReadFailedException, ContractMetadataParseException, StorageFunctionNotFound
from scalecodec.base import ScaleBytes, ScaleType
from scalecodec.types import GenericContractExecResult
from substrateinterface.base import SubstrateInterface, Keypair, ExtrinsicReceipt
Expand Down Expand Up @@ -718,6 +718,24 @@ def __init__(self, contract_address: str, metadata: ContractMetadata = None, sub
self.contract_address = contract_address
self.metadata = metadata

self.init()

def init(self):
# Determine ContractExecResult according to PalletVersion
try:
pallet_version = self.substrate.query("Contracts", "PalletVersion")

if pallet_version.value <= 9:
self.substrate.runtime_config.update_type_registry_types(
{"ContractExecResult": "ContractExecResultTo267"}
)
else:
self.substrate.runtime_config.update_type_registry_types(
{"ContractExecResult": "ContractExecResultTo269"}
)
except StorageFunctionNotFound:
pass

@classmethod
def create_from_address(cls, contract_address: str, metadata_file: str,
substrate: SubstrateInterface = None) -> "ContractInstance":
Expand Down
6 changes: 3 additions & 3 deletions test/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ def test_well_known_keys_not_found(self):

def test_well_known_pallet_version(self):

sf = self.kusama_substrate.get_metadata_storage_function("System", "PalletVersion")
sf = self.kusama_substrate.get_metadata_storage_function("Balances", "PalletVersion")
self.assertEqual(sf.value['name'], ':__STORAGE_VERSION__:')

result = self.kusama_substrate.query("System", "PalletVersion")
self.assertGreaterEqual(result.value, 0)
result = self.kusama_substrate.query("Balances", "PalletVersion")
self.assertGreaterEqual(result.value, 1)

def test_query_multi(self):

Expand Down

0 comments on commit 43fcc8e

Please sign in to comment.