Our front end use state call to query custom RuntimeApi of our solo chain. We try to use state call to replace custom rpcs.


Balance is our chain is u128.

RuntimeApi Definition

impl parami_ad_runtime_api::AdRuntimeApi<Block, <BlakeTwo256 as sp_runtime::traits::Hash>::Output, NftId, DecentralizedId, Balance> for Runtime {
   fn cal_reward(ad_id: <BlakeTwo256 as sp_runtime::traits::Hash>::Output, nft_id: NftId, did: DecentralizedId, referrer: Option<DecentralizedId>) -> Result<Balance, DispatchError> {
        let visitor_in_runtime = Ad::cal_reward(ad_id, nft_id, did, referrer).unwrap();

Frontend implementation

let resultOfFrontEnd = await api.call.adRuntimeApi.calReward("0x9cefa96e26ba9767a8c21fcc73d3b0912850e712b1a38945a1d2aaf8ccff5c58", 3, "0x5bc69eda41a4e6cf8b0a257b1c16b0d451eb95ee", null);

Problem When i run the frontend, and debug run a local substrate node, I got the following confusing result;

  1. resultOfFronEnd in the front end code is "256 Unit"
  2. result_in_runtime in the runtime api implementation code is "1 Unit"

I also try other return value, the regular pattern is resultOfFrontEnd = 256 * result_in_runtime.

I really want to know why, or how can I reason about it.

  • Sounds like scale decoding fun. If there was a byte (eight bits) then that would shift things 8 times leading to * 256. Could one be using compressed encoding and the other not?
    – Squirrel
    Commented Dec 23, 2022 at 12:38
  • @Squirrel except compressed encoding, which one should i select to escape this issue ? Commented Dec 26, 2022 at 9:53
  • @Squirrel shall i create a pull request to fix this issue on compressed encoding? Commented Dec 26, 2022 at 9:54
  • I think it might well be a feature of the two different routes rather than a bug.
    – Squirrel
    Commented Jan 7, 2023 at 10:31


Your Answer

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