Skip to content

Instantly share code, notes, and snippets.

@casweeney
Last active February 18, 2024 18:11
Show Gist options
  • Save casweeney/905b7e56c3e41f77b6e6c3a44b135a6d to your computer and use it in GitHub Desktop.
Save casweeney/905b7e56c3e41f77b6e6c3a44b135a6d to your computer and use it in GitHub Desktop.
import {
time,
loadFixture,
} from "@nomicfoundation/hardhat-toolbox/network-helpers";
import { anyValue } from "@nomicfoundation/hardhat-chai-matchers/withArgs";
import { expect } from "chai";
import { ethers } from "hardhat";
describe("Lock", function () {
// We define a fixture to reuse the same setup in every test.
// We use loadFixture to run this setup once, snapshot that state,
// and reset Hardhat Network to that snapshot in every test.
async function deployEventContract() {
// Contracts are deployed using the first signer/account by default
const [owner, anotherUser] = await ethers.getSigners();
const EventNFT = await ethers.getContractFactory("EventNft");
const eventNFT = await EventNFT.deploy();
const Event = await ethers.getContractFactory("NftGatedEvent");
const event = await Event.deploy(eventNFT.target);
return { eventNFT, event, owner, anotherUser };
}
describe("NFT Gated Event Contract", function () {
it("Should set the nft contract address correctly", async function () {
const { eventNFT, event } = await loadFixture(deployEventContract);
const nftAddress = await event.eventNft();
expect(nftAddress).to.equal(eventNFT.target);
});
it("Should create an event successfully", async function () {
const { event } = await loadFixture(deployEventContract);
const title = "Web3 Lagos Conference";
const desc = "This is the best Web3 event in Lagos";
const venue = "The Zone Gbagada Lagos";
const eventDate = "18th Oct 2024";
const tx = await event.createEvent(title, desc, venue, eventDate);
await tx.wait();
expect(await event.eventCount()).to.equal(1);
expect((await event.getAllEvents()).length).to.equal(1);
const createdEvent = await event.viewEvent(1);
expect(createdEvent.title).to.equal(title);
expect(createdEvent.description).to.equal(desc);
expect(createdEvent.venue).to.equal(venue);
expect(createdEvent.eventDate).to.equal(eventDate);
});
it("It should revert if user doesn't have NFT during registration", async function () {
const { event, anotherUser } = await loadFixture(deployEventContract);
const title = "Web3 Lagos Conference";
const desc = "This is the best Web3 event in Lagos";
const venue = "The Zone Gbagada Lagos";
const eventDate = "18th Oct 2024";
const tx = await event.createEvent(title, desc, venue, eventDate);
await tx.wait();
await expect(event.connect(anotherUser).registerForEvent(1)).to.be.rejectedWith("not eligible for event");
});
it("Should allow users with NFT register for event", async function () {
const { eventNFT, event, owner, anotherUser } = await loadFixture(deployEventContract);
const title = "Web3 Lagos Conference";
const desc = "This is the best Web3 event in Lagos";
const venue = "The Zone Gbagada Lagos";
const eventDate = "18th Oct 2024";
const tx = await event.createEvent(title, desc, venue, eventDate);
await tx.wait();
const mintTx = await eventNFT.safeMint(anotherUser.address, 1);
await mintTx.wait();
const regTx = await event.connect(anotherUser).registerForEvent(1);
await regTx.wait();
expect(await event.checkRegistrationValidity(1, anotherUser.address)).to.be.equal(true);
const createdEvent = await event.viewEvent(1);
expect(createdEvent.registeredUsers.length).to.be.eq(1);
});
});
});
import { HardhatUserConfig } from "hardhat/config";
import "@nomicfoundation/hardhat-toolbox";
require("dotenv").config();
module.exports = {
solidity: "0.8.21",
networks: {
mumbai: {
url: process.env.ALCHEMY_MUMBAI_API_KEY_URL,
accounts: [process.env.ACCOUNT_PRIVATE_KEY]
}
}
};
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.21;
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
contract NftGatedEvent {
address public eventNft;
uint256 public eventCount;
struct Event {
uint256 id;
string title;
string description;
string venue;
address[] registeredUsers;
string eventDate;
}
// mapping event id to Event struct. Using id to track events
mapping(uint256 => Event) public events;
// mapping of event ID to user address to bool
// this checks if user has registered for a particular event ID
mapping(uint256 => mapping(address => bool)) hasUserRegistered;
Event[] eventsArray;
constructor(address _eventNft) {
eventNft = _eventNft;
}
function createEvent(
string memory _title,
string memory _description,
string memory _venue,
string memory _eventDate
) external {
uint256 _newEventId = eventCount + 1;
Event storage _event = events[_newEventId];
_event.id = _newEventId;
_event.title = _title;
_event.description = _description;
_event.venue = _venue;
_event.eventDate = _eventDate;
eventsArray.push(_event);
eventCount = eventCount + 1;
}
function registerForEvent(uint256 _eventId) external {
require(IERC721(eventNft).balanceOf(msg.sender) > 0, "not eligible for event");
require(!hasUserRegistered[_eventId][msg.sender], "already registered");
Event storage _event = events[_eventId];
_event.registeredUsers.push(msg.sender);
hasUserRegistered[_eventId][msg.sender] = true;
}
function getAllEvents() external view returns(Event[] memory) {
return eventsArray;
}
function viewEvent(uint256 _eventId) external view returns(Event memory) {
return events[_eventId];
}
function checkRegistrationValidity(uint _eventId, address _userAddress) external view returns(bool) {
return hasUserRegistered[_eventId][_userAddress];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment