-
Notifications
You must be signed in to change notification settings - Fork 11
/
Franchiser.sol
211 lines (189 loc) · 7.6 KB
/
Franchiser.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.8.15;
import {IFranchiser} from "./interfaces/Franchiser/IFranchiser.sol";
import {FranchiserImmutableState} from "./base/FranchiserImmutableState.sol";
import {Owned} from "solmate/auth/Owned.sol";
import {EnumerableSet} from "openzeppelin-contracts/contracts/utils/structs/EnumerableSet.sol";
import {Address} from "openzeppelin-contracts/contracts/utils/Address.sol";
import {Clones} from "openzeppelin-contracts/contracts/proxy/Clones.sol";
import {SafeTransferLib, ERC20} from "solmate/utils/SafeTransferLib.sol";
import {IVotingToken} from "./interfaces/IVotingToken.sol";
contract Franchiser is IFranchiser, FranchiserImmutableState, Owned {
using EnumerableSet for EnumerableSet.AddressSet;
using Address for address;
using Clones for address;
using SafeTransferLib for ERC20;
/// @inheritdoc IFranchiser
uint96 public constant DECAY_FACTOR = 2;
/// @inheritdoc IFranchiser
Franchiser public immutable franchiserImplementation;
address private _delegator;
/// @inheritdoc IFranchiser
address public delegatee;
/// @inheritdoc IFranchiser
uint96 public maximumSubDelegatees;
EnumerableSet.AddressSet private _subDelegatees;
/// @inheritdoc IFranchiser
function delegator() public view returns (address) {
// if a delegator has explicitly been set, return it
if (_delegator != address(0)) return _delegator;
// otherwise, look it up from the owner
else if (owner != address(0)) return Franchiser(owner).delegatee();
// return 0 in the implementation contract
return address(0);
}
/// @inheritdoc IFranchiser
function subDelegatees() external view returns (address[] memory) {
return _subDelegatees.values();
}
/// @dev Reverts if called by any account other than the `delegatee`.
modifier onlyDelegatee() {
if (msg.sender != delegatee) revert NotDelegatee(msg.sender, delegatee);
_;
}
constructor(IVotingToken votingToken_)
FranchiserImmutableState(votingToken_)
Owned(address(0))
{
franchiserImplementation = Franchiser(address(this));
// this borks the implementation contract as desired,
// new instances should be cloned.
delegatee = address(1);
}
/// @inheritdoc IFranchiser
function initialize(
address delegator_,
address delegatee_,
uint96 maximumSubDelegatees_
) public {
// the following two conditions, along with the fact
// that delegatee is only set below (outside of the constructor),
// ensures that initialize can only be called once in clones
if (delegatee_ == address(0)) revert NoDelegatee();
if (delegatee != address(0)) revert AlreadyInitialized();
owner = msg.sender;
// only store the delegator if necessary
if (delegator_ != address(0)) _delegator = delegator_;
delegatee = delegatee_;
maximumSubDelegatees = maximumSubDelegatees_;
votingToken.delegate(delegatee_);
emit Initialized(
msg.sender,
// ensure that we return the delegator consistently
delegator(),
delegatee_,
maximumSubDelegatees_
);
}
/// @inheritdoc IFranchiser
function initialize(address delegatee_, uint96 maximumSubDelegatees_)
external
{
initialize(address(0), delegatee_, maximumSubDelegatees_);
}
function getSalt(address subDelegatee) private pure returns (bytes32) {
return bytes20(subDelegatee);
}
/// @inheritdoc IFranchiser
function getFranchiser(address subDelegatee)
public
view
returns (Franchiser)
{
return
Franchiser(
address(franchiserImplementation).predictDeterministicAddress(
getSalt(subDelegatee),
address(this)
)
);
}
/// @inheritdoc IFranchiser
function subDelegate(address subDelegatee, uint256 amount)
public
onlyDelegatee
returns (Franchiser franchiser)
{
franchiser = getFranchiser(subDelegatee);
if (!_subDelegatees.contains(subDelegatee)) {
if (_subDelegatees.length() == maximumSubDelegatees)
revert CannotExceedMaximumSubDelegatees(maximumSubDelegatees);
assert(_subDelegatees.add(subDelegatee));
if (!address(franchiser).isContract()) {
// deploy a new contract if necessary
address(franchiserImplementation).cloneDeterministic(
getSalt(subDelegatee)
);
franchiser.initialize(
subDelegatee,
maximumSubDelegatees / DECAY_FACTOR
);
}
emit SubDelegateeActivated(subDelegatee);
}
ERC20(address(votingToken)).safeTransfer(address(franchiser), amount);
}
/// @inheritdoc IFranchiser
function subDelegateMany(
address[] calldata subDelegatees_,
uint256[] calldata amounts
) external returns (Franchiser[] memory franchisers) {
if (subDelegatees_.length != amounts.length)
revert ArrayLengthMismatch(subDelegatees_.length, amounts.length);
franchisers = new Franchiser[](subDelegatees_.length);
unchecked {
for (uint256 i = 0; i < subDelegatees_.length; i++)
franchisers[i] = subDelegate(subDelegatees_[i], amounts[i]);
}
}
/// @inheritdoc IFranchiser
function unSubDelegate(address subDelegatee) external onlyDelegatee {
_unSubDelegate(subDelegatee, false);
}
/// @dev Must only set assumeExistence to true when the subDelegatee exists
/// and is already a subDelegatee. This saves gas in recall.
function _unSubDelegate(address subDelegatee, bool assumeExistence)
private
{
Franchiser franchiser = getFranchiser(subDelegatee);
if (assumeExistence || _subDelegatees.contains(subDelegatee)) {
assert(_subDelegatees.remove(subDelegatee));
franchiser.recall(address(this));
emit SubDelegateeDeactivated(subDelegatee);
}
// this condition can only be reached if unSubDelegate is called with a subDelegatee
// that has a franchiser contract but isn't currently active - when this is the case,
// calling recall is a no-op if the franchiser doesn't have tokens, so it's fine,
// but in the very odd case that the franchiser has received voting tokens out of
// band, this will retrieve them silently, which is also fine
else if (address(franchiser).isContract())
franchiser.recall(address(this));
}
/// @inheritdoc IFranchiser
function unSubDelegateMany(address[] calldata subDelegatees_)
external
onlyDelegatee
{
unchecked {
for (uint256 i = 0; i < subDelegatees_.length; i++)
_unSubDelegate(subDelegatees_[i], false);
}
}
/// @inheritdoc IFranchiser
function recall(address to) external onlyOwner {
uint256 numberOfSubDelegatees = _subDelegatees.length();
while (numberOfSubDelegatees != 0) {
unchecked {
_unSubDelegate(
// ordering isn't consistent across removals, but this works
_subDelegatees.at(--numberOfSubDelegatees),
true
);
}
}
ERC20(address(votingToken)).safeTransfer(
to,
votingToken.balanceOf(address(this))
);
}
}