{"id":15291891,"url":"https://github.com/sanjay-sol/solidity-diamond-design-pattern","last_synced_at":"2025-05-07T05:04:07.514Z","repository":{"id":257182616,"uuid":"857549979","full_name":"sanjay-sol/Solidity-Diamond-Design-Pattern","owner":"sanjay-sol","description":"A sample implementation to understand the Diamond Design Pattern in Solidity.","archived":false,"fork":false,"pushed_at":"2024-09-15T01:15:34.000Z","size":70,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-31T06:32:11.483Z","etag":null,"topics":["diamond-pattern","eip-2535","hardhat","solidity"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sanjay-sol.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-09-15T00:12:33.000Z","updated_at":"2024-09-16T11:39:37.000Z","dependencies_parsed_at":null,"dependency_job_id":"a530e263-bff0-4eee-8462-9f7c23210a44","html_url":"https://github.com/sanjay-sol/Solidity-Diamond-Design-Pattern","commit_stats":null,"previous_names":["sanjay-sol/solidity-design-patterns"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sanjay-sol%2FSolidity-Diamond-Design-Pattern","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sanjay-sol%2FSolidity-Diamond-Design-Pattern/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sanjay-sol%2FSolidity-Diamond-Design-Pattern/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sanjay-sol%2FSolidity-Diamond-Design-Pattern/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sanjay-sol","download_url":"https://codeload.github.com/sanjay-sol/Solidity-Diamond-Design-Pattern/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252817009,"owners_count":21808705,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["diamond-pattern","eip-2535","hardhat","solidity"],"created_at":"2024-09-30T16:15:00.949Z","updated_at":"2025-05-07T05:04:07.497Z","avatar_url":"https://github.com/sanjay-sol.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Solidity Diamond Pattern Example\n\n## Table of Contents\n- [Solidity Diamond Pattern Example](#solidity-diamond-pattern-example)\n  - [Table of Contents](#table-of-contents)\n  - [Introduction](#introduction)\n  - [Diagram and overview](#diagram-and-overview)\n  - [Diamond Proxy Pattern Overview](#diamond-proxy-pattern-overview)\n    - [Key Components](#key-components)\n    - [How It Works](#how-it-works)\n  - [Project Structure](#project-structure)\n    - [Description of Files](#description-of-files)\n  - [Deployment](#deployment)\n  - [How It Works](#how-it-works-1)\n  - [Storage Slots](#storage-slots)\n    - [How Function Delegation Works](#how-function-delegation-works)\n  - [Additional Resources](#additional-resources)\n\n## Introduction\nThis project demonstrates the Solidity Diamond Pattern, a design pattern that allows for modular and upgradable smart contracts. The Diamond Pattern is used to split a contract into multiple facets, each handling specific functionalities, while a central Diamond contract delegates calls to these facets.\n\n## Diagram and overview\n![Screenshot 2024-09-15 at 6 21 29 AM](https://github.com/user-attachments/assets/19ad3e19-f7f2-4804-b100-3066be164796)\n\n## Diamond Proxy Pattern Overview\nThe Diamond Pattern, also known as the Diamond Proxy Pattern, is a way to manage complex smart contracts in a modular and upgradeable manner. This pattern allows you to:\n\n- **Modularize Functionality**: Split a contract's functionality into multiple facets, each implemented in separate contracts.\n- **Upgrade Functionality**: Update or replace facets without altering the core Diamond contract, making the system flexible and scalable.\n\n### Key Components\n1. **Diamond Contract**: Central contract that manages function dispatching and facet updates.\n2. **Facets**: Separate contracts implementing specific functionalities. Each facet can be upgraded independently.\n3. **Function Selectors**: Used to determine which facet handles a particular function call. \n\n### How It Works\n1. **Function Call Delegation**: The Diamond contract uses a fallback function to delegate calls to the appropriate facet based on the function selector (hash of the function signature).\n2. **Facet Registration**: Facets are registered with the Diamond contract along with their function selectors. When a function call is made, the Diamond contract looks up the selector to determine which facet should handle the call.\n3. **Upgradeability**: New facets can be added or existing ones updated by modifying the facet-to-selector mapping in the Diamond contract.\n\n## Project Structure\nThe project is organized into the following structure:\n\ncontracts/\n  - facets/\n    - OwnershipFacet.sol\n    - TokenFacet.sol\n  - interfaces/\n    - IDiamond.sol\n    - IOwnershipFacet.sol\n    - ITokenFacet.sol\n  - Diamond.sol\nscripts/\n  - deploy.js\n\n\n### Description of Files\n\n- **Diamond.sol**: The central contract that manages facets and delegates function calls.\n- **OwnershipFacet.sol**: A facet handling ownership management functions.\n- **TokenFacet.sol**: A facet managing token-related functions.\n- **interfaces/**: Contains interfaces defining the functions exposed by the Diamond and facets.\n- **deploy.js**: Deployment script using Hardhat to deploy the Diamond contract and facets, and register function selectors.\n\n## Deployment\nThe deployment script (`deploy.js`) uses Hardhat to:\n1. Deploy the Diamond contract.\n2. Deploy facets (`OwnershipFacet` and `TokenFacet`).\n3. Register the function selectors of each facet with the Diamond contract.\n\n## How It Works\n1. **Deployment**: Deploy the Diamond contract and facets. Register the function selectors of each facet in the Diamond contract.\n2. **Function Calls**: When a function call is made, the Diamond contract uses the fallback function to delegate the call to the appropriate facet based on the function selector.\n3. **Facet Management**: Facets can be updated or replaced by updating the function selector mappings in the Diamond contract.\n\n## Storage Slots\nThe Diamond Pattern leverages storage slots to manage state efficiently:\n- **Diamond Storage**: The Diamond contract uses a mapping to store the address of each facet associated with function selectors.\n- **Facet Storage**: Each facet maintains its own state, which is managed independently of other facets.\n\n\n### How Function Delegation Works\n1. **Function Selector Lookup**: Diamond contract looks up the function selector.\n2. **Facet Invocation**: Calls the appropriate facet based on the function selector.\n\n## Additional Resources\n- [EIP Link](https://eips.ethereum.org/EIPS/eip-2535)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsanjay-sol%2Fsolidity-diamond-design-pattern","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsanjay-sol%2Fsolidity-diamond-design-pattern","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsanjay-sol%2Fsolidity-diamond-design-pattern/lists"}