2

I want to create two contracts in one ink file. The second contract use functions and mapping of the first contract. Here is an example

#[ink::contract]
mod contract_1 {
    pub struct Struct1 {}
    #[ink(storage)]
    pub struct MyContract {}
    impl MyContract {

        #[ink(constructor)]
        pub fn new() -> Self {}

        #[ink(constructor)]
        pub fn default() -> Self {}

        #[ink(message)]
        pub fn function_1(&self) -> Option<Struct1> {}
    }
}

#[ink::contract]
mod contract_2 {
    use super::contract_1::{MyContract, Struct1};
    use ink_prelude::string::String;
    use ink_storage::traits::SpreadAllocate;
    // use ink_storage::Mapping;

    #[ink(storage)]
    #[derive(SpreadAllocate)]
    pub struct Contract2 {
        pub contract_parent: MyContract,
    }
    impl Contract2 {
        #[ink(constructor)]
        pub fn new() -> Self {
            Self {
                contract_parent: MyContract::default(),
            }
        }
        #[ink(message)]
        pub fn function_2(&self, variable: String) -> Option<Struct1> {
            self.contract_parent.function_1(variable)
        }
    }
}

While compiling this ia ma getting an error "symbol call is already defined".

It appears in the #[ink(storage)] of the second contract.

How to fix this compilation error?

1
  • what's the difference between this and cross contract calling? Instead of this you can do cross contract calling.
    – go11li
    Commented Feb 23, 2023 at 12:42

1 Answer 1

5

You need to have two contracts in separate files, because the Smart Contract in ink! are not only the lib.rs file, it needs the Cargo.toml and the lib.rs, which both contain the necessary building blocks for using ink!.

So the when you run: cargo contract build on the folder with this two files you'll get the file target/your_contract.contract which is a JSON which bundles the contract's metadata and its Wasm blob and is the file you need use when deploying the contract.

To do what you want to do you need to create two different contracts and to deploy them on chain you will have to build and deploy them separately.

I'd suggest you to take a look at the docs to see how to do it and this example.

Not the answer you're looking for? Browse other questions tagged or ask your own question.