Skip to content

Commit

Permalink
Contract and tests cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
jannden committed May 30, 2024
1 parent 216cf95 commit 7fddfff
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 183 deletions.
33 changes: 7 additions & 26 deletions backend/contracts/NftMarketplaceV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,6 @@ contract NftMarketplaceV2 is ReentrancyGuard, Ownable {
/// @notice NftAddress -> Token ID -> Price
mapping(address => mapping(uint256 => uint256)) public listings;

/// @notice UserAddress -> Funds
// For increased security: https://fravoll.github.io/solidity-patterns/pull_over_push.html
// Why using call: https://consensys.github.io/smart-contract-best-practices/development-recommendations/general/external-calls/#dont-use-transfer-or-send
// Good practice: https://docs.soliditylang.org/en/develop/security-considerations.html?highlight=check%20effects#use-the-checks-effects-interactions-pattern
// mapping(address => uint256) private funds;

/// @notice UserAddress -> Deployed NFT contract addresses
mapping(address => address[]) public nfts;

Expand Down Expand Up @@ -113,11 +107,10 @@ contract NftMarketplaceV2 is ReentrancyGuard, Ownable {
// Set the price of the token (which lists it in the marketplace)
listings[_nftAddress][_tokenId] = _price;

// Add the listing fee to the funds for the contract owner
// Send the listing fee to the marketplace owner
address owner = owner();
// funds[owner] += msg.value;
(bool success, ) = payable(owner).call{value: msg.value}(""); // when funds aren't used
require(success, "Transfer failed"); // when funds aren't used
(bool success, ) = payable(owner).call{value: msg.value}("");
require(success, "Transfer failed");

// Emit event
emit ItemListed(msg.sender, _nftAddress, _tokenId, _price);
Expand All @@ -131,9 +124,9 @@ contract NftMarketplaceV2 is ReentrancyGuard, Ownable {
alreadyListed(_nftAddress, _tokenId)
{
IERC721 nft = IERC721(_nftAddress);
address owner = nft.ownerOf(_tokenId);
address listingOwner = nft.ownerOf(_tokenId);
require(
msg.sender == owner || msg.sender == _nftAddress,
msg.sender == listingOwner || msg.sender == owner() || msg.sender == _nftAddress,
"Caller isn't owner or nft contract."
);
delete (listings[_nftAddress][_tokenId]);
Expand All @@ -153,11 +146,10 @@ contract NftMarketplaceV2 is ReentrancyGuard, Ownable {
require(msg.value == listings[_nftAddress][_tokenId], "Price mismatch.");
IERC721 nft = IERC721(_nftAddress);
address owner = nft.ownerOf(_tokenId);
// funds[owner] += msg.value;
delete (listings[_nftAddress][_tokenId]);
IERC721(_nftAddress).safeTransferFrom(owner, msg.sender, _tokenId);
(bool success, ) = payable(owner).call{value: msg.value}(""); // when funds aren't used
require(success, "Transfer failed"); // when funds aren't used
(bool success, ) = payable(owner).call{value: msg.value}("");
require(success, "Transfer failed");
emit ItemBought(msg.sender, _nftAddress, _tokenId, msg.value);
}

Expand All @@ -174,17 +166,6 @@ contract NftMarketplaceV2 is ReentrancyGuard, Ownable {
emit CollectionAdded(msg.sender, _nftAddress);
}

/// @notice Method for withdrawing earned funds
/*
function withdrawFunds() external {
require(funds[msg.sender] > 0, "No funds to withdraw.");
uint256 amountToWithdraw = funds[msg.sender];
funds[msg.sender] = 0;
(bool success, ) = payable(msg.sender).call{value: amountToWithdraw}("");
require(success, "Transfer failed");
}
*/

/// @notice Method for changing the listing fee
function setListingFee(uint256 _newFee) external onlyOwner {
require(_newFee > 0, "Fee should be above zero.");
Expand Down
101 changes: 0 additions & 101 deletions backend/scripts/NftMarketplaceV2.interact.ts

This file was deleted.

Loading

0 comments on commit 7fddfff

Please sign in to comment.