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] #326

Open
1 task done
bidzyyys opened this issue Oct 9, 2024 · 0 comments · May be fixed by #423
Open
1 task done

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

bidzyyys opened this issue Oct 9, 2024 · 0 comments · May be fixed by #423
Labels
type: feature New feature request. type: test Changes to the testing suite. uphill Some research/design is needed before this can be implemented.

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
@bidzyyys bidzyyys added type: feature New feature request. uphill Some research/design is needed before this can be implemented. labels Oct 9, 2024
@ggonzalez94 ggonzalez94 added the type: test Changes to the testing suite. label Oct 10, 2024
@qalisander qalisander linked a pull request Nov 28, 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
type: feature New feature request. type: test Changes to the testing suite. uphill Some research/design is needed before this can be implemented.
Projects
Status: Todo
Development

Successfully merging a pull request may close this issue.

2 participants