0

I am trying to write offchain worker and read json file. But I faced this error.

ganesholi@Ganeshs-MacBook-Air Node_Hackathon_Apr2024 % cargo build --release
warning: virtual workspace defaulting to `resolver = "1"` despite one or more workspace members being on edition 2021 which implies `resolver = "2"`
note: to keep the current resolver, specify `workspace.resolver = "1"` in the workspace root's manifest
note: to use the edition 2021 resolver, specify `workspace.resolver = "2"` in the workspace root's manifest
note: for more details see https://doc.rust-lang.org/cargo/reference/resolver.html#resolver-versions
    Blocking waiting for file lock on package cache
    Blocking waiting for file lock on package cache
    Blocking waiting for file lock on package cache
    Blocking waiting for file lock on package cache
   Compiling node-template-runtime v4.0.0-dev (/Users/ganesholi/xcav/Node_Hackathon_Apr2024/runtime)
   Compiling pallet-template v4.0.0-dev (/Users/ganesholi/xcav/Node_Hackathon_Apr2024/pallets/template)
error[E0277]: the trait bound `T: Deserialize<'_>` is not satisfied
    --> pallets/template/src/lib.rs:122:5
     |
122  |                 serde_json::from_str(&resp_str).map_err(|_| <Error<T>>::DeserializeToObjError)?;
     |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Deserialize<'_>` is not implemented for `T`, which is required by `HackerNewsInfo<T>: Deserialize<'_>`
     |
note: required for `HackerNewsInfo<T>` to implement `Deserialize<'_>`
    --> pallets/template/src/lib.rs:52:55
     |
52   |     #[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
     |                                                          ^^^^^^^^^^^^^^^^^^ unsatisfied trait bound introduced in this `derive` macro
...
55   |     struct HackerNewsInfo<T: Config> {
     |            ^^^^^^^^^^^^^^^^^^^^^^^^^
note: required by a bound in `serde_json::from_str`
    --> /Users/ganesholi/.cargo/registry/src/index.crates.io-6f17d22bba15001f/serde_json-1.0.116/src/de.rs:2676:8
     |
2674 | pub fn from_str<'a, T>(s: &'a str) -> Result<T>
     |        -------- required by a bound in this function
2675 | where
2676 |     T: de::Deserialize<'a>,
     |        ^^^^^^^^^^^^^^^^^^^ required by this bound in `from_str`
     = note: this error originates in the derive macro `serde::Deserialize` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider further restricting this bound
     |
107  |     impl<T: Config + frame_support::Deserialize<'_>> Pallet<T> {
     |                    ++++++++++++++++++++++++++++++++

For more information about this error, try `rustc --explain E0277`.
error: could not compile `pallet-template` (lib) due to 1 previous error
warning: build failed, waiting for other jobs to finish...
error: failed to run custom build command for `node-template-runtime v4.0.0-dev (/Users/ganesholi/xcav/Node_Hackathon_Apr2024/runtime)`

Caused by:
  process didn't exit successfully: `/Users/ganesholi/xcav/Node_Hackathon_Apr2024/target/release/build/node-template-runtime-fb6c4231ab97d178/build-script-build` (exit status: 101)
  --- stdout
  Information that should be included in a bug report.
  Executing build command: env -u CARGO_ENCODED_RUSTFLAGS CARGO_TARGET_DIR="/Users/ganesholi/xcav/Node_Hackathon_Apr2024/target/release/wbuild/node-template-runtime/target" RUSTFLAGS="-C target-cpu=mvp -C target-feature=-sign-ext -C link-arg=--export-table -Clink-arg=--export=__heap_base -C link-arg=--import-memory  " SKIP_WASM_BUILD="" "/Users/ganesholi/.rustup/toolchains/nightly-aarch64-apple-darwin/bin/cargo" "rustc" "--target=wasm32-unknown-unknown" "--manifest-path=/Users/ganesholi/xcav/Node_Hackathon_Apr2024/target/release/wbuild/node-template-runtime/Cargo.toml" "--color=always" "--profile" "release" "-Z" "build-std"
  Using rustc version: rustc 1.80.0-nightly (f705de596 2024-04-30)


  --- stderr
      Blocking waiting for file lock on package cache
      Blocking waiting for file lock on package cache
       Locking 1 package to latest compatible version
        Adding node-template-runtime-wasm v1.0.0 (/Users/ganesholi/xcav/Node_Hackathon_Apr2024/target/release/wbuild/node-template-runtime)
      Blocking waiting for file lock on package cache
      Blocking waiting for file lock on package cache
     Compiling pallet-template v4.0.0-dev (/Users/ganesholi/xcav/Node_Hackathon_Apr2024/pallets/template)
  error[E0277]: the trait bound `HackerNewsInfo<T>: Deserialize<'_>` is not satisfied
      --> /Users/ganesholi/xcav/Node_Hackathon_Apr2024/pallets/template/src/lib.rs:122:5
       |
  122  |                 serde_json::from_str(&resp_str).map_err(|_| <Error<T>>::DeserializeToObjError)?;
       |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Deserialize<'_>` is not implemented for `HackerNewsInfo<T>`
       |
       = help: the following other types implement trait `Deserialize<'de>`:
                 <&'a [u8] as Deserialize<'de>>
                 <&'a str as Deserialize<'de>>
                 <() as Deserialize<'de>>
                 <(T0, T1) as Deserialize<'de>>
                 <(T0, T1, T2) as Deserialize<'de>>
                 <(T0, T1, T2, T3) as Deserialize<'de>>
                 <(T0, T1, T2, T3, T4) as Deserialize<'de>>
                 <(T0, T1, T2, T3, T4, T5) as Deserialize<'de>>
               and 204 others
  note: required by a bound in `serde_json::from_str`
      --> /Users/ganesholi/.cargo/registry/src/index.crates.io-6f17d22bba15001f/serde_json-1.0.116/src/de.rs:2676:8
       |
  2674 | pub fn from_str<'a, T>(s: &'a str) -> Result<T>
       |        -------- required by a bound in this function
  2675 | where
  2676 |     T: de::Deserialize<'a>,
       |        ^^^^^^^^^^^^^^^^^^^ required by this bound in `from_str`

  For more information about this error, try `rustc --explain E0277`.
  error: could not compile `pallet-template` (lib) due to 1 previous error
  thread 'main' panicked at /Users/ganesholi/.cargo/git/checkouts/polkadot-sdk-cff69157b985ed76/481165d/substrate/utils/wasm-builder/src/wasm_project.rs:822:37:
  Copying the bloaty file to the project dir.: Os { code: 2, kind: NotFound, message: "No such file or directory" }
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
ganesholi@Ganeshs-MacBook-Air Node_Hackathon_Apr2024 % 

Any hint please, why I am getting this error?

Github Link

1 Answer 1

1

You should find another solution. For example, use Codec instead of JSON.

https://github.com/XcavateBlockchain/Node_Hackathon_Apr2024/commit/ba0c46f36e83e5a143210014306fd2d721733375#diff-f191d0eb0afa874a64fb9be550a275f957e220b9076c87d4085c2797ca4e310cR124

This code will be compiled to WASM. But serde_json::from_str requries STD ENV.

1
  • hi @AurevoirXavier, I didn't find any way to convert JSON to trait using codec, could you please provide an example how to implement codec instead of JSON?
    – go11li
    Commented May 4 at 2:28

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.