Skip to content

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

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

Closed
1 task done
bidzyyys opened this issue Oct 9, 2024 · 0 comments · Fixed by #14
Closed
1 task done

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

bidzyyys opened this issue Oct 9, 2024 · 0 comments · Fixed by #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
@github-project-automation github-project-automation bot moved this from Todo to Done in Rust Contracts Stylus Feb 3, 2025
bidzyyys added a commit to OpenZeppelin/rust-contracts-stylus that referenced this issue Feb 11, 2025
This pr contains migration of unit test to the new layout of motsu unit
tests, that will be introduced in OpenZeppelin/stylus-test-helpers#8 and
OpenZeppelin/stylus-test-helpers#12

Resolves #470 

#### PR Checklist

- [x] Tests
- [ ] Documentation
- [ ] Changelog

---------

Co-authored-by: Daniel Bigos <[email protected]>
Co-authored-by: Daniel Bigos <[email protected]>
Co-authored-by: Nenad <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

1 participant