-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMidasFactory721.sol
215 lines (182 loc) · 6.71 KB
/
MidasFactory721.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
206
207
208
209
210
211
212
213
214
215
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;
import {LPToken} from "./LPToken.sol";
import {NoDelegateCall} from "./NoDelegateCall.sol";
import {IMidasPair721} from "./interfaces/IMidasPair721.sol";
import {IMidasFactory721} from "./interfaces/IMidasFactory721.sol";
import {IMidasFlashLoanCallback} from "./interfaces/IMidasFlashLoanCallback.sol";
import {IRoyaltyEngineV1} from "./interfaces/IRoyaltyEngineV1.sol";
import {ImmutableClone} from "./libraries/ImmutableClone.sol";
import {IERC721} from "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import {IERC721Enumerable} from "@openzeppelin/contracts/token/ERC721/extensions/IERC721Enumerable.sol";
/// @title Midas Pair Factory
/// @author midaswap
/// @notice Deploys Midaswap pairs and manages ownership
contract MidasFactory721 is IMidasFactory721, NoDelegateCall {
address private owner;
address private pairImplementation;
address private lptImplementation;
bool private createPairLock;
IRoyaltyEngineV1 private royaltyEngine;
mapping(address => mapping(address => address))
public
override getPairERC721;
mapping(address => mapping(address => address))
public
override getLPTokenERC721;
constructor(
address _royaltyEngine
) {
owner = msg.sender;
emit OwnerChanged(address(0), msg.sender);
royaltyEngine = IRoyaltyEngineV1(_royaltyEngine);
createPairLock = true;
}
function feeRecipient() external view returns (address _feeRecipient) {
_feeRecipient = owner;
}
/// @dev The function to create ERC721-ERC20 pair and the nonfungible lp Token contract
/// @param _tokenX The first input token address
/// @param _tokenY The second input token address
/// @return lpToken The address of lpToken
/// @return pair The address of Midas pair
function createERC721Pair(address _tokenX, address _tokenY)
external
override
noDelegateCall
returns (address lpToken, address pair)
{
require(createPairLock == false || msg.sender == owner);
require(_tokenX != _tokenY && _tokenY != address(0));
require(IERC721(_tokenX).supportsInterface(bytes4(0x80ac58cd)));
require(getPairERC721[_tokenX][_tokenY] == address(0));
lpToken = ImmutableClone.cloneDeterministic(
lptImplementation,
"",
keccak256(abi.encode(_tokenX, _tokenY, address(this)))
);
pair = ImmutableClone.cloneDeterministic(
pairImplementation,
abi.encodePacked(_tokenX, _tokenY, lpToken),
keccak256(abi.encode(_tokenX, _tokenY, lpToken))
);
IMidasPair721(pair).initialize();
LPToken(lpToken).initialize(
pair,
_tokenX,
_tokenY,
"MidasLPToken",
"MLPT"
);
getPairERC721[_tokenX][_tokenY] = pair;
getLPTokenERC721[_tokenX][_tokenY] = lpToken;
_setRoyaltyInfo(_tokenX, _tokenY, false);
emit PairCreated(_tokenX, _tokenY, pair, lpToken);
}
function _setRoyaltyInfo(address _tokenX, address _tokenY, bool isZero) internal {
address _pair = getPairERC721[_tokenX][_tokenY];
address payable[] memory _recipients;
uint256[] memory _shares;
if(isZero){
IMidasPair721(_pair).updateRoyalty(
uint128(0),
_recipients,
_shares
);
}else{
(_recipients, _shares) = royaltyEngine.getRoyaltyView(_tokenX, 1, 1e18);
if (_shares.length != 0) {
uint256 _shareSum;
for (uint256 i; i < _shares.length; ) {
_shareSum += _shares[i];
unchecked {
++i;
}
}
for (uint256 i; i < _shares.length; ) {
_shares[i] = (_shares[i] * 1e18) / _shareSum;
unchecked {
++i;
}
}
if(_shareSum > 5e16) _shareSum = 5e16;
IMidasPair721(_pair).updateRoyalty(
uint128(_shareSum),
_recipients,
_shares
);
} else {
IMidasPair721(_pair).updateRoyalty(
uint128(0),
_recipients,
_shares
);
}
}
}
/* ========== setting parameters in Factory ========== */
function setOwner(address _owner) external override {
require(msg.sender == owner);
emit OwnerChanged(owner, _owner);
owner = _owner;
}
function setPairImplementation(address _newPairImplementation)
external
override
{
require(
msg.sender == owner && pairImplementation != _newPairImplementation
);
address _oldPairImplementation = pairImplementation;
pairImplementation = _newPairImplementation;
emit PairImplementationSet(
_oldPairImplementation,
_newPairImplementation
);
}
function setLptImplementation(address _newLptImplementation)
external
override
{
require(
msg.sender == owner && lptImplementation != _newLptImplementation
);
address _oldLptImplementation = lptImplementation;
lptImplementation = _newLptImplementation;
emit LptImplementationSet(_oldLptImplementation, _newLptImplementation);
}
function setRoyaltyEngine(address _newRoyaltyEngine) external override {
require(msg.sender == owner);
royaltyEngine = IRoyaltyEngineV1(_newRoyaltyEngine);
}
function setCreatePairLock(bool _newLock) external {
require(msg.sender == owner);
createPairLock = _newLock;
}
/* ========== setting parameters in Pairs ========== */
function setRoyaltyInfo(address _tokenX, address _tokenY, bool isZero)
external
override
{
require(msg.sender == owner);
_setRoyaltyInfo(_tokenX, _tokenY, isZero);
}
function setSafetyLock(address _tokenX, address _tokenY, bool _newLock) external {
require(msg.sender == owner);
IMidasPair721(getPairERC721[_tokenX][_tokenY]).updateSafetyLock(_newLock);
}
function flashLoan(
address _tokenX,
address _tokenY,
IMidasFlashLoanCallback receiver,
uint256[] calldata _tokenIds,
bytes calldata data
) external {
require(msg.sender == owner);
IMidasPair721(getPairERC721[_tokenX][_tokenY]).flashLoan(
receiver,
_tokenIds,
data
);
}
}