-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathBeneficiarySuperApp.sol
205 lines (173 loc) · 7.11 KB
/
BeneficiarySuperApp.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.16;
import {SuperAppBase} from "@superfluid-finance/ethereum-contracts/contracts/apps/SuperAppBase.sol";
import "./interfaces/ICFABeneficiary.sol";
import {SuperAppDefinitions} from "@superfluid-finance/ethereum-contracts/contracts/interfaces/superfluid/Definitions.sol";
import {ISuperAgreement} from "@superfluid-finance/ethereum-contracts/contracts/interfaces/superfluid/ISuperAgreement.sol";
import {IConstantFlowAgreementV1} from "@superfluid-finance/ethereum-contracts/contracts/interfaces/agreements/IConstantFlowAgreementV1.sol";
import {CFAv1Library} from "@superfluid-finance/ethereum-contracts/contracts/apps/CFAv1Library.sol";
import "../registry/interfaces/IPCOLicenseParamsStore.sol";
import {ISuperToken} from "@superfluid-finance/ethereum-contracts/contracts/interfaces/superfluid/ISuperToken.sol";
import {ISuperfluid} from "@superfluid-finance/ethereum-contracts/contracts/interfaces/superfluid/ISuperfluid.sol";
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC20/utils/SafeERC20Upgradeable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC20/IERC20Upgradeable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "@superfluid-finance/ethereum-contracts/contracts/apps/SuperTokenV1Library.sol";
contract BeneficiarySuperApp is
SuperAppBase,
ICFABeneficiary,
Initializable,
OwnableUpgradeable
{
using CFAv1Library for CFAv1Library.InitData;
using SuperTokenV1Library for ISuperToken;
CFAv1Library.InitData private cfaV1;
IPCOLicenseParamsStore internal paramsStore;
/// @notice Timestamp of last deletion from each beacon proxy
mapping(address => uint256) public lastDeletion;
/// @notice Beneficiary of funds.
address public beneficiary;
function initialize(
IPCOLicenseParamsStore paramsStore_,
address beneficiary_,
string calldata registrationKey
) external initializer {
__Ownable_init();
require(
beneficiary_ != address(0x0),
"BeneficiarySuperApp: Beneficiary cannot be 0x0"
);
paramsStore = paramsStore_;
beneficiary = beneficiary_;
ISuperfluid host = paramsStore.getHost();
cfaV1 = CFAv1Library.InitData(
host,
IConstantFlowAgreementV1(
address(
host.getAgreementClass(
keccak256(
"org.superfluid-finance.agreements.ConstantFlowAgreement.v1"
)
)
)
)
);
// Approve beneficiary to transfer payment token
ISuperToken paymentToken = paramsStore.getPaymentToken();
SafeERC20Upgradeable.safeIncreaseAllowance(
IERC20Upgradeable(address(paymentToken)),
beneficiary,
type(uint256).max
);
uint256 configWord = SuperAppDefinitions.APP_LEVEL_FINAL |
SuperAppDefinitions.BEFORE_AGREEMENT_CREATED_NOOP |
SuperAppDefinitions.AFTER_AGREEMENT_CREATED_NOOP |
SuperAppDefinitions.BEFORE_AGREEMENT_UPDATED_NOOP |
SuperAppDefinitions.AFTER_AGREEMENT_UPDATED_NOOP |
SuperAppDefinitions.BEFORE_AGREEMENT_TERMINATED_NOOP;
host.registerAppWithKey(configWord, registrationKey);
}
/// @notice Params Store
function getParamsStore() external view returns (IPCOLicenseParamsStore) {
return paramsStore;
}
/// @notice Set Params Store
function setParamsStore(IPCOLicenseParamsStore paramsStore_)
external
onlyOwner
{
paramsStore = paramsStore_;
}
/// @notice Beneficiary
function getBeneficiary() external view returns (address) {
return beneficiary;
}
/// @notice Set Beneficiary
function setBeneficiary(address beneficiary_) external onlyOwner {
require(
beneficiary_ != address(0x0),
"BeneficiarySuperApp: Beneficiary cannot be 0x0"
);
address oldBeneficiary = beneficiary;
beneficiary = beneficiary_;
// Revoke old beneficiary to transfer payment token
ISuperToken paymentToken = paramsStore.getPaymentToken();
SafeERC20Upgradeable.safeDecreaseAllowance(
IERC20Upgradeable(address(paymentToken)),
oldBeneficiary,
type(uint256).max
);
// Approve beneficiary to transfer payment token
SafeERC20Upgradeable.safeIncreaseAllowance(
IERC20Upgradeable(address(paymentToken)),
beneficiary,
type(uint256).max
);
}
/// @notice Get last deletion for sender
function getLastDeletion(address sender)
external
view
override
returns (uint256)
{
return lastDeletion[sender];
}
/**
* @notice Set last deletion of beacon proxy to now
* @param beaconProxy Beacon proxy
*/
function _setLastDeletion(address beaconProxy) internal {
lastDeletion[beaconProxy] = block.timestamp;
}
/// @notice Withdraw ERC20 funds in an emergency
function emergencyWithdraw(address token) external onlyOwner {
IERC20Upgradeable(token).transfer(msg.sender, IERC20Upgradeable(token).balanceOf(address(this)));
}
/// @notice Manually distribute flow
function distributeFlow(
ISuperToken token,
ISuperfluidPool pool,
int96 requestedFlowRate
) external onlyOwner {
token.distributeFlow(address(this), pool, requestedFlowRate);
}
/**************************************************************************
* SuperApp callbacks
*************************************************************************/
function afterAgreementTerminated(
ISuperToken superToken,
address agreementClass,
bytes32,
bytes calldata agreementData,
bytes calldata,
bytes calldata ctx
) external override onlyHost returns (bytes memory newCtx) {
// According to the app basic law, we should never revert in a termination callback
if (!_isSameToken(superToken) || !_isCFAv1(agreementClass)) return ctx;
(address _sender, ) = abi.decode(agreementData, (address, address));
_setLastDeletion(_sender);
return ctx;
}
function _isSameToken(ISuperToken superToken) private view returns (bool) {
ISuperToken paymentToken = paramsStore.getPaymentToken();
return address(superToken) == address(paymentToken);
}
function _isCFAv1(address agreementClass) private view returns (bool) {
return
ISuperAgreement(agreementClass).agreementType() ==
keccak256(
"org.superfluid-finance.agreements.ConstantFlowAgreement.v1"
);
}
modifier onlyHost() {
ISuperToken paymentToken = paramsStore.getPaymentToken();
ISuperfluid host = paramsStore.getHost();
require(
msg.sender == address(host),
"BeneficiarySuperApp: support only one host"
);
_;
}
}