Skip to content

Commit

Permalink
refactor(excubiae): improve errors and save gas costs in case of pass…
Browse files Browse the repository at this point in the history
…() failing
  • Loading branch information
0xjei committed Jul 1, 2024
1 parent bb31966 commit b74bc41
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/excubiae/contracts/IExcubia.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ interface IExcubia {
error AccessDenied();

/// @notice Error thrown when the passerby has already passed the gate.
error AlreadyRegistered();
error AlreadyPassed();

/// @notice Sets the gate address.
/// @dev Only the owner can set the destination gate address.
Expand Down
6 changes: 3 additions & 3 deletions packages/excubiae/contracts/extensions/EASExcubia.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ contract EASExcubia is Excubia {
/// @param passerby The address of the entity attempting to pass the gate.
/// @param data Additional data required for the check (e.g., encoded attestation ID).
function _pass(address passerby, bytes calldata data) internal override {
super._pass(passerby, data);

bytes32 attestationId = abi.decode(data, (bytes32));

// Avoiding passing the gate twice using the same attestation.
if (registeredAttestations[attestationId]) revert AlreadyRegistered();
if (registeredAttestations[attestationId]) revert AlreadyPassed();

super._pass(passerby, data);

registeredAttestations[attestationId] = true;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/excubiae/contracts/extensions/ERC721Excubia.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ contract ERC721Excubia is Excubia {
/// @param passerby The address of the entity attempting to pass the gate.
/// @param data Additional data required for the check (e.g., encoded token ID).
function _pass(address passerby, bytes calldata data) internal override {
super._pass(passerby, data);

uint256 tokenId = abi.decode(data, (uint256));

// Avoiding passing the gate twice with the same token ID.
if (registeredTokenIds[tokenId]) revert AlreadyRegistered();
if (registeredTokenIds[tokenId]) revert AlreadyPassed();

super._pass(passerby, data);

registeredTokenIds[tokenId] = true;
}
Expand Down
8 changes: 5 additions & 3 deletions packages/excubiae/contracts/extensions/FreeForAllExcubia.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@ contract FreeForAllExcubia is Excubia {
/// @param passerby The address of the entity passing the gate.
/// @param data Additional data required for the pass (not used in this implementation).
function _pass(address passerby, bytes calldata data) internal override {
super._pass(passerby, data);

// Avoiding passing the gate twice with the same address.
if (registeredPassersby[passerby]) revert AlreadyRegistered();
if (registeredPassersby[passerby]) revert AlreadyPassed();

super._pass(passerby, data);

registeredPassersby[passerby] = true;
}

/// @notice Internal function to handle the gate protection logic.
/// @dev This function always returns true, signaling that any passerby is able to pass the gate.
/// @param passerby The address of the entity attempting to pass the gate.
/// @param data Additional data required for the check (e.g., encoded attestation ID).
/// @return True, allowing any passerby to pass the gate.
function _check(address passerby, bytes calldata data) internal view override returns (bool) {
super._check(passerby, data);
Expand Down
2 changes: 1 addition & 1 deletion packages/excubiae/test/EASExcubia.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ describe("EASExcubia", function () {
it("should prevent to pass twice", async () => {
await expect(
easExcubia.connect(gate).pass(signerAddress, validAttestationId)
).to.be.revertedWithCustomError(easExcubia, "AlreadyRegistered")
).to.be.revertedWithCustomError(easExcubia, "AlreadyPassed")
})
})
})
2 changes: 1 addition & 1 deletion packages/excubiae/test/ERC721Excubia.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ describe("ERC721Excubia", function () {
it("should prevent to pass twice", async () => {
await expect(
erc721Excubia.connect(gate).pass(signerAddress, encodedValidTokenId)
).to.be.revertedWithCustomError(erc721Excubia, "AlreadyRegistered")
).to.be.revertedWithCustomError(erc721Excubia, "AlreadyPassed")
})
})
})
2 changes: 1 addition & 1 deletion packages/excubiae/test/FreeForAllExcubia.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ describe("FreeForAllExcubia", function () {
await expect(
// `data` parameter value can be whatever (e.g., ZeroHash default).
freeForAllExcubia.connect(gate).pass(signerAddress, ZeroHash)
).to.be.revertedWithCustomError(freeForAllExcubia, "AlreadyRegistered")
).to.be.revertedWithCustomError(freeForAllExcubia, "AlreadyPassed")
})
})
})

0 comments on commit b74bc41

Please sign in to comment.