Skip to content

Commit

Permalink
add Lazymint for ERC1155 and ERC721
Browse files Browse the repository at this point in the history
  • Loading branch information
aminmalekzadeh committed May 9, 2022
1 parent c171e46 commit 57602af
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 40 deletions.
2 changes: 0 additions & 2 deletions contracts/NFTexchange/Lib/State.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
pragma solidity ^0.8.0;

import "./Order.sol";
import "../NFTexchangeCore.sol";


library State {

Expand Down
14 changes: 14 additions & 0 deletions contracts/NFTexchange/Marketmanage.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/access/Ownable.sol";

abstract contract marketmanage is Ownable {

address owner;
address protcolfee;

function


}
6 changes: 5 additions & 1 deletion contracts/NFTexchange/NFTexchange.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import "@openzeppelin/contracts/token/ERC1155/IERC1155.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
Expand All @@ -20,7 +21,7 @@ import "./Lib/interface/IOrder.sol";
import "../royalties/IERC2981Royalties.sol";


contract NFTexchange is ReentrancyGuard, Validate {
contract NFTexchange is ReentrancyGuard, Validate, Initializable {

using Counters for Counters.Counter;
using SafeMath for uint;
Expand Down Expand Up @@ -54,6 +55,9 @@ contract NFTexchange is ReentrancyGuard, Validate {
State.stateItem state
);

function initialize() public initializer {
// we can set param for update
}

function createMarketItem(Order.OrderItem memory _order) public {

Expand Down
30 changes: 17 additions & 13 deletions contracts/Tokens/LazyMint/TokenERC1155LazyMint.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,28 @@ pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
import "@openzeppelin/contracts/token/ERC1155/IERC1155.sol";
import "@openzeppelin/contracts/access/AccessControl.sol";
import "@openzeppelin/contracts/token/ERC1155/extensions/ERC1155Burnable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "../../royalties/impl/RoyaltiesV2impl.sol";
import "../../royalties/impl/RoyaltiesV2Impl.sol";
import "./signtureEIP712/signtureERC1155.sol";
import "../../royalties/LibRoyality.sol";
import "../../royalties/LibRoyaltiesV2.sol";

contract TokenERC1155LazyMint is ERC1155, ERC1155Burnable, Ownable, RoyaltiesV2Impl {
contract TokenERC1155LazyMint is ERC1155, ERC1155Burnable, Ownable, AccessControl, RoyaltiesV2Impl, signtureERC1155 {

bytes4 private constant _INTERFACE_ID_ERC2981 = 0x2a55205a;
address public contractAddress;
string TokenURI;
string contracturi;
mapping(uint256 => uint256) public tokenIds;
mapping (uint256 => string) public _tokenURIs;
bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");

string private _baseURI;

struct NFTVoucher {
uint256 tokenId;
uint256 minPrice;
uint256 supply;
address account;
string uri;
}

constructor(string memory _uri, address contractAddr) ERC1155(_uri) {
constructor(string memory _uri, address minter, address contractAddr) ERC1155(_uri) {
_setupRole(MINTER_ROLE, minter);
TokenURI = _uri;
contractAddress = contractAddr;
contracturi = _uri;
Expand All @@ -40,12 +36,20 @@ contract TokenERC1155LazyMint is ERC1155, ERC1155Burnable, Ownable, RoyaltiesV2

function Lazymint(NFTVoucher calldata voucher, address redeemer, bytes memory signature ,bytes memory data)
public
onlyOwner
payable
{
address signer = _verify(voucher, signature);

require(hasRole(MINTER_ROLE, signer), "Invalid signature - unknown signer");
require(msg.value >= voucher.minPrice, "Insufficient funds to redeem");

_mint(voucher.account, voucher.tokenId, voucher.supply, data);
tokenIds[voucher.tokenId] = voucher.tokenId;
setTokenURI(voucher.tokenId, voucher.uri);
safeTransferFrom(voucher.account, redeemer, voucher.tokenId, voucher.supply, data);
setApprovalForAll(contractAddress, true);
address payable receiver = payable(signer);
receiver.transfer(msg.value);
}

function mintBatch(address to, uint256[] memory ids, uint256[] memory amounts, bytes memory data)
Expand Down Expand Up @@ -101,7 +105,7 @@ contract TokenERC1155LazyMint is ERC1155, ERC1155Burnable, Ownable, RoyaltiesV2
public
view
virtual
override
override(AccessControl,ERC1155)
returns (bool)
{
if (interfaceId == LibRoyaltiesV2._INTERFACE_ID_ROYALTIES) {
Expand Down
28 changes: 4 additions & 24 deletions contracts/Tokens/LazyMint/TokenERC721LazyMint.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/access/AccessControl.sol";
import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import "@openzeppelin/contracts/utils/cryptography/draft-EIP712.sol";
import "../../royalties/impl/RoyaltiesV2impl.sol";
import "./signtureEIP712/signtureERC721.sol";
import "../../royalties/impl/RoyaltiesV2Impl.sol";
import "../../royalties/LibRoyality.sol";
import "../../royalties/LibRoyaltiesV2.sol";

contract TokenERC721LazyMint is ERC721URIStorage, ERC721Burnable, RoyaltiesV2Impl, Ownable, EIP712, AccessControl {
contract TokenERC721LazyMint is ERC721URIStorage, ERC721Burnable, RoyaltiesV2Impl, Ownable, signtureERC721, AccessControl {
using ECDSA for bytes32;
using Counters for Counters.Counter;
Counters.Counter private _tokenIdCounter;
Expand All @@ -28,11 +29,6 @@ contract TokenERC721LazyMint is ERC721URIStorage, ERC721Burnable, RoyaltiesV2Imp
mapping(uint256 => uint256) public getTokenIDs;
mapping(address => uint256) public tokenIds;

struct NFTVoucher {
uint256 tokenId;
uint256 minPrice;
string uri;
}

modifier isOwnerTokenId(uint256 _tokenId) {
require(ownerOf(_tokenId) == address(msg.sender) , "you should be owner this token id");
Expand All @@ -41,28 +37,12 @@ contract TokenERC721LazyMint is ERC721URIStorage, ERC721Burnable, RoyaltiesV2Imp

constructor (string memory _name, string memory _symbol, address minter, address contractAddr, string memory _contracturi)
ERC721(_name, _symbol)
EIP712("LazyNFT-Voucher", "1")
{
_setupRole(MINTER_ROLE, minter);
contractAddress = contractAddr;
contracturi = _contracturi;
}

function _hash(NFTVoucher calldata voucher) internal view returns (bytes32) {
return _hashTypedDataV4(keccak256(abi.encode(
keccak256("NFTVoucher(uint256 tokenId,uint256 minPrice,string uri)"),
voucher.tokenId,
voucher.minPrice,
keccak256(bytes(voucher.uri))
)));
}


function _verify(NFTVoucher calldata voucher, bytes memory signature) internal view returns (address) {
bytes32 digest = _hash(voucher);
return digest.toEthSignedMessageHash().recover(signature);
}

function contractURI() public view returns (string memory) {
return contracturi;
}
Expand All @@ -75,7 +55,7 @@ contract TokenERC721LazyMint is ERC721URIStorage, ERC721Burnable, RoyaltiesV2Imp
super._burn(tokenId);
}

function lazyMint(address redeemer, NFTVoucher calldata voucher, bytes memory signature) public payable {
function lazyMint(address redeemer, signtureEIP721.NFTVoucher calldata voucher, bytes memory signature) public payable {
address signer = _verify(voucher, signature);

require(hasRole(MINTER_ROLE, signer), "Invalid signature - unknown signer");
Expand Down
43 changes: 43 additions & 0 deletions contracts/Tokens/LazyMint/signtureEIP712/signtureERC1155.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;


import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import "@openzeppelin/contracts/utils/cryptography/draft-EIP712.sol";

abstract contract signtureERC1155 is EIP712 {
using ECDSA for bytes32;


struct NFTVoucher {
uint256 tokenId;
uint256 minPrice;
uint256 supply;
address account;
string uri;
}

constructor ()
EIP712("LazyNFT-Voucher", "1")
{

}

function _hash(NFTVoucher calldata voucher) internal view returns (bytes32) {
return _hashTypedDataV4(keccak256(abi.encode(
keccak256("NFTVoucher(uint256 tokenId,uint256 minPrice,uint256 supply,address account,string uri)"),
voucher.tokenId,
voucher.minPrice,
voucher.supply,
voucher.account,
keccak256(bytes(voucher.uri))
)));
}


function _verify(NFTVoucher calldata voucher, bytes memory signature) internal view returns (address) {
bytes32 digest = _hash(voucher);
return digest.toEthSignedMessageHash().recover(signature);
}

}
39 changes: 39 additions & 0 deletions contracts/Tokens/LazyMint/signtureEIP712/signtureERC721.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;


import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import "@openzeppelin/contracts/utils/cryptography/draft-EIP712.sol";

abstract contract signtureERC721 is EIP712 {
using ECDSA for bytes32;


struct NFTVoucher {
uint256 tokenId;
uint256 minPrice;
string uri;
}

constructor ()
EIP712("LazyNFT-Voucher", "1")
{

}

function _hash(NFTVoucher calldata voucher) internal view returns (bytes32) {
return _hashTypedDataV4(keccak256(abi.encode(
keccak256("NFTVoucher(uint256 tokenId,uint256 minPrice,string uri)"),
voucher.tokenId,
voucher.minPrice,
keccak256(bytes(voucher.uri))
)));
}


function _verify(NFTVoucher calldata voucher, bytes memory signature) internal view returns (address) {
bytes32 digest = _hash(voucher);
return digest.toEthSignedMessageHash().recover(signature);
}

}
8 changes: 8 additions & 0 deletions migrations/2_deploy_NFTexchange.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { deployProxy } = require('@openzeppelin/truffle-upgrades');

const NFTexchange = artifacts.require('NFTexchange');

module.exports = async function (deployer) {
const instance = await deployProxy(NFTexchange, { deployer });
console.log('Deployed', instance.address);
};
11 changes: 11 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"dependencies": {
"@openzeppelin/contracts": "^4.6.0",
"@openzeppelin/contracts-upgradeable": "^4.6.0",
"dotenv": "^16.0.0"
},
"devDependencies": {
Expand Down

0 comments on commit 57602af

Please sign in to comment.