-
Notifications
You must be signed in to change notification settings - Fork 0
/
SubaccountFactory.sol
50 lines (37 loc) · 1.5 KB
/
SubaccountFactory.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
/*
Copyright 2022 JOJO Exchange
SPDX-License-Identifier: BUSL-1.1
*/
import "@openzeppelin/contracts/proxy/Clones.sol";
import "./Subaccount.sol";
pragma solidity ^0.8.19;
contract SubaccountFactory {
// ========== storage ==========
// Subaccount template that can be cloned
address immutable template;
// Subaccount can only be added.
mapping(address => address[]) subaccountRegistry;
// ========== event ==========
event NewSubaccount(address indexed master, uint256 subaccountIndex, address subaccountAddress);
// ========== constructor ==========
constructor() {
template = address(new Subaccount());
Subaccount(template).init(address(this));
}
// ========== functions ==========
/// @notice https://eips.ethereum.org/EIPS/eip-1167[EIP 1167]
/// is a standard protocol for deploying minimal proxy contracts,
/// also known as "clones".
function newSubaccount() external returns (address subaccount) {
subaccount = Clones.clone(template);
Subaccount(subaccount).init(msg.sender);
subaccountRegistry[msg.sender].push(subaccount);
emit NewSubaccount(msg.sender, subaccountRegistry[msg.sender].length - 1, subaccount);
}
function getSubaccounts(address master) external view returns (address[] memory) {
return subaccountRegistry[master];
}
function getSubaccount(address master, uint256 index) external view returns (address) {
return subaccountRegistry[master][index];
}
}