all files / contracts/utils/ Registry.sol

100% Statements 20/20
100% Branches 10/10
100% Functions 5/5
100% Lines 20/20
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                                                                                                          21142× 21142× 21129×                     233× 233×                       11×                           280× 280× 279× 285× 279× 279× 279×   279× 210× 210×     279× 279× 279×        
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.8.6;
 
import "./Governable.sol";
import "./../interfaces/utils/IRegistry.sol";
 
/**
 * @title Registry
 * @author solace.fi
 * @notice Tracks the contracts of the Solaverse.
 *
 * [**Governance**](/docs/protocol/governance) can set the contract addresses and anyone can look them up.
 *
 * A key is a unique identifier for each contract. Use [`get(key)`](#get) or [`tryGet(key)`](#tryget) to get the address of the contract. Enumerate the keys with [`length()`](#length) and [`getKey(index)`](#getkey).
 */
contract Registry is IRegistry, Governable {
 
    /***************************************
    GLOBAL VARIABLES
    ***************************************/
 
    struct RegistryEntry {
        uint256 index;
        address value;
    }
 
    // contract name => contract address
    mapping(string => RegistryEntry) private _addresses;
 
    // index => key
    mapping(uint256 => string) private _keys;
 
    /// @notice The number of unique keys.
    uint256 public override length;
 
    /**
     * @notice Constructs the registry contract.
     * @param governance_ The address of the [governor](/docs/protocol/governance).
     */
    // solhint-disable-next-line no-empty-blocks
    constructor(address governance_) Governable(governance_) { }
 
    /***************************************
    VIEW FUNCTIONS
    ***************************************/
 
    /**
     * @notice Gets the `value` of a given `key`.
     * Reverts if the key is not in the mapping.
     * @param key The key to query.
     * @return value The value of the key.
     */
    function get(string calldata key) external view override returns (address value) {
        RegistryEntry memory entry = _addresses[key];
        require(entry.index != 0, "key not in mapping");
        return entry.value;
    }
 
    /**
     * @notice Gets the `value` of a given `key`.
     * Fails gracefully if the key is not in the mapping.
     * @param key The key to query.
     * @return success True if the key was found, false otherwise.
     * @return value The value of the key or zero if it was not found.
     */
    function tryGet(string calldata key) external view override returns (bool success, address value) {
        RegistryEntry memory entry = _addresses[key];
        return (entry.index == 0)
            ? (false, address(0x0))
            : (true, entry.value);
    }
 
    /**
     * @notice Gets the `key` of a given `index`.
     * @dev Iterable [1,length].
     * @param index The index to query.
     * @return key The key at that index.
     */
    function getKey(uint256 index) external view override returns (string memory key) {
        require(index != 0 && index <= length, "index out of range");
        return _keys[index];
    }
 
    /***************************************
    GOVERNANCE FUNCTIONS
    ***************************************/
 
    /**
     * @notice Sets keys and values.
     * Can only be called by the current [**governor**](/docs/protocol/governance).
     * @param keys The keys to set.
     * @param values The values to set.
     */
    function set(string[] calldata keys, address[] calldata values) external override onlyGovernance {
        uint256 len = keys.length;
        require(len == values.length, "length mismatch");
        for(uint256 i = 0; i < len; i++) {
            require(values[i] != address(0), "cannot set zero address");
            string memory key = keys[i];
            address value = values[i];
            RegistryEntry memory entry = _addresses[key];
            // add new record
            if(entry.index == 0) {
                entry.index = ++length; // autoincrement from 1
                _keys[entry.index] = key;
            }
            // store record
            entry.value = value;
            _addresses[key] = entry;
            emit RecordSet(key, value);
        }
    }
}