JSPM

@violetprotocol/extendable

1.2.3
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 10
  • Score
    100M100P100Q61646F
  • License MIT

Extendable smart contract pattern for Solidity

Package Exports

    This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@violetprotocol/extendable) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

    Readme

    Extendable Contract Pattern

    Upgradeable, modular, extensible smart contracts using Extendable<>Extension architecture.

    Add and remove selectively modular parts of functional logic known as Extensions, accessing modular storage definitions.

    import "@violetprotocol/extendable/extendable/Extendable.sol";
    
    contract YourContract is Extendable {
        ...
    }
    > deploy(YourContract)
    > YourContract.extend(extension);

    Get started!

    Simply install our package and get implementing!

    npm install @violetprotocol/extendable
    yarn add @violetprotocol/extendable

    Then import our library during development:

    import "@violetprotocol/extendable/extendable/Extendable.sol";
    import "@violetprotocol/extendable/extensions/Extension.sol";
    import "@violetprotocol/extendable/extensions/extend/ExtendLogic.sol";
    import "@violetprotocol/extendable/storage/ExtendableStorage.sol";
    ...

    Architecture

    Contracts are given their functionality by extending them with new functions. The first function that is added is the Extend function which provides the contract the ability to be extended.

    Extendable

    All contracts must inherit the Extendable contract.

    Extendable contracts have a unique interaction with the ExtendLogic contract where Extensions are added. Extendable contracts access the state written by the ExtendLogic extension in order to perform delegate calls to each extension. All calls are done from the context of the Extendable contract which is handled by delegatecall.

    Extendable contracts have an evolving interface which is accessible through the getFullInterface function supplied by the ExtendLogic extension. This allows developers to easily determine the current interface of an evolving Extendable contract directly on-chain without having to query separate processes that may not be in sync (GitHub, Documentation, Twitter etc.).

    Extensions

    Extension logic contracts implement functional logic that is called by the Extendable contract. Following extension principles of modularity and updateability, it is recommended to separate logic contracts into the most divisible units possible: single function contracts. Where logic contracts encompass more than a single function for dependency or cohesive reasons, it can envelope more functions but with the trade-off of being less modular; any upgrades to a single function require the entire logic extension to be updated and re-registered.

    Extension logic contracts can mutate state by accessing the storage of the delegator through custom storage slot access. Various different Extension logic contracts can access the same state but extensions should be written and extended mindfully to avoid incorrect state mutability.

    import "@violetprotocol/extendable/extensions/Extension.sol";
    
    contract YourExtension is IYourExtension, Extension {
        ...
    }
    > deploy(YourExtension)
    0x7F5b1b0a4929BF2bD9502CBF714c166931FC85dD
    > YourContract.extend(0x7F5b1b0a4929BF2bD9502CBF714c166931FC85dD)

    Storage

    Storage contracts define state variables that intend to be stored and used by an Extendable contract and accessed by Extension logic contracts. It uses a storage slot locator model where storage is allocated and accessed by address and different structures/types are located in different places to avoid collision.

    Storage contracts are libraries that are imported by the contract that requires access to storage state. These contracts can include reusable functions that might be useful related to computation over state (such as modifiers or get functions).

    struct YourState {
        // State variables are declared here
    }
    
    library YourStorage {
        bytes32 constant private STORAGE_NAME = keccak256("your_unique_storage_identifier");
    
        function _getState()
            internal 
            view
            returns (YourState storage state) 
        {
            bytes32 position = keccak256(abi.encodePacked(address(this), STORAGE_NAME));
            assembly {
                state.slot := position
            }
        }
    }
    import "@violetprotocol/extendable/extensions/Extension.sol";
    import "./YourStorage.sol";
    
    contract YourExtension is IYourExtension, Extension {
        function readStorage() public view {
            YourState storage state = YourStorage._getState();
    
            // access properties of state with `state.yourVar`
            // re-assign state properties with `state.yourVar = <value>`
        }
    }

    Requirements

    nodejs >=12.0

    Build

    yarn install to install all dependencies.

    yarn hardhat compile to build all contract artifacts.

    Test

    yarn hardhat test to run tests.