shawntabrizi / substrate-collectables-workshop

A guided tutorial for building an NFT marketplace with the Polkadot SDK
https://www.shawntabrizi.com/substrate-collectables-workshop/
MIT License
237 stars 101 forks source link

Use of undeclared type or module: runtime_primitives AND parity_codec #127

Closed Jovonni closed 5 years ago

Jovonni commented 5 years ago

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

BTW this is using substrate 2.0 Do you know of the correct module/library import syntax?... this is strange

shawntabrizi commented 5 years ago

I believe runtime_primitives is imported as primitives: https://github.com/paritytech/substrate/blob/master/node-template/runtime/Cargo.toml#L16

And parity_codec is imported as codec: https://github.com/paritytech/substrate/blob/master/node-template/runtime/Cargo.toml#L11

The secondary error may go away once you correctly import the things, so I would check first if you still have the other errors after this fix.

shawntabrizi commented 5 years ago

In terms of updating the code for 2.0. We are planning on doing a complete re-write of the whole tutorial for 2.0, since we can do it better the second time around :)

That being said, there are probably other samples across the substrate-developer-hub team that you can update to use 2.0

shawntabrizi commented 5 years ago

The As stuff has also been removed, see: https://stackoverflow.com/questions/56081117/how-do-you-convert-between-substrate-specific-types-and-rust-primitive-types