-
Notifications
You must be signed in to change notification settings - Fork 2
/
PassportIssuer.sol
287 lines (238 loc) · 10.8 KB
/
PassportIssuer.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.10;
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {Initializable} from "@openzeppelin/contracts/proxy/utils/Initializable.sol";
import {ERC20} from "@rari-capital/solmate/src/tokens/ERC20.sol";
import {SafeTransferLib} from "@rari-capital/solmate/src/utils/SafeTransferLib.sol";
import {IVotingEscrow} from "../governance/IVotingEscrow.sol";
import {Passport} from "./Passport.sol";
/// @notice Manage the issuance of Passport tokens.
/// @author Nation3 (https://github.com/nation3/app/blob/main/contracts/contracts/passport/PassportIssuer.sol).
contract PassportIssuer is Initializable, Ownable {
/*///////////////////////////////////////////////////////////////
LIBRARIES
//////////////////////////////////////////////////////////////*/
using SafeTransferLib for ERC20;
using SafeTransferLib for IVotingEscrow;
/*///////////////////////////////////////////////////////////////
ERRORS
//////////////////////////////////////////////////////////////*/
error NotEligible();
error NonRevocable();
error InvalidSignature();
error PassportAlreadyIssued();
error PassportNotIssued();
error IssuanceIsDisabled();
error IssuancesLimitReached();
/*///////////////////////////////////////////////////////////////
EVENTS
//////////////////////////////////////////////////////////////*/
/// @dev Events inspired by EIP-4973
event Attest(address indexed _to, uint256 indexed _tokenId);
event Revoke(address indexed _to, uint256 indexed _tokenId);
event UpdateRequirements(uint256 claimRequiredBalance, uint256 revokeUnderBalance);
/*///////////////////////////////////////////////////////////////
STORAGE
//////////////////////////////////////////////////////////////*/
/// @notice The token used to lock.
IVotingEscrow public veToken;
/// @notice The token that issues.
Passport public passToken;
/// @notice Status of the passport issance.
bool public enabled;
/// @notice Limit of passports to issue, cannot be change after initialization.
uint256 public maxIssuances;
/// @notice Number of passports issued.
uint256 public totalIssued;
/// @notice Balance of veToken required to claim.
uint256 public claimRequiredBalance;
/// @notice Balance of veToken under which a passport is revocable.
uint256 public revokeUnderBalance;
/// @notice Agreement statement to sign before claim.
string public statement;
/// @notice Agreement terms URL to sign before claim.
string public termsURI;
/// @dev Domain separator hash on initialization.
bytes32 internal INITIAL_DOMAIN_SEPARATOR;
/// @dev Map the passport status of an account: (0) Not issued, (1) Issued, (2) Revoked.
mapping(address => uint8) internal _status;
/// @dev Passport id issued by account.
mapping(address => uint256) internal _passportId;
/*///////////////////////////////////////////////////////////////
MODIFIERS
//////////////////////////////////////////////////////////////*/
/// @notice Requires to be enabled before performing function.
modifier isEnabled() {
if (!enabled) revert IssuanceIsDisabled();
_;
}
/*///////////////////////////////////////////////////////////////
INITIALITION
//////////////////////////////////////////////////////////////*/
/// @notice Sets tokens and supply.
/// @param _veToken Lock token which balance is required to claim.
/// @param _passToken Passport token to mint.
/// @param _maxIssuances Maximum number of tokens that can be issued with this contract.
function initialize(
IVotingEscrow _veToken,
Passport _passToken,
uint256 _maxIssuances
) external initializer {
veToken = _veToken;
passToken = _passToken;
maxIssuances = _maxIssuances;
INITIAL_DOMAIN_SEPARATOR = computeDomainSeparator();
}
/*///////////////////////////////////////////////////////////////
VIEWS
//////////////////////////////////////////////////////////////*/
/// @notice Returns the status of an account: (0) Not issued, (1) Issued, (2) Revoked.
function passportStatus(address account) external view virtual returns (uint8) {
return _status[account];
}
/// @notice Returns passport id of a given account.
/// @param account Holder account of a passport.
/// @dev Revert if the account has no passport.
function passportId(address account) public view virtual returns (uint256) {
if (_status[account] == 0) revert PassportNotIssued();
return _passportId[account];
}
/*///////////////////////////////////////////////////////////////
USER ACTIONS
//////////////////////////////////////////////////////////////*/
/// @notice Claims a new passport token with signature validation.
/// @param v Signature version.
/// @param r Signature fragment.
/// @param s Signature fragment.
function claim(
uint8 v,
bytes32 r,
bytes32 s
) external virtual isEnabled {
if (totalIssued >= maxIssuances) revert IssuancesLimitReached();
if (_status[msg.sender] > 0) revert PassportAlreadyIssued();
if (veToken.balanceOf(msg.sender) < claimRequiredBalance) revert NotEligible();
verifySignature(v, r, s);
_issue(msg.sender);
}
/// @notice Allows caller to renounce to the passport.
function renounce() external virtual {
_revoke(msg.sender);
}
/// @notice Revokes the passport of a given account if it's not eligible anymore.
function revoke(address account) external virtual {
if (veToken.balanceOf(account) >= revokeUnderBalance) revert NonRevocable();
_revoke(account);
}
/*///////////////////////////////////////////////////////////////
ADMIN ACTIONS
//////////////////////////////////////////////////////////////*/
/// @notice Set requirements to claim & revoke.
/// @param _claimRequiredBalance Minimum amount of voting escrow tokens required for a new issuance.
/// @param _revokeUnderBalance Amount of voting escrow tokens under which a passport is revocable.
function setParams(uint256 _claimRequiredBalance, uint256 _revokeUnderBalance) external virtual onlyOwner {
claimRequiredBalance = _claimRequiredBalance;
revokeUnderBalance = _revokeUnderBalance;
emit UpdateRequirements(_claimRequiredBalance, _revokeUnderBalance);
}
/// @notice Updates issuance status.
/// @dev Can be used by the owner to halt the issuance of new passports.
function setEnabled(bool status) external virtual onlyOwner {
enabled = status;
}
/// @notice Sets the statement of the issuance agreement.
function setStatement(string memory _statement) external virtual onlyOwner {
statement = _statement;
}
/// @notice Sets the terms URI of the issuance agreement.
function setTermsURI(string memory _termsURI) external virtual onlyOwner {
termsURI = _termsURI;
}
/// @notice Allows the owner to revoke the passport of any account.
function adminRevoke(address account) external virtual onlyOwner {
_revoke(account);
}
/// @notice Allows the owner to withdraw any ERC20 sent to the contract.
/// @param token Token to withdraw.
/// @param to Recipient address of the tokens.
function recoverTokens(ERC20 token, address to) external virtual onlyOwner returns (uint256 amount) {
amount = token.balanceOf(address(this));
token.safeTransfer(to, amount);
}
/*///////////////////////////////////////////////////////////////
EIP-712 LOGIC
//////////////////////////////////////////////////////////////*/
/// @notice Returns the domain separator hash for EIP-712 signature.
function DOMAIN_SEPARATOR() public view virtual returns (bytes32) {
return INITIAL_DOMAIN_SEPARATOR != "" ? INITIAL_DOMAIN_SEPARATOR : computeDomainSeparator();
}
/// @notice Verify if sender has signed the contract agreement following EIP-712.
/// @dev Reverts on signature missmatch.
function verifySignature(
uint8 v,
bytes32 r,
bytes32 s
) public virtual {
// Unchecked because no math here
unchecked {
address signer = ecrecover(
keccak256(
abi.encodePacked(
"\x19\x01",
DOMAIN_SEPARATOR(),
keccak256(
abi.encode(
keccak256("Agreement(string statement,string termsURI)"),
keccak256(abi.encodePacked(statement)),
keccak256(abi.encodePacked(termsURI))
)
)
)
),
v,
r,
s
);
if (signer != msg.sender) revert InvalidSignature();
}
}
// @dev Compute the EIP-712 domain's separator of the contract.
function computeDomainSeparator() internal view virtual returns (bytes32) {
return
keccak256(
abi.encode(
keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"),
keccak256(bytes("PassportIssuer")),
keccak256("1"),
block.chainid,
address(this)
)
);
}
/*///////////////////////////////////////////////////////////////
INTERNAL LOGIC
//////////////////////////////////////////////////////////////*/
/// @dev Mints a new passport token for the recipient.
/// @param recipient Address to issue the passport to.
function _issue(address recipient) internal virtual {
// Mint a new passport to the recipient account
uint256 tokenId = passToken.safeMint(recipient);
// Realistically won't overflow;
unchecked {
totalIssued++;
}
_status[recipient] = 1;
_passportId[recipient] = tokenId;
emit Attest(recipient, tokenId);
}
/// @dev Burns the passport token of the account.
/// @param account Address of the account to revoke the passport to.
function _revoke(address account) internal virtual {
uint256 tokenId = passportId(account);
// Burn passport
passToken.burn(tokenId);
_status[account] = 2;
delete _passportId[account];
emit Revoke(account, tokenId);
}
}