-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathTrancheFactory.sol
60 lines (50 loc) · 1.92 KB
/
TrancheFactory.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
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity 0.8.26;
import {Tranche} from "src/token/Tranche.sol";
import {ITrancheFactory} from "src/interfaces/factories/ITrancheFactory.sol";
import {Auth} from "src/Auth.sol";
/// @title Tranche Token Factory
/// @dev Utility for deploying new tranche token contracts
/// Ensures the addresses are deployed at a deterministic address
/// based on the pool id and tranche id.
contract TrancheFactory is Auth, ITrancheFactory {
address public immutable root;
constructor(address _root, address deployer) Auth(deployer) {
root = _root;
}
/// @inheritdoc ITrancheFactory
function newTranche(
uint64 poolId,
bytes16 trancheId,
string memory name,
string memory symbol,
uint8 decimals,
address[] calldata trancheWards
) public auth returns (address) {
// Salt is hash(poolId + trancheId)
// same tranche token address on every evm chain
bytes32 salt = keccak256(abi.encodePacked(poolId, trancheId));
Tranche token = new Tranche{salt: salt}(decimals);
token.file("name", name);
token.file("symbol", symbol);
token.rely(root);
uint256 wardsCount = trancheWards.length;
for (uint256 i; i < wardsCount; i++) {
token.rely(trancheWards[i]);
}
token.deny(address(this));
return address(token);
}
/// @inheritdoc ITrancheFactory
function getAddress(uint64 poolId, bytes16 trancheId, uint8 decimals) external view returns (address) {
bytes32 hash = keccak256(
abi.encodePacked(
bytes1(0xff),
address(this),
keccak256(abi.encodePacked(poolId, trancheId)),
keccak256(abi.encodePacked(type(Tranche).creationCode, abi.encode(decimals)))
)
);
return address(uint160(uint256(hash)));
}
}