Skip to content

Commit

Permalink
Added multi-id erc1155 discount validator
Browse files Browse the repository at this point in the history
  • Loading branch information
stevieraykatz committed Oct 31, 2024
1 parent 4e64f80 commit a3140cf
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/L2/discounts/ERC1155DiscountValidatorV2.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//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) {
return true;
}
}
return false;
}

Check notice

Code scanning / Slither

Calls inside a loop Low

}

0 comments on commit a3140cf

Please sign in to comment.