@diamondslab/diamonds 1.0.0 → 1.1.2

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.
Files changed (71) hide show
  1. package/dist/schemas/DeploymentSchema.d.ts +34 -248
  2. package/dist/schemas/DeploymentSchema.d.ts.map +1 -1
  3. package/package.json +4 -8
  4. package/docs/DIAMOND_ABI_CONFIGURATION_SUMMARY.md +0 -40
  5. package/docs/DIAMOND_ABI_GENERATION.md +0 -220
  6. package/docs/DIAMOND_ABI_GENERATOR_EXAMPLES.md +0 -1204
  7. package/docs/DIAMOND_ABI_GENERATOR_IMPLEMENTATION.md +0 -947
  8. package/docs/DIAMOND_ABI_GENERATOR_QUICK_REFERENCE.md +0 -336
  9. package/docs/README-DEFENDER.md +0 -394
  10. package/docs/README_DIAMOND_ABI_GENERATOR.md +0 -303
  11. package/docs/ROADMAP.md +0 -250
  12. package/docs/assets/image.png +0 -0
  13. package/docs/defender-integration.md +0 -451
  14. package/docs/diamond_module-BaseStrategy_design-v2.uxf +0 -247
  15. package/docs/diamond_module-BaseStrategy_design.uxf +0 -272
  16. package/docs/monitoring-troubleshooting.md +0 -556
  17. package/docs/testing-guide.md +0 -713
  18. package/examples/Diamond_Config_and_Deployment_examples/diamonds/ProxyDiamond/callbacks/ERC20ProxyFacet.ts +0 -31
  19. package/examples/Diamond_Config_and_Deployment_examples/diamonds/ProxyDiamond/proxydiamond.config.json +0 -27
  20. package/examples/Local_Hardhat_Deployer_Script_example/LocalDiamondDeployer.ts +0 -180
  21. package/examples/OZ_Defender_Deployer_Script_example/OZDiamondDeployer.ts +0 -107
  22. package/examples/OZ_Defender_Deployer_Script_example/run-oz-deploy.ts +0 -17
  23. package/examples/Test_examples/ProxyDiamondDeployment.test.ts +0 -202
  24. package/examples/defender-deployment/.env.example +0 -35
  25. package/examples/defender-deployment/contracts/ExampleDiamond.sol +0 -41
  26. package/examples/defender-deployment/contracts/ExampleFacet1.sol +0 -84
  27. package/examples/defender-deployment/contracts/ExampleFacet2.sol +0 -104
  28. package/examples/defender-deployment/contracts/UpgradeFacet.sol +0 -92
  29. package/examples/defender-deployment/deploy-script.ts +0 -170
  30. package/examples/defender-deployment/diamond-config.json +0 -36
  31. package/examples/defender-deployment/upgrade-script.ts +0 -237
  32. package/examples/hardhat-diamonds-config.example.ts +0 -41
  33. package/src/core/CallbackManager.ts +0 -70
  34. package/src/core/DeploymentManager.ts +0 -64
  35. package/src/core/Diamond.ts +0 -197
  36. package/src/core/DiamondDeployer.ts +0 -36
  37. package/src/core/index.ts +0 -4
  38. package/src/index.ts +0 -5
  39. package/src/repositories/DBDeploymentRepository.ts +0 -22
  40. package/src/repositories/DeploymentRepository.ts +0 -12
  41. package/src/repositories/FileDeploymentRepository.ts +0 -67
  42. package/src/repositories/databaseHandler.ts +0 -14
  43. package/src/repositories/index.ts +0 -4
  44. package/src/repositories/jsonFileHandler.ts +0 -252
  45. package/src/repositories/prismaDBHandler.ts +0 -10
  46. package/src/schemas/DeploymentSchema.ts +0 -71
  47. package/src/schemas/index.ts +0 -1
  48. package/src/strategies/BaseDeploymentStrategy.ts +0 -649
  49. package/src/strategies/DeploymentStrategy.ts +0 -25
  50. package/src/strategies/LocalDeploymentStrategy.ts +0 -5
  51. package/src/strategies/OZDefenderDeploymentStrategy.ts +0 -849
  52. package/src/strategies/RPCDeploymentStrategy.ts +0 -881
  53. package/src/strategies/index.ts +0 -5
  54. package/src/types/config.ts +0 -34
  55. package/src/types/defender.ts +0 -24
  56. package/src/types/deployments.ts +0 -102
  57. package/src/types/index.ts +0 -4
  58. package/src/types/rpc.ts +0 -37
  59. package/src/utils/common.ts +0 -54
  60. package/src/utils/configurationResolver.ts +0 -141
  61. package/src/utils/contractMapping.ts +0 -220
  62. package/src/utils/defenderClients.ts +0 -22
  63. package/src/utils/defenderStore.ts +0 -62
  64. package/src/utils/diamondAbiGenerator.ts +0 -523
  65. package/src/utils/diffDeployedFacets.ts +0 -131
  66. package/src/utils/index.ts +0 -15
  67. package/src/utils/loupe.ts +0 -159
  68. package/src/utils/rpcStore.ts +0 -152
  69. package/src/utils/signer.ts +0 -93
  70. package/src/utils/txlogging.ts +0 -97
  71. package/src/utils/workspaceSetup.ts +0 -315
@@ -1,84 +0,0 @@
1
- // SPDX-License-Identifier: MIT
2
- pragma solidity ^0.8.17;
3
-
4
- contract ExampleFacet1 {
5
- bytes32 constant STORAGE_POSITION = keccak256("example.facet1.storage");
6
-
7
- struct FacetStorage {
8
- uint256 value;
9
- bool initialized;
10
- address owner;
11
- string name;
12
- }
13
-
14
- function facetStorage() internal pure returns (FacetStorage storage fs) {
15
- bytes32 position = STORAGE_POSITION;
16
- assembly {
17
- fs.slot := position
18
- }
19
- }
20
-
21
- event Initialized(address indexed owner, string name);
22
- event ValueSet(uint256 oldValue, uint256 newValue);
23
- event UpgradedToV1(uint256 timestamp);
24
-
25
- function initialize() external {
26
- FacetStorage storage fs = facetStorage();
27
- require(!fs.initialized, "Already initialized");
28
-
29
- fs.initialized = true;
30
- fs.owner = msg.sender;
31
- fs.name = "ExampleFacet1";
32
- fs.value = 100;
33
-
34
- emit Initialized(msg.sender, fs.name);
35
- }
36
-
37
- function setValue(uint256 _value) external {
38
- FacetStorage storage fs = facetStorage();
39
- require(fs.initialized, "Not initialized");
40
- require(msg.sender == fs.owner, "Not owner");
41
-
42
- uint256 oldValue = fs.value;
43
- fs.value = _value;
44
-
45
- emit ValueSet(oldValue, _value);
46
- }
47
-
48
- function getValue() external view returns (uint256) {
49
- FacetStorage storage fs = facetStorage();
50
- return fs.value;
51
- }
52
-
53
- function getName() external view returns (string memory) {
54
- FacetStorage storage fs = facetStorage();
55
- return fs.name;
56
- }
57
-
58
- function getOwner() external view returns (address) {
59
- FacetStorage storage fs = facetStorage();
60
- return fs.owner;
61
- }
62
-
63
- function isInitialized() external view returns (bool) {
64
- FacetStorage storage fs = facetStorage();
65
- return fs.initialized;
66
- }
67
-
68
- // Version 1.0 upgrade function
69
- function upgradeToV1() external {
70
- FacetStorage storage fs = facetStorage();
71
- require(fs.initialized, "Not initialized");
72
- require(msg.sender == fs.owner, "Not owner");
73
-
74
- // Upgrade logic here
75
- fs.name = "ExampleFacet1_V1";
76
-
77
- emit UpgradedToV1(block.timestamp);
78
- }
79
-
80
- // Version selector for upgrades
81
- function version() external pure returns (string memory) {
82
- return "1.0.0";
83
- }
84
- }
@@ -1,104 +0,0 @@
1
- // SPDX-License-Identifier: MIT
2
- pragma solidity ^0.8.17;
3
-
4
- contract ExampleFacet2 {
5
- bytes32 constant STORAGE_POSITION = keccak256("example.facet2.storage");
6
-
7
- struct FacetStorage {
8
- mapping(address => uint256) balances;
9
- uint256 totalSupply;
10
- bool setupComplete;
11
- uint256 setupTimestamp;
12
- }
13
-
14
- function facetStorage() internal pure returns (FacetStorage storage fs) {
15
- bytes32 position = STORAGE_POSITION;
16
- assembly {
17
- fs.slot := position
18
- }
19
- }
20
-
21
- event Setup(uint256 timestamp);
22
- event BalanceUpdated(address indexed account, uint256 oldBalance, uint256 newBalance);
23
- event Transfer(address indexed from, address indexed to, uint256 amount);
24
-
25
- function setup() external {
26
- FacetStorage storage fs = facetStorage();
27
- require(!fs.setupComplete, "Already setup");
28
-
29
- fs.setupComplete = true;
30
- fs.setupTimestamp = block.timestamp;
31
- fs.totalSupply = 1000000; // 1M tokens
32
- fs.balances[msg.sender] = fs.totalSupply;
33
-
34
- emit Setup(block.timestamp);
35
- emit BalanceUpdated(msg.sender, 0, fs.totalSupply);
36
- }
37
-
38
- function isSetupComplete() external view returns (bool) {
39
- FacetStorage storage fs = facetStorage();
40
- return fs.setupComplete;
41
- }
42
-
43
- function getSetupTimestamp() external view returns (uint256) {
44
- FacetStorage storage fs = facetStorage();
45
- return fs.setupTimestamp;
46
- }
47
-
48
- function balanceOf(address account) external view returns (uint256) {
49
- FacetStorage storage fs = facetStorage();
50
- return fs.balances[account];
51
- }
52
-
53
- function totalSupply() external view returns (uint256) {
54
- FacetStorage storage fs = facetStorage();
55
- return fs.totalSupply;
56
- }
57
-
58
- function transfer(address to, uint256 amount) external returns (bool) {
59
- FacetStorage storage fs = facetStorage();
60
- require(fs.setupComplete, "Not setup");
61
- require(fs.balances[msg.sender] >= amount, "Insufficient balance");
62
-
63
- uint256 fromBalance = fs.balances[msg.sender];
64
- uint256 toBalance = fs.balances[to];
65
-
66
- fs.balances[msg.sender] = fromBalance - amount;
67
- fs.balances[to] = toBalance + amount;
68
-
69
- emit BalanceUpdated(msg.sender, fromBalance, fs.balances[msg.sender]);
70
- emit BalanceUpdated(to, toBalance, fs.balances[to]);
71
- emit Transfer(msg.sender, to, amount);
72
-
73
- return true;
74
- }
75
-
76
- function mint(address to, uint256 amount) external returns (bool) {
77
- FacetStorage storage fs = facetStorage();
78
- require(fs.setupComplete, "Not setup");
79
-
80
- uint256 oldBalance = fs.balances[to];
81
- fs.balances[to] = oldBalance + amount;
82
- fs.totalSupply += amount;
83
-
84
- emit BalanceUpdated(to, oldBalance, fs.balances[to]);
85
- emit Transfer(address(0), to, amount);
86
-
87
- return true;
88
- }
89
-
90
- function burn(uint256 amount) external returns (bool) {
91
- FacetStorage storage fs = facetStorage();
92
- require(fs.setupComplete, "Not setup");
93
- require(fs.balances[msg.sender] >= amount, "Insufficient balance");
94
-
95
- uint256 oldBalance = fs.balances[msg.sender];
96
- fs.balances[msg.sender] = oldBalance - amount;
97
- fs.totalSupply -= amount;
98
-
99
- emit BalanceUpdated(msg.sender, oldBalance, fs.balances[msg.sender]);
100
- emit Transfer(msg.sender, address(0), amount);
101
-
102
- return true;
103
- }
104
- }
@@ -1,92 +0,0 @@
1
- // SPDX-License-Identifier: MIT
2
- pragma solidity ^0.8.17;
3
-
4
- contract UpgradeFacet {
5
- bytes32 constant STORAGE_POSITION = keccak256("upgrade.facet.storage");
6
-
7
- struct FacetStorage {
8
- uint256 upgradeCount;
9
- mapping(uint256 => uint256) upgradeTimestamps;
10
- bool initialized;
11
- string[] upgradeLog;
12
- }
13
-
14
- function facetStorage() internal pure returns (FacetStorage storage fs) {
15
- bytes32 position = STORAGE_POSITION;
16
- assembly {
17
- fs.slot := position
18
- }
19
- }
20
-
21
- event Initialized(uint256 timestamp);
22
- event UpgradeLogged(uint256 indexed upgradeId, string description, uint256 timestamp);
23
- event SystemUpgraded(string version, uint256 timestamp);
24
-
25
- function initialize() external {
26
- FacetStorage storage fs = facetStorage();
27
- require(!fs.initialized, "Already initialized");
28
-
29
- fs.initialized = true;
30
- fs.upgradeCount = 0;
31
-
32
- emit Initialized(block.timestamp);
33
- }
34
-
35
- function logUpgrade(string memory description) external {
36
- FacetStorage storage fs = facetStorage();
37
- require(fs.initialized, "Not initialized");
38
-
39
- uint256 upgradeId = fs.upgradeCount++;
40
- fs.upgradeTimestamps[upgradeId] = block.timestamp;
41
- fs.upgradeLog.push(description);
42
-
43
- emit UpgradeLogged(upgradeId, description, block.timestamp);
44
- }
45
-
46
- function getUpgradeCount() external view returns (uint256) {
47
- FacetStorage storage fs = facetStorage();
48
- return fs.upgradeCount;
49
- }
50
-
51
- function getUpgradeTimestamp(uint256 upgradeId) external view returns (uint256) {
52
- FacetStorage storage fs = facetStorage();
53
- require(upgradeId < fs.upgradeCount, "Invalid upgrade ID");
54
- return fs.upgradeTimestamps[upgradeId];
55
- }
56
-
57
- function getUpgradeLog() external view returns (string[] memory) {
58
- FacetStorage storage fs = facetStorage();
59
- return fs.upgradeLog;
60
- }
61
-
62
- function getLatestUpgrade() external view returns (string memory, uint256) {
63
- FacetStorage storage fs = facetStorage();
64
- require(fs.upgradeCount > 0, "No upgrades logged");
65
-
66
- uint256 latestId = fs.upgradeCount - 1;
67
- return (fs.upgradeLog[latestId], fs.upgradeTimestamps[latestId]);
68
- }
69
-
70
- function markSystemUpgrade(string memory _version) external {
71
- FacetStorage storage fs = facetStorage();
72
- require(fs.initialized, "Not initialized");
73
-
74
- string memory description = string(abi.encodePacked("System upgrade to version ", _version));
75
-
76
- uint256 upgradeId = fs.upgradeCount++;
77
- fs.upgradeTimestamps[upgradeId] = block.timestamp;
78
- fs.upgradeLog.push(description);
79
-
80
- emit UpgradeLogged(upgradeId, description, block.timestamp);
81
- emit SystemUpgraded(_version, block.timestamp);
82
- }
83
-
84
- function isInitialized() external view returns (bool) {
85
- FacetStorage storage fs = facetStorage();
86
- return fs.initialized;
87
- }
88
-
89
- function version() external pure returns (string memory) {
90
- return "1.0.0";
91
- }
92
- }
@@ -1,170 +0,0 @@
1
- import { ethers } from 'hardhat';
2
- import { Diamond } from '../../src/core/Diamond';
3
- import { DiamondDeployer } from '../../src/core/DiamondDeployer';
4
- import { FileDeploymentRepository } from '../../src/repositories/FileDeploymentRepository';
5
- import { OZDefenderDeploymentStrategy } from '../../src/strategies/OZDefenderDeploymentStrategy';
6
- import * as path from 'path';
7
- import * as fs from 'fs-extra';
8
-
9
- async function main() {
10
- console.log('šŸš€ Starting Defender deployment...');
11
-
12
- // Get network information
13
- const network = await ethers.provider.getNetwork();
14
- const signers = await ethers.getSigners();
15
- const deployer = signers[0];
16
-
17
- console.log(`šŸ“ Network: ${network.name} (${network.chainId})`);
18
- console.log(`šŸ‘¤ Deployer: ${deployer.address}`);
19
-
20
- // Load configuration
21
- const configPath = path.join(__dirname, 'diamond-config.json');
22
- const exampleConfig = {
23
- diamondName: 'ExampleDiamond',
24
- networkName: network.name,
25
- chainId: network.chainId,
26
- deploymentsPath: path.join(__dirname, 'deployments'),
27
- contractsPath: path.join(__dirname, 'contracts'),
28
- callbacksPath: path.join(__dirname, 'callbacks'),
29
- configFilePath: configPath,
30
- deployedDiamondDataFilePath: path.join(__dirname, 'deployments', `examplediamond-${network.name}-${network.chainId}.json`)
31
- };
32
-
33
- // Ensure directories exist
34
- await fs.ensureDir(exampleConfig.deploymentsPath);
35
- await fs.ensureDir(exampleConfig.callbacksPath);
36
-
37
- // Create diamond instance
38
- const repository = new FileDeploymentRepository(exampleConfig);
39
- const diamond = new Diamond(exampleConfig, repository);
40
-
41
- // Setup provider and signer
42
- diamond.setProvider(ethers.provider);
43
- diamond.setSigner(deployer);
44
-
45
- // Validate environment variables
46
- const requiredEnvVars = [
47
- 'DEFENDER_API_KEY',
48
- 'DEFENDER_API_SECRET',
49
- 'DEFENDER_RELAYER_ADDRESS',
50
- 'DEFENDER_SAFE_ADDRESS'
51
- ];
52
-
53
- for (const envVar of requiredEnvVars) {
54
- if (!process.env[envVar]) {
55
- console.error(`āŒ Missing required environment variable: ${envVar}`);
56
- console.log('Please check your .env file and ensure all Defender credentials are set.');
57
- process.exit(1);
58
- }
59
- }
60
-
61
- // Create Defender strategy
62
- const strategy = new OZDefenderDeploymentStrategy(
63
- process.env.DEFENDER_API_KEY!,
64
- process.env.DEFENDER_API_SECRET!,
65
- process.env.DEFENDER_RELAYER_ADDRESS!,
66
- process.env.AUTO_APPROVE === 'true',
67
- process.env.DEFENDER_SAFE_ADDRESS!,
68
- 'Safe',
69
- true // verbose logging
70
- );
71
-
72
- console.log('šŸ›”ļø Defender strategy configured');
73
- console.log(`āš™ļø Auto-approve: ${process.env.AUTO_APPROVE === 'true' ? 'enabled' : 'disabled'}`);
74
-
75
- // Execute deployment
76
- const diamondDeployer = new DiamondDeployer(diamond, strategy);
77
-
78
- try {
79
- await diamondDeployer.deployDiamond();
80
-
81
- console.log('\nāœ… Deployment completed successfully!');
82
-
83
- // Output deployment information
84
- const deployedData = diamond.getDeployedDiamondData();
85
- console.log('\nšŸ“Š Deployment Summary:');
86
- console.log(`Diamond Address: ${deployedData.DiamondAddress}`);
87
- console.log(`Deployer Address: ${deployedData.DeployerAddress}`);
88
- console.log(`Network: ${network.name} (Chain ID: ${network.chainId})`);
89
-
90
- console.log('\nšŸ“‹ Deployed Facets:');
91
- if (deployedData.DeployedFacets) {
92
- Object.entries(deployedData.DeployedFacets).forEach(([name, facet]) => {
93
- console.log(` ${name}:`);
94
- console.log(` Address: ${facet.address}`);
95
- console.log(` Version: ${facet.version}`);
96
- console.log(` Selectors: ${facet.funcSelectors?.length || 0}`);
97
- if (facet.tx_hash) {
98
- console.log(` TX Hash: ${facet.tx_hash}`);
99
- }
100
- });
101
- }
102
-
103
- // Save deployment summary
104
- const summary = {
105
- timestamp: new Date().toISOString(),
106
- network: network.name,
107
- chainId: network.chainId,
108
- deployerAddress: deployer.address,
109
- diamondAddress: deployedData.DiamondAddress,
110
- facets: deployedData.DeployedFacets,
111
- deploymentMethod: 'OpenZeppelin Defender'
112
- };
113
-
114
- const summaryPath = path.join(exampleConfig.deploymentsPath, 'deployment-summary.json');
115
- await fs.writeJson(summaryPath, summary, { spaces: 2 });
116
-
117
- console.log(`\nšŸ“„ Deployment summary saved to: ${summaryPath}`);
118
-
119
- // Display next steps
120
- console.log('\nšŸŽÆ Next Steps:');
121
- console.log('1. Verify contracts on block explorer');
122
- console.log('2. Test diamond functionality');
123
- console.log('3. Set up monitoring and alerts');
124
- console.log('4. Prepare upgrade scenarios');
125
-
126
- if (process.env.AUTO_APPROVE !== 'true') {
127
- console.log('\nāš ļø Note: Auto-approval is disabled. Check your Defender dashboard for pending proposals.');
128
- }
129
-
130
- } catch (error) {
131
- console.error('\nāŒ Deployment failed:', error);
132
-
133
- // Save error information for debugging
134
- const errorInfo = {
135
- timestamp: new Date().toISOString(),
136
- error: error instanceof Error ? {
137
- message: error.message,
138
- stack: error.stack,
139
- name: error.name
140
- } : String(error),
141
- network: network.name,
142
- chainId: network.chainId,
143
- deployerAddress: deployer.address
144
- };
145
-
146
- const errorPath = path.join(exampleConfig.deploymentsPath, 'deployment-error.json');
147
- await fs.writeJson(errorPath, errorInfo, { spaces: 2 });
148
-
149
- console.log(`\nšŸ” Error details saved to: ${errorPath}`);
150
- console.log('\nTroubleshooting tips:');
151
- console.log('1. Check your Defender API credentials');
152
- console.log('2. Ensure sufficient balance for gas fees');
153
- console.log('3. Verify network connectivity');
154
- console.log('4. Check Defender dashboard for detailed logs');
155
-
156
- process.exit(1);
157
- }
158
- }
159
-
160
- // Handle script execution
161
- if (require.main === module) {
162
- main()
163
- .then(() => process.exit(0))
164
- .catch((error) => {
165
- console.error('Unhandled error:', error);
166
- process.exit(1);
167
- });
168
- }
169
-
170
- export { main as deployWithDefender };
@@ -1,36 +0,0 @@
1
- {
2
- "protocolVersion": 0.0,
3
- "protocolInitFacet": "ExampleFacet1",
4
- "facets": {
5
- "DiamondCutFacet": {
6
- "priority": 10,
7
- "versions": {
8
- "0.0": {}
9
- }
10
- },
11
- "DiamondLoupeFacet": {
12
- "priority": 20,
13
- "versions": {
14
- "0.0": {}
15
- }
16
- },
17
- "ExampleFacet1": {
18
- "priority": 30,
19
- "versions": {
20
- "0.0": {
21
- "deployInit": "initialize()",
22
- "callbacks": ["logDeployment"]
23
- }
24
- }
25
- },
26
- "ExampleFacet2": {
27
- "priority": 40,
28
- "versions": {
29
- "0.0": {
30
- "deployInit": "setup()",
31
- "callbacks": ["validateSetup"]
32
- }
33
- }
34
- }
35
- }
36
- }