Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip update: subsc nft #547

Open
wants to merge 6 commits into
base: feature/mint-point
Choose a base branch
from
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
59 changes: 51 additions & 8 deletions hardhat/contracts/SubscriptionNFT.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,75 @@
pragma solidity ^0.8.19;

import "@openzeppelin/contracts-upgradeable/token/ERC721/extensions/ERC721URIStorageUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import {AccessControlEnumerableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/AccessControlEnumerableUpgradeable.sol";
import {IMintPoint} from "./IMintPoint.sol";

contract SubscriptionNFT is
Initializable,
ERC721EnumerableUpgradeable,
OwnableUpgradeable
ERC721URIStorageUpgradeable,
AccessControlEnumerableUpgradeable
{
bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
uint256 private _tokenIds;
address public mintPointAddr;
mapping(uint256 => AirDropInfo) private airDropInfos;

struct AirDropInfo {
uint256[] dates;
uint256 dropIndex;
}

function initialize() public initializer {
__ERC721_init("SubscriptionNFT", "SNFT");
__ERC721URIStorage_init();
__Ownable_init();
__AccessControlEnumerable_init();

_grantRole(DEFAULT_ADMIN_ROLE, _msgSender());
_grantRole(MINTER_ROLE, _msgSender());
}

function supportsInterface(
bytes4 interfaceId
)
public
view
override(
ERC721Upgradeable,
AccessControlEnumerableUpgradeable
)
returns (bool)
{
return super.supportsInterface(interfaceId);
}

function setMintAddr(address _mintPointAddr) public onlyRole(DEFAULT_ADMIN_ROLE) {
mintPointAddr = _mintPointAddr;
}

function mintNFT(address recipient, string memory tokenURI)
public onlyOwner
function mintNFT(address recipient, string memory tokenURI, AirDropInfo calldata airDropInfo)
public onlyRole(MINTER_ROLE)
returns (uint256)
{
_tokenIds++;

_tokenIds++;
uint256 newItemId = _tokenIds;
_mint(recipient, newItemId);

airDropInfos[newItemId] = airDropInfo;

_setTokenURI(newItemId, tokenURI);
_mint(recipient, newItemId);

return newItemId;
}

function airdrop() public onlyRole(MINTER_ROLE) {
for (uint256 i = 1; i <= _tokenIds; i++) {
AirDropInfo memory airDropInfo = airDropInfos[i];
if (airDropInfo.dates[airDropInfo.dropIndex] > block.timestamp) {
address owner = ownerOf(i);
IMintPoint(mintPointAddr).mintByMinter(owner, 1, 1000);
}
airDropInfos[i].dropIndex++;
}
}
}
54 changes: 54 additions & 0 deletions hardhat/test/SubscriptionNFT.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { expect } from "chai";
import { ethers, upgrades } from "hardhat";
import {
SubscriptionNFT
} from "../typechain";
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";

const deploySubscriptionNFT = async () => {
const SubscriptionNFTFactory = await ethers.getContractFactory("SubscriptionNFT");
const deployedSubscriptionNFT: SubscriptionNFT = (await upgrades.deployProxy(
SubscriptionNFTFactory,
[],
{
initializer: "initialize",
}
)) as any;
await deployedSubscriptionNFT.deployed();

console.log("SubscriptionNFT address:", deployedSubscriptionNFT.address);

return deployedSubscriptionNFT;
};

// describe("SubscriptionNFT", function () {
// let subscriptionNFT: SubscriptionNFT;
// let owner: SignerWithAddress, addr1: SignerWithAddress;

// before(async function () {
// [owner, addr1] = await ethers.getSigners();
// subscriptionNFT = await deploySubscriptionNFT();
// });

// describe("Contract deployment", function () {
// it("Should set the right owner", async function () {
// expect(await subscriptionNFT.owner()).to.equal(owner.address);
// });

// it("Should have the correct name and symbol", async function () {
// expect(await subscriptionNFT.name()).to.equal("SubscriptionNFT");
// expect(await subscriptionNFT.symbol()).to.equal("SNFT");
// });
// });

// describe("Minting NFT", function () {
// it("Should mint an NFT to the specified address", async function () {
// const tokenURI = "https://example.com/nft";
// await expect(subscriptionNFT.mintNFT(addr1.address, tokenURI))
// .to.emit(subscriptionNFT, "Transfer")
// .withArgs(ethers.constants.AddressZero, addr1.address, 1);

// expect(await subscriptionNFT.tokenURI(1)).to.equal(tokenURI);
// });
// });
// });