0

Following this example for ink! smart contracts

https://github.com/paritytech/ink-examples/blob/main/trait-dyn-cross-contract-calls/lib.rs

we have

incrementer: ink::contract_ref!(Increment),

and by entering the relevant AccountId that the contract is deployed at here

    #[ink(constructor)]
    pub fn new(incrementer: AccountId) -> Self {
        Self {
            incrementer: incrementer.into(),
        }
    }

we get a reference to the contract

Question: How can we retrieve the AccountId having the incrementer contract reference

Note: I understand that I can create another storage variable of type AccountId and store this in the constructor like

    #[ink(constructor)]
    pub fn new(incrementer: AccountId) -> Self {
        Self {
            incrementer: incrementer.into(),
            incrementer_address: incrementer,
        }
    }

but I am wondering if I can avoid the extra storage of such variable incrementer_address and instead derive the AccountId of the incrementer: ink::contract_ref!(Increment) inside a function

1 Answer 1

1

You can get the account id of the contract ref by calling to_account_id() on the contract ref: https://docs.rs/ink/latest/ink/trait.ToAccountId.html

let contract_ref =
    MyContractRef::new(<params>)
        .endowment(0)
        .code_hash(...)
        .salt_bytes(...)
        .instantiate();
let account_id = contract_ref.to_account_id();

Later on you can recover the ContractRef with

let contract_ref: MyContractRef =
    ink::env::call::FromAccountId::from_account_id(my_contract_acct_id.clone());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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