This repository has been archived by the owner on Dec 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
ERC20BalanceTier.sol
81 lines (74 loc) · 2.83 KB
/
ERC20BalanceTier.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// SPDX-License-Identifier: CAL
pragma solidity ^0.8.10;
import "@openzeppelin/contracts/proxy/utils/Initializable.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
//solhint-disable-next-line max-line-length
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {TierConstants} from "./libraries/TierConstants.sol";
import {ValueTier} from "./ValueTier.sol";
import {ITier} from "./ITier.sol";
import "./ReadOnlyTier.sol";
/// Constructor config for ERC20BalanceTier.
struct ERC20BalanceTierConfig {
/// The erc20 token contract to check the balance
/// of at `report` time.
IERC20 erc20;
/// 8 values corresponding to minimum erc20
/// balances for tier 1 through tier 8.
uint256[8] tierValues;
}
/// @title ERC20BalanceTier
/// @notice `ERC20BalanceTier` inherits from `ReadOnlyTier`.
///
/// There is no internal accounting, the balance tier simply reads the balance
/// of the user whenever `report` is called.
///
/// `setTier` always fails.
///
/// There is no historical information so each tier will either be `0x00000000`
/// or `0xFFFFFFFF` for the block number.
///
/// @dev The `ERC20BalanceTier` simply checks the current balance of an erc20
/// against tier values. As the current balance is always read from the erc20
/// contract directly there is no historical block data.
/// All tiers held at the current value will be `0x00000000` and tiers not held
/// will be `0xFFFFFFFF`.
/// `setTier` will error as this contract has no ability to write to the erc20
/// contract state.
///
/// Balance tiers are useful for:
/// - Claim contracts that don't require backdated tier holding
/// (be wary of griefing!).
/// - Assets that cannot be transferred, so are not eligible for
/// `ERC20TransferTier`.
/// - Lightweight, realtime checks that encumber the tiered address
/// as little as possible.
contract ERC20BalanceTier is ReadOnlyTier, ValueTier, Initializable {
/// Result of initialize.
event Initialize(
/// `msg.sender` of the initialize.
address sender,
/// erc20 to check balance of.
address erc20
);
/// The erc20 to check balances against.
IERC20 internal erc20;
/// @param config_ Initialize config.
function initialize(ERC20BalanceTierConfig memory config_)
external
initializer
{
initializeValueTier(config_.tierValues);
erc20 = config_.erc20;
emit Initialize(msg.sender, address(config_.erc20));
}
/// Report simply truncates all tiers above the highest value held.
/// @inheritdoc ITier
function report(address account_) public view override returns (uint256) {
return
TierReport.truncateTiersAbove(
TierConstants.ALWAYS,
valueToTier(tierValues(), erc20.balanceOf(account_))
);
}
}