Skip to content

Commit 5c3c65b

Browse files
jacograthei
andauthored
Hex Balance deserialize for contracts_call RPC (#7807)
* Hex Balance deserialize for contracts_call RPC * Avoid temporary conversion into u128 Co-authored-by: Alexander Theißen <alex.theissen@me.com>
1 parent 1757cc2 commit 5c3c65b

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

rpc/src/lib.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use sp_runtime::{
3333
traits::{Block as BlockT, Header as HeaderT},
3434
DispatchError,
3535
};
36-
use std::convert::TryInto;
36+
use std::convert::{TryFrom, TryInto};
3737
use pallet_contracts_primitives::ContractExecResult;
3838

3939
pub use pallet_contracts_rpc_runtime_api::ContractsApi as ContractsRuntimeApi;
@@ -76,10 +76,10 @@ impl From<ContractAccessError> for Error {
7676
#[derive(Serialize, Deserialize)]
7777
#[serde(rename_all = "camelCase")]
7878
#[serde(deny_unknown_fields)]
79-
pub struct CallRequest<AccountId, Balance> {
79+
pub struct CallRequest<AccountId> {
8080
origin: AccountId,
8181
dest: AccountId,
82-
value: Balance,
82+
value: number::NumberOrHex,
8383
gas_limit: number::NumberOrHex,
8484
input_data: Bytes,
8585
}
@@ -141,7 +141,7 @@ pub trait ContractsApi<BlockHash, BlockNumber, AccountId, Balance> {
141141
#[rpc(name = "contracts_call")]
142142
fn call(
143143
&self,
144-
call_request: CallRequest<AccountId, Balance>,
144+
call_request: CallRequest<AccountId>,
145145
at: Option<BlockHash>,
146146
) -> Result<RpcContractExecResult>;
147147

@@ -201,11 +201,11 @@ where
201201
<<Block as BlockT>::Header as HeaderT>::Number,
202202
>,
203203
AccountId: Codec,
204-
Balance: Codec,
204+
Balance: Codec + TryFrom<number::NumberOrHex>,
205205
{
206206
fn call(
207207
&self,
208-
call_request: CallRequest<AccountId, Balance>,
208+
call_request: CallRequest<AccountId>,
209209
at: Option<<Block as BlockT>::Hash>,
210210
) -> Result<RpcContractExecResult> {
211211
let api = self.client.runtime_api();
@@ -221,6 +221,13 @@ where
221221
input_data,
222222
} = call_request;
223223

224+
// Make sure that value fits into the balance type.
225+
let value: Balance = value.try_into().map_err(|_| Error {
226+
code: ErrorCode::InvalidParams,
227+
message: format!("{:?} doesn't fit into the balance type", value),
228+
data: None,
229+
})?;
230+
224231
// Make sure that gas_limit fits into 64 bits.
225232
let gas_limit: u64 = gas_limit.try_into().map_err(|_| Error {
226233
code: ErrorCode::InvalidParams,
@@ -305,17 +312,18 @@ mod tests {
305312

306313
#[test]
307314
fn call_request_should_serialize_deserialize_properly() {
308-
type Req = CallRequest<String, u128>;
315+
type Req = CallRequest<String>;
309316
let req: Req = serde_json::from_str(r#"
310317
{
311318
"origin": "5CiPPseXPECbkjWCa6MnjNokrgYjMqmKndv2rSnekmSK2DjL",
312319
"dest": "5DRakbLVnjVrW6niwLfHGW24EeCEvDAFGEXrtaYS5M4ynoom",
313-
"value": 0,
320+
"value": "0x112210f4B16c1cb1",
314321
"gasLimit": 1000000000000,
315322
"inputData": "0x8c97db39"
316323
}
317324
"#).unwrap();
318325
assert_eq!(req.gas_limit.into_u256(), U256::from(0xe8d4a51000u64));
326+
assert_eq!(req.value.into_u256(), U256::from(1234567890987654321u128));
319327
}
320328

321329
#[test]

0 commit comments

Comments
 (0)