This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAutoRollerFactory.sol
114 lines (90 loc) · 4.2 KB
/
AutoRollerFactory.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
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity 0.8.15;
import { ERC20 } from "solmate/tokens/ERC20.sol";
import { Trust } from "sense-v1-utils/Trust.sol";
import { AutoRoller, DividerLike, OwnedAdapterLike, RollerUtils, PeripheryLike } from "./AutoRoller.sol";
import { BaseSplitCodeFactory } from "./BaseSplitCodeFactory.sol";
interface RollerPeripheryLike {
function approve(ERC20,address) external;
}
contract AutoRollerFactory is Trust, BaseSplitCodeFactory {
error RollerQuantityLimitExceeded();
DividerLike internal immutable divider;
address internal immutable balancerVault;
PeripheryLike public periphery;
RollerPeripheryLike public rollerPeriphery;
RollerUtils public utils;
mapping(address => AutoRoller[]) public rollers;
constructor(
DividerLike _divider,
address _balancerVault,
address _periphery,
address _rollerPeriphery,
RollerUtils _utils
) Trust(msg.sender) BaseSplitCodeFactory(type(AutoRoller).creationCode) {
divider = _divider;
balancerVault = _balancerVault;
periphery = PeripheryLike(_periphery);
rollerPeriphery = RollerPeripheryLike(_rollerPeriphery);
utils = _utils;
}
function create(
OwnedAdapterLike adapter,
address rewardRecipient,
uint256 targetDuration
) external returns (AutoRoller autoRoller) {
ERC20 target = ERC20(address(adapter.target()));
uint256 id = rollers[address(adapter)].length;
if (id > 0 && !isTrusted[msg.sender]) revert RollerQuantityLimitExceeded();
bytes memory constructorArgs = abi.encode(
target,
divider,
address(periphery),
address(periphery.spaceFactory()),
address(balancerVault),
adapter,
utils,
rewardRecipient
);
bytes32 salt = keccak256(abi.encode(constructorArgs, id));
autoRoller = AutoRoller(super._create(constructorArgs, salt));
// Factory must have adapter auth so that it can give auth to the roller
adapter.setIsTrusted(address(autoRoller), true);
autoRoller.setParam("TARGET_DURATION", targetDuration);
autoRoller.setParam("OWNER", msg.sender);
// Allow the new roller to move the roller periphery's target
rollerPeriphery.approve(target, address(autoRoller));
// Allow the adapter to move the roller periphery's underlying & target if it can't already
ERC20 underlying = ERC20(adapter.underlying());
if (underlying.allowance(address(rollerPeriphery), address(adapter)) == 0) {
rollerPeriphery.approve(underlying, address(adapter));
}
if (target.allowance(address(rollerPeriphery), address(adapter)) == 0) {
rollerPeriphery.approve(target, address(adapter));
}
rollers[address(adapter)].push(autoRoller);
emit RollerCreated(address(adapter), address(autoRoller));
}
/// @notice Update the address for the Periphery
/// @param newPeriphery The Periphery addresss to set
function setPeriphery(address newPeriphery) external requiresTrust {
emit PeripheryChanged(address(periphery), newPeriphery);
periphery = PeripheryLike(newPeriphery);
}
/// @notice Update the address for the Roller Periphery
/// @param newRollerPeriphery The Roller Periphery addresss to set
function setRollerPeriphery(address newRollerPeriphery) external requiresTrust {
emit RollerPeripheryChanged(address(rollerPeriphery), newRollerPeriphery);
rollerPeriphery = RollerPeripheryLike(newRollerPeriphery);
}
/// @notice Update the address for the Utils
/// @param newUtils The Utils addresss to set
function setUtils(address newUtils) external requiresTrust {
emit UtilsChanged(address(utils), newUtils);
utils = RollerUtils(newUtils);
}
event PeripheryChanged(address indexed adapter, address autoRoller);
event RollerPeripheryChanged(address indexed adapter, address autoRoller);
event UtilsChanged(address indexed adapter, address autoRoller);
event RollerCreated(address indexed adapter, address autoRoller);
}