3

u128 is not supported on rpc. So I am trying to use NumberOrHex

How to setup and RPC call that accepts a u128 input?

Here is my code for runtime api:

#![cfg_attr(not(feature = "std"), no_std)]
use codec::Codec;

sp_api::decl_runtime_apis! {
    pub trait ShivarthuApi<U128> where
    U128: Codec {
        fn hello_world() -> U128;
    }
}

Cargo.toml

[dependencies]
sp-api = { default-features = false, version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.18" }
codec = { package = "parity-scale-codec", version = "2.3.1" }

It gives error:

the trait bound `U128: sp_api_hidden_includes_DECL_RUNTIME_APIS::sp_api::parity_scale_codec::WrapperTypeDecode` is not satisfied

the trait `sp_api_hidden_includes_DECL_RUNTIME_APIS::sp_api::parity_scale_codec::WrapperTypeDecode` is not implemented for `U128`

note: required because of the requirements on the impl of `sp_api_hidden_includes_DECL_RUNTIME_APIS::sp_api::Decode` for `U128`

1 Answer 1

2

We had this same issue with our Polymesh chain. To fix it you need to enable the arbitrary_precision feature on serde_json and jsonrpc-core crates.

Check your Cargo.toml files (there might be more than one) for those two crates and make sure the feature is enabled like this:

jsonrpc-core = { version = "15.0", features = ["arbitrary_precision"] }
serde_json = { version = "1.0", features = ["arbitrary_precision"] }

You can see the commit where we fixed it here: https://github.com/PolymathNetwork/Polymesh/commit/5ee2801fcf9f34b7efa78e909782bf77154c0b0f

1
  • Thanks, it worked, I thought only serde_json has "arbitrary_precision", but I had only jsonrpc-core as dependancy. Commented Apr 29, 2022 at 11:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.