Skip to content

Commit

Permalink
Replace RPC calls with runtime calls (#279)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
arjanz authored Dec 12, 2022
1 parent 2c874f1 commit 28f95d7
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 26 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ eth_utils>=1.3.0,<3
pycryptodome>=3.11.0,<4
PyNaCl>=1.0.1,<2

scalecodec>=1.1.2,<1.2
scalecodec>=1.1.3,<1.2
py-sr25519-bindings>=0.2.0,<1
py-ed25519-zebra-bindings>=1.0,<2
py-bip39-bindings>=0.1.9,<1
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@
'eth_utils>=1.3.0,<3',
'pycryptodome>=3.11.0,<4',
'PyNaCl>=1.0.1,<2',
'scalecodec>=1.1.2,<1.2',
'scalecodec>=1.1.3,<1.2',
'py-sr25519-bindings>=0.2.0,<1',
'py-ed25519-zebra-bindings>=1.0,<2',
'py-bip39-bindings>=0.1.9,<1'
Expand Down
51 changes: 32 additions & 19 deletions substrateinterface/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1731,7 +1731,11 @@ def get_account_nonce(self, account_address) -> int:
-------
int
"""
response = self.rpc_request("system_accountNextIndex", [account_address])
if self.supports_rpc_method('state_call'):
nonce_obj = self.runtime_call("AccountNonceApi", "account_nonce", [account_address])
return nonce_obj.value
else:
response = self.rpc_request("system_accountNextIndex", [account_address])
return response.get('result', 0)

def generate_signature_payload(self, call: GenericCall, era=None, nonce: int = 0, tip: int = 0,
Expand Down Expand Up @@ -2143,27 +2147,36 @@ def get_payment_info(self, call: GenericCall, keypair: Keypair):
signature=signature
)

payment_info = self.rpc_request('payment_queryInfo', [str(extrinsic.data)])
if self.supports_rpc_method('state_call'):
extrinsic_len = self.runtime_config.create_scale_object('u32')
extrinsic_len.encode(len(extrinsic.data))

# convert partialFee to int
if 'result' in payment_info:
payment_info['result']['partialFee'] = int(payment_info['result']['partialFee'])
result = self.runtime_call("TransactionPaymentApi", "query_info", [extrinsic, extrinsic_len])

if type(payment_info['result']['weight']) is int:
# Transform format to WeightV2 if applicable as per https://github.com/paritytech/substrate/pull/12633
try:
weight_obj = self.runtime_config.create_scale_object("sp_weights::weight_v2::Weight")
if weight_obj is not None:
payment_info['result']['weight'] = {
'ref_time': payment_info['result']['weight'],
'proof_size': 0
}
except NotImplementedError:
pass

return payment_info['result']
return result.value
else:
raise SubstrateRequestException(payment_info['error']['message'])
# Backwards compatibility; deprecated RPC method
payment_info = self.rpc_request('payment_queryInfo', [str(extrinsic.data)])

# convert partialFee to int
if 'result' in payment_info:
payment_info['result']['partialFee'] = int(payment_info['result']['partialFee'])

if type(payment_info['result']['weight']) is int:
# Transform format to WeightV2 if applicable as per https://github.com/paritytech/substrate/pull/12633
try:
weight_obj = self.runtime_config.create_scale_object("sp_weights::weight_v2::Weight")
if weight_obj is not None:
payment_info['result']['weight'] = {
'ref_time': payment_info['result']['weight'],
'proof_size': 0
}
except NotImplementedError:
pass

return payment_info['result']
else:
raise SubstrateRequestException(payment_info['error']['message'])

def get_type_registry(self, block_hash: str = None, max_recursion: int = 4) -> dict:
"""
Expand Down
4 changes: 0 additions & 4 deletions substrateinterface/contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,10 +777,6 @@ def read(self, keypair: Keypair, method: str, args: dict = None,

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

# Check Weight format
# if self.substrate.config['is_weight_v2'] :
# gas_limit = {'ref_time': gas_limit, 'proof_size': 100}

if self.substrate.supports_rpc_method('state_call'):
call_result = self.substrate.runtime_call("ContractsApi", "call", {
'dest': self.contract_address,
Expand Down
2 changes: 1 addition & 1 deletion test/test_create_extrinsics.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def test_payment_info(self):
call_function='transfer',
call_params={
'dest': 'EaG2CRhJWPb7qmdcJvy3LiWdh26Jreu9Dx6R1rXxPmYXoDk',
'value': 2 * 10 ** 3
'value': 2000
}
)
payment_info = self.kusama_substrate.get_payment_info(call=call, keypair=keypair)
Expand Down

0 comments on commit 28f95d7

Please sign in to comment.