Substrate: Use of undeclared type or module: runtime_primitives AND parity_codec

Created on 14 Aug 2019  路  15Comments  路  Source: paritytech/substrate

I am using v2.0 (current repo), and I am building through the collectable workshop code on:
https://substrate.dev/substrate-collectables-workshop/#/1/storing-a-structure

The imports used

use support::{decl_storage, decl_module, StorageMap, dispatch::Result};
use system::ensure_signed;
use runtime_primitives::traits::{As, Hash};
use parity_codec::{Encode, Decode};

the error:

use of undeclared type or module `runtime_primitives`
error[E0433]: failed to resolve: use of undeclared type or module `runtime_primitives`

and

use of undeclared type or module `parity_codec`
error[E0432]: unresolved import `parity_codec`

I am assuming the name of the libraries has changed, or maybe the location of submodules has changed. I can get the workshop working with "substrate-package", but it isn't too far from working with the updated repo.

Also, I make a struct:

#[derive(Encode, Decode, Default, Clone, PartialEq)]
#[cfg_attr(feature = "std", derive(Debug))]

pub struct MyStruct<Hash, Balance> {
    id: Hash,
    dna: Hash,
    price: Balance,
    gen: u64,
}

and, inside my decl_storage! macro, I use:

OwnedStruct get(struct_of_owner): map T::AccountId => MyStruct<T::Hash, T::Balance>;

the following error occurs

error[E0220]: associated type `Balance` not found for `T`

Inside my decl_module, I create a Struct:

let new_struct = OwnedStruct {
                id: <T as system::Trait>::Hashing::hash_of(&0),
                dna: <T as system::Trait>::Hashing::hash_of(&0),
                price: <T::Balance as As<u64>>::sa(0),
                gen: 0,
};

and I can also pass in a Hash, and Balance:

 fn create_struct(origin, value: u32, balance: T::Balance, hash: T::Hash) -> Result {

and declare the same Struct like:

let new_struct = OwnedStruct {
                id: hash,
                dna: hash,
                price: balance,
                gen: 0,
};

The above is not working, any suggestions regarding these errors?

I'd love to help update the workshop code for the newer versions

Most helpful comment

@Jovonni I'm fairly sure the answer is to update substrate within the workshop repo to resolve the errors you received. Your syntax isn't incorrect in any way otherwise.

I've minimized your comment above for its hostile tone. You can communicate what you want to without taking that tone.

Closing this issue as it isn't relevant to the current version of substrate - there are other channels where you can discuss building on substrate, like the #substrate-technical riot channel or the individual repos for the workshops.

All 15 comments

I am sure this is something simple, as I have seen very simple fixes for other workshop code. For example, changing:

<MyItem<T>>::insert(...)

to

MyItem::insert(...)

CC @shawntabrizi

I think this issue should be moved to the repo for the workshop?

Maybe the workshop is out of date, however, when I look through code from other files, for example they are using the imports such as:

use runtime_primitives::traits::{As, Hash};
use parity_codec::{Encode, Decode};

and thats the code from the up-to-date repo... that's what's confusing me

I think the workshop is not yet ported to substrate-2.0

What I am trying to figure out is what is the correct way to include those modules, and create the structs?

I assume substrate still handles structs, so I am looking for a working example of using structs in substrate 2.0.

What you said makes sense, but you see what I am going for. Thanks for your responses thus far.

I saw that is hasn't been ported yet, thank you.

So I close this pr.

If you're going to close it, can you at least attempt to answer the question posed. There may be other people who end up on this thread in the future.

The goal is to find a working example of defining/using structs in substrate 2.0. Can anyone help on this matter?

I don't understand your question. Defining a struct is standard rust syntax and that never changed?

struct MyStruct {}

  1. You don't have posted any full code example, how should anybody know what you are doing based on your 5 code snippets? We are no wizards.
  2. You have written "of defining/using structs".
  3. Why not just look into the Substrate code?
  4. This repo is not for reporting issues in the individual workshops.
  5. I'm happily helping anybody with clear and understandable questions/bug reports.

@Jovonni I'm fairly sure the answer is to update substrate within the workshop repo to resolve the errors you received. Your syntax isn't incorrect in any way otherwise.

I've minimized your comment above for its hostile tone. You can communicate what you want to without taking that tone.

Closing this issue as it isn't relevant to the current version of substrate - there are other channels where you can discuss building on substrate, like the #substrate-technical riot channel or the individual repos for the workshops.

@joshua-mir I am not using the workshop repo, I am doing all of this with substrate 2.0. All of this is done with the current code.

Thanks for the response. If the syntax is correct, perhaps there is a linkage issue, and thats why the two modules are throwing errors as if they cannot be found... I am copying exactly how the modules are being imported, and still seeing that error.

Very weird. Will keep poking...

@joshua-mir I guess that
parity_codec::{Encode, Decode}
is now
parity_scale_codec::{Encode, Decode}

@joshua-mir I guess that
parity_codec::{Encode, Decode}
is now
parity_scale_codec::{Encode, Decode}

Tks for the answner of 'parity_codec', And Do u know how to change the 'runtime_primitives' import in 2.0,
example : use runtime_primitives::traits::{As, Hash}; -> ?

I believe runtime_primitives has been renamed to sr_primitives

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gavofyork picture gavofyork  路  4Comments

qalqi picture qalqi  路  3Comments

Mischi picture Mischi  路  5Comments

thiolliere picture thiolliere  路  3Comments

jiangfuyao picture jiangfuyao  路  3Comments