This repository has been archived by the owner on Sep 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
GelatoProviders.sol
391 lines (348 loc) · 13.6 KB
/
GelatoProviders.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.10;
pragma experimental ABIEncoderV2;
import {IGelatoProviders, TaskSpec} from "./interfaces/IGelatoProviders.sol";
import {GelatoSysAdmin} from "./GelatoSysAdmin.sol";
import {Address} from "../external/Address.sol";
import {GelatoString} from "../libraries/GelatoString.sol";
import {Math} from "../external/Math.sol";
import {SafeMath} from "../external/SafeMath.sol";
import {IGelatoProviderModule} from "../gelato_provider_modules/IGelatoProviderModule.sol";
import {ProviderModuleSet} from "../libraries/ProviderModuleSet.sol";
import {
Condition, Action, Operation, DataFlow, Provider, Task, TaskReceipt
} from "./interfaces/IGelatoCore.sol";
import {IGelatoCondition} from "../gelato_conditions/IGelatoCondition.sol";
/// @title GelatoProviders
/// @notice Provider Management API - Whitelist TaskSpecs
/// @dev Find all NatSpecs inside IGelatoProviders
abstract contract GelatoProviders is IGelatoProviders, GelatoSysAdmin {
using Address for address payable; /// for sendValue method
using GelatoString for string;
using ProviderModuleSet for ProviderModuleSet.Set;
using SafeMath for uint256;
// This is only for internal use by hashTaskSpec()
struct NoDataAction {
address addr;
Operation operation;
DataFlow dataFlow;
bool value;
bool termsOkCheck;
}
uint256 public constant override NO_CEIL = type(uint256).max;
mapping(address => uint256) public override providerFunds;
mapping(address => uint256) public override executorStake;
mapping(address => address) public override executorByProvider;
mapping(address => uint256) public override executorProvidersCount;
// The Task-Spec Gas-Price-Ceil => taskSpecGasPriceCeil
mapping(address => mapping(bytes32 => uint256)) public override taskSpecGasPriceCeil;
mapping(address => ProviderModuleSet.Set) internal _providerModules;
// GelatoCore: canSubmit
function isTaskSpecProvided(address _provider, TaskSpec memory _taskSpec)
public
view
override
returns(string memory)
{
if (taskSpecGasPriceCeil[_provider][hashTaskSpec(_taskSpec)] == 0)
return "TaskSpecNotProvided";
return OK;
}
// IGelatoProviderModule: GelatoCore canSubmit & canExec
function providerModuleChecks(
address _userProxy,
Provider memory _provider,
Task memory _task
)
public
view
override
returns(string memory)
{
if (!isModuleProvided(_provider.addr, _provider.module))
return "InvalidProviderModule";
if (_userProxy != _provider.addr) {
IGelatoProviderModule providerModule = IGelatoProviderModule(
_provider.module
);
try providerModule.isProvided(_userProxy, _provider.addr, _task)
returns(string memory res)
{
return res;
} catch {
return "GelatoProviders.providerModuleChecks";
}
} else return OK;
}
// GelatoCore: canSubmit
function isTaskProvided(
address _userProxy,
Provider memory _provider,
Task memory _task
)
public
view
override
returns(string memory res)
{
TaskSpec memory _taskSpec = _castTaskToSpec(_task);
res = isTaskSpecProvided(_provider.addr, _taskSpec);
if (res.startsWithOK())
return providerModuleChecks(_userProxy, _provider, _task);
}
// GelatoCore canExec Gate
function providerCanExec(
address _userProxy,
Provider memory _provider,
Task memory _task,
uint256 _gelatoGasPrice
)
public
view
override
returns(string memory)
{
if (_userProxy == _provider.addr) {
if (_task.selfProviderGasPriceCeil < _gelatoGasPrice)
return "SelfProviderGasPriceCeil";
} else {
bytes32 taskSpecHash = hashTaskSpec(_castTaskToSpec(_task));
if (taskSpecGasPriceCeil[_provider.addr][taskSpecHash] < _gelatoGasPrice)
return "taskSpecGasPriceCeil-OR-notProvided";
}
return providerModuleChecks(_userProxy, _provider, _task);
}
// Provider Funding
function provideFunds(address _provider) public payable override {
require(msg.value > 0, "GelatoProviders.provideFunds: zero value");
uint256 newProviderFunds = providerFunds[_provider].add(msg.value);
emit LogFundsProvided(_provider, msg.value, newProviderFunds);
providerFunds[_provider] = newProviderFunds;
}
// Unprovide funds
function unprovideFunds(uint256 _withdrawAmount)
public
override
returns(uint256 realWithdrawAmount)
{
uint256 previousProviderFunds = providerFunds[msg.sender];
realWithdrawAmount = Math.min(_withdrawAmount, previousProviderFunds);
uint256 newProviderFunds = previousProviderFunds - realWithdrawAmount;
// Effects
providerFunds[msg.sender] = newProviderFunds;
// Interaction
msg.sender.sendValue(realWithdrawAmount);
emit LogFundsUnprovided(msg.sender, realWithdrawAmount, newProviderFunds);
}
// Called by Providers
function providerAssignsExecutor(address _newExecutor) public override {
address currentExecutor = executorByProvider[msg.sender];
// CHECKS
require(
currentExecutor != _newExecutor,
"GelatoProviders.providerAssignsExecutor: already assigned."
);
if (_newExecutor != address(0)) {
require(
isExecutorMinStaked(_newExecutor),
"GelatoProviders.providerAssignsExecutor: isExecutorMinStaked()"
);
}
// EFFECTS: Provider reassigns from currentExecutor to newExecutor (or no executor)
if (currentExecutor != address(0)) executorProvidersCount[currentExecutor]--;
executorByProvider[msg.sender] = _newExecutor;
if (_newExecutor != address(0)) executorProvidersCount[_newExecutor]++;
emit LogProviderAssignedExecutor(msg.sender, currentExecutor, _newExecutor);
}
// Called by Executors
function executorAssignsExecutor(address _provider, address _newExecutor) public override {
address currentExecutor = executorByProvider[_provider];
// CHECKS
require(
currentExecutor == msg.sender,
"GelatoProviders.executorAssignsExecutor: msg.sender is not assigned executor"
);
require(
currentExecutor != _newExecutor,
"GelatoProviders.executorAssignsExecutor: already assigned."
);
// Checks at the same time if _nexExecutor != address(0)
require(
isExecutorMinStaked(_newExecutor),
"GelatoProviders.executorAssignsExecutor: isExecutorMinStaked()"
);
// EFFECTS: currentExecutor reassigns to newExecutor
executorProvidersCount[currentExecutor]--;
executorByProvider[_provider] = _newExecutor;
executorProvidersCount[_newExecutor]++;
emit LogExecutorAssignedExecutor(_provider, currentExecutor, _newExecutor);
}
// (Un-)provide Condition Action Combos at different Gas Price Ceils
function provideTaskSpecs(TaskSpec[] memory _taskSpecs) public override {
for (uint i; i < _taskSpecs.length; i++) {
if (_taskSpecs[i].gasPriceCeil == 0) _taskSpecs[i].gasPriceCeil = NO_CEIL;
bytes32 taskSpecHash = hashTaskSpec(_taskSpecs[i]);
setTaskSpecGasPriceCeil(taskSpecHash, _taskSpecs[i].gasPriceCeil);
emit LogTaskSpecProvided(msg.sender, taskSpecHash);
}
}
function unprovideTaskSpecs(TaskSpec[] memory _taskSpecs) public override {
for (uint i; i < _taskSpecs.length; i++) {
bytes32 taskSpecHash = hashTaskSpec(_taskSpecs[i]);
require(
taskSpecGasPriceCeil[msg.sender][taskSpecHash] != 0,
"GelatoProviders.unprovideTaskSpecs: redundant"
);
delete taskSpecGasPriceCeil[msg.sender][taskSpecHash];
emit LogTaskSpecUnprovided(msg.sender, taskSpecHash);
}
}
function setTaskSpecGasPriceCeil(bytes32 _taskSpecHash, uint256 _gasPriceCeil)
public
override
{
uint256 currentTaskSpecGasPriceCeil = taskSpecGasPriceCeil[msg.sender][_taskSpecHash];
require(
currentTaskSpecGasPriceCeil != _gasPriceCeil,
"GelatoProviders.setTaskSpecGasPriceCeil: Already whitelisted with gasPriceCeil"
);
taskSpecGasPriceCeil[msg.sender][_taskSpecHash] = _gasPriceCeil;
emit LogTaskSpecGasPriceCeilSet(
msg.sender,
_taskSpecHash,
currentTaskSpecGasPriceCeil,
_gasPriceCeil
);
}
// Provider Module
function addProviderModules(IGelatoProviderModule[] memory _modules) public override {
for (uint i; i < _modules.length; i++) {
require(
!isModuleProvided(msg.sender, _modules[i]),
"GelatoProviders.addProviderModules: redundant"
);
_providerModules[msg.sender].add(_modules[i]);
emit LogProviderModuleAdded(msg.sender, _modules[i]);
}
}
function removeProviderModules(IGelatoProviderModule[] memory _modules) public override {
for (uint i; i < _modules.length; i++) {
require(
isModuleProvided(msg.sender, _modules[i]),
"GelatoProviders.removeProviderModules: redundant"
);
_providerModules[msg.sender].remove(_modules[i]);
emit LogProviderModuleRemoved(msg.sender, _modules[i]);
}
}
// Batch (un-)provide
function multiProvide(
address _executor,
TaskSpec[] memory _taskSpecs,
IGelatoProviderModule[] memory _modules
)
public
payable
override
{
if (msg.value != 0) provideFunds(msg.sender);
if (_executor != address(0)) providerAssignsExecutor(_executor);
provideTaskSpecs(_taskSpecs);
addProviderModules(_modules);
}
function multiUnprovide(
uint256 _withdrawAmount,
TaskSpec[] memory _taskSpecs,
IGelatoProviderModule[] memory _modules
)
public
override
{
if (_withdrawAmount != 0) unprovideFunds(_withdrawAmount);
unprovideTaskSpecs(_taskSpecs);
removeProviderModules(_modules);
}
// Provider Liquidity
function minExecProviderFunds(uint256 _gelatoMaxGas, uint256 _gelatoGasPrice)
public
view
override
returns(uint256)
{
uint256 maxExecTxCost = (EXEC_TX_OVERHEAD + _gelatoMaxGas) * _gelatoGasPrice;
return maxExecTxCost + (maxExecTxCost * totalSuccessShare) / 100;
}
function isProviderLiquid(
address _provider,
uint256 _gelatoMaxGas,
uint256 _gelatoGasPrice
)
public
view
override
returns(bool)
{
return minExecProviderFunds(_gelatoMaxGas, _gelatoGasPrice) <= providerFunds[_provider];
}
// An Executor qualifies and remains registered for as long as he has minExecutorStake
function isExecutorMinStaked(address _executor) public view override returns(bool) {
return executorStake[_executor] >= minExecutorStake;
}
// Providers' Executor Assignment
function isExecutorAssigned(address _executor) public view override returns(bool) {
return executorProvidersCount[_executor] != 0;
}
// Helper fn that can also be called to query taskSpecHash off-chain
function hashTaskSpec(TaskSpec memory _taskSpec) public view override returns(bytes32) {
NoDataAction[] memory noDataActions = new NoDataAction[](_taskSpec.actions.length);
for (uint i = 0; i < _taskSpec.actions.length; i++) {
NoDataAction memory noDataAction = NoDataAction({
addr: _taskSpec.actions[i].addr,
operation: _taskSpec.actions[i].operation,
dataFlow: _taskSpec.actions[i].dataFlow,
value: _taskSpec.actions[i].value == 0 ? false : true,
termsOkCheck: _taskSpec.actions[i].termsOkCheck
});
noDataActions[i] = noDataAction;
}
return keccak256(abi.encode(_taskSpec.conditions, noDataActions));
}
// Providers' Module Getters
function isModuleProvided(address _provider, IGelatoProviderModule _module)
public
view
override
returns(bool)
{
return _providerModules[_provider].contains(_module);
}
function providerModules(address _provider)
external
view
override
returns(IGelatoProviderModule[] memory)
{
return _providerModules[_provider].enumerate();
}
// Internal helper for is isTaskProvided() and providerCanExec
function _castTaskToSpec(Task memory _task)
private
pure
returns(TaskSpec memory taskSpec)
{
taskSpec = TaskSpec({
conditions: _stripConditionData(_task.conditions),
actions: _task.actions,
gasPriceCeil: 0 // default: provider can set gasPriceCeil dynamically.
});
}
function _stripConditionData(Condition[] memory _conditionsWithData)
private
pure
returns(IGelatoCondition[] memory conditionInstances)
{
conditionInstances = new IGelatoCondition[](_conditionsWithData.length);
for (uint i; i < _conditionsWithData.length; i++)
conditionInstances[i] = _conditionsWithData[i].inst;
}
}