Skip to content

Commit

Permalink
Single base router (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
nkrishang authored Sep 12, 2023
1 parent 43b5697 commit d801083
Show file tree
Hide file tree
Showing 9 changed files with 730 additions and 3,160 deletions.
14 changes: 11 additions & 3 deletions src/example/RouterImmutable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@

pragma solidity ^0.8.0;

import "../presets/BaseRouterWithDefaults.sol";
import "../presets/BaseRouter.sol";

/**
* This smart contract is an EXAMPLE, and is not meant for use in production.
*/

contract RouterImmutable is BaseRouterWithDefaults {
abstract contract MyRouter is BaseRouter {

constructor(Extension[] memory _extensions) BaseRouter(_extensions) {
// Initialize the router with a set of default extensions.
__BaseRouter_init();
}
}

contract RouterImmutable is MyRouter {

constructor(Extension[] memory _extensions) BaseRouterWithDefaults(_extensions) {}
constructor(Extension[] memory _extensions) MyRouter(_extensions) {}

/*///////////////////////////////////////////////////////////////
Overrides
Expand Down
2 changes: 1 addition & 1 deletion src/example/RouterRegistryConstrained.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ contract RouterRegistryConstrained is BaseRouter {
ExtensionRegistry public registry;

/// @dev Cannot initialize with extensions before registry is set, so we pass empty array to base constructor.
constructor(address _registry) {
constructor(address _registry) BaseRouter(new Extension[](0)) {
admin = msg.sender;
registry = ExtensionRegistry(_registry);
}
Expand Down
3 changes: 1 addition & 2 deletions src/example/RouterUpgradeable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
pragma solidity ^0.8.0;

import "../presets/BaseRouter.sol";

/**
* This smart contract is an EXAMPLE, and is not meant for use in production.
*/
contract RouterUpgradeable is BaseRouter {

address public admin;

constructor() {
constructor() BaseRouter(new Extension[](0)) {
admin = msg.sender;
}

Expand Down
25 changes: 25 additions & 0 deletions src/lib/BaseRouterStorage.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

/// @title BaseRouterStorage
/// @author thirdweb (https://github.com/thirdweb-dev/dynamic-contracts)
/// @notice Defined storage for the base router preset.

library BaseRouterStorage {

/// @custom:storage-location erc7201:base.router.storage
bytes32 public constant BASE_ROUTER_STORAGE_POSITION = keccak256(abi.encode(uint256(keccak256("base.router.storage")) - 1));

struct Data {
/// @dev Mapping from default extension name -> whether the extension has been removed or replaced at least once.
mapping(string => bool) isRemovedOrReplaced;
}

/// @dev Returns access to the extension manager's storage.
function data() internal pure returns (Data storage data_) {
bytes32 position = BASE_ROUTER_STORAGE_POSITION;
assembly {
data_.slot := position
}
}
}
52 changes: 44 additions & 8 deletions src/presets/BaseRouter.sol
Original file line number Diff line number Diff line change
@@ -1,20 +1,56 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "../core/Router.sol";
import "./ExtensionManager.sol";
import { Router, IRouter } from "../core/Router.sol";
import { IRouterState } from "../interface/IRouterState.sol";
import { IRouterStateGetters } from "../interface/IRouterStateGetters.sol";
import { ExtensionManager } from "./ExtensionManager.sol";
import { DefaultExtensionSet } from "./DefaultExtensionSet.sol";
import { BaseRouterStorage } from "../lib/BaseRouterStorage.sol";
import { StringSet } from "../lib/StringSet.sol";

/// @title BaseRouter
/// @author thirdweb (https://github.com/thirdweb-dev/dynamic-contracts)
/// @notice A preset Router + ExtensionManager.
/// @notice A router with an API to manage its extensions.

abstract contract BaseRouter is Router, ExtensionManager {

using StringSet for StringSet.Set;

/// @notice The address where the router's default extension set is stored.
address public immutable defaultExtensions;

/**
* @notice Returns the implementation address to delegateCall for the given function selector.
* @param _functionSelector The function selector to get the implementation address for.
* @return implementation The implementation address to delegateCall for the given function selector.
*/
/// @notice Initialize the Router with a set of default extensions.
constructor(Extension[] memory _extensions) {
address defaultExtensionsAddr = _extensions.length > 0 ? address(new DefaultExtensionSet(_extensions)) : address(0);
defaultExtensions = defaultExtensionsAddr;
}

/// @notice Initialize the Router with a set of default extensions.
function __BaseRouter_init() internal {
Extension[] memory defaults = IRouterState(defaultExtensions).getAllExtensions();

for(uint256 i = 0; i < defaults.length; i += 1) {

Extension memory extension = defaults[i];
// Store: new extension name.
require(_extensionManagerStorage().extensionNames.add(extension.metadata.name), "ExtensionManager: extension already exists.");

// 1. Store: metadata for extension.
_setMetadataForExtension(extension.metadata.name, extension.metadata);
uint256 len = extension.functions.length;
for (uint256 j = 0; j < len; j += 1) {
// 2. Store: function for extension.
_addToFunctionMap(extension.metadata.name, extension.functions[j]);
// 3. Store: metadata for function.
_setMetadataForFunction(extension.functions[j].functionSelector, extension.metadata);
}

emit ExtensionAdded(extension.metadata.name, extension.metadata.implementation, extension);
}
}

/// @notice Returns the implementation contract address for a given function signature.
function getImplementationForFunction(bytes4 _functionSelector) public view virtual override returns (address) {
return getMetadataForFunction(_functionSelector).implementation;
}
Expand Down
168 changes: 0 additions & 168 deletions src/presets/BaseRouterWithDefaults.sol

This file was deleted.

Loading

0 comments on commit d801083

Please sign in to comment.