Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: Mock another contract in #[motsu::test] #8

Open
1 task done
bidzyyys opened this issue Oct 9, 2024 · 0 comments · May be fixed by OpenZeppelin/rust-contracts-stylus#423 or #14
Open
1 task done

[Feature]: Mock another contract in #[motsu::test] #8

bidzyyys opened this issue Oct 9, 2024 · 0 comments · May be fixed by OpenZeppelin/rust-contracts-stylus#423 or #14

Comments

@bidzyyys
Copy link
Collaborator

bidzyyys commented Oct 9, 2024

What is the feature you would like to see?

Some contracts need to call another contracts, e.g. Erc721::_check_on_erc721_received.
We want to be able to mock some behaviour, or at least mock return data in motsu unit tests.

pub fn _check_on_erc721_received(
        &mut self,
        operator: Address,
        from: Address,
        to: Address,
        token_id: U256,
        data: Bytes,
    ) -> Result<(), Error> {
        const RECEIVER_FN_SELECTOR: FixedBytes<4> = fixed_bytes!("150b7a02");

        if !to.has_code() {
            return Ok(());
        }

        let receiver = IERC721Receiver::new(to);
        let call = Call::new_in(self);
        let result = receiver.on_erc_721_received(
            call,
            operator,
            from,
            token_id,
            data.to_vec().into(),
        );

        let id = match result {
            Ok(id) => id,
            Err(e) => {
                if let call::Error::Revert(ref reason) = e {
                    if reason.len() > 0 {
                        // Non-IERC721Receiver implementer.
                        return Err(Error::InvalidReceiverWithReason(e));
                    }
                }

                return Err(ERC721InvalidReceiver { receiver: to }.into());
            }
        };

        // Token rejected.
        if id != RECEIVER_FN_SELECTOR {
            return Err(ERC721InvalidReceiver { receiver: to }.into());
        }

        Ok(())
    }
}

Contribution Guidelines

  • I agree to follow this project's Contribution Guidelines
@ggonzalez94 ggonzalez94 transferred this issue from OpenZeppelin/rust-contracts-stylus Dec 17, 2024
@qalisander qalisander linked a pull request Dec 19, 2024 that will close this issue
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Todo
1 participant