-
Notifications
You must be signed in to change notification settings - Fork 18
/
DelegationModule.sol
64 lines (55 loc) · 2.22 KB
/
DelegationModule.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
/* --- External Libraries --- */
import { Create2 } from "@openzeppelin/contracts/utils/Create2.sol";
import "../libraries/TransferHelper.sol";
import "../libraries/CloneLibrary.sol";
import "./SubDelegationModuleImplementation.sol";
import "../interfaces/IDelegationModule.sol";
contract DelegationModule is IDelegationModule {
using TransferHelper for address;
address public immutable override moduleImplementation;
address public immutable override depositToken;
/**
* @dev Contains the address of the sub-delegation module for a user
* if one has been deployed.
*/
mapping(address => ISubDelegationModule) public override subDelegationModuleForUser;
constructor(address depositToken_) {
depositToken = depositToken_;
moduleImplementation = address(new SubDelegationModuleImplementation(depositToken_));
}
function getOrCreateModule(address account) internal returns (ISubDelegationModule module) {
module = subDelegationModuleForUser[account];
if (address(module) == address(0)) {
module = ISubDelegationModule(CloneLibrary.createClone(moduleImplementation));
subDelegationModuleForUser[account] = module;
module.delegate(account);
emit SubDelegationModuleCreated(account, address(module));
}
}
/**
* @dev Send `amount` of the delegatable token to the sub-delegation
* module for `account`.
*/
function _depositToModule(address account, uint256 amount) internal {
ISubDelegationModule module = getOrCreateModule(account);
depositToken.safeTransferFrom(account, address(module), amount);
}
/**
* @dev Withdraw the full balance of the delegatable token from the
* sub-delegation module for `account` to `to`.
*/
function _withdrawFromModule(address account, address to, uint256 amount) internal {
ISubDelegationModule module = subDelegationModuleForUser[account];
module.transfer(to, amount);
}
/**
* @dev Delegates the balance of the sub-delegation module for `account`
* to `delegatee`.
*/
function _delegateFromModule(address account, address delegatee) internal {
ISubDelegationModule module = subDelegationModuleForUser[account];
module.delegate(delegatee);
}
}