Skip to content

Commit

Permalink
Merge pull request #544 from hackdays-io/feature/mtx-events
Browse files Browse the repository at this point in the history
Feature/mtx events
  • Loading branch information
yu23ki14 authored Mar 17, 2024
2 parents e052dfe + 03ad4a7 commit fb3d12c
Show file tree
Hide file tree
Showing 7 changed files with 322 additions and 15 deletions.
73 changes: 59 additions & 14 deletions hardhat/contracts/Event.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ pragma solidity ^0.8.4;
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/metatx/MinimalForwarderUpgradeable.sol";
import "./IMintNFT.sol";
import "./IOperationController.sol";
import "./ERC2771ContextUpgradeable.sol";
import "hardhat/console.sol";

contract EventManager is OwnableUpgradeable {
contract EventManager is OwnableUpgradeable, ERC2771ContextUpgradeable {
struct Group {
uint256 groupId;
address ownerAddress;
Expand Down Expand Up @@ -60,23 +62,27 @@ contract EventManager is OwnableUpgradeable {
mapping(uint256 => mapping(address => mapping(bytes32 => bool)))
private memberRolesByGroupId;
mapping(uint256 => address[]) private memberAddressesByGroupId;
//mapping(address => uint256) private userPoints;

modifier onlyGroupOwner(uint256 _groupId) {
require(_isGroupOwner(_groupId, msg.sender), "You have no permission");
require(
_isGroupOwner(_groupId, _msgSender()),
"You have no permission"
);
_;
}

modifier onlyAdminAccess(uint256 _groupId) {
require(
_hasAdminAccess(_groupId, msg.sender),
_hasAdminAccess(_groupId, _msgSender()),
"You have no permission"
);
_;
}

modifier onlyCollaboratorAccess(uint256 _groupId) {
require(
_hasCollaboratorAccess(_groupId, msg.sender),
_hasCollaboratorAccess(_groupId, _msgSender()),
"You have no permission"
);
_;
Expand Down Expand Up @@ -129,6 +135,7 @@ contract EventManager is OwnableUpgradeable {

// Currently, reinitializer(3) was executed as constructor.
function initialize(
MinimalForwarderUpgradeable _trustedForwarder,
address _owner,
address _relayerAddr,
uint256 _mtxPrice,
Expand All @@ -141,22 +148,59 @@ contract EventManager is OwnableUpgradeable {
_groupIds.increment();
_eventRecordIds.increment();
}
__ERC2771Context_init(address(_trustedForwarder));
relayerAddr = _relayerAddr;
mtxPrice = _mtxPrice;
maxMintLimit = _maxMintLimit;
operationControllerAddr = _operationControllerAddr;
}

function _msgSender()
internal
view
virtual
override(ContextUpgradeable, ERC2771ContextUpgradeable)
returns (address sender)
{
if (isTrustedForwarder(msg.sender)) {
// The assembly code is more direct than the Solidity version using `abi.decode`.
/// @solidity memory-safe-assembly
assembly {
sender := shr(96, calldataload(sub(calldatasize(), 20)))
}
} else {
return super._msgSender();
}
}

function _msgData()
internal
view
virtual
override(ContextUpgradeable, ERC2771ContextUpgradeable)
returns (bytes calldata)
{
if (isTrustedForwarder(msg.sender)) {
return msg.data[:msg.data.length - 20];
} else {
return super._msgData();
}
}

function createGroup(string memory _name) external whenNotPaused {
uint256 _newGroupId = _groupIds.current();
_groupIds.increment();

groups.push(
Group({groupId: _newGroupId, ownerAddress: msg.sender, name: _name})
Group({
groupId: _newGroupId,
ownerAddress: _msgSender(),
name: _name
})
);
ownGroupIds[msg.sender].push(_newGroupId);
ownGroupIds[_msgSender()].push(_newGroupId);

emit CreateGroup(msg.sender, _newGroupId);
emit CreateGroup(_msgSender(), _newGroupId);
}

function getGroups() public view returns (Group[] memory) {
Expand Down Expand Up @@ -219,17 +263,17 @@ contract EventManager is OwnableUpgradeable {

ownGroupIds[_newOwnerAddress].push(_groupId);

for (uint256 i = 0; i < ownGroupIds[msg.sender].length; i++) {
if (ownGroupIds[msg.sender][i] == _groupId) {
ownGroupIds[msg.sender][i] = ownGroupIds[msg.sender][
ownGroupIds[msg.sender].length - 1
for (uint256 i = 0; i < ownGroupIds[_msgSender()].length; i++) {
if (ownGroupIds[_msgSender()][i] == _groupId) {
ownGroupIds[_msgSender()][i] = ownGroupIds[_msgSender()][
ownGroupIds[_msgSender()].length - 1
];
ownGroupIds[msg.sender].pop();
ownGroupIds[_msgSender()].pop();
break;
}
}

emit TransferGroupOwner(msg.sender, _newOwnerAddress, _groupId);
emit TransferGroupOwner(_msgSender(), _newOwnerAddress, _groupId);
}

function _isGroupOwner(
Expand All @@ -248,6 +292,7 @@ contract EventManager is OwnableUpgradeable {
bool _useMtx,
bool _nonTransferable,
bytes32 _secretPhrase,
// uint256 _points,
IMintNFT.NFTAttribute[] memory _eventNFTAttributes
) external payable onlyCollaboratorAccess(_groupId) whenNotPaused {
require(
Expand Down Expand Up @@ -291,7 +336,7 @@ contract EventManager is OwnableUpgradeable {
eventIdsByGroupId[_groupId].push(_newEventId);
groupIdByEventId[_newEventId] = _groupId;

emit CreateEvent(msg.sender, _newEventId);
emit CreateEvent(_msgSender(), _newEventId);
}

function getEventRecordCount() public view returns (uint256) {
Expand Down
10 changes: 10 additions & 0 deletions hardhat/contracts/IMintNFT.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ interface IMintNFT {
string memory _secretPhrase
) external;

function getCountOfParticipation(uint256 _groupId, address _address)
external
view
returns (uint256);

function isHoldingEventNFTByAddress(
address _address,
uint256 _eventId
) external view returns (bool);

function name() external view returns (string memory);

function owner() external view returns (address);
Expand Down
8 changes: 8 additions & 0 deletions hardhat/contracts/MintNFT.sol
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,14 @@ contract MintNFT is
isHoldingEventNFT[Hashing.hashingAddressUint256(_addr, _eventId)];
}

function getCountOfParticipation(
uint256 _groupId,
address _address
) public view returns (uint256) {
bytes32 groupHash = Hashing.hashingAddressUint256(_address, _groupId);
return countOfParticipation[groupHash];
}

function setEventInfo(
uint256 _eventId,
uint256 _mintLimit,
Expand Down
9 changes: 8 additions & 1 deletion hardhat/test/EventManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ describe("EventManager", function () {
const deployedEventManagerContract: any = await upgrades.deployProxy(
eventManagerContractFactory,
[
"0xdCb93093424447bF4FE9Df869750950922F1E30B",
organizer.address,
relayer.address,
250000,
Expand All @@ -95,14 +96,14 @@ describe("EventManager", function () {
}
);
eventManager = await deployedEventManagerContract.deployed();

await eventManager.setMintNFTAddr(mintNFT.address);
await mintNFT.setEventManagerAddr(eventManager.address);

const { publicInputCalldata: _publicInputCalldata } =
await generateProof();
publicInputCalldata = _publicInputCalldata;
});

it("Should create", async () => {
// does not exist any groups
const groupsBeforeCreate = await eventManager.getGroups();
Expand All @@ -126,6 +127,7 @@ describe("EventManager", function () {

// revert if paused
await operationController.connect(organizer).pause();

await expect(
eventManager.createEventRecord(
groupsAfterCreate[0].groupId.toNumber(),
Expand Down Expand Up @@ -485,6 +487,7 @@ describe("EventManager", function () {
const deployedEventManagerContract: any = await upgrades.deployProxy(
eventManagerContractFactory,
[
"0xdCb93093424447bF4FE9Df869750950922F1E30B",
organizer.address,
relayer.address,
500000,
Expand Down Expand Up @@ -540,6 +543,7 @@ describe("EventManager", function () {
const deployedEventManagerContract: any = await upgrades.deployProxy(
eventManagerContractFactory,
[
"0xdCb93093424447bF4FE9Df869750950922F1E30B",
organizer.address,
relayer.address,
500000,
Expand Down Expand Up @@ -615,6 +619,7 @@ describe("EventManager", function () {
const deployedEventManagerContract: any = await upgrades.deployProxy(
eventManagerContractFactory,
[
"0xdCb93093424447bF4FE9Df869750950922F1E30B",
organizer.address,
relayer.address,
250000,
Expand Down Expand Up @@ -709,6 +714,7 @@ describe("EventManager", function () {
const deployedEventManagerContract = await upgrades.deployProxy(
eventManagerContractFactory,
[
"0xdCb93093424447bF4FE9Df869750950922F1E30B",
organizer.address,
relayer.address,
250000,
Expand Down Expand Up @@ -783,6 +789,7 @@ describe("EventManager", function () {
const deployedEventManagerContract: any = await upgrades.deployProxy(
eventManagerContractFactory,
[
"0xdCb93093424447bF4FE9Df869750950922F1E30B",
organizer.address,
relayer.address,
250000,
Expand Down
Loading

0 comments on commit fb3d12c

Please sign in to comment.