Skip to content

Commit 28f95d7

Browse files
authored
Replace RPC calls with runtime calls (#279)
* Updated contracts code to use runtime call * Replace RPC calls with runtime calls * system_accountNextIndex -> AccountNonceApi.account_nonce * payment_queryInfo -> TransactionPaymentApi.query_info * Bump scalecodec
1 parent 2c874f1 commit 28f95d7

File tree

5 files changed

+35
-26
lines changed

5 files changed

+35
-26
lines changed

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ eth_utils>=1.3.0,<3
1111
pycryptodome>=3.11.0,<4
1212
PyNaCl>=1.0.1,<2
1313

14-
scalecodec>=1.1.2,<1.2
14+
scalecodec>=1.1.3,<1.2
1515
py-sr25519-bindings>=0.2.0,<1
1616
py-ed25519-zebra-bindings>=1.0,<2
1717
py-bip39-bindings>=0.1.9,<1

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@
189189
'eth_utils>=1.3.0,<3',
190190
'pycryptodome>=3.11.0,<4',
191191
'PyNaCl>=1.0.1,<2',
192-
'scalecodec>=1.1.2,<1.2',
192+
'scalecodec>=1.1.3,<1.2',
193193
'py-sr25519-bindings>=0.2.0,<1',
194194
'py-ed25519-zebra-bindings>=1.0,<2',
195195
'py-bip39-bindings>=0.1.9,<1'

substrateinterface/base.py

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,7 +1731,11 @@ def get_account_nonce(self, account_address) -> int:
17311731
-------
17321732
int
17331733
"""
1734-
response = self.rpc_request("system_accountNextIndex", [account_address])
1734+
if self.supports_rpc_method('state_call'):
1735+
nonce_obj = self.runtime_call("AccountNonceApi", "account_nonce", [account_address])
1736+
return nonce_obj.value
1737+
else:
1738+
response = self.rpc_request("system_accountNextIndex", [account_address])
17351739
return response.get('result', 0)
17361740

17371741
def generate_signature_payload(self, call: GenericCall, era=None, nonce: int = 0, tip: int = 0,
@@ -2143,27 +2147,36 @@ def get_payment_info(self, call: GenericCall, keypair: Keypair):
21432147
signature=signature
21442148
)
21452149

2146-
payment_info = self.rpc_request('payment_queryInfo', [str(extrinsic.data)])
2150+
if self.supports_rpc_method('state_call'):
2151+
extrinsic_len = self.runtime_config.create_scale_object('u32')
2152+
extrinsic_len.encode(len(extrinsic.data))
21472153

2148-
# convert partialFee to int
2149-
if 'result' in payment_info:
2150-
payment_info['result']['partialFee'] = int(payment_info['result']['partialFee'])
2154+
result = self.runtime_call("TransactionPaymentApi", "query_info", [extrinsic, extrinsic_len])
21512155

2152-
if type(payment_info['result']['weight']) is int:
2153-
# Transform format to WeightV2 if applicable as per https://github.com/paritytech/substrate/pull/12633
2154-
try:
2155-
weight_obj = self.runtime_config.create_scale_object("sp_weights::weight_v2::Weight")
2156-
if weight_obj is not None:
2157-
payment_info['result']['weight'] = {
2158-
'ref_time': payment_info['result']['weight'],
2159-
'proof_size': 0
2160-
}
2161-
except NotImplementedError:
2162-
pass
2163-
2164-
return payment_info['result']
2156+
return result.value
21652157
else:
2166-
raise SubstrateRequestException(payment_info['error']['message'])
2158+
# Backwards compatibility; deprecated RPC method
2159+
payment_info = self.rpc_request('payment_queryInfo', [str(extrinsic.data)])
2160+
2161+
# convert partialFee to int
2162+
if 'result' in payment_info:
2163+
payment_info['result']['partialFee'] = int(payment_info['result']['partialFee'])
2164+
2165+
if type(payment_info['result']['weight']) is int:
2166+
# Transform format to WeightV2 if applicable as per https://github.com/paritytech/substrate/pull/12633
2167+
try:
2168+
weight_obj = self.runtime_config.create_scale_object("sp_weights::weight_v2::Weight")
2169+
if weight_obj is not None:
2170+
payment_info['result']['weight'] = {
2171+
'ref_time': payment_info['result']['weight'],
2172+
'proof_size': 0
2173+
}
2174+
except NotImplementedError:
2175+
pass
2176+
2177+
return payment_info['result']
2178+
else:
2179+
raise SubstrateRequestException(payment_info['error']['message'])
21672180

21682181
def get_type_registry(self, block_hash: str = None, max_recursion: int = 4) -> dict:
21692182
"""

substrateinterface/contracts.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -777,10 +777,6 @@ def read(self, keypair: Keypair, method: str, args: dict = None,
777777

778778
input_data = self.metadata.generate_message_data(name=method, args=args)
779779

780-
# Check Weight format
781-
# if self.substrate.config['is_weight_v2'] :
782-
# gas_limit = {'ref_time': gas_limit, 'proof_size': 100}
783-
784780
if self.substrate.supports_rpc_method('state_call'):
785781
call_result = self.substrate.runtime_call("ContractsApi", "call", {
786782
'dest': self.contract_address,

test/test_create_extrinsics.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def test_payment_info(self):
175175
call_function='transfer',
176176
call_params={
177177
'dest': 'EaG2CRhJWPb7qmdcJvy3LiWdh26Jreu9Dx6R1rXxPmYXoDk',
178-
'value': 2 * 10 ** 3
178+
'value': 2000
179179
}
180180
)
181181
payment_info = self.kusama_substrate.get_payment_info(call=call, keypair=keypair)

0 commit comments

Comments
 (0)