Skip to content

Commit

Permalink
feat: add env
Browse files Browse the repository at this point in the history
  • Loading branch information
Guo Hong committed Jun 23, 2024
1 parent 76e3ed1 commit 2c3385a
Show file tree
Hide file tree
Showing 5 changed files with 33,496 additions and 24 deletions.
2 changes: 2 additions & 0 deletions members/xiangnuans/task3/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
INFURA_PROJECT_ID=""
PRIVATE_KEY="
63 changes: 39 additions & 24 deletions members/xiangnuans/task3/contracts/NFTMarket.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ contract NFTMarket {
address nftContract;
uint256 tokenId;
uint256 price;
bool isActive;
}

Listing[] public listings;
mapping(address => mapping(uint256 => Listing)) public listings;
IERC20 public paymentToken;

event NFTListed(
Expand All @@ -29,37 +30,51 @@ contract NFTMarket {
);

constructor(IERC20 _paymentToken) {
paymentToken = _paymentToken;
paymentToken = IERC20(_paymentToken);
}

function listNFT(
address nftContract,
uint256 tokenId,
uint256 price
) public {
IERC721(nftContract).transferFrom(msg.sender, address(this), tokenId);
listings.push(Listing(msg.sender, nftContract, tokenId, price));
emit NFTListed(msg.sender, nftContract, tokenId, price);
}

function buyNFT(uint256 index) external {
Listing storage listing = listings[index];
require(listing.price > 0, "NFT not for sale");
address _nftContract,
uint256 _tokenId,
uint256 _price
) external {
IERC721 nft = IERC721(_nftContract);
require(
nft.ownerOf(_tokenId) == msg.sender,
"Not the owner of the NFT"
);
require(
nft.isApprovedForAll(msg.sender, address(this)),
"Contract not approved"
);

paymentToken.transferFrom(msg.sender, listing.seller, listing.price);
IERC721(listing.nftContract).transferFrom(
address(this),
listings[_nftContract][_tokenId] = Listing(
msg.sender,
listing.tokenId
_nftContract,
_tokenId,
_price,
true
);
emit NFTListed(msg.sender, _nftContract, _tokenId, _price);
}

emit NFTBought(
msg.sender,
listing.nftContract,
listing.tokenId,
listing.price
function buyNFT(address _nftContract, uint256 _tokenId) external {
Listing storage listing = listings[_nftContract][_tokenId];
require(listing.isActive, "Not not listed for sale");

IERC721 nft = IERC721(_nftContract);
require(
paymentToken.transferFrom(
msg.sender,
listing.seller,
listing.price
),
"Payment failed"
);

delete listings[index];
nft.transferFrom(listing.seller, msg.sender, _tokenId);
listing.isActive = false;

emit NFTBought(msg.sender, _nftContract, _tokenId, listing.price);
}
}
Loading

0 comments on commit 2c3385a

Please sign in to comment.