The title of the question is a bit open ended, but I am trying to make cross contract calling work right now and realised, that there is a lot of open question on how it exactly works and how the parts fit together.
I am using the directory structure recommended by openbrush (more or less). At the moment, my project looks like this:
- contracts
- contract_1
- Cargo.toml
- lib.rs
- contract_2
- Cargo.toml
- lib.rs
- contract_1
- traits
- contract_1.rs
- contract_2.rs
- mod.rs
- Cargo.toml
- lib.rs
I want to call contract_1 in contract_2.
Defining the traits I have a wrapper for contract_1 like this:
#[openbrush::wrapper]
pub type ContractOneRef = dyn ContractOne;
Then I try to call on contract_1 in contract_2 like this:
#![cfg_attr(not(feature = "std"), no_std)]
use ink_lang as ink;
#[ink::contract]
mod contract_2 {
use c1_contract::contract_1::*;
use project::traits::contract_2::*;
#[ink(storage)]
pub struct Manager {
contract_1: ContractOneRef
}
impl ContractTwo {
#[ink(constructor)]
pub fn new(
code_hash: Hash
) -> Self {
// Here I am not even sure what exactly to do.
}
...
}
The idea is, that contract_1 is already deployed on the chain and I want to call on it with contract_2. I have found conflicting information online on how to reference the contract which I want to call on.
Here there are two examples on how to do it. The answer is, that the first example is deprecated and the second example does not really get any explanation. Just plain code. The wrapper feature of openbrush is also mentioned, but the only thing it seems to explain is how to do the trait definition.
Looking at the code example I am thinking, that the way to define/link contract_1 could look like this:
let total_balance = Self::env().balance();
let salt = version.to_le_bytes();
let contract_1 = ContractOneRef::new(input_values)
.endowment(total_balance / 4)
.code_hash(accumulator_code_hash)
.salt_bytes(salt)
.instantiate()
.unwrap_or_else(|error| {
panic!(
"failed at instantiating the Accumulator contract: {:?}",
error
)
}
);
But this just throws a lot of errors like:
33 | let salt = version.to_le_bytes(); | ^^^^^^^ not found in this scope
or
| 37 | .instantiate() | ^^^^^^^^^^^ method not found in `CreateBuilder<DefaultEnvironment, Set<ink_env::Hash>, Unset<u64>, Set<u128>, Set<ExecutionInput<ArgumentList<ink_env::call::utils::Argument<String>, ArgumentList<ink_env::call::utils::Argument<String>, ArgumentList<ink_env::call::utils::Argument<ink_env::AccountId>, ArgumentList<ink_env::call::utils::Argument<Vec<u8>>, ArgumentList<ArgumentListEnd, ArgumentListEnd>>>>>>>, Unset<Salt>, c1_contract::contract_one::ContractOneRef>` |
Is this the right way to call on a contract which is using the wrapper of openbrush? And if yes, what am I doing wrong?
Also, what are the different values they are trying passing over there?
- endowment
- code_hash
- isntantiate
- salt
And if this is calling on a contract which should already be deployed on chain, why would I have to put call the constructor and pass in the arguments there?