6

From what I understand from the docs, every contract gets its own AccountId. But in the contract-transfer tests, I'm getting the same AccountId from both accounts.alice and contract_id().

I modified the contract to return its AccountId using self.env().account_id(), but it still returns Alice's AccountId.

1 Answer 1

8

The testing Engine is still limited in some capabilities: It is correct that a Smart Contract obtains its own AccountId, but I doubt that this is currently reflected by the testing Engine, since it currently doesn't simulate the instantiation of a Smart Contract, which would assign an AccountId to the contract.

Therefore self.env().account_id() currently returns the callee's AccountId, which is manually set by calling ink_env::test::set_callee::<ink_env::DefaultEnvironment>(account_id). If you want to test it, you need to configure an AccountId for your Smart Contract first. I added a test in my example here.

I added a function getId() to the contract-transfer example here.

After starting Substrate contracts node locally (version substrate-contracts-node 0.11.0-bb0cd97-x86_64-linux-gnu) with substrate-contracts-node --dev --tmp, and connecting it with the contracts UI, I can call the function to obtain the contracts AccountId, which is different from Alice's AccountId:

enter image description here

So it indeed returns the Smart Contracts AccountId when deplyed on the Substrate Node.

2
  • 1
    I basically created a function in the contract that returns self.env().account_id() . The issue is only in the contract's unit tests, it works properly in contracts-ui.
    – Kanishka
    Commented Apr 4, 2022 at 7:34
  • 1
    Okay, I see the problem! I updated my answer above.
    – Achim
    Commented Apr 4, 2022 at 13:32

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.