@diamondslab/diamonds 1.0.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 +618 -0
- package/diamonds/README.md +3 -0
- package/dist/core/CallbackManager.d.ts +13 -0
- package/dist/core/CallbackManager.d.ts.map +1 -0
- package/dist/core/CallbackManager.js +95 -0
- package/dist/core/CallbackManager.js.map +1 -0
- package/dist/core/DeploymentManager.d.ts +10 -0
- package/dist/core/DeploymentManager.d.ts.map +1 -0
- package/dist/core/DeploymentManager.js +50 -0
- package/dist/core/DeploymentManager.js.map +1 -0
- package/dist/core/Diamond.d.ts +58 -0
- package/dist/core/Diamond.d.ts.map +1 -0
- package/dist/core/Diamond.js +146 -0
- package/dist/core/Diamond.js.map +1 -0
- package/dist/core/DiamondDeployer.d.ts +10 -0
- package/dist/core/DiamondDeployer.d.ts.map +1 -0
- package/dist/core/DiamondDeployer.js +33 -0
- package/dist/core/DiamondDeployer.js.map +1 -0
- package/dist/core/index.d.ts +5 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +12 -0
- package/dist/core/index.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +22 -0
- package/dist/index.js.map +1 -0
- package/dist/repositories/DBDeploymentRepository.d.ts +1 -0
- package/dist/repositories/DBDeploymentRepository.d.ts.map +1 -0
- package/dist/repositories/DBDeploymentRepository.js +20 -0
- package/dist/repositories/DBDeploymentRepository.js.map +1 -0
- package/dist/repositories/DeploymentRepository.d.ts +8 -0
- package/dist/repositories/DeploymentRepository.d.ts.map +1 -0
- package/dist/repositories/DeploymentRepository.js +7 -0
- package/dist/repositories/DeploymentRepository.js.map +1 -0
- package/dist/repositories/FileDeploymentRepository.d.ts +18 -0
- package/dist/repositories/FileDeploymentRepository.d.ts.map +1 -0
- package/dist/repositories/FileDeploymentRepository.js +58 -0
- package/dist/repositories/FileDeploymentRepository.js.map +1 -0
- package/dist/repositories/databaseHandler.d.ts +1 -0
- package/dist/repositories/databaseHandler.d.ts.map +1 -0
- package/dist/repositories/databaseHandler.js +13 -0
- package/dist/repositories/databaseHandler.js.map +1 -0
- package/dist/repositories/index.d.ts +4 -0
- package/dist/repositories/index.d.ts.map +1 -0
- package/dist/repositories/index.js +20 -0
- package/dist/repositories/index.js.map +1 -0
- package/dist/repositories/jsonFileHandler.d.ts +81 -0
- package/dist/repositories/jsonFileHandler.d.ts.map +1 -0
- package/dist/repositories/jsonFileHandler.js +223 -0
- package/dist/repositories/jsonFileHandler.js.map +1 -0
- package/dist/repositories/prismaDBHandler.d.ts +1 -0
- package/dist/repositories/prismaDBHandler.d.ts.map +1 -0
- package/dist/repositories/prismaDBHandler.js +11 -0
- package/dist/repositories/prismaDBHandler.js.map +1 -0
- package/dist/schemas/DeploymentSchema.d.ts +309 -0
- package/dist/schemas/DeploymentSchema.d.ts.map +1 -0
- package/dist/schemas/DeploymentSchema.js +56 -0
- package/dist/schemas/DeploymentSchema.js.map +1 -0
- package/dist/schemas/index.d.ts +2 -0
- package/dist/schemas/index.d.ts.map +1 -0
- package/dist/schemas/index.js +18 -0
- package/dist/schemas/index.js.map +1 -0
- package/dist/strategies/BaseDeploymentStrategy.d.ts +41 -0
- package/dist/strategies/BaseDeploymentStrategy.d.ts.map +1 -0
- package/dist/strategies/BaseDeploymentStrategy.js +545 -0
- package/dist/strategies/BaseDeploymentStrategy.js.map +1 -0
- package/dist/strategies/DeploymentStrategy.d.ts +19 -0
- package/dist/strategies/DeploymentStrategy.d.ts.map +1 -0
- package/dist/strategies/DeploymentStrategy.js +3 -0
- package/dist/strategies/DeploymentStrategy.js.map +1 -0
- package/dist/strategies/LocalDeploymentStrategy.d.ts +4 -0
- package/dist/strategies/LocalDeploymentStrategy.d.ts.map +1 -0
- package/dist/strategies/LocalDeploymentStrategy.js +8 -0
- package/dist/strategies/LocalDeploymentStrategy.js.map +1 -0
- package/dist/strategies/OZDefenderDeploymentStrategy.d.ts +62 -0
- package/dist/strategies/OZDefenderDeploymentStrategy.d.ts.map +1 -0
- package/dist/strategies/OZDefenderDeploymentStrategy.js +757 -0
- package/dist/strategies/OZDefenderDeploymentStrategy.js.map +1 -0
- package/dist/strategies/RPCDeploymentStrategy.d.ts +139 -0
- package/dist/strategies/RPCDeploymentStrategy.d.ts.map +1 -0
- package/dist/strategies/RPCDeploymentStrategy.js +710 -0
- package/dist/strategies/RPCDeploymentStrategy.js.map +1 -0
- package/dist/strategies/index.d.ts +6 -0
- package/dist/strategies/index.d.ts.map +1 -0
- package/dist/strategies/index.js +12 -0
- package/dist/strategies/index.js.map +1 -0
- package/dist/types/config.d.ts +26 -0
- package/dist/types/config.d.ts.map +1 -0
- package/dist/types/config.js +3 -0
- package/dist/types/config.js.map +1 -0
- package/dist/types/defender.d.ts +22 -0
- package/dist/types/defender.d.ts.map +1 -0
- package/dist/types/defender.js +3 -0
- package/dist/types/defender.js.map +1 -0
- package/dist/types/deployments.d.ts +71 -0
- package/dist/types/deployments.d.ts.map +1 -0
- package/dist/types/deployments.js +20 -0
- package/dist/types/deployments.js.map +1 -0
- package/dist/types/index.d.ts +5 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +21 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/rpc.d.ts +35 -0
- package/dist/types/rpc.d.ts.map +1 -0
- package/dist/types/rpc.js +3 -0
- package/dist/types/rpc.js.map +1 -0
- package/dist/utils/common.d.ts +20 -0
- package/dist/utils/common.d.ts.map +1 -0
- package/dist/utils/common.js +45 -0
- package/dist/utils/common.js.map +1 -0
- package/dist/utils/configurationResolver.d.ts +30 -0
- package/dist/utils/configurationResolver.d.ts.map +1 -0
- package/dist/utils/configurationResolver.js +151 -0
- package/dist/utils/configurationResolver.js.map +1 -0
- package/dist/utils/contractMapping.d.ts +29 -0
- package/dist/utils/contractMapping.d.ts.map +1 -0
- package/dist/utils/contractMapping.js +224 -0
- package/dist/utils/contractMapping.js.map +1 -0
- package/dist/utils/defenderClients.d.ts +5 -0
- package/dist/utils/defenderClients.d.ts.map +1 -0
- package/dist/utils/defenderClients.js +21 -0
- package/dist/utils/defenderClients.js.map +1 -0
- package/dist/utils/defenderStore.d.ts +14 -0
- package/dist/utils/defenderStore.d.ts.map +1 -0
- package/dist/utils/defenderStore.js +92 -0
- package/dist/utils/defenderStore.js.map +1 -0
- package/dist/utils/diamondAbiGenerator.d.ts +113 -0
- package/dist/utils/diamondAbiGenerator.d.ts.map +1 -0
- package/dist/utils/diamondAbiGenerator.js +415 -0
- package/dist/utils/diamondAbiGenerator.js.map +1 -0
- package/dist/utils/diffDeployedFacets.d.ts +26 -0
- package/dist/utils/diffDeployedFacets.d.ts.map +1 -0
- package/dist/utils/diffDeployedFacets.js +106 -0
- package/dist/utils/diffDeployedFacets.js.map +1 -0
- package/dist/utils/index.d.ts +16 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +35 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/loupe.d.ts +44 -0
- package/dist/utils/loupe.d.ts.map +1 -0
- package/dist/utils/loupe.js +128 -0
- package/dist/utils/loupe.js.map +1 -0
- package/dist/utils/rpcStore.d.ts +36 -0
- package/dist/utils/rpcStore.d.ts.map +1 -0
- package/dist/utils/rpcStore.js +166 -0
- package/dist/utils/rpcStore.js.map +1 -0
- package/dist/utils/signer.d.ts +36 -0
- package/dist/utils/signer.d.ts.map +1 -0
- package/dist/utils/signer.js +91 -0
- package/dist/utils/signer.js.map +1 -0
- package/dist/utils/txlogging.d.ts +13 -0
- package/dist/utils/txlogging.d.ts.map +1 -0
- package/dist/utils/txlogging.js +87 -0
- package/dist/utils/txlogging.js.map +1 -0
- package/dist/utils/workspaceSetup.d.ts +32 -0
- package/dist/utils/workspaceSetup.d.ts.map +1 -0
- package/dist/utils/workspaceSetup.js +311 -0
- package/dist/utils/workspaceSetup.js.map +1 -0
- package/docs/DIAMOND_ABI_CONFIGURATION_SUMMARY.md +40 -0
- package/docs/DIAMOND_ABI_GENERATION.md +220 -0
- package/docs/DIAMOND_ABI_GENERATOR_EXAMPLES.md +1204 -0
- package/docs/DIAMOND_ABI_GENERATOR_IMPLEMENTATION.md +947 -0
- package/docs/DIAMOND_ABI_GENERATOR_QUICK_REFERENCE.md +336 -0
- package/docs/README-DEFENDER.md +394 -0
- package/docs/README_DIAMOND_ABI_GENERATOR.md +303 -0
- package/docs/ROADMAP.md +250 -0
- package/docs/assets/image.png +0 -0
- package/docs/defender-integration.md +451 -0
- package/docs/diamond_module-BaseStrategy_design-v2.uxf +247 -0
- package/docs/diamond_module-BaseStrategy_design.uxf +272 -0
- package/docs/monitoring-troubleshooting.md +556 -0
- package/docs/testing-guide.md +713 -0
- package/examples/Diamond_Config_and_Deployment_examples/diamonds/ProxyDiamond/callbacks/ERC20ProxyFacet.ts +31 -0
- package/examples/Diamond_Config_and_Deployment_examples/diamonds/ProxyDiamond/proxydiamond.config.json +27 -0
- package/examples/Local_Hardhat_Deployer_Script_example/LocalDiamondDeployer.ts +180 -0
- package/examples/OZ_Defender_Deployer_Script_example/OZDiamondDeployer.ts +107 -0
- package/examples/OZ_Defender_Deployer_Script_example/run-oz-deploy.ts +17 -0
- package/examples/Test_examples/ProxyDiamondDeployment.test.ts +202 -0
- package/examples/defender-deployment/.env.example +35 -0
- package/examples/defender-deployment/README.md +415 -0
- package/examples/defender-deployment/contracts/ExampleDiamond.sol +41 -0
- package/examples/defender-deployment/contracts/ExampleFacet1.sol +84 -0
- package/examples/defender-deployment/contracts/ExampleFacet2.sol +104 -0
- package/examples/defender-deployment/contracts/UpgradeFacet.sol +92 -0
- package/examples/defender-deployment/deploy-script.ts +170 -0
- package/examples/defender-deployment/diamond-config.json +36 -0
- package/examples/defender-deployment/upgrade-script.ts +237 -0
- package/examples/hardhat-diamonds-config.example.ts +41 -0
- package/package.json +228 -0
- package/src/core/CallbackManager.ts +70 -0
- package/src/core/DeploymentManager.ts +64 -0
- package/src/core/Diamond.ts +197 -0
- package/src/core/DiamondDeployer.ts +36 -0
- package/src/core/index.ts +4 -0
- package/src/index.ts +5 -0
- package/src/repositories/DBDeploymentRepository.ts +22 -0
- package/src/repositories/DeploymentRepository.ts +12 -0
- package/src/repositories/FileDeploymentRepository.ts +67 -0
- package/src/repositories/databaseHandler.ts +14 -0
- package/src/repositories/index.ts +4 -0
- package/src/repositories/jsonFileHandler.ts +252 -0
- package/src/repositories/prismaDBHandler.ts +10 -0
- package/src/schemas/DeploymentSchema.ts +71 -0
- package/src/schemas/index.ts +1 -0
- package/src/strategies/BaseDeploymentStrategy.ts +649 -0
- package/src/strategies/DeploymentStrategy.ts +25 -0
- package/src/strategies/LocalDeploymentStrategy.ts +5 -0
- package/src/strategies/OZDefenderDeploymentStrategy.ts +849 -0
- package/src/strategies/RPCDeploymentStrategy.ts +881 -0
- package/src/strategies/index.ts +5 -0
- package/src/types/config.ts +34 -0
- package/src/types/defender.ts +24 -0
- package/src/types/deployments.ts +102 -0
- package/src/types/index.ts +4 -0
- package/src/types/rpc.ts +37 -0
- package/src/utils/common.ts +54 -0
- package/src/utils/configurationResolver.ts +141 -0
- package/src/utils/contractMapping.ts +220 -0
- package/src/utils/defenderClients.ts +22 -0
- package/src/utils/defenderStore.ts +62 -0
- package/src/utils/diamondAbiGenerator.ts +523 -0
- package/src/utils/diffDeployedFacets.ts +131 -0
- package/src/utils/index.ts +15 -0
- package/src/utils/loupe.ts +159 -0
- package/src/utils/rpcStore.ts +152 -0
- package/src/utils/signer.ts +93 -0
- package/src/utils/txlogging.ts +97 -0
- package/src/utils/workspaceSetup.ts +315 -0
- package/test/README.md +136 -0
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
import * as fs from 'fs-extra';
|
|
2
|
+
import * as path from 'path';
|
|
3
|
+
import { ConfigurationResolver } from './configurationResolver';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Workspace setup utility for diamonds module
|
|
7
|
+
* Supports both standalone and hardhat-diamonds plugin configurations
|
|
8
|
+
*/
|
|
9
|
+
export class WorkspaceSetup {
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Initialize a new diamond workspace with default structure
|
|
13
|
+
*/
|
|
14
|
+
static async initializeWorkspace(
|
|
15
|
+
diamondName: string,
|
|
16
|
+
options: {
|
|
17
|
+
useHardhatPlugin?: boolean;
|
|
18
|
+
contractsPath?: string;
|
|
19
|
+
deploymentsPath?: string;
|
|
20
|
+
createExampleConfig?: boolean;
|
|
21
|
+
} = {}
|
|
22
|
+
): Promise<void> {
|
|
23
|
+
const {
|
|
24
|
+
useHardhatPlugin = false,
|
|
25
|
+
contractsPath = './contracts',
|
|
26
|
+
deploymentsPath = './diamonds',
|
|
27
|
+
createExampleConfig = true
|
|
28
|
+
} = options;
|
|
29
|
+
|
|
30
|
+
console.log(`Initializing ${diamondName} workspace...`);
|
|
31
|
+
|
|
32
|
+
// Create basic directory structure
|
|
33
|
+
const diamondsDir = path.join(process.cwd(), deploymentsPath);
|
|
34
|
+
const diamondDir = path.join(diamondsDir, diamondName);
|
|
35
|
+
const deploymentsDir = path.join(diamondDir, 'deployments');
|
|
36
|
+
const callbacksDir = path.join(diamondDir, 'callbacks');
|
|
37
|
+
const contractsDir = path.join(process.cwd(), contractsPath);
|
|
38
|
+
|
|
39
|
+
await fs.ensureDir(diamondsDir);
|
|
40
|
+
await fs.ensureDir(diamondDir);
|
|
41
|
+
await fs.ensureDir(deploymentsDir);
|
|
42
|
+
await fs.ensureDir(callbacksDir);
|
|
43
|
+
await fs.ensureDir(contractsDir);
|
|
44
|
+
|
|
45
|
+
console.log(`✅ Created directory structure for ${diamondName}`);
|
|
46
|
+
|
|
47
|
+
// Create default configuration file
|
|
48
|
+
if (createExampleConfig) {
|
|
49
|
+
await this.createDefaultConfig(diamondName, diamondDir);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Create example callback file
|
|
53
|
+
await this.createExampleCallback(diamondName, callbacksDir);
|
|
54
|
+
|
|
55
|
+
// Create example contracts if they don't exist
|
|
56
|
+
await this.createExampleContracts(contractsDir);
|
|
57
|
+
|
|
58
|
+
// Create hardhat config extension example if requested
|
|
59
|
+
if (useHardhatPlugin) {
|
|
60
|
+
await this.createHardhatConfigExample(diamondName);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
console.log(`✅ ${diamondName} workspace initialized successfully!`);
|
|
64
|
+
console.log(`
|
|
65
|
+
Configuration files created:
|
|
66
|
+
- Diamond config: ${path.join(diamondDir, `${diamondName.toLowerCase()}.config.json`)}
|
|
67
|
+
- Callbacks: ${path.join(callbacksDir, 'ExampleFacet.ts')}
|
|
68
|
+
- Contracts: ${contractsDir}
|
|
69
|
+
|
|
70
|
+
${useHardhatPlugin ?
|
|
71
|
+
'- Hardhat config example: ./hardhat-diamonds-config.example.ts' :
|
|
72
|
+
'- Using standalone mode (no hardhat plugin required)'
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
Next steps:
|
|
76
|
+
1. Customize your diamond configuration in the config file
|
|
77
|
+
2. Add your facet contracts to ${contractsPath}
|
|
78
|
+
3. Implement callback functions in ${path.join(callbacksDir)}
|
|
79
|
+
4. Deploy using: DiamondDeployer with your chosen strategy
|
|
80
|
+
`);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Create a default diamond configuration file
|
|
85
|
+
*/
|
|
86
|
+
private static async createDefaultConfig(diamondName: string, diamondDir: string): Promise<void> {
|
|
87
|
+
const configPath = path.join(diamondDir, `${diamondName.toLowerCase()}.config.json`);
|
|
88
|
+
|
|
89
|
+
if (await fs.pathExists(configPath)) {
|
|
90
|
+
console.log(`⚠️ Configuration file already exists: ${configPath}`);
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const defaultConfig = {
|
|
95
|
+
protocolVersion: 0.0,
|
|
96
|
+
facets: {
|
|
97
|
+
DiamondCutFacet: {
|
|
98
|
+
priority: 10,
|
|
99
|
+
versions: {
|
|
100
|
+
"0.0": {}
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
DiamondLoupeFacet: {
|
|
104
|
+
priority: 20,
|
|
105
|
+
versions: {
|
|
106
|
+
"0.0": {}
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
ExampleFacet: {
|
|
110
|
+
priority: 30,
|
|
111
|
+
versions: {
|
|
112
|
+
"0.0": {
|
|
113
|
+
callbacks: ["exampleCallback"],
|
|
114
|
+
deployInit: "initialize()",
|
|
115
|
+
upgradeInit: "reinitialize()"
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
await fs.writeJson(configPath, defaultConfig, { spaces: 2 });
|
|
123
|
+
console.log(`✅ Created default configuration: ${configPath}`);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Create an example callback file
|
|
128
|
+
*/
|
|
129
|
+
private static async createExampleCallback(diamondName: string, callbacksDir: string): Promise<void> {
|
|
130
|
+
const callbackPath = path.join(callbacksDir, 'ExampleFacet.ts');
|
|
131
|
+
|
|
132
|
+
if (await fs.pathExists(callbackPath)) {
|
|
133
|
+
console.log(`⚠️ Callback file already exists: ${callbackPath}`);
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const callbackContent = `import { CallbackArgs } from "../../../src/types";
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Example callback function for ExampleFacet
|
|
141
|
+
* This function will be called after the facet is deployed
|
|
142
|
+
*/
|
|
143
|
+
export async function exampleCallback(args: CallbackArgs) {
|
|
144
|
+
const { diamond } = args;
|
|
145
|
+
|
|
146
|
+
console.log(\`Running example callback for \${diamond.diamondName} on \${diamond.networkName}\`);
|
|
147
|
+
|
|
148
|
+
// Add your post-deployment logic here
|
|
149
|
+
// For example:
|
|
150
|
+
// - Initialize contract state
|
|
151
|
+
// - Set up permissions
|
|
152
|
+
// - Configure parameters
|
|
153
|
+
|
|
154
|
+
console.log('Example callback completed successfully');
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Initialize function called during initial deployment
|
|
159
|
+
*/
|
|
160
|
+
export async function initialize(args: CallbackArgs) {
|
|
161
|
+
const { diamond } = args;
|
|
162
|
+
console.log(\`Initializing \${diamond.diamondName}...\`);
|
|
163
|
+
// Add initialization logic here
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Reinitialize function called during upgrades
|
|
168
|
+
*/
|
|
169
|
+
export async function reinitialize(args: CallbackArgs) {
|
|
170
|
+
const { diamond } = args;
|
|
171
|
+
console.log(\`Reinitializing \${diamond.diamondName}...\`);
|
|
172
|
+
// Add reinitialization logic here
|
|
173
|
+
}
|
|
174
|
+
`;
|
|
175
|
+
|
|
176
|
+
await fs.writeFile(callbackPath, callbackContent);
|
|
177
|
+
console.log(`✅ Created example callback: ${callbackPath}`);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Create example contract files if they don't exist
|
|
182
|
+
*/
|
|
183
|
+
private static async createExampleContracts(contractsDir: string): Promise<void> {
|
|
184
|
+
const exampleFacetPath = path.join(contractsDir, 'ExampleFacet.sol');
|
|
185
|
+
|
|
186
|
+
if (await fs.pathExists(exampleFacetPath)) {
|
|
187
|
+
console.log(`⚠️ Example contract already exists: ${exampleFacetPath}`);
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const exampleFacetContent = `// SPDX-License-Identifier: MIT
|
|
192
|
+
pragma solidity ^0.8.0;
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* @title ExampleFacet
|
|
196
|
+
* @dev Example facet contract for diamond pattern
|
|
197
|
+
*/
|
|
198
|
+
contract ExampleFacet {
|
|
199
|
+
// State variables
|
|
200
|
+
uint256 private value;
|
|
201
|
+
bool private initialized;
|
|
202
|
+
|
|
203
|
+
// Events
|
|
204
|
+
event ValueSet(uint256 newValue);
|
|
205
|
+
event Initialized();
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* @dev Initialize the facet
|
|
209
|
+
*/
|
|
210
|
+
function initialize() external {
|
|
211
|
+
require(!initialized, "Already initialized");
|
|
212
|
+
initialized = true;
|
|
213
|
+
value = 0;
|
|
214
|
+
emit Initialized();
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* @dev Reinitialize during upgrades
|
|
219
|
+
*/
|
|
220
|
+
function reinitialize() external {
|
|
221
|
+
initialized = true;
|
|
222
|
+
emit Initialized();
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* @dev Set a value
|
|
227
|
+
*/
|
|
228
|
+
function setValue(uint256 _value) external {
|
|
229
|
+
require(initialized, "Not initialized");
|
|
230
|
+
value = _value;
|
|
231
|
+
emit ValueSet(_value);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* @dev Get the current value
|
|
236
|
+
*/
|
|
237
|
+
function getValue() external view returns (uint256) {
|
|
238
|
+
return value;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* @dev Check if initialized
|
|
243
|
+
*/
|
|
244
|
+
function isInitialized() external view returns (bool) {
|
|
245
|
+
return initialized;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
`;
|
|
249
|
+
|
|
250
|
+
await fs.writeFile(exampleFacetPath, exampleFacetContent);
|
|
251
|
+
console.log(`✅ Created example contract: ${exampleFacetPath}`);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Create hardhat config example for hardhat-diamonds plugin
|
|
256
|
+
*/
|
|
257
|
+
private static async createHardhatConfigExample(diamondName: string): Promise<void> {
|
|
258
|
+
const configPath = path.join(process.cwd(), 'hardhat-diamonds-config.example.ts');
|
|
259
|
+
|
|
260
|
+
if (await fs.pathExists(configPath)) {
|
|
261
|
+
console.log(`⚠️ Hardhat config example already exists: ${configPath}`);
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const configContent = `// Example hardhat.config.ts extension for hardhat-diamonds
|
|
266
|
+
// Copy this configuration to your hardhat.config.ts when using the hardhat-diamonds plugin
|
|
267
|
+
|
|
268
|
+
import { HardhatUserConfig } from "hardhat/config";
|
|
269
|
+
import "@nomiclabs/hardhat-waffle";
|
|
270
|
+
import "@nomiclabs/hardhat-ethers";
|
|
271
|
+
// import "@gnus.ai/hardhat-diamonds"; // Uncomment when plugin is available
|
|
272
|
+
|
|
273
|
+
declare module "hardhat/types/config" {
|
|
274
|
+
interface HardhatUserConfig {
|
|
275
|
+
diamonds?: {
|
|
276
|
+
[diamondName: string]: {
|
|
277
|
+
deploymentsPath?: string;
|
|
278
|
+
contractsPath?: string;
|
|
279
|
+
callbacksPath?: string;
|
|
280
|
+
configFilePath?: string;
|
|
281
|
+
};
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
const config: HardhatUserConfig = {
|
|
287
|
+
solidity: "0.8.17",
|
|
288
|
+
|
|
289
|
+
// Diamond configurations
|
|
290
|
+
diamonds: {
|
|
291
|
+
${diamondName}: {
|
|
292
|
+
deploymentsPath: "./diamonds",
|
|
293
|
+
contractsPath: "./contracts",
|
|
294
|
+
callbacksPath: "./diamonds/${diamondName}/callbacks",
|
|
295
|
+
configFilePath: "./diamonds/${diamondName}/${diamondName.toLowerCase()}.config.json",
|
|
296
|
+
},
|
|
297
|
+
|
|
298
|
+
// Add more diamond configurations as needed
|
|
299
|
+
},
|
|
300
|
+
|
|
301
|
+
networks: {
|
|
302
|
+
hardhat: {
|
|
303
|
+
chainId: 31337,
|
|
304
|
+
},
|
|
305
|
+
// Add other networks as needed
|
|
306
|
+
},
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
export default config;
|
|
310
|
+
`;
|
|
311
|
+
|
|
312
|
+
await fs.writeFile(configPath, configContent);
|
|
313
|
+
console.log(`✅ Created Hardhat config example: ${configPath}`);
|
|
314
|
+
}
|
|
315
|
+
}
|
package/test/README.md
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
# Test Setup and Execution Guide
|
|
2
|
+
|
|
3
|
+
This guide walks through setting up and running the test suite for the `diamonds` module.
|
|
4
|
+
|
|
5
|
+
## Prerequisites
|
|
6
|
+
|
|
7
|
+
- Node.js (v14+)
|
|
8
|
+
- Yarn or npm
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
1. Clone the repository:
|
|
13
|
+
```bash
|
|
14
|
+
git clone https://github.com/your-org/diamonds.git
|
|
15
|
+
cd diamonds
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
2. Install dependencies:
|
|
19
|
+
```bash
|
|
20
|
+
yarn install
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Test Configuration
|
|
24
|
+
|
|
25
|
+
The test suite uses Hardhat for smart contract testing and includes mock implementations of:
|
|
26
|
+
|
|
27
|
+
- Diamond contracts and facets
|
|
28
|
+
- OpenZeppelin Defender API
|
|
29
|
+
- File system repositories
|
|
30
|
+
|
|
31
|
+
All test configuration is in `hardhat.config.ts`. The mock contracts are located in `test/mocks/contracts`.
|
|
32
|
+
|
|
33
|
+
## Running Tests
|
|
34
|
+
|
|
35
|
+
### All Tests
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
npm test
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Unit Tests Only
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
npm run test:unit
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Integration Tests Only
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
npm run test:integration
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### Test Coverage
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
npm run test:coverage
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Test Structure
|
|
60
|
+
|
|
61
|
+
- **Unit Tests**: Test individual components in isolation
|
|
62
|
+
- Core classes (Diamond, DiamondDeployer, etc.)
|
|
63
|
+
- Strategy implementations
|
|
64
|
+
- Repository implementations
|
|
65
|
+
|
|
66
|
+
- **Integration Tests**: Test end-to-end workflows
|
|
67
|
+
- Local deployment
|
|
68
|
+
- OZ Defender deployment
|
|
69
|
+
- Facet upgrades
|
|
70
|
+
|
|
71
|
+
## Mock Contracts
|
|
72
|
+
|
|
73
|
+
The test suite includes mock implementations of:
|
|
74
|
+
|
|
75
|
+
- `MockDiamond.sol`: A simplified diamond contract
|
|
76
|
+
- `MockDiamondCutFacet.sol`: Diamond cut facet
|
|
77
|
+
- `MockDiamondLoupeFacet.sol`: Diamond loupe facet
|
|
78
|
+
- `MockTestFacet.sol`: Test facet with various functions
|
|
79
|
+
|
|
80
|
+
## Test Helpers
|
|
81
|
+
|
|
82
|
+
The `setup.ts` file provides helper functions for:
|
|
83
|
+
|
|
84
|
+
- Setting up the test environment
|
|
85
|
+
- Creating temporary configuration files
|
|
86
|
+
- Deploying mock contracts
|
|
87
|
+
- Cleaning up after tests
|
|
88
|
+
|
|
89
|
+
## Debugging Tests
|
|
90
|
+
|
|
91
|
+
When tests fail, check:
|
|
92
|
+
|
|
93
|
+
1. Console output for error messages
|
|
94
|
+
2. Temporary files in `.tmp-test*` directories
|
|
95
|
+
3. Hardhat error traces
|
|
96
|
+
|
|
97
|
+
You can add `console.log` statements in test files for debugging.
|
|
98
|
+
|
|
99
|
+
## Cleaning Up
|
|
100
|
+
|
|
101
|
+
To clean up temporary test files:
|
|
102
|
+
|
|
103
|
+
```bash
|
|
104
|
+
npm run clean
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## Adding New Tests
|
|
108
|
+
|
|
109
|
+
1. Create a new test file in the appropriate directory
|
|
110
|
+
2. Import needed components and test helpers
|
|
111
|
+
3. Use the setup helper to initialize the environment
|
|
112
|
+
4. Write your tests using Mocha/Chai syntax
|
|
113
|
+
5. Clean up after tests using the cleanup helper
|
|
114
|
+
|
|
115
|
+
## Mocking External Services
|
|
116
|
+
|
|
117
|
+
The OZ Defender tests use sinon to mock the API:
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
// Example mock setup
|
|
121
|
+
mockDeployClient.deployContract.resolves({
|
|
122
|
+
deploymentId: 'defender-deploy-id',
|
|
123
|
+
status: 'pending'
|
|
124
|
+
});
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
## Environment Variables
|
|
128
|
+
|
|
129
|
+
Set these environment variables for local testing:
|
|
130
|
+
|
|
131
|
+
```
|
|
132
|
+
DEFENDER_API_KEY=test-api-key
|
|
133
|
+
DEFENDER_API_SECRET=test-api-secret
|
|
134
|
+
DEFENDER_RELAYER_ADDRESS=0x1234...
|
|
135
|
+
DEFENDER_SAFE_ADDRESS=0x5678...
|
|
136
|
+
```
|