-
Notifications
You must be signed in to change notification settings - Fork 6
/
FlapJob.sol
87 lines (71 loc) · 2.94 KB
/
FlapJob.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
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2021 Dai Foundation
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
pragma solidity 0.8.13;
import {IJob} from "./interfaces/IJob.sol";
interface SequencerLike {
function isMaster(bytes32 network) external view returns (bool);
}
interface VatLike {
function sin(address) external view returns (uint256);
}
interface VowLike {
function Sin() external view returns (uint256);
function Ash() external view returns (uint256);
function heal(uint256) external;
function flap() external;
}
/// @title Call flap when possible
contract FlapJob is IJob {
SequencerLike public immutable sequencer;
VatLike public immutable vat;
VowLike public immutable vow;
uint256 public immutable maxGasPrice;
// --- Errors ---
error NotMaster(bytes32 network);
error GasPriceTooHigh(uint256 gasPrice, uint256 maxGasPrice);
// --- Events ---
event Work(bytes32 indexed network);
constructor(address _sequencer, address _vat, address _vow, uint256 _maxGasPrice) {
sequencer = SequencerLike(_sequencer);
vat = VatLike(_vat);
vow = VowLike(_vow);
maxGasPrice = _maxGasPrice;
}
function work(bytes32 network, bytes calldata args) public {
if (!sequencer.isMaster(network)) revert NotMaster(network);
if (tx.gasprice > maxGasPrice) revert GasPriceTooHigh(tx.gasprice, maxGasPrice);
uint256 toHeal = abi.decode(args, (uint256));
if (toHeal > 0) vow.heal(toHeal);
vow.flap();
emit Work(network);
}
function workable(bytes32 network) external override returns (bool, bytes memory) {
if (!sequencer.isMaster(network)) return (false, bytes("Network is not master"));
bytes memory args;
uint256 unbackedTotal = vat.sin(address(vow));
uint256 unbackedVow = vow.Sin() + vow.Ash();
// Check if need to cancel out free unbacked debt with system surplus
uint256 toHeal = unbackedTotal > unbackedVow ? unbackedTotal - unbackedVow : 0;
args = abi.encode(toHeal);
try this.work(network, args) {
// Flap succeeds
return (true, args);
} catch {
// Can not flap -- carry on
}
return (false, bytes("Flap not possible"));
}
}