Skip to content

Commit

Permalink
fix: dont doublecount group size
Browse files Browse the repository at this point in the history
  • Loading branch information
abdulla-cb committed Sep 6, 2024
1 parent 4c2f19d commit b336319
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 15 deletions.
75 changes: 75 additions & 0 deletions onchain/script/DemoBigEvent.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import {Script, console} from "forge-std/Script.sol";
import {EventRegistry} from "../src/EventRegistry.sol";
import {ITicket} from "../src/interfaces/ITicket.sol";

contract RunBigEvent is Script {
EventRegistry public eventRegistry;

function setUp() public {
eventRegistry = new EventRegistry();
}

function run() public {
// Create the event so the sale starts in one week
bytes32 eventId = eventRegistry.registerEvent(
"Noasis",
"Noasis announce their first tour in 16 years. These will be their only shows, and could be one of the biggest live moments and hottest tickets of the decade.",
"Neaton Park, Nanchester",
uint32(block.timestamp + 7 days),
uint32(block.timestamp + 365 days),
uint32(block.timestamp + 365 days),
60_000,
4
);

// Get lots of people to apply!
uint256 i = 0;
for (; i < 50_000; i++) {
address[] memory noFriends = new address[](0);
vm.prank(address(uint160(i)));
eventRegistry.requestTicket(eventId, noFriends);
}
for (; i < 100_000; i++) {
address[] memory oneFriend = new address[](1);
vm.prank(address(uint160(i)));
oneFriend[0] = address(uint160(i++));
eventRegistry.requestTicket(eventId, oneFriend);
}
for (; i < 120_000; i++) {
address[] memory twoFriends = new address[](2);
vm.prank(address(uint160(i)));
twoFriends[0] = address(uint160(i++));
twoFriends[1] = address(uint160(i++));
eventRegistry.requestTicket(eventId, twoFriends);
}
for (; i < 150_000; i++) {
address[] memory threeFriends = new address[](3);
vm.prank(address(uint160(i)));
threeFriends[0] = address(uint160(i++));
threeFriends[1] = address(uint160(i++));
threeFriends[2] = address(uint160(i++));
eventRegistry.requestTicket(eventId, threeFriends);
}

// The ticket sale will be open now
vm.warp(block.timestamp + 10 days);

// Do the roll
eventRegistry.issueTickets(eventId);

for (i = 0; i < 150_000; i++) {
vm.prank(address(uint160(i)));
try eventRegistry.claimTickets(eventId) {}
catch {
// they didn't apply for tickets...
}
}

ITicket ticket = eventRegistry.getTicketContract(eventId);
uint256 tokensIssued = uint256(vm.load(address(ticket), bytes32(uint256(6))));
console.log(tokensIssued);
}
}
16 changes: 4 additions & 12 deletions onchain/src/EventRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,7 @@ contract EventRegistry is IEventRegistry {

// maybe you got lucky... maybe you didn't
// this function might overflow - need to add checks
if (
checkSeed(
seed, ticketBallotAllocation[eventId], eventInfo.maxEventCapacity, groupFriends[groupId].length + 1
)
) {
if (checkSeed(seed, ticketBallotAllocation[eventId], eventInfo.maxEventCapacity)) {
_distributeTickets(eventId, msg.sender, groupFriends[groupId]);
}
}
Expand All @@ -157,11 +153,7 @@ contract EventRegistry is IEventRegistry {
revert TicketSaleHasntStarted();
}
bytes32 seed = keccak256(abi.encode(ticketBallotSeed[eventId], groupId));
if (
checkSeed(
seed, ticketBallotAllocation[eventId], eventInfo.maxEventCapacity, groupFriends[groupId].length + 1
)
) {
if (checkSeed(seed, ticketBallotAllocation[eventId], eventInfo.maxEventCapacity)) {
return groupFriends[groupId].length + 1;
}
return 0;
Expand All @@ -181,12 +173,12 @@ contract EventRegistry is IEventRegistry {
}
}

function checkSeed(bytes32 seed, uint256 ballotedAllocation, uint256 maxEventCapacity, uint256 usersInGroup)
function checkSeed(bytes32 seed, uint256 ballotedAllocation, uint256 maxEventCapacity)
internal
pure
returns (bool)
{
return (uint256(seed) % ballotedAllocation < maxEventCapacity * usersInGroup);
return (uint256(seed) % ballotedAllocation < maxEventCapacity);
}

function getEventById(bytes32 eventId) external view returns (EventInformation memory) {
Expand Down
6 changes: 3 additions & 3 deletions onchain/src/Ticket.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ contract Ticket is ERC721 {
error OnlyEventRegistry();

constructor(string memory _name, bytes32 _eventId)
ERC721(_name, LibString.concat("TICKIT-",LibString.toHexStringNoPrefix(uint256(_eventId)).slice(0, 8)))
ERC721(_name, LibString.concat("TICKIT-", LibString.toHexStringNoPrefix(uint256(_eventId)).slice(0, 8)))
{
eventRegistry = IEventRegistry(msg.sender);
eventId = _eventId;
Expand Down Expand Up @@ -47,8 +47,8 @@ contract Ticket is ERC721 {
'{"name": "Ticket #',
LibString.toString(tokenId),
'", "description": "',
eventInfo.description,
'", "image": "data:image/svg+xml;base64,',
eventInfo.description,
'", "image": "data:image/svg+xml;base64,',
Base64.encode(bytes(output)),
'"}'
)
Expand Down

0 comments on commit b336319

Please sign in to comment.