use-ink / ink

Polkadot's ink! to write smart contracts.
https://use.ink
Apache License 2.0
1.36k stars 430 forks source link

`scale_info::TypeInfo` is not implemented for struct #688

Closed 9876691 closed 3 years ago

9876691 commented 3 years ago

I'm having trouble with the following contract. It builds OK with cargo +nightly contract build but I can't run the tests with cargo +nightly test

So is the issue that I'm trying to store a struct in the storage?

I couldn't find an example anywhere of anyone trying to do this.

Thanks

#![cfg_attr(not(feature = "std"), no_std)]

use ink_lang as ink;

#[ink::contract]
mod negotiate {

    use ink_storage::{
        collections::{
            Vec as StorageVec,
        },
        traits::{
            PackedLayout,
            SpreadLayout,
        },
    };

    #[derive(scale::Encode, scale::Decode, 
        SpreadLayout, PackedLayout)]
    #[cfg_attr(
        feature = "std",
        derive(
            Debug,
            PartialEq,
            Eq,
            scale_info::TypeInfo,
            ink_storage::traits::StorageLayout
        )
    )]
    struct ConjunctionDataMessage {
        pub object1_norad_id: u32,
        pub object2_norad_id: u32,
        pub collision_probabilty: u32,
        pub time_of_closest_pass: u32
    }

    /// Defines the storage of your contract.
    /// Add new fields to the below struct in order
    /// to add new static storage fields to your contract.
    #[ink(storage)]
    pub struct Negotiate {
        cdms: StorageVec<ConjunctionDataMessage>,
        ca_providers: StorageVec<AccountId>
    }

    impl Negotiate {
        #[ink(constructor)]
        pub fn new() -> Self {
            Self { 
                cdms: Default::default(), 
                ca_providers: Default::default(), 
            }
        }

        #[ink(message)]
        pub fn add_ca_provider(&mut self, 
            provider_account: AccountId) {
            // Only the owner of this smart contract 
            // can call this method.
            assert_eq!(self.env().caller(), 
                self.env().account_id());

            // Add this account to our list of roviders
            self.ca_providers.push(provider_account);
        }

        #[ink(message)]
        pub fn submit_cdm(&mut self, 
            cdm: ConjunctionDataMessage) {
        }
    }
}

Compilation errors

Compiling negotiate v0.1.0 (/vscode)
error[E0277]: the trait bound `ConjunctionDataMessage: scale_info::TypeInfo` is not satisfied
   --> lib.rs:5:1
    |
5   | #[ink::contract]
    | ^^^^^^^^^^^^^^^^ the trait `scale_info::TypeInfo` is not implemented for `ConjunctionDataMessage`
    | 
   ::: /usr/local/cargo/git/checkouts/ink-c30a98df39dc6c13/402a694/crates/metadata/src/specs.rs:855:12
    |
855 |         T: TypeInfo + 'static,
    |            -------- required by this bound in `TypeSpec::with_name_segs`
    |
    = note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `ConjunctionDataMessage: scale_info::TypeInfo` is not satisfied
  --> lib.rs:37:5
   |
37 | /     /// Defines the storage of your contract.
38 | |     /// Add new fields to the below struct in order
39 | |     /// to add new static storage fields to your contract.
40 | |     #[ink(storage)]
...  |
43 | |         ca_providers: StorageVec<AccountId>
44 | |     }
   | |_____^ the trait `scale_info::TypeInfo` is not implemented for `ConjunctionDataMessage`
   |
   = note: required because of the requirements on the impl of `StorageLayout` for `ink_storage::Vec<ConjunctionDataMessage>`
   = note: required by `layout`
   = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 2 previous errors
ascjones commented 3 years ago

Can you link to the source code, it looks like it should work. It might be different versions of scale-info: check that the version of scale-info in the version of ink! you are using matches with the one in the contract's Cargo.toml

9876691 commented 3 years ago

Hi Code is here https://github.com/ianpurton/rust-smart-contract

It's basically what you get when you run cargo contract new flipper

ascjones commented 3 years ago

The fork of ink! you are using depends on scale-info 0.4, so try updating your version here:

https://github.com/ianpurton/rust-smart-contract/blob/master/Cargo.toml#L15

9876691 commented 3 years ago

Thanks that worked.

So the issue is that cargo contract new is using the wrong version of scale-info

ascjones commented 3 years ago

Indeed it is. Once ink! 3.0 is stabilized we will provide a new cargo-contract release which has the correct version of scale-info in the template. In the meantime you will have to manually correct it.