0
  • I want to implement a base contract that multiple contracts should implement.
  • I decided to use "#[openbrush::trait_definition]" because "[#ink(message)]" cannot be added in the normal implementation of traits.
  • However, traits declared in another library crate cannot be referenced.
  • If you remove "#[openbrush::trait_definition]" as a trial, you can refer to it.
  • Could you tell me what is wrong?

definition of trait: commons/lib.rs

    #[openbrush::trait_definition]
    pub trait Destination {
        #[ink(message)]
        fn execute_function(&mut self) -> Result<(), ()>;
    }

use of trait: destination_base/lib.rs

#[openbrush::contract]
mod destination_base {
    use commons::Destination;    
    /// 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 DestinationBase {
        /// Stores a single `bool` value on the storage.
        value: bool,
    }

    impl Destination for DestinationBase {
        #[ink(message)]
        fn execute_function(&mut self) -> Result<(),()> {
            self.flip();
            Ok(())
        }
    }

error:

    Checking destination_base v0.1.0 (/Users/shin.takahashi/develop/work/delegate_test2/contracts/destination_base)
error[E0432]: unresolved import `commons::Destination`
 --> src/lib.rs:9:9
  |
9 |     use commons::Destination;    
  |         ^^^^^^^^^^^^^^^^^^^^ no `Destination` in the root

3
  • can you share github link to your code?
    – kriko.eth
    Commented Jan 7, 2023 at 14:59
  • I guess you haven't include commons = { path = "../../commons", default-features = false } inside dependencies of destination_base cargo.toml. also don't forgot to include "commons/std" inside std
    – go11li
    Commented Jan 7, 2023 at 15:54
  • Thank you. I tried but The situation remains the same.I made a similar sample and pushed it below, could you check it out? github.com/realtakahashi/trait_test Commented Jan 7, 2023 at 23:12

2 Answers 2

0

I have added contract/logics in contracts because your contract/commons is different. I think you have generated this folder using cargo new commons? instead you can use cargo contract new logics command to create new contract. I have fixed your code. Include logics = { path = "../logics", default-features = false, features = ["ink-as-dependency"] } inside dependencies of test/Cargo.toml and include "commons/std" inside std. Aslo don't forgot to add "rlib" to the crate-type options under "cdylib" in your logics/Cargo.toml. I have included github.com repo of the code.

6
  • Thank you for your proposal of the solution. Your sample can be compiled, but the "execute_function" is not a contract function. I want to call "execute_function" as a contract function like the "flip" function. Is it possible? Commented Jan 10, 2023 at 12:30
  • Do you mean execuate_function Methods flagged with #[ink(message)] in impl Base for test {}?
    – go11li
    Commented Jan 10, 2023 at 14:29
  • yes,right. When I added "#[ink(message)]" to execute function, I got errors. Commented Jan 10, 2023 at 22:15
  • 1
    @s.Takahashi Instead of #[openbrush::trait_definition] if you use [#[ink::trait_defination]] it solves. I don't know why exactly it doesn't work with openbrush. You can view changes here
    – go11li
    Commented Jan 14, 2023 at 11:29
  • 1
    oh wow it fix, thank you @s.Takahashi
    – go11li
    Commented Jan 16, 2023 at 2:08
0

I asked on Element's BrushFam. And "Dominik | Brushfam" told me. To fix this error you need to import everything. As below.

use commons::*;

trait_definition macro generates some code which is then used by the ink_contract so you need to expose that too in order to impl that trait for your contract

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.