Skip to content

Commit 9dd9ef7

Browse files
committed
feat: Enhance CookieJar functionality with ERC1155 support and error handling
This commit introduces support for ERC1155 tokens in the CookieJar contract, allowing for more flexible withdrawal options. It adds new error handling for withdrawal attempts that have already been executed, improving user feedback. Additionally, the test suite is updated to include comprehensive tests for the new ERC1155 functionality, ensuring robust coverage and reliability. Co-authored-by: contact <[email protected]>
1 parent f9c114d commit 9dd9ef7

File tree

5 files changed

+229
-146
lines changed

5 files changed

+229
-146
lines changed

client/config/deployments.auto.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* It contains the latest factory addresses and V2 chain detection.
66
*
77
* Generated by: contracts/script/DeployLocal.s.sol
8-
* Last updated: 1759203080
8+
* Last updated: 1759257540
99
* Chain ID: 31337 (Local Development)
1010
*/
1111

@@ -23,10 +23,10 @@ export const DEPLOYMENTS: Record<number, DeploymentInfo> = {
2323
// Local Development (Anvil)
2424
31337: {
2525
chainId: 31337,
26-
factoryAddress: "0x019fC5F69832631B713350b8e7DF784a01d53c92",
26+
factoryAddress: "0x0F3D1fD06658007BDE049366C9d2091d243D2e52",
2727
isV2: true,
2828
blockNumber: 0,
29-
timestamp: 1759203080
29+
timestamp: 1759257540
3030
},
3131

3232
// Legacy V1 deployments (manually maintained)
@@ -69,6 +69,6 @@ export function getDeploymentInfo(chainId: number): DeploymentInfo | undefined {
6969
}
7070

7171
// Generation metadata
72-
export const GENERATED_AT = "1759203080"
72+
export const GENERATED_AT = "1759257540"
7373
export const GENERATOR = "Cookie Jar Local Development System v2.0"
7474
export const DEPLOYED_CHAIN = 31337

client/generated.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,7 @@ export const cookieJarAbi = [
899899
inputs: [{ name: 'chainId', internalType: 'uint256', type: 'uint256' }],
900900
name: 'UnsupportedChain',
901901
},
902+
{ type: 'error', inputs: [], name: 'WithdrawalAlreadyDone' },
902903
{
903904
type: 'error',
904905
inputs: [

contracts/src/CookieJar.sol

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,16 +360,19 @@ contract CookieJar is AccessControl, Pausable, ReentrancyGuard {
360360

361361
/// @notice Legacy function for allowlist mode (backwards compatibility)
362362
function withdrawAllowlistMode(uint256 amount, string calldata purpose) external {
363+
if (ACCESS_TYPE != CookieJarLib.AccessType.Allowlist) revert CookieJarLib.InvalidAccessType();
363364
withdraw(amount, purpose);
364365
}
365366

366367
/// @notice Legacy function for ERC721 mode (backwards compatibility)
367368
function withdrawWithErc721(uint256 amount, string calldata purpose) external {
369+
if (ACCESS_TYPE != CookieJarLib.AccessType.ERC721) revert CookieJarLib.InvalidAccessType();
368370
withdraw(amount, purpose);
369371
}
370372

371373
/// @notice Legacy function for ERC1155 mode (backwards compatibility)
372374
function withdrawWithErc1155(uint256 amount, string calldata purpose) external {
375+
if (ACCESS_TYPE != CookieJarLib.AccessType.ERC1155) revert CookieJarLib.InvalidAccessType();
373376
withdraw(amount, purpose);
374377
}
375378

@@ -479,7 +482,7 @@ contract CookieJar is AccessControl, Pausable, ReentrancyGuard {
479482

480483
// Validate one-time constraint
481484
if (ONE_TIME_WITHDRAWAL && totalWithdrawn[msg.sender] > 0) {
482-
revert CookieJarLib.NotAuthorized();
485+
revert CookieJarLib.WithdrawalAlreadyDone();
483486
}
484487

485488
// Validate period limits using user-specific rolling period
@@ -525,13 +528,37 @@ contract CookieJar is AccessControl, Pausable, ReentrancyGuard {
525528

526529
function _grantRoles(bytes32 role, address[] memory users) internal {
527530
for (uint256 i = 0; i < users.length; i++) {
531+
// Only add to allowlist if role wasn't previously granted
532+
bool hadRole = hasRole(role, users[i]);
528533
_grantRole(role, users[i]);
534+
// Update allowlist array for JAR_ALLOWLISTED role (only if newly granted)
535+
if (role == CookieJarLib.JAR_ALLOWLISTED && !hadRole) {
536+
allowlist.push(users[i]);
537+
}
529538
}
530539
}
531540

532541
function _revokeRoles(bytes32 role, address[] memory users) internal {
533542
for (uint256 i = 0; i < users.length; i++) {
534543
_revokeRole(role, users[i]);
544+
// Update allowlist array for JAR_ALLOWLISTED role
545+
if (role == CookieJarLib.JAR_ALLOWLISTED) {
546+
_removeFromAllowlist(users[i]);
547+
}
548+
}
549+
}
550+
551+
/// @notice Remove address from allowlist array
552+
/// @dev Helper function to maintain allowlist array consistency
553+
function _removeFromAllowlist(address user) internal {
554+
for (uint256 i = 0; i < allowlist.length; i++) {
555+
if (allowlist[i] == user) {
556+
// Move last element to the position of removed element
557+
allowlist[i] = allowlist[allowlist.length - 1];
558+
// Remove last element
559+
allowlist.pop();
560+
break;
561+
}
535562
}
536563
}
537564

0 commit comments

Comments
 (0)