|
| 1 | +# Extendable Contract Pattern |
1 | 2 |
|
| 3 | +Upgradeable, modular, extensible smart contracts using Extendable<>Extension architecture. |
| 4 | + |
| 5 | +Add and remove selectively modular parts of functional logic known as Extensions, accessing modular storage definitions. |
| 6 | + |
| 7 | +```solidity |
| 8 | +contract YourContract is Extendable { |
| 9 | + ... |
| 10 | +} |
| 11 | +``` |
| 12 | + |
| 13 | +``` |
| 14 | +> deploy(YourContract) |
| 15 | +> YourContract.extend(extension); |
| 16 | +``` |
| 17 | + |
| 18 | +## Architecture |
| 19 | + |
| 20 | +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. |
| 21 | + |
| 22 | +### Extendable |
| 23 | + |
| 24 | +All contracts must inherit the _Extendable_ contract. |
| 25 | + |
| 26 | +_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`. |
| 27 | + |
| 28 | +_Extendable_ contracts have an evolving interface which is accessible through the `getCurrentInterface` 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.). |
| 29 | + |
| 30 | +### Extensions |
| 31 | + |
| 32 | +_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. |
| 33 | + |
| 34 | +_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. |
| 35 | + |
| 36 | +```solidity |
| 37 | +import "@violetprotocol/extendable/extensions/Extension.sol"; |
| 38 | +
|
| 39 | +contract YourExtension is IYourExtension, Extension { |
| 40 | + ... |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +``` |
| 45 | +> deploy(YourExtension) |
| 46 | +0x7F5b1b0a4929BF2bD9502CBF714c166931FC85dD |
| 47 | +> YourContract.extend(0x7F5b1b0a4929BF2bD9502CBF714c166931FC85dD) |
| 48 | +``` |
| 49 | + |
| 50 | +### Storage |
| 51 | + |
| 52 | +_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. |
| 53 | + |
| 54 | +_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). |
| 55 | + |
| 56 | +```solidity |
| 57 | +struct YourState { |
| 58 | + // State variables are declared here |
| 59 | +} |
| 60 | +
|
| 61 | +library YourStorage { |
| 62 | + bytes32 constant private STORAGE_NAME = keccak256("your_unique_storage_identifier"); |
| 63 | +
|
| 64 | + function _getStorage() |
| 65 | + internal |
| 66 | + view |
| 67 | + returns (YourState storage state) |
| 68 | + { |
| 69 | + bytes32 position = keccak256(abi.encodePacked(address(this), STORAGE_NAME)); |
| 70 | + assembly { |
| 71 | + state.slot := position |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +```solidity |
| 78 | +import "@violetprotocol/extendable/extensions/Extension.sol"; |
| 79 | +import "./YourStorage.sol"; |
| 80 | +
|
| 81 | +contract YourExtension is IYourExtension, Extension { |
| 82 | + function readStorage() public view { |
| 83 | + YourState storage state = YourStorage._getStorage(); |
| 84 | +
|
| 85 | + // access properties of state with `state.yourVar` |
| 86 | + // re-assign state properties with `state.yourVar = <value>` |
| 87 | + } |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +## Requirements |
| 92 | + |
| 93 | +`nodejs >=12.0` |
| 94 | + |
| 95 | +## Build |
| 96 | + |
| 97 | +`yarn install` to install all dependencies. |
| 98 | + |
| 99 | +`yarn hardhat compile` to build all contract artifacts. |
| 100 | + |
| 101 | +## Test |
| 102 | + |
| 103 | +`yarn hardhat test` to run tests. |
0 commit comments