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

Add support for runtime calls at other blocks than the most recent. #346

Merged
merged 1 commit into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions substrateinterface/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1202,7 +1202,7 @@ def retrieve_pending_extrinsics(self) -> list:

return extrinsics

def runtime_call(self, api: str, method: str, params: Union[list, dict] = None) -> ScaleType:
def runtime_call(self, api: str, method: str, params: Union[list, dict] = None, block_hash: str = None) -> ScaleType:
"""
Calls a runtime API method

Expand All @@ -1211,6 +1211,7 @@ def runtime_call(self, api: str, method: str, params: Union[list, dict] = None)
api: Name of the runtime API e.g. 'TransactionPaymentApi'
method: Name of the method e.g. 'query_fee_details'
params: List of parameters needed to call the runtime API
block_hash: Hash of the block at which to make the runtime API call

Returns
-------
Expand Down Expand Up @@ -1251,7 +1252,7 @@ def runtime_call(self, api: str, method: str, params: Union[list, dict] = None)
param_data += scale_obj.encode(params[param['name']])

# RPC request
result_data = self.rpc_request("state_call", [f'{api}_{method}', str(param_data)])
result_data = self.rpc_request("state_call", [f'{api}_{method}', str(param_data), block_hash])

# Decode result
result_obj = self.runtime_config.create_scale_object(runtime_call_def['type'])
Expand Down
5 changes: 3 additions & 2 deletions substrateinterface/contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ def create_from_address(cls, contract_address: str, metadata_file: str,
return cls(contract_address=contract_address, metadata=metadata, substrate=substrate)

def read(self, keypair: Keypair, method: str, args: dict = None,
value: int = 0, gas_limit: int = None) -> GenericContractExecResult:
value: int = 0, gas_limit: int = None, block_hash: str = None) -> GenericContractExecResult:
"""
Used to execute non-mutable messages to for example read data from the contract using getters. Can also be used
to predict gas limits and 'dry-run' the execution when a mutable message is used.
Expand All @@ -754,6 +754,7 @@ def read(self, keypair: Keypair, method: str, args: dict = None,
args: arguments of message in {'name': value} format
value: value to send when executing the message
gas_limit: dict repesentation of `WeightV2` type
block_hash: hash of the block to execute the message on

Returns
-------
Expand All @@ -770,7 +771,7 @@ def read(self, keypair: Keypair, method: str, args: dict = None,
'origin': keypair.ss58_address,
'value': value,
'storage_deposit_limit': None
})
}, block_hash)
if 'Error' in call_result['result']:
raise ContractReadFailedException(call_result.value['result']['Error'])

Expand Down
6 changes: 6 additions & 0 deletions test/test_contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,12 @@ def test_instance_read(self):

self.assertEqual(False, result.contract_result_data.value)

def test_instance_read_at_not_best_block(self):
parent_hash = self.substrate.get_block_header()['header']['parentHash']
result = self.contract.read(self.keypair, 'get', block_hash = parent_hash)

self.assertEqual(False, result.contract_result_data.value)


class FlipperInstanceV4TestCase(FlipperInstanceTestCase):
def setUp(self) -> None:
Expand Down
7 changes: 7 additions & 0 deletions test/test_runtime_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ def test_core_version(self):
self.assertGreater(result.value['spec_version'], 0)
self.assertEqual('polkadot', result.value['spec_name'])

def test_core_version_at_not_best_block(self):
parent_hash = self.substrate.get_block_header()['header']['parentHash']
result = self.substrate.runtime_call("Core", "version", block_hash = parent_hash)

self.assertGreater(result.value['spec_version'], 0)
self.assertEqual('polkadot', result.value['spec_name'])

def test_transaction_payment(self):
call = self.substrate.compose_call(
call_module='Balances',
Expand Down