Make NumberOrHex a common primitive. (#6321)

* Make NumberOrHex a common primitive.

* Update primitives/rpc/src/number.rs

Co-authored-by: Nikolay Volf <nikvolf@gmail.com>

Co-authored-by: Nikolay Volf <nikvolf@gmail.com>
This commit is contained in:
Sergei Shulepov
2020-06-10 17:08:15 +02:00
committed by GitHub
parent 62412ab03c
commit 3ec4844616
5 changed files with 103 additions and 60 deletions
+23 -5
View File
@@ -32,6 +32,7 @@ use sp_runtime::{
generic::BlockId,
traits::{Block as BlockT, Header as HeaderT},
};
use std::convert::TryInto;
pub use self::gen_client::Client as ContractsClient;
pub use pallet_contracts_rpc_runtime_api::{
@@ -80,7 +81,7 @@ pub struct CallRequest<AccountId, Balance> {
origin: AccountId,
dest: AccountId,
value: Balance,
gas_limit: number::NumberOrHex<u64>,
gas_limit: number::NumberOrHex,
input_data: Bytes,
}
@@ -203,9 +204,11 @@ where
gas_limit,
input_data,
} = call_request;
let gas_limit = gas_limit.to_number().map_err(|e| Error {
// Make sure that gas_limit fits into 64 bits.
let gas_limit: u64 = gas_limit.try_into().map_err(|_| Error {
code: ErrorCode::InvalidParams,
message: e,
message: format!("{:?} doesn't fit in 64 bit unsigned value", gas_limit),
data: None,
})?;
@@ -282,15 +285,30 @@ fn runtime_error_into_rpc_err(err: impl std::fmt::Debug) -> Error {
#[cfg(test)]
mod tests {
use super::*;
use sp_core::U256;
#[test]
fn should_serialize_deserialize_properly() {
fn call_request_should_serialize_deserialize_properly() {
type Req = CallRequest<String, u128>;
let req: Req = serde_json::from_str(r#"
{
"origin": "5CiPPseXPECbkjWCa6MnjNokrgYjMqmKndv2rSnekmSK2DjL",
"dest": "5DRakbLVnjVrW6niwLfHGW24EeCEvDAFGEXrtaYS5M4ynoom",
"value": 0,
"gasLimit": 1000000000000,
"inputData": "0x8c97db39"
}
"#).unwrap();
assert_eq!(req.gas_limit.into_u256(), U256::from(0xe8d4a51000u64));
}
#[test]
fn result_should_serialize_deserialize_properly() {
fn test(expected: &str) {
let res: RpcContractExecResult = serde_json::from_str(expected).unwrap();
let actual = serde_json::to_string(&res).unwrap();
assert_eq!(actual, expected);
}
test(r#"{"success":{"status":5,"data":"0x1234"}}"#);
test(r#"{"error":null}"#);
}