如何从 rust-ink 中的另一个合约调用现有合约?

How do I call an existing contract from another contract in rust-ink?

我有两个非常基本的 rust-ink dapp 合约:

Dapp 1

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

use ink_lang as ink;
use dapp2;

#[ink::contract]
pub mod dapp1 {
    use dapp2::dapp2::Dapp2;

    #[ink(storage)]
    pub struct Dapp1 {
        dapp2_instance: Dapp2
    }

    impl Dapp1 {
        /// Get existing `Dapp2` contract at `address`
        #[ink(constructor)]
        pub fn new(address: AccountId) -> Self {
            let dapp2_instance: Dapp2 = ink_env::call::FromAccountId::from_account_id(address);
            Self {
                dapp2_instance
            }
        }

        /// Calls the Dapp2 contract.
        #[ink(message)]
        pub fn dapp2_do_something(&mut self) -> u8 {
            self.dapp2_instance.do_something()
        }
    }
}

Dapp 1 在 toml:

中将 dapp2 指定为依赖项

dapp2 = { version = "3.0.0-rc7", path ="../dapp2", default-features = false, features = ["ink-as-dependency"] }

Dapp 2

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

use ink_lang as ink;

#[ink::contract]
pub mod dapp2 {
    #[ink(storage)]
    pub struct Dapp2 {
        pub what: u8,
    }

    impl Dapp2 {
        #[ink(constructor)]
        pub fn new() -> Self {
            let what = 2.into();
            Self {
                what
            }
        }

        #[ink(message)]
        pub fn do_something(&mut self) -> u8 {
            ink_env::debug_println!("{}", self.what);
            self.what
        }
    }
}

当我 运行 这个构建时,dapp2 编译但是 dapp1 失败

error[E0432]: unresolved import `dapp2`
 --> /home/usr/dev/dapp-example/dapp1/lib.rs:4:5
  |
4 | use dapp2;
  |     ^^^^^ no external crate `dapp2`

甚至我的 IDE 在点击时也能找到 dapp2 那么这个导入样式有什么问题吗?

我见过其他示例 (1, 2, 3),其中合同只是简单地导入到模块中,但这似乎对我不起作用。如果我这样做,我会得到:

7 |     use dapp2::Dapp2Ref;
  |         ^^^^^ use of undeclared crate or module `dapp2`

墨迹设置为master分支。 Rust nightly 是最新的。版本是 2021。示例代码是 on github

问题是在 toml 文件中没有将 crate-type 设置为 rlib

crate-type = [
    # Used for normal contract Wasm blobs.
    "cdylib",
    # This was not set
    "rlib"
]

https://github.com/prosopo-io/dapp-example/blob/5b31491bfa02ccd9603c1ac91f0907c2c9b84856/dapp1/Cargo.toml#L25