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

Custom entitlement factory to enable POAP gating #905

Closed
wants to merge 15 commits into from
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.23;

import {Deployer} from "contracts/scripts/common/Deployer.s.sol";
import {PoapEntitlementFactory} from "contracts/src/spaces/entitlements/poap/PoapEntitlementFactory.sol";

contract DeployPoapEntitlementFactory is Deployer {
function versionName() public pure override returns (string memory) {
return "DeployPoapEntitlementFactory";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the version name is the name of the contract you're deploying poapEntitlementFactory

}

function __deploy(address deployer) public override returns (address) {
vm.broadcast(deployer);

PoapEntitlementFactory factory = new PoapEntitlementFactory();

return address(factory);
}
}
48 changes: 48 additions & 0 deletions contracts/src/spaces/entitlements/poap/PoapEntitlement.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;

import {ICustomEntitlement} from "contracts/src/spaces/entitlements/ICustomEntitlement.sol";

interface IPOAP {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this to a different file IPoapEntitlement.sol

function balanceOf(address owner) external view returns (uint256);
function tokenDetailsOfOwnerByIndex(
address owner,
uint256 index
) external view returns (uint256 eventId, uint256 tokenId);
}

IPOAP constant poapContract = IPOAP(0x22C1f6050E56d2876009903609a2cC3fEf83B415);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would move this inside the contract


contract PoapEntitlement is ICustomEntitlement {
uint256 public immutable eventId;

constructor(uint256 _eventId) {
require(_eventId > 0, "Invalid event ID");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use if with custom error

eventId = _eventId;
}

function isEntitled(
address[] calldata wallets
) external view override returns (bool) {
for (uint256 i = 0; i < wallets.length; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cache wallet length

if (_hasEventPoap(wallets[i])) {
return true;
}
}
return false;
}

function _hasEventPoap(address user) internal view returns (bool) {
uint256 balance = poapContract.balanceOf(user);
for (uint256 j = 0; j < balance; j++) {
(uint256 ownedEventId, ) = poapContract.tokenDetailsOfOwnerByIndex(
user,
j
);
if (eventId == ownedEventId) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;

import {Factory} from "contracts/src/utils/Factory.sol";
import {PoapEntitlement} from "./PoapEntitlement.sol";

contract PoapEntitlementFactory is Factory {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

provide an interface for PoapEntitlementFactory in separate file

function deployEntitlement(uint256 eventId) public {
bytes32 salt = getSalt(eventId);
address predictedAddress = predictEntitlementAddress(eventId);

// Check if the contract is already deployed
uint32 size;
assembly {
size := extcodesize(predictedAddress)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use predictedAddress.code.length instead of assembly

}
require(size == 0, "Entitlement already deployed for this event");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use if with custom error


bytes memory initCode = abi.encodePacked(
type(PoapEntitlement).creationCode,
abi.encode(eventId)
);

address deployment = _deploy(initCode, salt);
if (deployment != predictedAddress) {
revert("Deployment failed");
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add an event that emits the deployed address

}

function getEntitlementAddress(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't think we need this function if we have predictEntitlementAddress or replace with this one

uint256 eventId
) public view returns (address) {
return predictEntitlementAddress(eventId);
}

function getSalt(uint256 eventId) public pure returns (bytes32) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make internal

return keccak256(abi.encodePacked("POAP", eventId));
}

function predictEntitlementAddress(
uint256 eventId
) public view returns (address) {
bytes32 initCodeHash = keccak256(
abi.encodePacked(type(PoapEntitlement).creationCode, abi.encode(eventId))
);
return _calculateDeploymentAddress(initCodeHash, getSalt(eventId));
}
}
1 change: 1 addition & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ sepolia = "${SEPOLIA_RPC_URL}"
base_sepolia = "${BASE_SEPOLIA_RPC_URL}"
base = "${BASE_RPC_URL}"
mainnet = "${MAINNET_RPC_URL}"
gnosis = "${GNOSIS_RPC_URL}"

[fmt]
line_length = 80
Expand Down
Loading