-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTopUpRewardPrograms.sol
129 lines (112 loc) · 4.43 KB
/
TopUpRewardPrograms.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
// SPDX-FileCopyrightText: 2021 Lido <info@lido.fi>
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.4;
import "../TrustedCaller.sol";
import "../RewardProgramsRegistry.sol";
import "../interfaces/IFinance.sol";
import "../libraries/EVMScriptCreator.sol";
import "../interfaces/IEVMScriptFactory.sol";
/// @author psirex
/// @notice Creates EVMScript to top up balances of reward programs
contract TopUpRewardPrograms is TrustedCaller, IEVMScriptFactory {
// -------------
// ERRORS
// -------------
string private constant ERROR_LENGTH_MISMATCH = "LENGTH_MISMATCH";
string private constant ERROR_EMPTY_DATA = "EMPTY_DATA";
string private constant ERROR_ZERO_AMOUNT = "ZERO_AMOUNT";
string private constant ERROR_REWARD_PROGRAM_NOT_ALLOWED = "REWARD_PROGRAM_NOT_ALLOWED";
// -------------
// VARIABLES
// -------------
/// @notice Address of Aragon's Finance contract
IFinance public immutable finance;
/// @notice Address of reward token
address public immutable rewardToken;
/// @notice Address of RewardProgramsRegistry
RewardProgramsRegistry public immutable rewardProgramsRegistry;
// -------------
// CONSTRUCTOR
// -------------
constructor(
address _trustedCaller,
address _rewardProgramsRegistry,
address _finance,
address _rewardToken
) TrustedCaller(_trustedCaller) {
finance = IFinance(_finance);
rewardToken = _rewardToken;
rewardProgramsRegistry = RewardProgramsRegistry(_rewardProgramsRegistry);
}
// -------------
// EXTERNAL METHODS
// -------------
/// @notice Creates EVMScript to top up balances of reward programs
/// @param _creator Address who creates EVMScript
/// @param _evmScriptCallData Encoded tuple: (address[] _rewardPrograms, uint256[] _amounts) where
/// _rewardPrograms - addresses of reward programs to top up
/// _amounts - corresponding amount of tokens to transfer
function createEVMScript(address _creator, bytes memory _evmScriptCallData)
external
view
override
onlyTrustedCaller(_creator)
returns (bytes memory)
{
(address[] memory rewardPrograms, uint256[] memory amounts) =
_decodeEVMScriptCallData(_evmScriptCallData);
_validateEVMScriptCallData(rewardPrograms, amounts);
bytes[] memory evmScriptsCalldata = new bytes[](rewardPrograms.length);
for (uint256 i = 0; i < rewardPrograms.length; ++i) {
evmScriptsCalldata[i] = abi.encode(
rewardToken,
rewardPrograms[i],
amounts[i],
"Reward program top up"
);
}
return
EVMScriptCreator.createEVMScript(
address(finance),
finance.newImmediatePayment.selector,
evmScriptsCalldata
);
}
/// @notice Decodes call data used by createEVMScript method
/// @param _evmScriptCallData Encoded tuple: (address[] _rewardPrograms, uint256[] _amounts) where
/// _rewardPrograms - addresses of reward programs to top up
/// _amounts - corresponding amount of tokens to transfer
/// @return _rewardPrograms Addresses of reward programs to top up
/// @return _amounts Amounts of tokens to transfer
function decodeEVMScriptCallData(bytes memory _evmScriptCallData)
external
pure
returns (address[] memory _rewardPrograms, uint256[] memory _amounts)
{
return _decodeEVMScriptCallData(_evmScriptCallData);
}
// ------------------
// PRIVATE METHODS
// ------------------
function _validateEVMScriptCallData(address[] memory _rewardPrograms, uint256[] memory _amounts)
private
view
{
require(_rewardPrograms.length == _amounts.length, ERROR_LENGTH_MISMATCH);
require(_rewardPrograms.length > 0, ERROR_EMPTY_DATA);
for (uint256 i = 0; i < _rewardPrograms.length; ++i) {
require(_amounts[i] > 0, ERROR_ZERO_AMOUNT);
require(
rewardProgramsRegistry.isRewardProgram(_rewardPrograms[i]),
ERROR_REWARD_PROGRAM_NOT_ALLOWED
);
}
}
function _decodeEVMScriptCallData(bytes memory _evmScriptCallData)
private
pure
returns (address[] memory _rewardPrograms, uint256[] memory _amounts)
{
return abi.decode(_evmScriptCallData, (address[], uint256[]));
}
}