ERC721 Exercise 2
Intro
OpenOcean wants to become the world's largest digital marketplace for crypto collectibles and non-fungible tokens (NFTs).
Users should be able to buy, sell, and discover exclusive digital items.
You've been given the task to develop their marketplace smart contract. What a terrific opportunity to improve your skills in ERC721 tokens, right? ;)
Accounts
- 0 - Deployer
- 1 - User1
- 2 - User2
- 3 - User3
Tasks
Task 1
Complete the OpenOcean.sol smart contract, implement all the open TODOs
- Create a
maxPriceconstant of uint256, 100 ether (maximum NFT listing price) - Create an
Itemstruct which represents an erc721 item that is on sale. - The struct should have the following data:
- itemId (uint256)
- collection contract (address)
- tokenId (uint256)
- price (uint256)
- seller (address payable)
- isSold (bool)
- Create a
itemsCounterstate variable (uint256) - Create an
listedItemsmapping which maps betweenitemId(uint256) toItem(struct). - Create an external
listItemfunction which allows users to list their nft in the marketplace. The function should receive the collection contract address, the tokenId, and the price it will be listed for:- Make sure the parameters are correct
- Increment
itemsCounter - Transfer token from the sender to the contract
- Add item to the
listedItemsmapping
- Create an external payable
purchasefunction which receivesitemIdand allows someone to purchase the item:- Check that the item exists and not sold
- Check that enough ETH was paid
- Change item status to "sold"
- Transfer NFT to buyer
- Transfer ETH to seller
Task 2
You will use the DummyERC721.sol contract to test your Marketplace
Deployment and listings tests
- Deploy the marketplace contract from the deployer account
- From user1: List Crypto Cuties token IDs 1 - 10, for 5 ETH each
- Test that the
itemsCounteris 10 - Test that the marketplace contract owns 10 Crypto Cuties NFTs
- Check that all the parameters of the last item that was listed are correct
- Test that the
- From user3: List Rare Boobles token IDs 1 - 5, for 7 ETH each
- Test that the
itemsCounteris 15 - Test that the marketplace contract owns 5 Rare Boobles NFTs
- Check that all the parameters of the last item that was listed are correct
- Test that the
Purchases tests
- From User2:
- Try to purchase itemId 100 (doesn't exist), and make sure the transaction is reverted
- Try to purchase item number 3, without providing any ETH, and make sure the transaction is reverted
- Try to purchase item number 3 with the correct amount of ETH, and make sure the transaction goes through
- Try to purchase item number 3 again, and make sure the transaction is reverted with the relevant error message
- Make sure that User2 owns item number 3
- Make sure that the User1 got the right amount of ETH for the sale
- Try to purchase item number 11 with the correct amount of ETH, make sure the transaction goes through
- Make sure User2 owns item number 11
- Make sure that User3 got the right amount of ETH for the sale
Links that might be useful
- ERC721 Standard on Ethereum Website https://eips.ethereum.org/EIPS/eip-721
- ERC721 Source code on Github https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/ERC721.sol
- Hardhat Tests https://hardhat.org/tutorial/testing-contracts