-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRewardManager.sol
133 lines (116 loc) · 4.69 KB
/
RewardManager.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
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
contract RewardManager {
error NotEnoughFunds();
address public immutable REWARD_TREASURY_ADDRESS;
uint256 public immutable FEE_PER_EPOCH;
uint256 public immutable PROTOCOL_FEE_PERCENTAGE; // scaled by 8
mapping(uint256 => UserInstance) public idToUserInstance;
uint256 public nonce;
event NewUserInstance(uint256 indexed instanceId, uint256 numberOfEpochs);
event CollectTrainingFees(
uint256 indexed instanceId,
uint256 numberOfEpochs
);
struct UserInstance {
address[] clientAddress;
uint256 numberOfEpochs;
}
/// @notice Constructor for RewardManager
/// @param _rewardTreasuryAddress Address of reward treasury
/// @param _feePerEpoch Fee per epoch
/// @param _protocolFeePercentage Percentage of fee to be sent to protocol
constructor(
address _rewardTreasuryAddress,
uint256 _feePerEpoch,
uint256 _protocolFeePercentage
) {
REWARD_TREASURY_ADDRESS = _rewardTreasuryAddress;
FEE_PER_EPOCH = _feePerEpoch;
PROTOCOL_FEE_PERCENTAGE = _protocolFeePercentage;
}
/// @notice Function to create new user instance
/// @dev This function is used to create new user instance
/// @param _clientAddress Address of client
/// @param _numberOfEpochs Number of epochs for which client wants to train
/// @return Instance id of user
function newUserInstance(
address[] calldata _clientAddress,
uint256 _numberOfEpochs
) external returns (uint256) {
uint256 _instanceId = uint256(
keccak256(
abi.encodePacked(
nonce,
_clientAddress,
_numberOfEpochs,
block.timestamp
)
)
);
idToUserInstance[_instanceId] = UserInstance(
_clientAddress,
_numberOfEpochs
);
nonce++;
emit NewUserInstance(_instanceId, _numberOfEpochs);
return _instanceId;
}
/// @notice Function to collect training fees
/// @dev This function is used to collect training fees
/// @param _instanceId Instance id of user
function collectTrainingFees(uint256 _instanceId) external payable {
uint256 _numberOfEpochs = idToUserInstance[_instanceId].numberOfEpochs;
address[] memory _clientAddress = idToUserInstance[_instanceId]
.clientAddress;
uint256 trainingFee = _numberOfEpochs * FEE_PER_EPOCH;
if (msg.value < trainingFee) {
revert NotEnoughFunds();
}
uint256 protocolFee = (trainingFee * PROTOCOL_FEE_PERCENTAGE) / 1e8;
uint256 balanceBeforeFeeDistribution = address(this).balance;
uint256 clientFee = (trainingFee - protocolFee) / _clientAddress.length;
for (uint256 i = 0; i < _clientAddress.length; i++) {
(bool _success, ) = _clientAddress[i].call{value: clientFee}("");
require(_success, "Transfer failed.");
}
uint256 balanceAfterFeeDistribution = address(this).balance;
uint256 protocolFeeToTransfer = trainingFee -
(balanceBeforeFeeDistribution - balanceAfterFeeDistribution);
(bool success, ) = REWARD_TREASURY_ADDRESS.call{
value: protocolFeeToTransfer
}("");
require(success, "Transfer failed.");
emit CollectTrainingFees(_instanceId, _numberOfEpochs);
}
/// @notice Function to get reward treasury address
/// @dev This function is used to get reward treasury address
/// @return Address of reward treasury
function getRewardTreasuryAddress() external view returns (address) {
return REWARD_TREASURY_ADDRESS;
}
/// @notice Function to get fee per epoch
/// @dev This function is used to get fee per epoch
/// @return Fee per epoch
function getFeePerEpoch() external view returns (uint256) {
return FEE_PER_EPOCH;
}
/// @notice Function to get protocol fee percentage
/// @dev This function is used to get protocol fee percentage
/// @return Protocol fee percentage
function getProtocolFeePercentage() external view returns (uint256) {
return PROTOCOL_FEE_PERCENTAGE;
}
/// @notice Function to get user instance
/// @dev This function is used to get user instance
/// @param _instanceId Instance id of user
/// @return clientAddress Address of client
function getUserInstance(
uint256 _instanceId
) external view returns (address[] memory, uint256) {
return (
idToUserInstance[_instanceId].clientAddress,
idToUserInstance[_instanceId].numberOfEpochs
);
}
}