-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathSubscribePaidMw.sol
164 lines (139 loc) · 5.22 KB
/
SubscribePaidMw.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
// 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 { ISubscribeMiddleware } from "../../interfaces/ISubscribeMiddleware.sol";
import { ICyberEngine } from "../../interfaces/ICyberEngine.sol";
import { Constants } from "../../libraries/Constants.sol";
import { FeeMw } from "../base/FeeMw.sol";
/**
* @title Subscribe Paid Middleware
* @author CyberConnect
* @notice This contract is a middleware to only allow users to subscribe when they pay a certain fee to the profile owner.
*/
contract SubscribePaidMw is ISubscribeMiddleware, FeeMw {
using SafeERC20 for IERC20;
/*//////////////////////////////////////////////////////////////
MODIFIERS
//////////////////////////////////////////////////////////////*/
modifier onlyValidNamespace() {
require(_namespace == msg.sender, "ONLY_VALID_NAMESPACE");
_;
}
/*//////////////////////////////////////////////////////////////
EVENT
//////////////////////////////////////////////////////////////*/
event SubscribePaidMwSet(
address indexed namespace,
uint256 indexed profileId,
uint256 indexed amount,
address recipient,
address currency,
bool nftRequired,
address nftAddress
);
/*//////////////////////////////////////////////////////////////
STATES
//////////////////////////////////////////////////////////////*/
struct PaidSubscribeData {
uint256 amount;
address recipient;
address currency;
bool nftRequired;
address nftAddress;
}
mapping(uint256 => PaidSubscribeData) internal _paidSubscribeData;
address internal _namespace;
/*//////////////////////////////////////////////////////////////
CONSTRUCTOR
//////////////////////////////////////////////////////////////*/
constructor(address treasury, address namespace) FeeMw(treasury) {
_namespace = namespace;
}
/*//////////////////////////////////////////////////////////////
EXTERNAL
//////////////////////////////////////////////////////////////*/
/**
* @inheritdoc ISubscribeMiddleware
* @notice Stores the parameters for setting up the paid subscribe middleware, checks if the amount, recipient, and
* currency is valid and approved, and whether a special NFT is needed to subscribe
*/
function setSubscribeMwData(uint256 profileId, bytes calldata data)
external
override
onlyValidNamespace
returns (bytes memory)
{
(
uint256 amount,
address recipient,
address currency,
bool nftRequired,
address nftAddress
) = abi.decode(data, (uint256, address, address, bool, address));
require(amount != 0, "INVALID_AMOUNT");
require(recipient != address(0), "INVALID_ADDRESS");
require(_currencyAllowed(currency), "CURRENCY_NOT_ALLOWED");
_paidSubscribeData[profileId].amount = amount;
_paidSubscribeData[profileId].recipient = recipient;
_paidSubscribeData[profileId].currency = currency;
_paidSubscribeData[profileId].nftRequired = nftRequired;
_paidSubscribeData[profileId].nftAddress = nftAddress;
emit SubscribePaidMwSet(
_namespace,
profileId,
amount,
recipient,
currency,
nftRequired,
nftAddress
);
return new bytes(0);
}
/**
* @inheritdoc ISubscribeMiddleware
* @notice Checks if the subscriber has the required NFT, then transfers the amount required from the subscriber to the treasury
*/
function preProcess(
uint256 profileId,
address subscriber,
address,
bytes calldata
) external override onlyValidNamespace {
address currency = _paidSubscribeData[profileId].currency;
uint256 amount = _paidSubscribeData[profileId].amount;
uint256 treasuryCollected = (amount * _treasuryFee()) /
Constants._MAX_BPS;
uint256 actualPaid = amount - treasuryCollected;
if (_paidSubscribeData[profileId].nftRequired) {
require(
ERC721(_paidSubscribeData[profileId].nftAddress).balanceOf(
subscriber
) > 0,
"NO_REQUIRED_NFT"
);
}
IERC20(currency).safeTransferFrom(
subscriber,
_paidSubscribeData[profileId].recipient,
actualPaid
);
if (treasuryCollected > 0) {
IERC20(currency).safeTransferFrom(
subscriber,
_treasuryAddress(),
treasuryCollected
);
}
}
/// @inheritdoc ISubscribeMiddleware
function postProcess(
uint256,
address,
address,
bytes calldata
) external {
// do nothing
}
}