-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathCollectPaidMw.sol
197 lines (167 loc) · 6.53 KB
/
CollectPaidMw.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
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.8.14;
import { IERC20 } from "openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
import { ERC721 } from "../../dependencies/solmate/ERC721.sol";
import { SafeERC20 } from "openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol";
import { IEssenceMiddleware } from "../../interfaces/IEssenceMiddleware.sol";
import { ICyberEngine } from "../../interfaces/ICyberEngine.sol";
import { IProfileNFT } from "../../interfaces/IProfileNFT.sol";
import { Constants } from "../../libraries/Constants.sol";
import { FeeMw } from "../base/FeeMw.sol";
/**
* @title Collect Paid Middleware
* @author CyberConnect
* @notice This contract is a middleware to only allow users to collect when they pay a certain fee to the essence owner.
* the essence creator can choose to set rules including whether collecting this essence require previous subscription and
* has a total supply.
*/
contract CollectPaidMw is IEssenceMiddleware, FeeMw {
using SafeERC20 for IERC20;
/*//////////////////////////////////////////////////////////////
MODIFIERS
//////////////////////////////////////////////////////////////*/
modifier onlyValidNamespace() {
require(_namespace == msg.sender, "ONLY_VALID_NAMESPACE");
_;
}
/*//////////////////////////////////////////////////////////////
EVENT
//////////////////////////////////////////////////////////////*/
event CollectPaidMwSet(
address indexed namespace,
uint256 indexed profileId,
uint256 indexed essenceId,
uint256 totalSupply,
uint256 amount,
address recipient,
address currency,
bool subscribeRequired
);
/*//////////////////////////////////////////////////////////////
STATES
//////////////////////////////////////////////////////////////*/
struct CollectPaidData {
uint256 totalSupply;
uint256 currentCollect;
uint256 amount;
address recipient;
address currency;
bool subscribeRequired;
}
mapping(uint256 => mapping(uint256 => CollectPaidData))
internal _paidEssenceData;
address internal _namespace;
/*//////////////////////////////////////////////////////////////
CONSTRUCTOR
//////////////////////////////////////////////////////////////*/
constructor(address treasury, address namespace) FeeMw(treasury) {
_namespace = namespace;
}
/*//////////////////////////////////////////////////////////////
EXTERNAL
//////////////////////////////////////////////////////////////*/
/**
* @inheritdoc IEssenceMiddleware
* @notice Stores the parameters for setting up the paid essence middleware, checks if the amount, recipient, and
* currency is valid and approved
*/
function setEssenceMwData(
uint256 profileId,
uint256 essenceId,
bytes calldata data
) external override onlyValidNamespace returns (bytes memory) {
(
uint256 totalSupply,
uint256 amount,
address recipient,
address currency,
bool subscribeRequired
) = abi.decode(data, (uint256, uint256, address, address, bool));
require(amount != 0, "INVALID_AMOUNT");
require(recipient != address(0), "INVALID_ADDRESS");
require(_currencyAllowed(currency), "CURRENCY_NOT_ALLOWED");
_paidEssenceData[profileId][essenceId].totalSupply = totalSupply;
_paidEssenceData[profileId][essenceId].amount = amount;
_paidEssenceData[profileId][essenceId].recipient = recipient;
_paidEssenceData[profileId][essenceId].currency = currency;
_paidEssenceData[profileId][essenceId]
.subscribeRequired = subscribeRequired;
emit CollectPaidMwSet(
msg.sender,
profileId,
essenceId,
totalSupply,
amount,
recipient,
currency,
subscribeRequired
);
return new bytes(0);
}
/**
* @inheritdoc IEssenceMiddleware
* @notice Determines whether the collection requires prior subscription and whether there is a limit, and processes the transaction
* from the essence collector to the essence owner.
*/
function preProcess(
uint256 profileId,
uint256 essenceId,
address collector,
address,
bytes calldata
) external override onlyValidNamespace {
require(
_paidEssenceData[profileId][essenceId].totalSupply >
_paidEssenceData[profileId][essenceId].currentCollect,
"COLLECT_LIMIT_EXCEEDED"
);
require(tx.origin == collector, "NOT_FROM_COLLECTOR");
address currency = _paidEssenceData[profileId][essenceId].currency;
uint256 amount = _paidEssenceData[profileId][essenceId].amount;
uint256 treasuryCollected = (amount * _treasuryFee()) /
Constants._MAX_BPS;
uint256 actualPaid = amount - treasuryCollected;
if (_paidEssenceData[profileId][essenceId].subscribeRequired == true) {
require(
_checkSubscribe(_namespace, profileId, collector),
"NOT_SUBSCRIBED"
);
}
IERC20(currency).safeTransferFrom(
collector,
_paidEssenceData[profileId][essenceId].recipient,
actualPaid
);
if (treasuryCollected > 0) {
IERC20(currency).safeTransferFrom(
collector,
_treasuryAddress(),
treasuryCollected
);
}
_paidEssenceData[profileId][essenceId].currentCollect++;
}
/// @inheritdoc IEssenceMiddleware
function postProcess(
uint256,
uint256,
address,
address,
bytes calldata
) external {
// do nothing
}
/*//////////////////////////////////////////////////////////////
INTERNAL
//////////////////////////////////////////////////////////////*/
function _checkSubscribe(
address namespace,
uint256 profileId,
address collector
) internal view returns (bool) {
address essenceOwnerSubscribeNFT = IProfileNFT(namespace)
.getSubscribeNFT(profileId);
return (essenceOwnerSubscribeNFT != address(0) &&
ERC721(essenceOwnerSubscribeNFT).balanceOf(collector) != 0);
}
}