-
Notifications
You must be signed in to change notification settings - Fork 65
Updated ERC1155 Discount Validator #100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a3140cf
Added multi-id erc1155 discount validator
stevieraykatz 50abbf8
lint
stevieraykatz 90427f4
test: add unit tests for ERC1155DiscountValidatorV2
abdulla-cb 4d6bf4a
Address reentrancy vuln with balance check via staticcall
stevieraykatz b7b711a
lint
stevieraykatz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
//SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.23; | ||
|
||
import {Address} from "@openzeppelin/contracts/utils/Address.sol"; | ||
import {IERC1155} from "@openzeppelin/contracts/token/ERC1155/IERC1155.sol"; | ||
|
||
import {IDiscountValidator} from "src/L2/interface/IDiscountValidator.sol"; | ||
|
@@ -13,18 +14,20 @@ import {IDiscountValidator} from "src/L2/interface/IDiscountValidator.sol"; | |
/// | ||
/// @author Coinbase (https://github.com/base-org/usernames) | ||
contract ERC1155DiscountValidatorV2 is IDiscountValidator { | ||
using Address for address; | ||
|
||
/// @notice The ERC1155 token contract to validate against. | ||
IERC1155 immutable token; | ||
address 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 token_ 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); | ||
constructor(address token_, uint256[] memory tokenIds) { | ||
token = token_; | ||
for (uint256 i; i < tokenIds.length; i++) { | ||
approvedTokenIds[tokenIds[i]] = true; | ||
} | ||
|
@@ -47,10 +50,17 @@ contract ERC1155DiscountValidatorV2 is IDiscountValidator { | |
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) { | ||
if (approvedTokenIds[id] && _getBalance(claimer, id) > 0) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
/// @notice Helper for staticcalling getBalance to avoid reentrancy vector. | ||
function _getBalance(address claimer, uint256 id) internal view returns (uint256) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is great. I like that you pulled in the OZ Address library for this instead of writing a new implementation. |
||
bytes memory data = abi.encodeWithSelector(IERC1155.balanceOf.selector, claimer, id); | ||
(bytes memory returnData) = token.functionStaticCall(data); | ||
return(abi.decode(returnData, (uint256))); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.