|
| 1 | +//SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.0; |
| 3 | + |
| 4 | +import "@synthetixio/core-contracts/contracts/ownership/OwnableMixin.sol"; |
| 5 | +import "@synthetixio/core-contracts/contracts/proxy/UUPSProxy.sol"; |
| 6 | +import "../interfaces/IAssociatedSystemsModule.sol"; |
| 7 | +import "../mixins/AssociatedSystemsMixin.sol"; |
| 8 | + |
| 9 | +import "@synthetixio/core-contracts/contracts/interfaces/IUUPSImplementation.sol"; |
| 10 | +import "../interfaces/IOwnerModule.sol"; |
| 11 | +import "../interfaces/ITokenModule.sol"; |
| 12 | +import "../interfaces/INftModule.sol"; |
| 13 | + |
| 14 | +contract AssociatedSystemsModule is IAssociatedSystemsModule, OwnableMixin, AssociatedSystemsMixin { |
| 15 | + function initOrUpgradeToken( |
| 16 | + bytes32 id, |
| 17 | + string memory name, |
| 18 | + string memory symbol, |
| 19 | + uint8 decimals, |
| 20 | + address impl |
| 21 | + ) external override onlyOwner { |
| 22 | + AssociatedSystemsStore storage store = _associatedSystemsStore(); |
| 23 | + |
| 24 | + if (store.satellites[id].proxy != address(0)) { |
| 25 | + _requireKind(id, _KIND_ERC20); |
| 26 | + |
| 27 | + store.satellites[id].impl = impl; |
| 28 | + |
| 29 | + address proxy = store.satellites[id].proxy; |
| 30 | + |
| 31 | + // tell the associated proxy to upgrade to the new implementation |
| 32 | + IUUPSImplementation(proxy).upgradeTo(impl); |
| 33 | + |
| 34 | + _setAssociatedSystem(id, _KIND_ERC20, proxy, impl); |
| 35 | + } else { |
| 36 | + // create a new proxy and own it |
| 37 | + address proxy = address(new UUPSProxy(impl)); |
| 38 | + |
| 39 | + IOwnerModule(proxy).initializeOwnerModule(address(this)); |
| 40 | + ITokenModule(proxy).initialize(name, symbol, decimals); |
| 41 | + |
| 42 | + _setAssociatedSystem(id, _KIND_ERC20, proxy, impl); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + function initOrUpgradeNft( |
| 47 | + bytes32 id, |
| 48 | + string memory name, |
| 49 | + string memory symbol, |
| 50 | + string memory uri, |
| 51 | + address impl |
| 52 | + ) external override onlyOwner { |
| 53 | + AssociatedSystemsStore storage store = _associatedSystemsStore(); |
| 54 | + |
| 55 | + if (store.satellites[id].proxy != address(0)) { |
| 56 | + _requireKind(id, _KIND_ERC721); |
| 57 | + |
| 58 | + address proxy = store.satellites[id].proxy; |
| 59 | + |
| 60 | + // tell the associated proxy to upgrade to the new implementation |
| 61 | + IUUPSImplementation(proxy).upgradeTo(impl); |
| 62 | + |
| 63 | + _setAssociatedSystem(id, _KIND_ERC721, proxy, impl); |
| 64 | + } else { |
| 65 | + // create a new proxy and own it |
| 66 | + address proxy = address(new UUPSProxy(impl)); |
| 67 | + |
| 68 | + IOwnerModule(proxy).initializeOwnerModule(address(this)); |
| 69 | + INftModule(proxy).initialize(name, symbol, uri); |
| 70 | + |
| 71 | + _setAssociatedSystem(id, _KIND_ERC721, proxy, impl); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * sets a token implementation without the corresponding upgrade functionality |
| 77 | + * useful for adaptation of ex. old SNX token. The connected system does not need to be |
| 78 | + * |
| 79 | + * *NOTE:* the contract you are connecting should still be owned by your dao. The |
| 80 | + * system is not expected to be able to do upgrades for you. |
| 81 | + */ |
| 82 | + function registerUnmanagedSystem(bytes32 id, address endpoint) external override onlyOwner { |
| 83 | + // empty string require kind will make sure the system is either unregistered or already unmanaged |
| 84 | + _requireKind(id, ""); |
| 85 | + |
| 86 | + _setAssociatedSystem(id, _KIND_UNMANAGED, endpoint, endpoint); |
| 87 | + } |
| 88 | + |
| 89 | + function _setAssociatedSystem( |
| 90 | + bytes32 id, |
| 91 | + bytes32 kind, |
| 92 | + address proxy, |
| 93 | + address impl |
| 94 | + ) internal { |
| 95 | + _associatedSystemsStore().satellites[id] = AssociatedSystem(proxy, impl, kind); |
| 96 | + emit AssociatedSystemSet(kind, id, proxy, impl); |
| 97 | + } |
| 98 | + |
| 99 | + function getAssociatedSystem(bytes32 id) external view override returns (address proxy, bytes32 kind) { |
| 100 | + proxy = _associatedSystemsStore().satellites[id].proxy; |
| 101 | + kind = _associatedSystemsStore().satellites[id].kind; |
| 102 | + } |
| 103 | + |
| 104 | + event AssociatedSystemSet(bytes32 indexed kind, bytes32 indexed id, address proxy, address impl); |
| 105 | +} |
0 commit comments