I want to instantiate another contract inside a contract and get the AccountId of that contract.
Can anyone tell me how to do that?
- Instantiate SampleContract2 in the add_contract function of SampleContract1.
- I want to get the AccountId of the instantiated SampleContract2, manage it in a list, and use the AccountId later to be able to access SmapleContract2.
- In the future, I would like to increase the types of contracts, so I would like to use AccountId, which can be used for general purposes, instead of the SmapleContract2 type for the list.
Why does "sample_contract_2.env().account_id()" return the address of SampleContract1?
-- snip --
#[ink(storage)]
pub struct SampleContract1 {
next_id:u128,
account_id_list:Mapping<u128, AccountId>
}
-- snip --
impl SampleContract1 {
#[ink(constructor)]
pub fn new() -> Self {
Self {
account_id_list: Mapping::default(),
next_id:0,
}
}
#[ink(message)]
pub fn add_contract(&mut self ) -> Result<()> {
let sample_contract_2 = SampleContract2::new();
let contract_address = sample_contract_2.env().account_id(); <- ###### Address of SampleContract1 is obtained.
self.token_list.insert(&self.next_id, &contract_address );
self.next_id = self.next_id + 1;
Ok(())
}
Thank you.