Supercolony-net/openbrush-contracts

Mintable transfer_from panicks with "off-chain environment does not support contract invocation" in test

Closed this issue · 2 comments

s-yi commented

Hi!

The following test (cargo test) is causing a panic for me where it outputs the following:

panicked at 'not implemented: off-chain environment does not support contract invocation'

        #[ink::test]
        fn transfer_from_works() {
            let accounts = ink_env::test::default_accounts::<ink_env::DefaultEnvironment>();

            ink_env::test::set_caller::<ink_env::DefaultEnvironment>(accounts.django);
            assert_eq!(contract.approve(accounts.django, 1_000_000).is_ok(), true);
            assert_eq!(contract.transfer_from(contract.env().account_id(), accounts.django, 10, Vec::<u8>::new()).is_ok(), true);
        }

I am using Ink! 3.3 and Openbrush 2.2.0. Is this a known error or is this something on my end?
Thank you!

Under the hood, transfer_from tries to do a cross-contract call to django, and it causes a failure because the ink doesn't support cross-contract calls in unit tests.

You can fix it by overriding _do_safe_transfer_check to not do the cross-contract call if the test feature is enabled.

impl psp22::Internal for Contract {
    #[cfg(feature = "test")]
    fn _do_safe_transfer_check(
        &mut self,
        _from: &AccountId,
        _to: &AccountId,
        _value: &Balance,
        _data: &Vec<u8>,
    ) -> Result<(), PSP22Error> {
        Ok(())
    }
}
s-yi commented

I just tried it out and it works like a charm -- thank you very much for the help! :)