I've been unit testing an ink smart contract by calling the constructor and testing various methods on the smart contract.
However, I now need to obtain the account of the contract in order to set its balance for testing purposes. How can I do this?
Lots of methods, such as getting/setting the current balance, code hash, etc, require the address of the contract.
What I've tried already:
a)
let mut contract = MyContract::default();
let code_hash = contract.env().own_code_hash(); // cannot access .env()
b)
let mut contract = MyContract::default();
let contract_addr = ink::env::account_id::<ink::env::DefaultEnvironment>(contract); // doesn't accept the contract as a parameter
c)
let mut contract = MyContract::default();
let contract_addr = ink::env::account_id::<ink::env::DefaultEnvironment>(); // using it correctly, but always returns the account [1, 1, 1, ... , 1]
let mut contract2 = MyContract::default();
let contract_addr2 = ink::env::account_id::<ink::env::DefaultEnvironment>(); // returns the account [1, 1, 1, ... , 1] again, the two contracts can't have the same address!
Essentially, I need access to the .env()
in the unit tests somehow. Not sure how to do this or if this is the correct way to be doing it.