npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@violetprotocol/extendable

v1.2.3

Published

Extendable smart contract pattern for Solidity

Downloads

100

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.