Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 155 additions & 0 deletions contracts/BlockMarketPlace.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";

struct Listing{
address owner;
uint256 tokenId;
IERC20 paymentToken;
address NftToken;
bool isNative;
uint256 price;
bool sold;
uint minOffer;
}

struct OfferDetails{
uint256 listId;
uint256 offerAmount;
address offerrer;
bool status;
}

contract BlockMarketPlace {

mapping (uint256 listid => Listing list) public idToListing;
mapping (uint256 offerid => OfferDetails offer) public idToOffer;

uint256 public lastUpdatedid;
uint256 public lastOfferId;
address public marketOwner;

constructor() {
marketOwner = msg.sender;
}

function listNft(Listing memory list) external {
uint listId = lastUpdatedid++;
require(list.price > 0, "Invalid price");
require(list.minOffer > 0, "Invalid min offer");
if(list.isNative){
require(address(list.paymentToken) == address(0), "ERC20 Payment is not supported");
}
Listing memory listing;
listing.owner = msg.sender;
listing.tokenId = list.tokenId;
listing.paymentToken = list.paymentToken;
listing.price = list.price;
listing.isNative = list.isNative;
listing.minOffer = list.minOffer;
listing.NftToken = list.NftToken;
idToListing[listId] = listing;


IERC721(list.NftToken).transferFrom(msg.sender, address(this), list.tokenId);
}
function getListing(uint256 listId) external view returns(Listing memory){
return idToListing[listId];
}
function buyNft(uint256 listId) external payable {
Listing memory l = idToListing[listId];
require(!l.sold, "ALready Sold");
idToListing[listId].sold = true;

if(l.isNative){
require(msg.value == l.price, "Incorrect price");
(bool s,) = l.owner.call{value: l.price * 97/100}("");
(bool ss,) = marketOwner.call{value: l.price * 3/100}("");
require(s, "Owner transfer failed");
require(ss, "MarketOwner Transfer failed");
}else{
l.paymentToken.transferFrom(msg.sender, l.owner, l.price * 97/100);
l.paymentToken.transferFrom(msg.sender, marketOwner, l.price * 3/100);
}
IERC721(l.NftToken).transferFrom(address(this), msg.sender, l.tokenId);
}

function offer(uint256 listid, uint256 offerAmount) external payable {
uint256 offerId = lastOfferId++;
Listing memory l = idToListing[listid];
require(!l.sold, "Already sold");
if(l.isNative){
require(msg.value >= l.minOffer, "Invalid offer");
require(offerAmount == 0, "Cannot offer erc20");
}else {
require(offerAmount >= l.minOffer, "Invalid offer");
l.paymentToken.transferFrom(msg.sender, address(this), offerAmount);
}
require(msg.sender != l.owner, "Owner cannot offer");
OfferDetails memory offer_;
offer_.listId = listid;
offer_.offerrer = msg.sender;
offer_.offerAmount = l.isNative ? msg.value : offerAmount;

idToOffer[offerId] = offer_;
}

function getOffer(uint256 offerId) external view returns(OfferDetails memory o) {
o = idToOffer[offerId];
}

function acceptOffer(uint256 offerid) external {
OfferDetails memory offer_ = idToOffer[offerid];
Listing memory l = idToListing[offer_.listId];
// Checks
require(l.owner == msg.sender, "Unauthorized seller");
require(!l.sold, "Already Sold");
require(offer_.offerrer != address(0), "Invalid offer");
// Effects
idToListing[offer_.listId].sold = true;
idToOffer[offerid].status = true;
// Interactions
if(l.isNative){
(bool success,) = l.owner.call{value: offer_.offerAmount * 97/100}("");
(bool success2,) = marketOwner.call{value: offer_.offerAmount * 3/100}("");
require(success, "Failed owner transfer");
require(success2, "Failed marketPlace commission transfer");
}else{
l.paymentToken.transfer(l.owner, offer_.offerAmount * 97/100);
l.paymentToken.transfer(marketOwner, offer_.offerAmount * 3/100);
}
IERC721(l.NftToken).transferFrom(address(this), offer_.offerrer, l.tokenId);
}

function cancelOffer(uint256 offerid) external{
OfferDetails memory offer_ = idToOffer[offerid];
Listing memory l = idToListing[offer_.listId];
// Checks
require(!offer_.status, "Offer already accepted");
require(msg.sender == offer_.offerrer, "Unauthorized offerrer");
// Effects
delete idToOffer[offerid];
// Interactions
if(l.isNative){
(bool s,) = offer_.offerrer.call{value: offer_.offerAmount}("");
require(s, "Failed refund");
}else{
l.paymentToken.transfer(offer_.offerrer, offer_.offerAmount);
}

}

function cancelListing(uint256 listid) external {
Listing memory l = idToListing[listid];
// Checks
require(msg.sender == l.owner, "Unauthorized user");
require(!l.sold, "Already sold");
// Effects
delete idToListing[listid];
// Interaction
IERC721(l.NftToken).transferFrom(address(this), l.owner, l.tokenId);
}

}
16 changes: 16 additions & 0 deletions contracts/BlockNft.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-License-Identifier: SEE LICENSE IN LICENSE
pragma solidity ^0.8.28;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";

contract BlockNft is ERC721{
uint256 public tokenID;
constructor() ERC721("BlockNft", "BKN"){}

function mint(address recepient) external returns(uint256){
tokenID++;
uint256 tokenId = tokenID;
_safeMint(recepient, tokenId);
return tokenId;
}
}
37 changes: 37 additions & 0 deletions contracts/BlockToken.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract BlockToken is ERC20{

address public owner;

modifier onlyOwner {
require(msg.sender == owner, "BlockToken:: Unauthorized User");
_;
}

modifier notAmount0(uint256 _amount){
require(_amount != 0, "BlockToken:: Zero amount not supported");
_;
}
constructor(string memory _name, string memory _symbol, address _owner) ERC20(_name, _symbol){
require(_owner != address(0), "BlockToken:: Zero address not supported");
owner = _owner;
}

function mint(uint256 _amount, address _recepient) onlyOwner notAmount0(_amount) external {
_mint(_recepient, _amount);
}

function burn(uint256 _amount) notAmount0(_amount) external {
_burn(msg.sender, _amount);
}

function burnFrom(address _user, uint256 _amount)onlyOwner notAmount0(_amount) external {
_burn(_user, _amount);
}


}
48 changes: 0 additions & 48 deletions contracts/Counter.sol

This file was deleted.

63 changes: 63 additions & 0 deletions contracts/CounterV2.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;

import "../interfaces/ICounterV2.sol";

// interface ICounterV2{
// function setCount(uint count) external;
// function getCount() external returns (uint);
// function resetCount() external;
// function decreaseCount() external;
// }

// contract CounterV2 {
contract CounterV2 is ICounterV2{

uint count;
address owner;

constructor(){
owner = msg.sender;
}

// modifier onlyOwner() {
// require (owner == msg.sender, "unauthorized");
// _;
// }

function setCount(uint _count) public {
require(owner == msg.sender, "unauthorized");
require(_count<=10, "count should not be greater than 10");
count = _count;
}

function getCount() public view returns(uint){
return count;
}

function resetCount() public{
require(owner == msg.sender, "unauthorized");
count = 0;
}

function decreaseCount() public{
count -= 1;
}

function increaseCount() public{
count+=1;
}

}

// contract CounterV2Caller{
// ICounterV2 public icounterv2;

// constructor(address _counterV2address){
// icounterv2 = ICounterV2(_counterV2address);
// }

// function decreaseCount() public{
// icounterv2.decreaseCount();
// }
// }
14 changes: 14 additions & 0 deletions contracts/CounterV2Caller.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;
import "../interfaces/ICounterV2.sol";
contract CounterV2Caller{
ICounterV2 public icounterv2;

constructor(address _counterV2address){
icounterv2 = ICounterV2(_counterV2address);
}

function decreaseCount() public{
icounterv2.decreaseCount();
}
}
9 changes: 9 additions & 0 deletions interfaces/ICounterV2.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;

interface ICounterV2{
function setCount(uint count) external;
function getCount() external returns (uint);
function resetCount() external;
function decreaseCount() external;
}
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
"devDependencies": {
"@nomicfoundation/hardhat-toolbox": "^5.0.0",
"hardhat": "^2.26.1"
},
},
"scripts": {
"test": "npx hardhat test",
"compile": "npx hardhat compile",
"test": "npx hardhat test",
"compile": "npx hardhat compile",
"node": "npx hardhat node"
},
"dependencies": {
"@openzeppelin/contracts": "^5.4.0"
}
}
Loading