forked from wighawag/hardhat-deploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Diamantaire.sol
81 lines (70 loc) · 2.21 KB
/
Diamantaire.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
pragma solidity ^0.6.4;
pragma experimental ABIEncoderV2;
import "./DiamondHeaders.sol";
import "./DiamondBase.sol";
import "./ERC173.sol";
contract Diamantaire is ERC173 {
event DiamondCreated(Diamond diamond);
address _owner;
Diamond _diamond;
constructor(
address owner,
bytes[] memory _diamondCut,
bytes memory data
) public payable {
_owner = owner;
Diamond diamond = Diamond(address(new DiamondBase(address(this))));
emit DiamondCreated(diamond);
_diamond = diamond;
if (_diamondCut.length > 0) {
_cut(diamond, _diamondCut);
}
if (data.length > 0) {
_execute(diamond, msg.value, data);
}
}
function transferOwnership(address newOwner) external override {
address previousOwner = _owner;
require(msg.sender == previousOwner, "NOT_AUTHORIZED");
_owner = newOwner;
emit OwnershipTransferred(previousOwner, newOwner);
}
function owner() external override view returns (address) {
return _owner;
}
function cutAndExecute(bytes[] memory _diamondCut, bytes memory data)
public
payable
{
require(msg.sender == _owner, "NOT_AUTHORIZED");
Diamond diamond = _diamond;
if (_diamondCut.length > 0) {
_cut(diamond, _diamondCut);
}
if (data.length > 0) {
_execute(diamond, msg.value, data);
}
}
function execute(bytes calldata data) external payable {
require(msg.sender == _owner, "NOT_AUTHORIZED");
_execute(_diamond, msg.value, data);
}
function _cut(Diamond diamond, bytes[] memory _diamondCut) internal {
diamond.diamondCut(_diamondCut);
}
function _execute(
Diamond diamond,
uint256 value,
bytes memory data
) internal {
(bool success, ) = address(diamond).call{value: value}(data);
if (!success) {
// solhint-disable-next-line security/no-inline-assembly
assembly {
let returnDataSize := returndatasize()
returndatacopy(0, 0, returnDataSize)
revert(0, returnDataSize)
}
}
}
}