@diamondslab/diamonds 1.3.2 โ†’ 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,115 +1,122 @@
1
1
  # Diamonds Module
2
2
 
3
- [![npm version](https://badge.fury.io/js/@geniusventures%2Fdiamonds.svg)](https://badge.fury.io/js/@geniusventures%2Fdiamonds)
3
+ [![npm version](https://badge.fury.io/js/@diamondslab%2Fdiamonds.svg)](https://www.npmjs.com/package/@diamondslab/diamonds)
4
4
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5
5
  [![TypeScript](https://img.shields.io/badge/TypeScript-5.0+-blue.svg)](https://www.typescriptlang.org/)
6
6
  [![Hardhat](https://img.shields.io/badge/Hardhat-2.19+-orange.svg)](https://hardhat.org/)
7
7
 
8
- A comprehensive TypeScript framework for deploying, upgrading, and managing ERC-2535 Diamond Proxy contracts with enterprise-grade features including OpenZeppelin Defender integration, sophisticated version management, and flexible deployment strategies.
8
+ `@diamondslab/diamonds` is a TypeScript framework for deploying, upgrading, and
9
+ managing [ERC-2535 Diamond Proxy](https://eips.ethereum.org/EIPS/eip-2535)
10
+ contracts. It provides a pluggable deployment-strategy system, automatic function
11
+ selector management, configuration-driven facet versioning, and ABI tooling.
12
+
13
+ > **Package manager:** this project uses **Yarn 4** (`packageManager: yarn@4.x`).
14
+ > The examples below use `yarn`. npm/pnpm work for consuming the published package
15
+ > but the repo scripts assume Yarn.
9
16
 
10
17
  ## โœจ Key Features
11
18
 
12
- ### ๐Ÿ—๏ธ **Complete Diamond Implementation**
19
+ ### ๐Ÿ—๏ธ Complete Diamond Implementation
13
20
 
14
21
  - Full ERC-2535 Diamond Proxy Standard support
15
22
  - Modular facet architecture with automated function selector management
16
- - Smart collision detection and resolution
17
- - Comprehensive state management and validation
18
-
19
- ### ๐Ÿ›ก๏ธ **Enterprise Security**
23
+ - Selector collision detection and resolution
24
+ - State management and validation
20
25
 
21
- - **OpenZeppelin Defender Integration**: Secure deployments through Defender's infrastructure
22
- - **Multi-signature Support**: Gnosis Safe and custom multisig workflows
23
- - **Automated Verification**: Contract verification on Etherscan and other explorers
24
- - **Access Control**: Role-based deployment permissions
26
+ ### ๐Ÿ”„ Deployment Management
25
27
 
26
- ### ๐Ÿ”„ **Advanced Deployment Management**
28
+ - **Strategy Pattern**: pluggable deployment strategies (Local, RPC, Base, Defender)
29
+ - **Version Control**: configuration-driven versioning for facets and protocols
30
+ - **Upgrade Automation**: `DiamondDeployer` detects an existing deployment and
31
+ performs an upgrade instead of a fresh deploy
32
+ - **Post-deployment Callbacks**: run custom initialization after a cut
27
33
 
28
- - **Strategy Pattern**: Pluggable deployment strategies (Local, Defender, Custom)
29
- - **Version Control**: Sophisticated versioning for facets and protocols
30
- - **Upgrade Automation**: Intelligent upgrade detection and execution
31
- - **Rollback Support**: Safe rollback mechanisms
34
+ ### ๐Ÿญ Production Ready
32
35
 
33
- ### ๐Ÿญ **Production Ready**
34
-
35
- - **Repository Pattern**: Flexible data persistence (File-based, Database-ready)
36
- - **Configuration Management**: JSON-based configuration with validation
37
- - **Comprehensive Testing**: Unit, integration, and end-to-end test suites
38
- - **CLI Tools**: Command-line interface for deployment operations
36
+ - **Repository Pattern**: pluggable persistence (file-based out of the box)
37
+ - **Configuration Management**: JSON configuration with Zod validation
38
+ - **ABI Tooling**: combined Diamond ABI generation, preview, compare, and validate
39
+ - **Comprehensive Testing**: unit and integration test suites
39
40
 
40
41
  ## ๐Ÿš€ Quick Start
41
42
 
42
43
  ### Installation
43
44
 
44
45
  ```bash
45
- npm install @geniusventures/diamonds
46
+ yarn add @diamondslab/diamonds
47
+ # peer deps (if not already present)
48
+ yarn add --dev hardhat @nomicfoundation/hardhat-ethers ethers
46
49
  ```
47
50
 
48
51
  ### Basic Usage
49
52
 
50
- ```typescript
51
- import { Diamond, DiamondDeployer, FileDeploymentRepository } from '@geniusventures/diamonds';
52
- import { LocalDeploymentStrategy } from '@geniusventures/diamonds/strategies';
53
- import { ethers } from 'hardhat';
53
+ Everything is exported from the package root (`@diamondslab/diamonds`):
54
54
 
55
- // Create diamond configuration
55
+ ```typescript
56
+ import {
57
+ Diamond,
58
+ DiamondDeployer,
59
+ FileDeploymentRepository,
60
+ LocalDeploymentStrategy,
61
+ } from "@diamondslab/diamonds";
62
+ import { ethers } from "hardhat";
63
+
64
+ // Diamond configuration
56
65
  const config = {
57
- diamondName: 'MyDiamond',
58
- networkName: 'localhost',
66
+ diamondName: "MyDiamond",
67
+ networkName: "localhost",
59
68
  chainId: 31337,
60
- deploymentsPath: './diamonds',
61
- contractsPath: './contracts'
69
+ deploymentsPath: "./diamonds",
70
+ contractsPath: "./contracts",
62
71
  };
63
72
 
64
- // Setup diamond and deployment components
73
+ // Set up diamond + deployment components
65
74
  const repository = new FileDeploymentRepository(config);
66
75
  const diamond = new Diamond(config, repository);
67
76
 
68
- // Set provider and signer
77
+ // Provide a provider and signer
69
78
  diamond.setProvider(ethers.provider);
70
- diamond.setSigner(await ethers.getSigners()[0]);
79
+ const [signer] = await ethers.getSigners();
80
+ diamond.setSigner(signer);
71
81
 
72
- // Deploy using local strategy
73
- const strategy = new LocalDeploymentStrategy(true); // verbose logging
82
+ // Deploy using the local strategy (verbose logging on)
83
+ const strategy = new LocalDeploymentStrategy(true);
74
84
  const deployer = new DiamondDeployer(diamond, strategy);
75
85
 
86
+ // Deploys if new, upgrades if already deployed
76
87
  await deployer.deployDiamond();
77
88
  ```
78
89
 
79
90
  ## ๐Ÿ“‹ Project Structure
80
91
 
81
- ```bash
82
- diamonds/
83
- โ”œโ”€โ”€ src/ # Source code
84
- โ”‚ โ”œโ”€โ”€ core/ # Core classes
85
- โ”‚ โ”‚ โ”œโ”€โ”€ Diamond.ts # Main diamond management
86
- โ”‚ โ”‚ โ”œโ”€โ”€ DiamondDeployer.ts # Deployment orchestration
87
- โ”‚ โ”‚ โ”œโ”€โ”€ DeploymentManager.ts # Deployment lifecycle
88
- โ”‚ โ”‚ โ””โ”€โ”€ CallbackManager.ts # Post-deployment callbacks
89
- โ”‚ โ”œโ”€โ”€ strategies/ # Deployment strategies
90
- โ”‚ โ”‚ โ”œโ”€โ”€ BaseDeploymentStrategy.ts
91
- โ”‚ โ”‚ โ”œโ”€โ”€ LocalDeploymentStrategy.ts
92
- โ”‚ โ”‚ โ””โ”€โ”€ OZDefenderDeploymentStrategy.ts
93
- โ”‚ โ”œโ”€โ”€ repositories/ # Data persistence
94
- โ”‚ โ”‚ โ”œโ”€โ”€ DeploymentRepository.ts
95
- โ”‚ โ”‚ โ”œโ”€โ”€ FileDeploymentRepository.ts
96
- โ”‚ โ”‚ โ””โ”€โ”€ jsonFileHandler.ts
97
- โ”‚ โ”œโ”€โ”€ schemas/ # Zod validation schemas
98
- โ”‚ โ”‚ โ””โ”€โ”€ DeploymentSchema.ts
99
- โ”‚ โ”œโ”€โ”€ types/ # TypeScript definitions
100
- โ”‚ โ”œโ”€โ”€ utils/ # Utility functions
101
- โ”‚ โ””โ”€โ”€ helpers/ # Helper functions
102
- โ”œโ”€โ”€ docs/ # Documentation
103
- โ”œโ”€โ”€ examples/ # Usage examples
104
- โ”œโ”€โ”€ test/ # Test suites
105
- โ””โ”€โ”€ scripts/ # CLI and utility scripts
92
+ ```text
93
+ src/
94
+ โ”œโ”€โ”€ core/ # Core classes
95
+ โ”‚ โ”œโ”€โ”€ Diamond.ts # Diamond state + selector registry
96
+ โ”‚ โ”œโ”€โ”€ DiamondDeployer.ts # Deploy/upgrade orchestration
97
+ โ”‚ โ”œโ”€โ”€ DeploymentManager.ts # Deployment lifecycle
98
+ โ”‚ โ””โ”€โ”€ CallbackManager.ts # Post-deployment callbacks
99
+ โ”œโ”€โ”€ strategies/ # Deployment strategies
100
+ โ”‚ โ”œโ”€โ”€ DeploymentStrategy.ts # Strategy interface
101
+ โ”‚ โ”œโ”€โ”€ BaseDeploymentStrategy.ts
102
+ โ”‚ โ”œโ”€โ”€ LocalDeploymentStrategy.ts
103
+ โ”‚ โ”œโ”€โ”€ RPCDeploymentStrategy.ts
104
+ โ”‚ โ””โ”€โ”€ OZDefenderDeploymentStrategy.ts # legacy (see note below)
105
+ โ”œโ”€โ”€ repositories/ # Data persistence
106
+ โ”‚ โ”œโ”€โ”€ DeploymentRepository.ts
107
+ โ”‚ โ”œโ”€โ”€ FileDeploymentRepository.ts
108
+ โ”‚ โ””โ”€โ”€ jsonFileHandler.ts
109
+ โ”œโ”€โ”€ schemas/ # Zod validation schemas
110
+ โ”œโ”€โ”€ types/ # TypeScript definitions
111
+ โ”œโ”€โ”€ utils/ # Utilities (ABI generation, loupe, selectors, โ€ฆ)
112
+ โ””โ”€โ”€ cli/ # `diamond-abi` CLI
106
113
  ```
107
114
 
108
115
  ## ๐Ÿ”ง Configuration
109
116
 
110
117
  ### Diamond Configuration
111
118
 
112
- Create a diamond configuration file (`myDiamond.config.json`):
119
+ Create a diamond configuration file (e.g. `myDiamond.config.json`):
113
120
 
114
121
  ```json
115
122
  {
@@ -118,15 +125,11 @@ Create a diamond configuration file (`myDiamond.config.json`):
118
125
  "facets": {
119
126
  "DiamondCutFacet": {
120
127
  "priority": 10,
121
- "versions": {
122
- "1.0": {}
123
- }
128
+ "versions": { "1.0": {} }
124
129
  },
125
130
  "DiamondLoupeFacet": {
126
131
  "priority": 20,
127
- "versions": {
128
- "1.0": {}
129
- }
132
+ "versions": { "1.0": {} }
130
133
  },
131
134
  "MyCustomFacet": {
132
135
  "priority": 30,
@@ -152,105 +155,87 @@ NETWORK_NAME=sepolia
152
155
  CHAIN_ID=11155111
153
156
  RPC_URL=https://sepolia.infura.io/v3/YOUR_KEY
154
157
 
155
- # OpenZeppelin Defender (Optional)
158
+ # OpenZeppelin Defender (only for the legacy Defender strategy)
156
159
  DEFENDER_API_KEY=your_api_key
157
160
  DEFENDER_API_SECRET=your_api_secret
158
161
  DEFENDER_RELAYER_ADDRESS=0x...
159
162
  DEFENDER_SAFE_ADDRESS=0x...
160
163
 
161
164
  # Deployment Options
162
- AUTO_APPROVE_DEFENDER_PROPOSALS=false
163
165
  VERBOSE_DEPLOYMENT=true
164
166
  ```
165
167
 
166
- ## ๐Ÿ›ก๏ธ OpenZeppelin Defender Integration
167
-
168
- The Diamonds module provides seamless integration with OpenZeppelin Defender for enterprise-grade deployments.
169
-
170
- ### Setup Defender
171
-
172
- 1. **Create Defender Account**: Visit [OpenZeppelin Defender](https://defender.openzeppelin.com/)
173
- 2. **Generate API Credentials**: Create API keys with Deploy and Admin permissions
174
- 3. **Configure Environment**: Set your API credentials in `.env`
175
-
176
- ### Deploy with Defender
177
-
178
- ```typescript
179
- import { OZDefenderDeploymentStrategy } from '@geniusventures/diamonds/strategies';
180
-
181
- const strategy = new OZDefenderDeploymentStrategy(
182
- process.env.DEFENDER_API_KEY!,
183
- process.env.DEFENDER_API_SECRET!,
184
- process.env.DEFENDER_RELAYER_ADDRESS!,
185
- false, // manual approval for production
186
- process.env.DEFENDER_SAFE_ADDRESS!,
187
- 'Safe'
188
- );
189
-
190
- const deployer = new DiamondDeployer(diamond, strategy);
191
- await deployer.deployDiamond();
192
- ```
193
-
194
- ### Features
195
-
196
- - **Secure Deployments**: All deployments go through Defender's secure infrastructure
197
- - **Multi-signature Support**: Integrate with Gnosis Safe for production deployments
198
- - **Automated Monitoring**: Real-time deployment tracking and status updates
199
- - **Gas Optimization**: Automatic gas price optimization and retry logic
200
-
201
168
  ## ๐Ÿ”„ Deployment Strategies
202
169
 
170
+ All strategies extend `BaseDeploymentStrategy` and accept an optional `verbose`
171
+ flag. Pass a strategy instance to `DiamondDeployer`.
172
+
203
173
  ### Local Strategy
204
174
 
205
- For development and testing:
175
+ For development and testing against a local/forked node:
206
176
 
207
177
  ```typescript
208
- import { LocalDeploymentStrategy } from '@geniusventures/diamonds/strategies';
178
+ import { LocalDeploymentStrategy } from "@diamondslab/diamonds";
209
179
 
210
- const strategy = new LocalDeploymentStrategy(true); // verbose logging
180
+ const strategy = new LocalDeploymentStrategy(true); // verbose
211
181
  ```
212
182
 
213
- ### Defender Strategy
183
+ ### RPC Strategy
214
184
 
215
- For production deployments:
185
+ For deploying through a configured RPC endpoint / signer:
216
186
 
217
187
  ```typescript
218
- import { OZDefenderDeploymentStrategy } from '@geniusventures/diamonds/strategies';
188
+ import { RPCDeploymentStrategy } from "@diamondslab/diamonds";
219
189
 
220
- const strategy = new OZDefenderDeploymentStrategy(
221
- apiKey,
222
- apiSecret,
223
- relayerAddress,
224
- autoApprove,
225
- viaAddress,
226
- viaType,
227
- verbose
228
- );
190
+ const strategy =
191
+ new RPCDeploymentStrategy(/* see RPCDeploymentStrategy options */);
229
192
  ```
230
193
 
231
194
  ### Custom Strategy
232
195
 
233
- Implement your own deployment strategy:
196
+ Implement your own by extending `BaseDeploymentStrategy` and overriding the
197
+ protected task hooks:
234
198
 
235
199
  ```typescript
236
- import { BaseDeploymentStrategy } from '@geniusventures/diamonds/strategies';
200
+ import { BaseDeploymentStrategy, Diamond } from "@diamondslab/diamonds";
237
201
 
238
202
  export class CustomDeploymentStrategy extends BaseDeploymentStrategy {
239
203
  protected async deployDiamondTasks(diamond: Diamond): Promise<void> {
240
204
  // Custom deployment logic
241
205
  }
242
-
206
+
243
207
  protected async performDiamondCutTasks(diamond: Diamond): Promise<void> {
244
208
  // Custom diamond cut logic
245
209
  }
246
210
  }
247
211
  ```
248
212
 
213
+ ### OpenZeppelin Defender Strategy (legacy)
214
+
215
+ > โš ๏ธ **Deprecated / legacy.** `OZDefenderDeploymentStrategy` is being phased out
216
+ > and is not recommended for new work. It remains exported for backward
217
+ > compatibility. Prefer `LocalDeploymentStrategy` / `RPCDeploymentStrategy` or a
218
+ > custom strategy.
219
+
220
+ ```typescript
221
+ import { OZDefenderDeploymentStrategy } from "@diamondslab/diamonds";
222
+
223
+ const strategy = new OZDefenderDeploymentStrategy(
224
+ process.env.DEFENDER_API_KEY!,
225
+ process.env.DEFENDER_API_SECRET!,
226
+ process.env.DEFENDER_RELAYER_ADDRESS!,
227
+ false, // auto-approve
228
+ process.env.DEFENDER_SAFE_ADDRESS!,
229
+ "Safe",
230
+ );
231
+ ```
232
+
249
233
  ## ๐Ÿ“Š Advanced Features
250
234
 
251
235
  ### Version Management
252
236
 
253
- The Diamonds module provides sophisticated version management:
237
+ Facets are versioned in the configuration; upgrades declare which prior versions
238
+ they apply from:
254
239
 
255
240
  ```json
256
241
  {
@@ -258,10 +243,7 @@ The Diamonds module provides sophisticated version management:
258
243
  "MyFacet": {
259
244
  "priority": 100,
260
245
  "versions": {
261
- "1.0": {
262
- "deployInit": "initialize()",
263
- "upgradeInit": "upgradeToV1()"
264
- },
246
+ "1.0": { "deployInit": "initialize()", "upgradeInit": "upgradeToV1()" },
265
247
  "2.0": {
266
248
  "deployInit": "initialize()",
267
249
  "upgradeInit": "upgradeToV2()",
@@ -275,344 +257,207 @@ The Diamonds module provides sophisticated version management:
275
257
 
276
258
  ### Function Selector Management
277
259
 
278
- Automatic function selector management with collision detection:
260
+ Automatic selector management with collision detection:
279
261
 
280
- - **Priority-based Resolution**: Higher priority facets take precedence
281
- - **Include/Exclude Lists**: Fine-grained control over function selectors
282
- - **Collision Detection**: Automatic detection and resolution of selector conflicts
283
- - **Orphaned Selector Prevention**: Validation to prevent deployment issues
262
+ - **Priority-based resolution**: higher-priority facets take precedence
263
+ - **Include/Exclude lists**: fine-grained selector control (`deployInclude` / `deployExclude`)
264
+ - **Collision detection**: automatic detection and resolution of selector conflicts
265
+ - **Orphaned selector prevention**: validation guards against bad cuts
284
266
 
285
267
  ### Post-Deployment Callbacks
286
268
 
287
- Execute custom logic after deployment:
288
-
289
269
  ```typescript
290
270
  // In your callbacks directory
291
271
  export async function postDeployCallback(args: CallbackArgs) {
292
272
  const { diamond } = args;
293
- console.log(`Post-deployment callback executed for ${diamond.diamondName}`);
294
-
273
+ console.log(`Post-deployment callback for ${diamond.diamondName}`);
295
274
  // Custom post-deployment logic
296
- await customInitialization();
297
275
  }
298
276
  ```
299
277
 
300
- ## ๐Ÿงช Testing
301
-
302
- ### Running Tests
303
-
304
- ```bash
305
- # Install dependencies
306
- npm install
307
-
308
- # Run all tests
309
- npm test
310
-
311
- # Run unit tests only
312
- npm run test:unit
313
-
314
- # Run integration tests
315
- npm run test:integration
316
-
317
- # Run with coverage
318
- npm run test:coverage
319
-
320
- # Test specific network
321
- TEST_NETWORK=sepolia npm test
322
- ```
323
-
324
- ### Test Categories
325
-
326
- - **Unit Tests**: Individual component testing
327
- - **Integration Tests**: Component interaction testing
328
- - **Defender Integration**: End-to-end Defender testing
329
- - **Performance Tests**: Deployment speed and resource usage
330
-
331
- ### Example Test
332
-
333
- ```typescript
334
- describe('Diamond Deployment', () => {
335
- let diamond: Diamond;
336
- let strategy: LocalDeploymentStrategy;
337
-
338
- beforeEach(() => {
339
- const config = {
340
- diamondName: 'TestDiamond',
341
- networkName: 'hardhat',
342
- chainId: 31337,
343
- deploymentsPath: './test-diamonds',
344
- contractsPath: './contracts'
345
- };
346
-
347
- const repository = new FileDeploymentRepository(config);
348
- diamond = new Diamond(config, repository);
349
- strategy = new LocalDeploymentStrategy();
350
- });
351
-
352
- it('should deploy diamond successfully', async () => {
353
- const deployer = new DiamondDeployer(diamond, strategy);
354
- await deployer.deployDiamond();
355
-
356
- const deployedData = diamond.getDeployedDiamondData();
357
- expect(deployedData.DiamondAddress).to.not.be.undefined;
358
- });
359
- });
360
- ```
361
-
362
- ## ๐Ÿ› ๏ธ CLI Tools
278
+ ## ๐Ÿ’Ž Diamond ABI Tooling
363
279
 
364
- The Diamonds module includes a comprehensive CLI for deployment management:
280
+ This package ships a `diamond-abi` CLI for working with the combined Diamond ABI.
365
281
 
366
282
  ```bash
367
- # Install CLI globally
368
- npm install -g @geniusventures/diamonds
369
-
370
- # Deploy a diamond
371
- diamonds deploy --diamond MyDiamond --network sepolia
372
-
373
- # Upgrade a diamond
374
- diamonds upgrade --diamond MyDiamond
283
+ # Generate the combined ABI for a deployed diamond (requires a Hardhat project)
284
+ npx diamond-abi generate --diamond MyDiamond --network localhost
375
285
 
376
- # Check deployment status
377
- diamonds status --diamond MyDiamond
286
+ # Preview the ABI changes implied by planned cuts (requires a Hardhat project)
287
+ npx diamond-abi preview --diamond MyDiamond --verbose
378
288
 
379
- # Verify contracts
380
- diamonds verify --diamond MyDiamond
289
+ # Compare two ABI files (no chain / Hardhat needed)
290
+ npx diamond-abi compare old-abi.json new-abi.json
381
291
 
382
- # Initialize new project
383
- diamonds init --diamond MyNewDiamond --network mainnet
292
+ # Validate an ABI artifact file (no chain / Hardhat needed)
293
+ npx diamond-abi validate diamond-abi.json
384
294
  ```
385
295
 
386
- ### CLI Options
296
+ > `generate` and `preview` connect to a chain and load your project's Hardhat
297
+ > config (run them inside a Hardhat project). `compare` and `validate` operate on
298
+ > ABI JSON files and run standalone. Run `npx diamond-abi <command> --help` for
299
+ > all options.
387
300
 
388
- - `--diamond <name>`: Diamond name
389
- - `--network <name>`: Target network
390
- - `--config <path>`: Configuration file path
391
- - `--auto-approve`: Auto-approve Defender proposals
392
- - `--verbose`: Verbose output
393
- - `--batch <path>`: Batch deployment configuration
301
+ The same combined ABI can also be produced programmatically via
302
+ `generateDiamondAbi` / `previewDiamondAbi` from `@diamondslab/diamonds`.
394
303
 
395
304
  ## ๐Ÿ“– Examples
396
305
 
397
306
  ### Basic Diamond Deployment
398
307
 
399
308
  ```typescript
400
- import { Diamond, DiamondDeployer, FileDeploymentRepository } from '@geniusventures/diamonds';
401
- import { LocalDeploymentStrategy } from '@geniusventures/diamonds/strategies';
309
+ import {
310
+ Diamond,
311
+ DiamondDeployer,
312
+ FileDeploymentRepository,
313
+ LocalDeploymentStrategy,
314
+ } from "@diamondslab/diamonds";
315
+ import { ethers } from "hardhat";
402
316
 
403
317
  async function deployBasicDiamond() {
404
318
  const config = {
405
- diamondName: 'BasicDiamond',
406
- networkName: 'localhost',
319
+ diamondName: "BasicDiamond",
320
+ networkName: "localhost",
407
321
  chainId: 31337,
408
- deploymentsPath: './diamonds',
409
- contractsPath: './contracts'
322
+ deploymentsPath: "./diamonds",
323
+ contractsPath: "./contracts",
410
324
  };
411
325
 
412
326
  const repository = new FileDeploymentRepository(config);
413
327
  const diamond = new Diamond(config, repository);
414
-
328
+
415
329
  diamond.setProvider(ethers.provider);
416
- diamond.setSigner(await ethers.getSigners()[0]);
330
+ const [signer] = await ethers.getSigners();
331
+ diamond.setSigner(signer);
417
332
 
418
333
  const strategy = new LocalDeploymentStrategy();
419
334
  const deployer = new DiamondDeployer(diamond, strategy);
420
335
 
421
336
  await deployer.deployDiamond();
422
- console.log('Diamond deployed successfully!');
423
- }
424
- ```
425
-
426
- ### Production Deployment with Defender
427
-
428
- ```typescript
429
- import { OZDefenderDeploymentStrategy } from '@geniusventures/diamonds/strategies';
430
-
431
- async function deployProductionDiamond() {
432
- const config = {
433
- diamondName: 'ProductionDiamond',
434
- networkName: 'mainnet',
435
- chainId: 1,
436
- deploymentsPath: './diamonds',
437
- contractsPath: './contracts'
438
- };
439
-
440
- const repository = new FileDeploymentRepository(config);
441
- const diamond = new Diamond(config, repository);
442
-
443
- // Setup provider and signer for mainnet
444
- diamond.setProvider(mainnetProvider);
445
- diamond.setSigner(productionSigner);
446
-
447
- const strategy = new OZDefenderDeploymentStrategy(
448
- process.env.DEFENDER_API_KEY!,
449
- process.env.DEFENDER_API_SECRET!,
450
- process.env.DEFENDER_RELAYER_ADDRESS!,
451
- false, // manual approval for production
452
- process.env.DEFENDER_SAFE_ADDRESS!,
453
- 'Safe'
454
- );
455
-
456
- const deployer = new DiamondDeployer(diamond, strategy);
457
- await deployer.deployDiamond();
337
+ console.log("Diamond deployed successfully!");
458
338
  }
459
339
  ```
460
340
 
461
- ### Complex Multi-Facet Upgrade
341
+ ### Multi-Facet Upgrade
462
342
 
463
343
  ```typescript
464
- async function performComplexUpgrade() {
465
- // Load existing diamond
466
- const diamond = await loadExistingDiamond('MyDiamond');
467
-
468
- // Update configuration for new version
344
+ async function performUpgrade(diamond: Diamond) {
345
+ // Update configuration for the new version
469
346
  const config = diamond.repository.loadDeployConfig();
470
347
  config.protocolVersion = 2.0;
471
-
472
- // Add new facet
348
+
349
+ // Add a new facet
473
350
  config.facets.NewFeatureFacet = {
474
351
  priority: 150,
475
352
  versions: {
476
- "2.0": {
477
- deployInit: "initialize()",
478
- callbacks: ["setupNewFeature"]
479
- }
480
- }
353
+ "2.0": { deployInit: "initialize()", callbacks: ["setupNewFeature"] },
354
+ },
481
355
  };
482
-
483
- // Upgrade existing facet
356
+
357
+ // Upgrade an existing facet
484
358
  config.facets.ExistingFacet.versions["2.0"] = {
485
359
  upgradeInit: "upgradeToV2()",
486
- fromVersions: [1.0]
360
+ fromVersions: [1.0],
487
361
  };
488
-
489
- await diamond.repository.saveDeployConfig(config);
490
-
491
- // Execute upgrade
492
- const strategy = new OZDefenderDeploymentStrategy(/* config */);
493
- const deployer = new DiamondDeployer(diamond, strategy);
362
+
363
+ diamond.repository.saveDeployConfig(config);
364
+
365
+ // DiamondDeployer detects the existing deployment and performs the upgrade
366
+ const deployer = new DiamondDeployer(diamond, new LocalDeploymentStrategy());
494
367
  await deployer.deployDiamond();
495
368
  }
496
369
  ```
497
370
 
498
371
  ## ๐Ÿ” Monitoring and Debugging
499
372
 
500
- ### Enable Verbose Logging
501
-
502
- ```typescript
503
- const strategy = new LocalDeploymentStrategy(true); // Enable verbose mode
504
- ```
505
-
506
- ### Debug Function Selectors
373
+ ### Inspect Function Selectors
507
374
 
508
375
  ```typescript
509
- import { getSighash } from '@geniusventures/diamonds/utils';
376
+ import { getSighash } from "@diamondslab/diamonds";
510
377
 
511
- // Get function selector
512
- const selector = getSighash('transfer(address,uint256)');
513
- console.log('Selector:', selector);
378
+ const selector = getSighash("transfer(address,uint256)");
379
+ console.log("Selector:", selector);
514
380
 
515
- // Check if selector is registered
516
- const isRegistered = diamond.isFunctionSelectorRegistered(selector);
517
- console.log('Is registered:', isRegistered);
381
+ console.log("Registered:", diamond.isFunctionSelectorRegistered(selector));
518
382
  ```
519
383
 
520
- ### Monitor Deployment State
384
+ ### Compare On-Chain vs. Local State
521
385
 
522
386
  ```typescript
523
- // Check deployment status
524
- const deployedData = diamond.getDeployedDiamondData();
525
- console.log('Diamond address:', deployedData.DiamondAddress);
526
- console.log('Deployed facets:', Object.keys(deployedData.DeployedFacets || {}));
387
+ import { diffDeployedFacets } from "@diamondslab/diamonds";
388
+ import { ethers } from "hardhat";
527
389
 
528
- // Validate on-chain state
529
- import { diffDeployedFacets } from '@geniusventures/diamonds/utils';
530
- const isConsistent = await diffDeployedFacets(deployedData, ethers.provider, true);
390
+ const deployedData = diamond.getDeployedDiamondData();
391
+ console.log("Diamond address:", deployedData.DiamondAddress);
392
+ console.log("Deployed facets:", Object.keys(deployedData.DeployedFacets ?? {}));
393
+
394
+ // Returns true when on-chain facets match the local deployment record
395
+ const isConsistent = await diffDeployedFacets(
396
+ deployedData,
397
+ ethers.provider,
398
+ true,
399
+ );
531
400
  ```
532
401
 
533
- ## ๐Ÿ” Security Considerations
534
-
535
- ### Production Best Practices
402
+ ## ๐Ÿงช Testing & Development
536
403
 
537
- 1. **Use Multi-signature Wallets**: Always use multi-sig for mainnet deployments
538
- 2. **Test Thoroughly**: Test all upgrades on testnets first
539
- 3. **Verify Contracts**: Ensure all contracts are verified on Etherscan
540
- 4. **Monitor Deployments**: Use Defender monitoring for production systems
541
- 5. **Access Control**: Implement proper role-based access controls
404
+ Run from the package directory:
542
405
 
543
- ### Secure Configuration
406
+ ```bash
407
+ yarn install # install dependencies (Yarn 4)
408
+ yarn build # compile TypeScript to dist/
409
+ yarn test # run the Hardhat test suite
410
+ yarn test:unit # unit tests only
411
+ yarn test:integration # integration tests only
412
+ yarn test:coverage # tests with coverage
413
+ yarn lint # lint
414
+ ```
544
415
 
545
- ```typescript
546
- // Production configuration example
547
- const productionConfig = {
548
- diamondName: 'ProductionDiamond',
549
- networkName: 'mainnet',
550
- chainId: 1,
551
- deploymentsPath: './production-diamonds',
552
- contractsPath: './contracts',
553
- writeDeployedDiamondData: true
554
- };
416
+ ## ๐Ÿ” Security Considerations
555
417
 
556
- const strategy = new OZDefenderDeploymentStrategy(
557
- process.env.PROD_DEFENDER_API_KEY!,
558
- process.env.PROD_DEFENDER_API_SECRET!,
559
- process.env.PROD_RELAYER_ADDRESS!,
560
- false, // Never auto-approve in production
561
- process.env.PROD_SAFE_ADDRESS!,
562
- 'Safe'
563
- );
564
- ```
418
+ 1. **Use multi-signature wallets** for mainnet deployments.
419
+ 2. **Test upgrades on a testnet/fork first.**
420
+ 3. **Verify contracts** on the relevant block explorer.
421
+ 4. **Apply role-based access control** to privileged diamond functions.
422
+ 5. **Never commit secrets** โ€” deployment records may contain addresses/keys and are gitignored.
565
423
 
566
424
  ## ๐Ÿค Contributing
567
425
 
568
- We welcome contributions! Please see our [Contributing Guide](CONTRIBUTING.md) for details.
569
-
570
- ### Development Setup
426
+ Contributions are welcome. Please open an issue or pull request on
427
+ [GitHub](https://github.com/DiamondsLab/diamonds).
571
428
 
572
429
  ```bash
573
- # Clone the repository
574
- git clone https://github.com/geniusventures/diamonds.git
430
+ git clone https://github.com/DiamondsLab/diamonds.git
575
431
  cd diamonds
576
-
577
- # Install dependencies
578
- npm install
579
-
580
- # Run tests
581
- npm test
582
-
583
- # Build the project
584
- npm run build
585
-
586
- # Run linting
587
- npm run lint
432
+ yarn install
433
+ yarn test
434
+ yarn build
435
+ yarn lint
588
436
  ```
589
437
 
590
438
  ### Coding Standards
591
439
 
592
- - Follow TypeScript best practices
593
- - Maintain 90%+ test coverage
594
- - Use conventional commit messages
440
+ - Follow the existing TypeScript style
441
+ - Use Conventional Commits
442
+ - Add tests for new functionality
595
443
  - Update documentation for new features
596
- - Add comprehensive tests for new functionality
597
444
 
598
445
  ## ๐Ÿ“„ License
599
446
 
600
- This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
447
+ MIT โ€” see the [LICENSE](LICENSE) file.
601
448
 
602
449
  ## ๐Ÿ™ Acknowledgments
603
450
 
604
- - [OpenZeppelin](https://openzeppelin.com/) for Defender platform and security tools
605
451
  - [ERC-2535 Diamond Standard](https://eips.ethereum.org/EIPS/eip-2535) authors
606
452
  - [Hardhat](https://hardhat.org/) development framework
607
- - The Ethereum community for continuous innovation
453
+ - [OpenZeppelin](https://openzeppelin.com/) for security tooling
454
+ - The Ethereum community
608
455
 
609
456
  ## ๐Ÿ“ž Support
610
457
 
611
- - **Documentation**: [Full Documentation](https://docs.geniusventures.com/diamonds)
612
- - **GitHub Issues**: [Report Issues](https://github.com/geniusventures/diamonds/issues)
613
- - **Discord**: [Join our Community](https://discord.gg/geniusventures)
614
- - **Email**: <support@geniusventures.com>
458
+ - **Documentation**: see the [`docs/`](docs/) directory
459
+ - **Issues**: [github.com/DiamondsLab/diamonds/issues](https://github.com/DiamondsLab/diamonds/issues)
615
460
 
616
461
  ---
617
462
 
618
- **Built with โค๏ธ for the Ethereum ecosystem by [Genius Ventures](https://geniusventures.com)**
463
+ **Built with โค๏ธ for the Ethereum ecosystem by [DiamondsLab](https://github.com/DiamondsLab)**