【问题标题】:How do you test cross-contract calling with Substrate using `ink!`?你如何使用“ink!”测试与 Substrate 的跨合约调用?
【发布时间】:2021-08-08 05:43:01
【问题描述】:

鉴于使用ink! 编写的 2 合约 Substrate 区块链,我如何运行一个实例化两个合约的单元测试?

“根”MyContract

use ink_storage::Lazy;
use other_contract::OtherContract;

//--snip--
#[ink(storage)]
struct MyContract {
    /// The other contract.
    other_contract: Lazy<OtherContract>,
}

impl MyContract {
    /// Instantiate `MyContract with the given
    /// sub-contract codes and some initial value.
    #[ink(constructor)]
    pub fn new(
        other_contract_code_hash: Hash,
    ) -> Self {
        let other_contract = OtherContract::new(1337)
            .endowment(total_balance / 4)
            .code_hash(other_contract_code_hash)
            .instantiate()
            .expect("failed at instantiating the `OtherContract` contract");
        Self {
            other_contract
        }
    }

    /// Calls the other contract.
    #[ink(message)]
    pub fn call_other_contract(&self) -> i32 {
        self.other_contract.get_value()
    }
}
//--snip--

OtherContract

#[ink::contract]
pub mod other_contract {
    /// Storage for the other contract.
    #[ink(storage)]
    pub struct OtherContract {
        value: i32,
    }

    impl OtherContract {
        /// Initializes the contract.
        #[ink(constructor)]
        pub fn new(value: i32) -> Self {
            Self { value }
        }

        /// Returns the current state.
        #[ink(message)]
        pub fn get_value(&self) -> i32 {
            self.value
        }
    }
}

delegator example 也代表多个合约的游乐场。

【问题讨论】:

    标签: substrate rust-ink


    【解决方案1】:

    您还不能使用常规测试框架对此进行单元测试。 https://redspot.patract.io/en/ 是一个允许您编写集成测试的框架。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-09-30
      • 1970-01-01
      • 2013-04-19
      • 2020-03-15
      • 1970-01-01
      • 2016-01-19
      • 2020-11-09
      • 2019-02-01
      相关资源
      最近更新 更多