How mudgen/diamond-1-hardhat works
Project type and purpose
• This is a blockchain / smart-contract reference implementation built with Hardhat.
• It implements the EIP-2535 “Diamond Standard,” allowing a single on-chain contract (“diamond”) to route calls to multiple modular facets (contracts).
• Its purpose is to provide boilerplate facets (DiamondCutFacet, DiamondLoupeFacet), library code (LibDiamond), deployment scripts, and tests, so that users can deploy and upgrade a diamond-style proxy contract.
File structure
Top-level:
• contracts/ ← Solidity sources
– Diamond.sol ← the proxy entry point
– facets/ ← modular upgradeable logic contracts
· DiamondCutFacet.sol
· DiamondLoupeFacet.sol
· …other user facets
– interfaces/ ← facet and EIP interfaces
– libraries/ ← LibDiamond.sol (storage + internal diamondCut)
– upgradeInitializers/ ← optional state init contracts
• scripts/ ← deployment and helper scripts
– deploy.js
– libraries/diamond.js ← helper for constructing cut arrays
• test/ ← Mocha/Chai tests
– diamondTest.js
– cacheBugTest.js
• hardhat.config.js ← Hardhat configuration
• package.json ← JS dependencies & scripts
Patterns:
• Proxy pattern (Diamond.sol) delegating calls
• Library for shared storage and internal logic
• Facet modules loaded at deploy or upgrade time
README for additional insights
• Confirms EIP-2535 compliance, notes which loupe functions are gas-heavy vs. on-chain safe.
• Deployment instructions use Hardhat + scripts/deploy.js.
• Tests cover diamondCut/upgrade and loupe queries.
• Encourages copying facets/library into new diamonds.
a. Main components:
• Developer environment (Hardhat + scripts)
• Deployed Diamond contract (proxy)
• Facet contracts (DiamondCutFacet, DiamondLoupeFacet, plus any custom facets)
• LibDiamond library (internal code linked at compile-time)
• Ethereum network (testnet/mainnet)
• Off-chain clients (web3.js/ethers.js front-ends, tests)
b. Relationships & interactions:
• scripts/deploy.js → compiles & deploys Diamond.sol + facets → Ethereum network
• Diamond proxy forwards calls to facet addresses via delegatecall
• Facets implement logic; LibDiamond manages storage & diamondCut
• Off-chain clients call facets through the diamond address (using each facet’s ABI)
• Tests simulate contract calls & upgrades
c. Architectural patterns:
• Diamond proxy (multi-facet upgradeable proxy)
• Delegatecall routing table stored in diamond storage
• Single-transaction upgrades (cut + optional initializer)
d. Key technologies:
• Solidity (contracts)
• Hardhat (compile, test, deploy)
• Ethers.js or web3.js (client libraries)
• Mocha/Chai (tests)