/
ReliquaryWithFee.sol
435 lines (391 loc) · 15.5 KB
/
ReliquaryWithFee.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/// SPDX-License-Identifier: UNLICENSED
/// (c) Theori, Inc. 2022
/// All rights reserved
pragma solidity >=0.8.12;
import "@openzeppelin/contracts/interfaces/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "./Reliquary.sol";
struct FeeAccount {
uint64 subscriberUntilTime;
uint192 credits;
}
/**
* @title Holder of Relics and Artifacts with fees
* @author Theori, Inc.
* @notice The Reliquary is the heart of Relic. All issuers of Relics and Artifacts
* must be added to the Reliquary. Queries about Relics and Artifacts should
* be made to the Reliquary. This Reliquary may charge fees for proving or
* accessing facts.
*/
contract ReliquaryWithFee is Reliquary {
using SafeERC20 for IERC20;
bytes32 public constant CREDITS_ROLE = keccak256("CREDITS_ROLE");
bytes32 public constant SUBSCRIPTION_ROLE = keccak256("SUBSCRIPTION_ROLE");
/// Mapping of fact classes to fee infos about them
mapping(uint8 => FeeInfo) public factFees;
/// Information about fee accounts (credits & subscriptions)
mapping(address => FeeAccount) public feeAccounts;
/// External tokens and fee delegates for use by
mapping(uint256 => address) public feeExternals;
uint32 internal feeExternalsCount;
/// FeeInfo struct for block queries
FeeInfo public verifyBlockFeeInfo;
constructor() Reliquary() {}
/**
* @notice Query for associated information for a fact
* @param account The address to which the fact belongs
* @param factSig The unique signature identifying the fact
* @return exists whether or not a fact with the given signature
* is associated with the queried account
* @return version the prover version id that proved this fact
* @return data any associated fact data
* @dev A fee may be required based on the factSig
*/
function verifyFact(address account, FactSignature factSig)
external
payable
returns (
bool exists,
uint64 version,
bytes memory data
)
{
checkVerifyFactFee(factSig);
return _verifyFact(account, factSig);
}
/**
* @notice Query for some information for a fact
* @param account The address to which the fact belongs
* @param factSig The unique signature identifying the fact
* @return exists whether or not a fact with the given signature
* is associated with the queried account
* @return version the prover version id that proved this fact
* @dev A fee may be required based on the factSig
*/
function verifyFactVersion(address account, FactSignature factSig)
external
payable
returns (bool exists, uint64 version)
{
checkVerifyFactFee(factSig);
return _verifyFactVersion(account, factSig);
}
/**
* @notice Verify if a particular block had a particular hash
* @param verifier The block history verifier to use for the query
* @param hash The block hash in question
* @param num The block number to query
* @param proof Any witness information needed by the verifier
* @return boolean indication of whether or not the given block was
* proven to have the given hash.
* @dev A fee may be required based on the block in question
*/
function validBlockHash(
address verifier,
bytes32 hash,
uint256 num,
bytes calldata proof
) public payable returns (bool) {
checkValidBlockFee(num);
// validBlockHash is a view function, so it cannot modify state and is safe to call
return IBlockHistory(verifier).validBlockHash(hash, num, proof);
}
/**
* @notice Asserts that a particular block had a particular hash
* @param verifier The block history verifier to use for the query
* @param hash The block hash in question
* @param num The block number to query
* @param proof Any witness information needed by the verifier
* @dev Reverts if the given block was not proven to have the given hash.
* @dev A fee may be required based on the block in question
*/
function assertValidBlockHash(
address verifier,
bytes32 hash,
uint256 num,
bytes calldata proof
) external payable {
require(validBlockHash(verifier, hash, num, proof), "invalid block hash");
}
/**
* @notice Helper function to calculate fee for a given feeInfo struct
* @param feeInfo The FeeInfo struct in question
* @return The associated fee in wei
*/
function getFeeWei(FeeInfo memory feeInfo) internal pure returns (uint256) {
return feeInfo.feeWeiMantissa * (10**feeInfo.feeWeiExponent);
}
/**
* @notice Require that an appropriate fee is paid for queries
* @param sender The initiator of the query
* @param feeInfo The FeeInfo struct associated with the query
* @param data Opaque data that may be needed by downstream fee functions
* @dev Reverts if the fee is not sufficient
*/
function checkFeeInfo(
address sender,
FeeInfo memory feeInfo,
bytes memory data
) internal {
uint256 feeWei = getFeeWei(feeInfo);
if (feeInfo.flags & (1 << uint256(FeeFlags.FeeNone)) != 0) {
return;
}
if (feeInfo.flags & (1 << uint256(FeeFlags.FeeNative)) != 0) {
if (msg.value >= feeWei) {
return;
}
}
if (feeInfo.flags & (1 << uint256(FeeFlags.FeeCredits)) != 0) {
FeeAccount memory feeAccount = feeAccounts[sender];
// check if sender has a valid subscription
if (feeAccount.subscriberUntilTime > block.timestamp) {
return;
}
// otherwise subtract credits
if (feeAccount.credits >= feeInfo.feeCredits) {
feeAccount.credits -= feeInfo.feeCredits;
feeAccounts[sender] = feeAccount;
return;
}
}
if (feeInfo.flags & (1 << uint256(FeeFlags.FeeExternalDelegate)) != 0) {
require(feeInfo.feeExternalId != 0);
address delegate = feeExternals[feeInfo.feeExternalId];
require(delegate != address(0));
IFeeDelegate(delegate).checkFee{value: msg.value}(sender, data);
return;
}
if (feeInfo.flags & (1 << uint256(FeeFlags.FeeExternalToken)) != 0) {
require(feeInfo.feeExternalId != 0);
address token = feeExternals[feeInfo.feeExternalId];
require(token != address(0));
IERC20(token).safeTransferFrom(sender, address(this), feeWei);
return;
}
revert("insufficient fee");
}
/**
* @notice Require that an appropriate fee is paid for verify fact queries
* @param factSig The signature of the desired fact
* @dev Reverts if the fee is not sufficient
* @dev Only to be used internally
*/
function checkVerifyFactFee(FactSignature factSig) internal {
uint8 cls = Facts.toFactClass(factSig);
if (cls != Facts.NO_FEE) {
checkFeeInfo(msg.sender, factFees[cls], abi.encode("verifyFact", factSig));
}
}
/**
* @notice Require that an appropriate fee is paid for proving a fact
* @param sender The account wanting to prove a fact
* @dev The fee is derived from the prover which calls this function
* @dev Reverts if the fee is not sufficient
* @dev Only to be called by a prover
*/
function checkProveFactFee(address sender) external payable {
address prover = msg.sender;
ProverInfo memory proverInfo = provers[prover];
checkProver(proverInfo);
checkFeeInfo(sender, proverInfo.feeInfo, abi.encode("proveFact", prover));
}
/**
* @notice Require that an appropriate fee is paid for verify block queries
* @param blockNum The block number to verify
* @dev Reverts if the fee is not sufficient
* @dev Only to be used internally
*/
function checkValidBlockFee(uint256 blockNum) internal {
checkFeeInfo(msg.sender, verifyBlockFeeInfo, abi.encode("verifyBlock", blockNum));
}
// Functions to help callers determine how much fee they need to pay
/**
* @notice Determine the appropriate ETH fee to query a fact
* @param factSig The signature of the desired fact
* @return the fee in wei
* @dev Reverts if the fee is not to be paid in ETH
*/
function getVerifyFactNativeFee(FactSignature factSig) external view returns (uint256) {
uint8 cls = Facts.toFactClass(factSig);
if (cls == Facts.NO_FEE) {
return 0;
}
FeeInfo memory feeInfo = factFees[cls];
require(feeInfo.flags & (1 << uint256(FeeFlags.FeeNative)) != 0);
return getFeeWei(feeInfo);
}
/**
* @notice Determine the appropriate token fee to query a fact
* @param factSig The signature of the desired fact
* @return the fee in wei
* @dev Reverts if the fee is not to be paid in external tokens
*/
function getVerifyFactTokenFee(FactSignature factSig) external view returns (uint256) {
uint8 cls = Facts.toFactClass(factSig);
if (cls == Facts.NO_FEE) {
return 0;
}
FeeInfo memory feeInfo = factFees[cls];
require(feeInfo.flags & (1 << uint256(FeeFlags.FeeExternalToken)) != 0);
return getFeeWei(feeInfo);
}
/**
* @notice Determine the appropriate ETH fee to prove a fact
* @param prover The prover of the desired fact
* @return the fee in wei
* @dev Reverts if the fee is not to be paid in ETH
*/
function getProveFactNativeFee(address prover) external view returns (uint256) {
ProverInfo memory proverInfo = provers[prover];
checkProver(proverInfo);
require(proverInfo.feeInfo.flags & (1 << uint256(FeeFlags.FeeNative)) != 0);
return getFeeWei(proverInfo.feeInfo);
}
/**
* @notice Determine the appropriate token fee to prove a fact
* @param prover The prover of the desired fact
* @return the fee in wei
* @dev Reverts if the fee is not to be paid in external tokens
*/
function getProveFactTokenFee(address prover) external view returns (uint256) {
ProverInfo memory proverInfo = provers[prover];
checkProver(proverInfo);
require(proverInfo.feeInfo.flags & (1 << uint256(FeeFlags.FeeExternalToken)) != 0);
return getFeeWei(proverInfo.feeInfo);
}
/**
* @notice Check how many credits a given account possesses
* @param user The account in question
* @return The number of credits
*/
function credits(address user) public view returns (uint192) {
return feeAccounts[user].credits;
}
/**
* @notice Check if an account has an active subscription
* @param user The account in question
* @return True if the account is active, otherwise false
*/
function isSubscriber(address user) public view returns (bool) {
return feeAccounts[user].subscriberUntilTime > block.timestamp;
}
/**
* @notice Adds a new external fee provider (token or delegate) to a feeInfo
* @param feeInfo The feeInfo to update with this provider
* @dev Always appends to the global feeExternals
*/
function _setFeeExternalId(FeeInfo memory feeInfo, address feeExternal)
internal
returns (FeeInfo memory)
{
uint32 feeExternalId = 0;
if (feeExternal != address(0)) {
feeExternalsCount++;
feeExternalId = feeExternalsCount;
feeExternals[feeExternalId] = feeExternal;
}
feeInfo.feeExternalId = feeExternalId;
return feeInfo;
}
/**
* @notice Sets the FeeInfo for a particular fee class
* @param cls The fee class
* @param feeInfo The FeeInfo to use for the class
* @param feeExternal An external fee provider (token or delegate). If
* none is required, this should be set to 0.
*/
function setFactFee(
uint8 cls,
FeeInfo memory feeInfo,
address feeExternal
) external onlyRole(GOVERNANCE_ROLE) {
feeInfo = _setFeeExternalId(feeInfo, feeExternal);
factFees[cls] = feeInfo;
}
/**
* @notice Sets the FeeInfo for a particular prover
* @param prover The prover in question
* @param feeInfo The FeeInfo to use for the class
* @param feeExternal An external fee provider (token or delegate). If
* none is required, this should be set to 0.
*/
function setProverFee(
address prover,
FeeInfo memory feeInfo,
address feeExternal
) external onlyRole(GOVERNANCE_ROLE) {
ProverInfo memory proverInfo = provers[prover];
checkProver(proverInfo);
feeInfo = _setFeeExternalId(feeInfo, feeExternal);
proverInfo.feeInfo = feeInfo;
provers[prover] = proverInfo;
}
/**
* @notice Sets the FeeInfo for block verification
* @param feeInfo The FeeInfo to use for the class
* @param feeExternal An external fee provider (token or delegate). If
* none is required, this should be set to 0.
*/
function setValidBlockFee(FeeInfo memory feeInfo, address feeExternal)
external
onlyRole(GOVERNANCE_ROLE)
{
feeInfo = _setFeeExternalId(feeInfo, feeExternal);
verifyBlockFeeInfo = feeInfo;
}
/**
* @notice Add/update a subscription
* @param user The subscriber account to modify
* @param ts The new block timestamp at which the subscription expires
*/
function addSubscriber(address user, uint64 ts) external onlyRole(SUBSCRIPTION_ROLE) {
require(feeAccounts[user].subscriberUntilTime < ts);
feeAccounts[user].subscriberUntilTime = ts;
}
/**
* @notice Remove a subscription
* @param user The subscriber account to modify
*/
function removeSubscriber(address user) external onlyRole(SUBSCRIPTION_ROLE) {
feeAccounts[user].subscriberUntilTime = 0;
}
/**
* @notice Set credits for an account
* @param user The account for which credits should be set
* @param amount The credits that the account should be updated to hold
*/
function setCredits(address user, uint192 amount) external onlyRole(CREDITS_ROLE) {
feeAccounts[user].credits = amount;
}
/**
* @notice Add credits to an account
* @param user The account to which more credits should be granted
* @param amount The number of credits to be added
*/
function addCredits(address user, uint192 amount) external onlyRole(CREDITS_ROLE) {
feeAccounts[user].credits += amount;
}
/**
* @notice Remove credits from an account
* @param user The account from which credits should be removed
* @param amount The number of credits to be removed
*/
function removeCredits(address user, uint192 amount) external onlyRole(CREDITS_ROLE) {
feeAccounts[user].credits -= amount;
}
/**
* @notice Extract accumulated fees
* @param token The ERC20 token from which to extract fees. Or the 0 address for
* native ETH
* @param dest The address to which fees should be transferred
*/
function withdrawFees(address token, address payable dest) external onlyRole(GOVERNANCE_ROLE) {
require(dest != address(0));
if (token == address(0)) {
dest.transfer(address(this).balance);
} else {
IERC20(token).transfer(dest, IERC20(token).balanceOf(address(this)));
}
}
}