Skip to content
Merged
Changes from 2 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
56 changes: 56 additions & 0 deletions src/L2/discounts/ERC1155DiscountValidatorV2.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;

import {IERC1155} from "@openzeppelin/contracts/token/ERC1155/IERC1155.sol";

import {IDiscountValidator} from "src/L2/interface/IDiscountValidator.sol";

/// @title Discount Validator for: ERC1155 NFTs
///
/// @notice Implements an NFT ownership validator for a stored mapping of `approvedTokenIds` for an ERC1155
/// `token` contract.
/// IMPORTANT: This discount validator should only be used for "soul-bound" tokens.
///
/// @author Coinbase (https://github.com/base-org/usernames)
contract ERC1155DiscountValidatorV2 is IDiscountValidator {
/// @notice The ERC1155 token contract to validate against.
IERC1155 immutable token;

/// @notice The approved token Ids of the ERC1155 token contract.
mapping(uint256 tokenId => bool approved) approvedTokenIds;

/// @notice ERC1155 Discount Validator constructor.
///
/// @param tokenAddress The address of the token contract.
/// @param tokenIds The approved token ids the token `claimer` must hold.
constructor(address tokenAddress, uint256[] memory tokenIds) {
token = IERC1155(tokenAddress);
for (uint256 i; i < tokenIds.length; i++) {
approvedTokenIds[tokenIds[i]] = true;
}
}

/// @notice Required implementation for compatibility with IDiscountValidator.
///
/// @dev Encoded array of token Ids to check, set by `abi.encode(uint256[] ids)`
///
/// @param claimer the discount claimer's address.
/// @param validationData opaque bytes for performing the validation.
///
/// @return `true` if the validation data provided is determined to be valid for the specified claimer, else `false`.
function isValidDiscountRegistration(address claimer, bytes calldata validationData)
public
view
override
returns (bool)
{
uint256[] memory ids = abi.decode(validationData, (uint256[]));
for (uint256 i; i < ids.length; i++) {
uint256 id = ids[i];
if (approvedTokenIds[id] && token.balanceOf(claimer, id) > 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This technically adds a reentrancy risk, which should be mitigated by the fact that this a token that we are specifying.

Can we wrap this external call in a staticcall to remove any risk? It will also make it easier when we want to add more validators, nobody will have to review the balance function beforehand.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good catch, updated to include a staticcall helper method for balance checks.

return true;
}
}
return false;
}
}