Verified contractExactMIT
HolderAnyErc20BalanceEligibility
Address0xe3a3f0c1b8a021ea206a0157318648eb22229fb2
- Balance
0 ETH
- Transactions
1
- Compiler
solc 0.8.17
- Same code
Only this contract
Details
- Compiler
- solc 0.8.17+commit.8df45f5f
- Language
- solidity
- Optimization
- Yes, 1,000 runs
- EVM version
- london
- License
- MIT
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 26,330,365
- Creation transaction
- 0x3cb151b638…6da0a848bf
src/eligibility/HolderAnyErc20BalanceEligibility.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;
import {SSTORE2} from "solady/utils/SSTORE2.sol";
import {IEligibilityCondition} from "../interfaces/IEligibilityCondition.sol";
import {IOwnerOf} from "../interfaces/IOwnerOf.sol";
import {IBalanceOf} from "../interfaces/IBalanceOf.sol";
/// @notice Erfuellt, wenn IRGENDEIN Token aus einer fest hinterlegten Liste von
/// (ERC-20-Adresse, Schwelle)-Paaren seine eigene Schwelle im Wallet des AKTUELLEN
/// HALTERS erreicht -- ein einziges Requirement anstelle vieler gleichartiger
/// HolderErc20BalanceEligibility-Instanzen auf derselben Variante.
///
/// Gedacht fuer Quests wie Mouth::Joint ("haelt ca. 50 USD eines von 18 Liquid-Staking-/
/// Restaking-/Vote-Escrow-Token"), wo jedes Token seine EIGENE Schwelle hat und die
/// Verknuepfung ein ODER ist. Semantisch identisch zu N einzeln registrierten
/// HolderErc20BalanceEligibility(nft, [token], schwelle) -- der TraitManager verknuepft
/// mehrere Requirements derselben Variante ohnehin per ODER --, spart aber pro Token den
/// kompletten Overhead eines eigenen Contract-Aufrufs (kalter Account-Zugriff, Kalldaten,
/// eigener ownerOf()-Aufruf, eigener kalter SLOAD auf das tokens[]-Array).
///
/// @dev Bewusst KEIN `sum`-Verhalten: die Schwellen gehoeren zu je einem Token
/// (unterschiedliche Preise), Aufsummieren waere eine andere Quest. Wer "Summe ueber
/// mehrere Token" braucht, nimmt weiterhin HolderErc20BalanceEligibility.
/// @dev Jeder Token wird per LOW-LEVEL-Staticcall befragt, nicht per typisiertem Call.
/// Grund: bei N einzelnen Requirements faengt der TraitManager den Revert eines kaputten
/// Tokens pro Requirement ab (dieses Requirement = "nicht erfuellt", die anderen laufen
/// weiter). Ein typisierter Call hier wuerde dagegen das GESAMTE Sammel-Requirement
/// reissen und damit auch die 17 gesunden Token entwerten -- ein Verhaltensunterschied.
/// Ein Token ohne Code, mit Revert oder mit zu kurzer Antwort gilt deshalb genau wie
/// frueher als "dieses eine Token erfuellt nicht", ohne die anderen zu stoeren.
/// @dev Die Paare liegen als SSTORE2-Blob im Code eines Datencontracts (je 32 Bytes
/// Adresse + 32 Bytes Schwelle) statt als Storage-Arrays: EIN kalter Zugriff
/// (EXTCODECOPY) statt 2100 Gas pro gelesenem Slot.
contract HolderAnyErc20BalanceEligibility is IEligibilityCondition {
error LengthMismatch();
/// Gas-Deckel je Token-Aufruf (wie bei PastVotes/ENS/Deployer): ein Token, das sein gesamtes Gas verbrennt, darf weder die
/// uebrigen Token noch den restlichen Mint mitreissen. Ein echtes `balanceOf` braucht ca. 10-60k (Proxy mit
/// Share-Umrechnung), 200k sind grosszuegig.
uint256 private constant CALL_GAS = 200_000;
address public immutable nft;
/// @notice Anzahl der (Token, Schwelle)-Paare.
uint256 public immutable tokenCount;
/// @dev Datencontract: je Paar 32 Bytes Adresse, dann 32 Bytes Schwelle.
address private immutable _pairsPointer;
constructor(address nft_, address[] memory tokens_, uint256[] memory thresholds_) {
if (tokens_.length != thresholds_.length) revert LengthMismatch();
nft = nft_;
tokenCount = tokens_.length;
bytes memory blob = new bytes(tokens_.length * 64);
for (uint256 i = 0; i < tokens_.length; i++) {
uint256 token = uint256(uint160(tokens_[i]));
uint256 amount = thresholds_[i];
assembly {
let slot := add(add(blob, 32), mul(i, 64))
mstore(slot, token)
mstore(add(slot, 32), amount)
}
}
_pairsPointer = SSTORE2.write(blob);
}
/// @notice Token an `index` (Panic(0x32) bei zu grossem Index, wie ein Array-Getter).
function tokenAt(uint256 index) external view returns (address token) {
(address[] memory tokens,) = allPairs();
return tokens[index];
}
/// @notice Schwelle an `index` (Panic(0x32) bei zu grossem Index).
function thresholdAt(uint256 index) external view returns (uint256) {
(, uint256[] memory thresholds) = allPairs();
return thresholds[index];
}
function allPairs() public view returns (address[] memory tokens, uint256[] memory thresholds) {
bytes memory blob = SSTORE2.read(_pairsPointer);
uint256 n = blob.length / 64;
tokens = new address[](n);
thresholds = new uint256[](n);
for (uint256 i = 0; i < n; i++) {
uint256 token;
uint256 amount;
assembly {
let slot := add(add(blob, 32), mul(i, 64))
token := mload(slot)
amount := mload(add(slot, 32))
}
tokens[i] = address(uint160(token));
thresholds[i] = amount;
}
}
function isEligible(address, uint256 tokenId) external view returns (bool) {
address holder = IOwnerOf(nft).ownerOf(tokenId);
bytes memory blob = SSTORE2.read(_pairsPointer);
// Die Kalldaten sind fuer alle Token identisch -- einmal bauen statt 18-mal.
bytes memory payload = abi.encodeWithSelector(IBalanceOf.balanceOf.selector, holder);
uint256 n = blob.length / 64;
for (uint256 i = 0; i < n; i++) {
uint256 token;
uint256 amount;
assembly {
let slot := add(add(blob, 32), mul(i, 64))
token := mload(slot)
amount := mload(add(slot, 32))
}
// GAS (optE): gleicher Aufruf wie `token.staticcall{gas: CALL_GAS}(payload)`, aber ohne die komplette Rueckgabe in
// frischen Speicher zu kopieren (nur das erste Wort zaehlt). Gleiche Auswertung: Revert oder weniger als 32 Byte ->
// dieses Token erfuellt nicht; sonst erstes Wort >= Schwelle.
bool hit;
assembly {
if staticcall(CALL_GAS, token, add(payload, 32), mload(payload), 0x00, 0x20) {
if iszero(lt(returndatasize(), 32)) { hit := iszero(lt(mload(0x00), amount)) }
}
}
if (hit) return true;
}
return false;
}
}
lib/solady/src/utils/SSTORE2.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
/// @notice Read and write to persistent storage at a fraction of the cost.
/// @author Solady (https://github.com/vectorized/solady/blob/main/src/utils/SSTORE2.sol)
/// @author Saw-mon-and-Natalie (https://github.com/Saw-mon-and-Natalie)
/// @author Modified from Solmate (https://github.com/transmissions11/solmate/blob/main/src/utils/SSTORE2.sol)
/// @author Modified from 0xSequence (https://github.com/0xSequence/sstore2/blob/master/contracts/SSTORE2.sol)
/// @author Modified from SSTORE3 (https://github.com/Philogy/sstore3)
library SSTORE2 {
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CONSTANTS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev The proxy initialization code.
uint256 private constant _CREATE3_PROXY_INITCODE = 0x67363d3d37363d34f03d5260086018f3;
/// @dev Hash of the `_CREATE3_PROXY_INITCODE`.
/// Equivalent to `keccak256(abi.encodePacked(hex"67363d3d37363d34f03d5260086018f3"))`.
bytes32 internal constant CREATE3_PROXY_INITCODE_HASH =
0x21c35dbe1b344a2488cf3321d6ce542f8e9f305544ff09e4993a62319a497c1f;
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CUSTOM ERRORS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Unable to deploy the storage contract.
error DeploymentFailed();
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* WRITE LOGIC */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Writes `data` into the bytecode of a storage contract and returns its address.
function write(bytes memory data) internal returns (address pointer) {
/// @solidity memory-safe-assembly
assembly {
let n := mload(data) // Let `l` be `n + 1`. +1 as we prefix a STOP opcode.
/**
* ---------------------------------------------------+
* Opcode | Mnemonic | Stack | Memory |
* ---------------------------------------------------|
* 61 l | PUSH2 l | l | |
* 80 | DUP1 | l l | |
* 60 0xa | PUSH1 0xa | 0xa l l | |
* 3D | RETURNDATASIZE | 0 0xa l l | |
* 39 | CODECOPY | l | [0..l): code |
* 3D | RETURNDATASIZE | 0 l | [0..l): code |
* F3 | RETURN | | [0..l): code |
* 00 | STOP | | |
* ---------------------------------------------------+
* @dev Prefix the bytecode with a STOP opcode to ensure it cannot be called.
* Also PUSH2 is used since max contract size cap is 24,576 bytes which is less than 2 ** 16.
*/
// Do a out-of-gas revert if `n + 1` is more than 2 bytes.
mstore(add(data, gt(n, 0xfffe)), add(0xfe61000180600a3d393df300, shl(0x40, n)))
// Deploy a new contract with the generated creation code.
pointer := create(0, add(data, 0x15), add(n, 0xb))
if iszero(pointer) {
mstore(0x00, 0x30116425) // `DeploymentFailed()`.
revert(0x1c, 0x04)
}
mstore(data, n) // Restore the length of `data`.
}
}
/// @dev Writes `data` into the bytecode of a storage contract with `salt`
/// and returns its normal CREATE2 deterministic address.
function writeCounterfactual(bytes memory data, bytes32 salt)
internal
returns (address pointer)
{
/// @solidity memory-safe-assembly
assembly {
let n := mload(data)
// Do a out-of-gas revert if `n + 1` is more than 2 bytes.
mstore(add(data, gt(n, 0xfffe)), add(0xfe61000180600a3d393df300, shl(0x40, n)))
// Deploy a new contract with the generated creation code.
pointer := create2(0, add(data, 0x15), add(n, 0xb), salt)
if iszero(pointer) {
mstore(0x00, 0x30116425) // `DeploymentFailed()`.
revert(0x1c, 0x04)
}
mstore(data, n) // Restore the length of `data`.
}
}
/// @dev Writes `data` into the bytecode of a storage contract and returns its address.
/// This uses the so-called "CREATE3" workflow,
/// which means that `pointer` is agnostic to `data, and only depends on `salt`.
function writeDeterministic(bytes memory data, bytes32 salt)
internal
returns (address pointer)
{
/// @solidity memory-safe-assembly
assembly {
let n := mload(data)
mstore(0x00, _CREATE3_PROXY_INITCODE) // Store the `_PROXY_INITCODE`.
let proxy := create2(0, 0x10, 0x10, salt)
if iszero(proxy) {
mstore(0x00, 0x30116425) // `DeploymentFailed()`.
revert(0x1c, 0x04)
}
mstore(0x14, proxy) // Store the proxy's address.
// 0xd6 = 0xc0 (short RLP prefix) + 0x16 (length of: 0x94 ++ proxy ++ 0x01).
// 0x94 = 0x80 + 0x14 (0x14 = the length of an address, 20 bytes, in hex).
mstore(0x00, 0xd694)
mstore8(0x34, 0x01) // Nonce of the proxy contract (1).
pointer := keccak256(0x1e, 0x17)
// Do a out-of-gas revert if `n + 1` is more than 2 bytes.
mstore(add(data, gt(n, 0xfffe)), add(0xfe61000180600a3d393df300, shl(0x40, n)))
if iszero(
mul( // The arguments of `mul` are evaluated last to first.
extcodesize(pointer),
call(gas(), proxy, 0, add(data, 0x15), add(n, 0xb), codesize(), 0x00)
)
) {
mstore(0x00, 0x30116425) // `DeploymentFailed()`.
revert(0x1c, 0x04)
}
mstore(data, n) // Restore the length of `data`.
}
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* ADDRESS CALCULATIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Returns the initialization code hash of the storage contract for `data`.
/// Used for mining vanity addresses with create2crunch.
function initCodeHash(bytes memory data) internal pure returns (bytes32 hash) {
/// @solidity memory-safe-assembly
assembly {
let n := mload(data)
// Do a out-of-gas revert if `n + 1` is more than 2 bytes.
returndatacopy(returndatasize(), returndatasize(), gt(n, 0xfffe))
mstore(data, add(0x61000180600a3d393df300, shl(0x40, n)))
hash := keccak256(add(data, 0x15), add(n, 0xb))
mstore(data, n) // Restore the length of `data`.
}
}
/// @dev Equivalent to `predictCounterfactualAddress(data, salt, address(this))`
function predictCounterfactualAddress(bytes memory data, bytes32 salt)
internal
view
returns (address pointer)
{
pointer = predictCounterfactualAddress(data, salt, address(this));
}
/// @dev Returns the CREATE2 address of the storage contract for `data`
/// deployed with `salt` by `deployer`.
/// Note: The returned result has dirty upper 96 bits. Please clean if used in assembly.
function predictCounterfactualAddress(bytes memory data, bytes32 salt, address deployer)
internal
pure
returns (address predicted)
{
bytes32 hash = initCodeHash(data);
/// @solidity memory-safe-assembly
assembly {
// Compute and store the bytecode hash.
mstore8(0x00, 0xff) // Write the prefix.
mstore(0x35, hash)
mstore(0x01, shl(96, deployer))
mstore(0x15, salt)
predicted := keccak256(0x00, 0x55)
// Restore the part of the free memory pointer that has been overwritten.
mstore(0x35, 0)
}
}
/// @dev Equivalent to `predictDeterministicAddress(salt, address(this))`.
function predictDeterministicAddress(bytes32 salt) internal view returns (address pointer) {
pointer = predictDeterministicAddress(salt, address(this));
}
/// @dev Returns the "CREATE3" deterministic address for `salt` with `deployer`.
function predictDeterministicAddress(bytes32 salt, address deployer)
internal
pure
returns (address pointer)
{
/// @solidity memory-safe-assembly
assembly {
let m := mload(0x40) // Cache the free memory pointer.
mstore(0x00, deployer) // Store `deployer`.
mstore8(0x0b, 0xff) // Store the prefix.
mstore(0x20, salt) // Store the salt.
mstore(0x40, CREATE3_PROXY_INITCODE_HASH) // Store the bytecode hash.
mstore(0x14, keccak256(0x0b, 0x55)) // Store the proxy's address.
mstore(0x40, m) // Restore the free memory pointer.
// 0xd6 = 0xc0 (short RLP prefix) + 0x16 (length of: 0x94 ++ proxy ++ 0x01).
// 0x94 = 0x80 + 0x14 (0x14 = the length of an address, 20 bytes, in hex).
mstore(0x00, 0xd694)
mstore8(0x34, 0x01) // Nonce of the proxy contract (1).
pointer := keccak256(0x1e, 0x17)
}
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* READ LOGIC */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Equivalent to `read(pointer, 0, 2 ** 256 - 1)`.
function read(address pointer) internal view returns (bytes memory data) {
/// @solidity memory-safe-assembly
assembly {
data := mload(0x40)
let n := and(0xffffffffff, sub(extcodesize(pointer), 0x01))
extcodecopy(pointer, add(data, 0x1f), 0x00, add(n, 0x21))
mstore(data, n) // Store the length.
mstore(0x40, add(n, add(data, 0x40))) // Allocate memory.
}
}
/// @dev Equivalent to `read(pointer, start, 2 ** 256 - 1)`.
function read(address pointer, uint256 start) internal view returns (bytes memory data) {
/// @solidity memory-safe-assembly
assembly {
data := mload(0x40)
let n := and(0xffffffffff, sub(extcodesize(pointer), 0x01))
let l := sub(n, and(0xffffff, mul(lt(start, n), start)))
extcodecopy(pointer, add(data, 0x1f), start, add(l, 0x21))
mstore(data, mul(sub(n, start), lt(start, n))) // Store the length.
mstore(0x40, add(data, add(0x40, mload(data)))) // Allocate memory.
}
}
/// @dev Returns a slice of the data on `pointer` from `start` to `end`.
/// `start` and `end` will be clamped to the range `[0, args.length]`.
/// The `pointer` MUST be deployed via the SSTORE2 write functions.
/// Otherwise, the behavior is undefined.
/// Out-of-gas reverts if `pointer` does not have any code.
function read(address pointer, uint256 start, uint256 end)
internal
view
returns (bytes memory data)
{
/// @solidity memory-safe-assembly
assembly {
data := mload(0x40)
if iszero(lt(end, 0xffff)) { end := 0xffff }
let d := mul(sub(end, start), lt(start, end))
extcodecopy(pointer, add(data, 0x1f), start, add(d, 0x01))
if iszero(and(0xff, mload(add(data, d)))) {
let n := sub(extcodesize(pointer), 0x01)
returndatacopy(returndatasize(), returndatasize(), shr(40, n))
d := mul(gt(n, start), sub(d, mul(gt(end, n), sub(end, n))))
}
mstore(data, d) // Store the length.
mstore(add(add(data, 0x20), d), 0) // Zeroize the slot after the bytes.
mstore(0x40, add(add(data, 0x40), d)) // Allocate memory.
}
}
}
src/interfaces/IBalanceOf.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;
/// @notice Minimales gemeinsames Interface fuer balanceOf(address) -- identisch fuer
/// ERC-20 und ERC-721, deshalb hier nur einmal definiert statt zwei fast identische
/// Interfaces zu pflegen.
interface IBalanceOf {
function balanceOf(address account) external view returns (uint256);
}
src/interfaces/IEligibilityCondition.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;
/// @notice Ja/Nein-Gegenstueck zu ITraitCondition: statt eine Tier-Stufe liefert
/// isEligible() nur "erfuellt der Halter diese eine Bedingung gerade oder nicht" --
/// Grundlage fuer TraitManagers freischaltbare Wahl-Traits (equipTrait).
interface IEligibilityCondition {
function isEligible(address tba, uint256 tokenId) external view returns (bool);
}
src/interfaces/IOwnerOf.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;
/// @notice Minimales gemeinsames Interface fuer ownerOf(uint256) -- fuer JEDEN ERC-721,
/// nicht nur die eigene Kollektion (anders als INftTraitView, das zusaetzlich tbaOf()
/// verlangt und damit nur fuer den eigenen NFT-Contract passt). Gleiches Minimalprinzip
/// wie IBalanceOf.
interface IOwnerOf {
function ownerOf(uint256 tokenId) external view returns (address);
}
Compiler settingsShow
{
"viaIR": false,
"metadata": {
"bytecodeHash": "ipfs",
"useLiteralContent": false
},
"optimizer": {
"runs": 1000,
"enabled": true
},
"evmVersion": "london",
"remappings": [
"solady/=lib/solady/src/",
"forge-std/=lib/forge-std/src/",
"ERC721A/=lib/ERC721A/contracts/",
"solmate/=lib/solmate/src/",
"utility-contracts/=lib/utility-contracts/src/",
"openzeppelin-contracts/=lib/openzeppelin-contracts/contracts/",
"seadrop/=lib/seadrop/",
"ds-test/=/home/philipp/Schreibtisch/Projekte/Crypto/Auditors-EigeneKollektion/contract/contracts/lib/solmate/lib/ds-test/src/"
]
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.