@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,849 @@
|
|
|
1
|
+
import { Defender } from '@openzeppelin/defender-sdk';
|
|
2
|
+
import { DeployContractRequest, DeploymentResponse, DeploymentStatus } from '@openzeppelin/defender-sdk-deploy-client';
|
|
3
|
+
import {
|
|
4
|
+
CreateProposalRequest
|
|
5
|
+
} from '@openzeppelin/defender-sdk-proposal-client';
|
|
6
|
+
import {
|
|
7
|
+
ExternalApiCreateProposalRequest
|
|
8
|
+
} from "@openzeppelin/defender-sdk-proposal-client/lib/models/proposal";
|
|
9
|
+
import chalk from 'chalk';
|
|
10
|
+
import { randomInt } from 'crypto';
|
|
11
|
+
import { ethers } from 'ethers';
|
|
12
|
+
import * as fs from 'fs';
|
|
13
|
+
import hre from 'hardhat';
|
|
14
|
+
import { join } from 'path';
|
|
15
|
+
import { Diamond } from '../core';
|
|
16
|
+
import { FacetCutAction, FacetCuts, PollOptions } from '../types';
|
|
17
|
+
import { getContractArtifact, getContractName, getDiamondContractName } from '../utils/contractMapping';
|
|
18
|
+
import { DefenderDeploymentStore } from '../utils/defenderStore';
|
|
19
|
+
import { BaseDeploymentStrategy } from './BaseDeploymentStrategy';
|
|
20
|
+
|
|
21
|
+
export class OZDefenderDeploymentStrategy extends BaseDeploymentStrategy {
|
|
22
|
+
private client: Defender;
|
|
23
|
+
// private proposalClient: ProposalClient;
|
|
24
|
+
private relayerAddress: string;
|
|
25
|
+
private autoApprove: boolean;
|
|
26
|
+
private via: ExternalApiCreateProposalRequest['via'];
|
|
27
|
+
private viaType: ExternalApiCreateProposalRequest['viaType'];
|
|
28
|
+
|
|
29
|
+
constructor(
|
|
30
|
+
apiKey: string,
|
|
31
|
+
apiSecret: string,
|
|
32
|
+
relayerAddress: string,
|
|
33
|
+
autoApprove: boolean = false,
|
|
34
|
+
via: ExternalApiCreateProposalRequest['via'],
|
|
35
|
+
viaType: ExternalApiCreateProposalRequest['viaType'],
|
|
36
|
+
verbose: boolean = true,
|
|
37
|
+
customClient?: Defender // Optional for testing
|
|
38
|
+
) {
|
|
39
|
+
super(verbose);
|
|
40
|
+
this.client = customClient || new Defender({ apiKey, apiSecret });
|
|
41
|
+
// this.proposalClient = new ProposalClient({ apiKey, apiSecret });
|
|
42
|
+
this.relayerAddress = relayerAddress;
|
|
43
|
+
this.via = via;
|
|
44
|
+
this.viaType = viaType;
|
|
45
|
+
this.autoApprove = autoApprove;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
protected async checkAndUpdateDeployStep(stepName: string, diamond: Diamond): Promise<void> {
|
|
50
|
+
const config = diamond.getDiamondConfig();
|
|
51
|
+
const network = config.networkName!;
|
|
52
|
+
const deploymentId = `${diamond.diamondName}-${network}-${config.chainId}`;
|
|
53
|
+
const store = new DefenderDeploymentStore(diamond.diamondName, deploymentId, config.deploymentsPath);
|
|
54
|
+
|
|
55
|
+
const step = store.getStep(stepName);
|
|
56
|
+
if (!step || !step.proposalId) return;
|
|
57
|
+
|
|
58
|
+
try {
|
|
59
|
+
const deployment = await this.client.deploy.getDeployedContract(step.proposalId);
|
|
60
|
+
const status = deployment.status as DeploymentStatus;
|
|
61
|
+
|
|
62
|
+
if (status === 'completed') {
|
|
63
|
+
console.log(chalk.green(`✅ Defender deployment for ${stepName} completed.`));
|
|
64
|
+
store.updateStatus(stepName, 'executed');
|
|
65
|
+
} else if (status === 'failed') {
|
|
66
|
+
console.error(chalk.red(`❌ Defender deployment for ${stepName} failed.`));
|
|
67
|
+
store.updateStatus(stepName, 'failed');
|
|
68
|
+
throw new Error(`Defender deployment ${step.proposalId} failed for step ${stepName}`);
|
|
69
|
+
} else {
|
|
70
|
+
console.log(chalk.yellow(`⏳ Defender deployment for ${stepName} is still ${status}.`));
|
|
71
|
+
// Optionally you can wait/poll here
|
|
72
|
+
}
|
|
73
|
+
} catch (err) {
|
|
74
|
+
console.error(chalk.red(`⚠️ Error while querying Defender deploy status for ${stepName}:`), err);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Polls the Defender API until the deployment is complete or fails.
|
|
80
|
+
* @param stepName The name of the step to poll.
|
|
81
|
+
* @param diamond The diamond instance.
|
|
82
|
+
* @param options Polling options.
|
|
83
|
+
* @returns The deployment response or null if not found.
|
|
84
|
+
*/
|
|
85
|
+
private async pollUntilComplete(
|
|
86
|
+
stepName: string,
|
|
87
|
+
diamond: Diamond,
|
|
88
|
+
options: PollOptions = {}
|
|
89
|
+
): Promise<DeploymentResponse | null> {
|
|
90
|
+
const {
|
|
91
|
+
maxAttempts = process.env.NODE_ENV === 'test' ? 1 : 30, // Increase for manual approval workflows
|
|
92
|
+
// Use shorter delays in test environments
|
|
93
|
+
initialDelayMs = process.env.NODE_ENV === 'test' ? 100 : 8000,
|
|
94
|
+
maxDelayMs = process.env.NODE_ENV === 'test' ? 1000 : 60000,
|
|
95
|
+
jitter = true
|
|
96
|
+
} = options;
|
|
97
|
+
|
|
98
|
+
const config = diamond.getDiamondConfig();
|
|
99
|
+
const network = config.networkName!;
|
|
100
|
+
const deploymentId = `${diamond.diamondName}-${network}-${config.chainId}`;
|
|
101
|
+
const store = new DefenderDeploymentStore(diamond.diamondName, deploymentId, config.deploymentsPath);
|
|
102
|
+
|
|
103
|
+
const step = store.getStep(stepName);
|
|
104
|
+
if (!step?.proposalId) {
|
|
105
|
+
console.warn(`⚠️ No Defender deployment ID found for step ${stepName}`);
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
let attempt = 0;
|
|
110
|
+
let delay = initialDelayMs;
|
|
111
|
+
|
|
112
|
+
while (attempt < maxAttempts) {
|
|
113
|
+
try {
|
|
114
|
+
console.log(chalk.blue(`🔍 Polling deployment status for ${stepName} (ID: ${step.proposalId})...`));
|
|
115
|
+
const deployment = await this.client.deploy.getDeployedContract(step.proposalId);
|
|
116
|
+
console.log(chalk.gray(`📊 Deployment response:`, JSON.stringify(deployment, null, 2)));
|
|
117
|
+
const status = deployment.status;
|
|
118
|
+
|
|
119
|
+
if (status === 'completed') {
|
|
120
|
+
console.log(chalk.green(`✅ Deployment succeeded for ${stepName}.`));
|
|
121
|
+
store.updateStatus(stepName, 'executed');
|
|
122
|
+
|
|
123
|
+
// Update diamond data with deployed contract information
|
|
124
|
+
await this.updateDiamondWithDeployment(diamond, stepName, deployment);
|
|
125
|
+
|
|
126
|
+
return deployment;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (status === 'failed') {
|
|
130
|
+
console.error(chalk.red(`❌ Deployment failed for ${stepName}.`));
|
|
131
|
+
store.updateStatus(stepName, 'failed');
|
|
132
|
+
const errorMsg = (deployment as any).error || 'Unknown deployment error';
|
|
133
|
+
// Don't catch this error - let it bubble up immediately
|
|
134
|
+
const error = new Error(`Deployment failed for ${stepName}: ${errorMsg}`);
|
|
135
|
+
(error as any).deployment = deployment;
|
|
136
|
+
throw error;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (status === 'submitted') {
|
|
140
|
+
const approvalProcessId = (deployment as any).approvalProcessId;
|
|
141
|
+
const safeTxHash = (deployment as any).safeTxHash;
|
|
142
|
+
if (approvalProcessId) {
|
|
143
|
+
console.log(chalk.yellow(`⏳ Deployment ${stepName} is submitted and waiting for approval.`));
|
|
144
|
+
console.log(chalk.blue(`🔗 Please approve in Defender dashboard: https://defender.openzeppelin.com/`));
|
|
145
|
+
if (safeTxHash) {
|
|
146
|
+
console.log(chalk.blue(`📋 Safe Transaction Hash: ${safeTxHash}`));
|
|
147
|
+
}
|
|
148
|
+
} else {
|
|
149
|
+
console.log(chalk.yellow(`⏳ Deployment ${stepName} is submitted and processing...`));
|
|
150
|
+
}
|
|
151
|
+
} else {
|
|
152
|
+
console.log(chalk.yellow(`⏳ Deployment ${stepName} still ${status}. Retrying in ${delay}ms...`));
|
|
153
|
+
}
|
|
154
|
+
} catch (err) {
|
|
155
|
+
// Only catch network/API errors, not deployment failures
|
|
156
|
+
if (err instanceof Error && err.message.includes('Deployment failed')) {
|
|
157
|
+
throw err; // Re-throw deployment failures immediately
|
|
158
|
+
}
|
|
159
|
+
console.error(chalk.red(`⚠️ Error polling Defender for ${stepName}:`), err);
|
|
160
|
+
if (attempt >= maxAttempts - 1) {
|
|
161
|
+
throw err;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
attempt++;
|
|
166
|
+
|
|
167
|
+
// Apply jitter
|
|
168
|
+
const jitterValue = jitter ? await randomInt(Math.floor(delay / 2)) : 0;
|
|
169
|
+
const sleep = delay + jitterValue;
|
|
170
|
+
|
|
171
|
+
await new Promise(res => setTimeout(res, sleep));
|
|
172
|
+
|
|
173
|
+
// Exponential backoff
|
|
174
|
+
delay = Math.min(delay * 2, maxDelayMs);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
console.warn(chalk.red(`⚠️ Deployment for ${stepName} did not complete after ${maxAttempts} attempts.`));
|
|
178
|
+
console.log(chalk.blue(`🔗 Please check the Defender dashboard for pending approvals: https://defender.openzeppelin.com/`));
|
|
179
|
+
console.log(chalk.yellow(`📋 Deployment ID: ${step.proposalId}`));
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Updates the diamond data with deployment information from Defender
|
|
185
|
+
*/
|
|
186
|
+
private async updateDiamondWithDeployment(
|
|
187
|
+
diamond: Diamond,
|
|
188
|
+
stepName: string,
|
|
189
|
+
deployment: DeploymentResponse
|
|
190
|
+
): Promise<void> {
|
|
191
|
+
const deployedDiamondData = diamond.getDeployedDiamondData();
|
|
192
|
+
const contractAddress = deployment.address;
|
|
193
|
+
|
|
194
|
+
if (!contractAddress) {
|
|
195
|
+
console.warn(chalk.yellow(`⚠️ No contract address found in deployment response for ${stepName}`));
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
if (stepName === 'deploy-diamondcutfacet') {
|
|
200
|
+
// Get DiamondCutFacet interface for function selectors
|
|
201
|
+
let diamondCutFacetFunctionSelectors: string[] = [];
|
|
202
|
+
try {
|
|
203
|
+
const diamondCutContractName = await getContractName("DiamondCutFacet", diamond);
|
|
204
|
+
const diamondCutFactory = await hre.ethers.getContractFactory(diamondCutContractName, diamond.getSigner()!);
|
|
205
|
+
diamondCutFacetFunctionSelectors = [];
|
|
206
|
+
diamondCutFactory.interface.forEachFunction((func: ethers.FunctionFragment) => {
|
|
207
|
+
diamondCutFacetFunctionSelectors.push(func.selector);
|
|
208
|
+
});
|
|
209
|
+
} catch (error) {
|
|
210
|
+
console.log(chalk.yellow(`⚠️ Could not get function selectors for DiamondCutFacet (likely in test environment): ${error}`));
|
|
211
|
+
// Use default selectors for DiamondCutFacet in test environments
|
|
212
|
+
diamondCutFacetFunctionSelectors = ['0x1f931c1c']; // diamondCut function
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
deployedDiamondData.DeployedFacets = deployedDiamondData.DeployedFacets || {};
|
|
216
|
+
deployedDiamondData.DeployedFacets["DiamondCutFacet"] = {
|
|
217
|
+
address: contractAddress,
|
|
218
|
+
tx_hash: (deployment as any).txHash || 'defender-deployment',
|
|
219
|
+
version: 0,
|
|
220
|
+
funcSelectors: diamondCutFacetFunctionSelectors,
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
// Register the DiamondCutFacet function selectors
|
|
224
|
+
const diamondCutFacetSelectorsRegistry = diamondCutFacetFunctionSelectors.reduce((acc, selector) => {
|
|
225
|
+
acc[selector] = {
|
|
226
|
+
facetName: "DiamondCutFacet",
|
|
227
|
+
priority: diamond.getFacetsConfig()?.DiamondCutFacet?.priority || 1000,
|
|
228
|
+
address: contractAddress,
|
|
229
|
+
action: 0, // RegistryFacetCutAction.Deployed
|
|
230
|
+
};
|
|
231
|
+
return acc;
|
|
232
|
+
}, {} as Record<string, any>);
|
|
233
|
+
|
|
234
|
+
diamond.registerFunctionSelectors(diamondCutFacetSelectorsRegistry);
|
|
235
|
+
|
|
236
|
+
} else if (stepName === 'deploy-diamond') {
|
|
237
|
+
deployedDiamondData.DiamondAddress = contractAddress;
|
|
238
|
+
|
|
239
|
+
} else if (stepName.startsWith('deploy-')) {
|
|
240
|
+
// Extract facet name from step name
|
|
241
|
+
const facetName = stepName.replace('deploy-', '');
|
|
242
|
+
|
|
243
|
+
try {
|
|
244
|
+
// Get facet interface for function selectors
|
|
245
|
+
let facetSelectors: string[] = [];
|
|
246
|
+
try {
|
|
247
|
+
const facetFactory = await hre.ethers.getContractFactory(facetName, diamond.getSigner()!);
|
|
248
|
+
facetSelectors = [];
|
|
249
|
+
facetFactory.interface.forEachFunction((func: ethers.FunctionFragment) => {
|
|
250
|
+
facetSelectors.push(func.selector);
|
|
251
|
+
});
|
|
252
|
+
} catch (error) {
|
|
253
|
+
console.log(chalk.yellow(`⚠️ Could not get function selectors for ${facetName} (likely in test environment): ${error}`));
|
|
254
|
+
// Use empty selectors in test environments
|
|
255
|
+
facetSelectors = [];
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
const deployConfig = diamond.getDeployConfig();
|
|
259
|
+
const facetConfig = deployConfig.facets[facetName];
|
|
260
|
+
const availableVersions = Object.keys(facetConfig.versions ?? {}).map(Number);
|
|
261
|
+
const targetVersion = Math.max(...availableVersions);
|
|
262
|
+
|
|
263
|
+
deployedDiamondData.DeployedFacets = deployedDiamondData.DeployedFacets || {};
|
|
264
|
+
deployedDiamondData.DeployedFacets[facetName] = {
|
|
265
|
+
address: contractAddress,
|
|
266
|
+
tx_hash: (deployment as any).txHash || 'defender-deployment',
|
|
267
|
+
version: targetVersion,
|
|
268
|
+
funcSelectors: facetSelectors,
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
// Update new deployed facets for diamond cut preparation
|
|
272
|
+
const initFn = diamond.newDeployment
|
|
273
|
+
? facetConfig.versions?.[targetVersion]?.deployInit || ""
|
|
274
|
+
: facetConfig.versions?.[targetVersion]?.upgradeInit || "";
|
|
275
|
+
|
|
276
|
+
if (initFn && facetName !== deployConfig.protocolInitFacet) {
|
|
277
|
+
diamond.initializerRegistry.set(facetName, initFn);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const newFacetData = {
|
|
281
|
+
priority: facetConfig.priority || 1000,
|
|
282
|
+
address: contractAddress,
|
|
283
|
+
tx_hash: (deployment as any).txHash || 'defender-deployment',
|
|
284
|
+
version: targetVersion,
|
|
285
|
+
funcSelectors: facetSelectors,
|
|
286
|
+
deployInclude: facetConfig.versions?.[targetVersion]?.deployInclude || [],
|
|
287
|
+
deployExclude: facetConfig.versions?.[targetVersion]?.deployExclude || [],
|
|
288
|
+
initFunction: initFn,
|
|
289
|
+
verified: false,
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
diamond.updateNewDeployedFacets(facetName, newFacetData);
|
|
293
|
+
|
|
294
|
+
} catch (err) {
|
|
295
|
+
console.warn(chalk.yellow(`⚠️ Could not get interface for facet ${facetName}: ${err}`));
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
diamond.updateDeployedDiamondData(deployedDiamondData);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
protected async preDeployDiamondTasks(diamond: Diamond): Promise<void> {
|
|
304
|
+
if (this.verbose) {
|
|
305
|
+
console.log(chalk.yellowBright(`\n🪓 Pre-deploy diamond tasks for ${diamond.diamondName} from ${this.constructor.name}...`));
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
await this.checkAndUpdateDeployStep('deploy-diamondcutfacet', diamond);
|
|
309
|
+
await this.checkAndUpdateDeployStep('deploy-diamond', diamond);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
protected async deployDiamondTasks(diamond: Diamond): Promise<void> {
|
|
313
|
+
const diamondConfig = diamond.getDiamondConfig();
|
|
314
|
+
const network = diamondConfig.networkName!;
|
|
315
|
+
const deploymentId = `${diamond.diamondName}-${network}-${diamondConfig.chainId}`;
|
|
316
|
+
const store = new DefenderDeploymentStore(diamond.diamondName, deploymentId, diamondConfig.deploymentsPath);
|
|
317
|
+
|
|
318
|
+
const signer = diamond.getSigner()!;
|
|
319
|
+
const deployerAddress = await signer.getAddress();
|
|
320
|
+
|
|
321
|
+
// ---- Deploy DiamondCutFacet ----
|
|
322
|
+
const stepNameCut = 'deploy-diamondcutfacet';
|
|
323
|
+
const cutStep = store.getStep(stepNameCut);
|
|
324
|
+
if (!cutStep || (cutStep.status !== 'executed' && cutStep.status !== 'failed')) {
|
|
325
|
+
const diamondCutContractName = await getContractName('DiamondCutFacet', diamond);
|
|
326
|
+
const diamondCutArtifact = await getContractArtifact('DiamondCutFacet', diamond);
|
|
327
|
+
|
|
328
|
+
// Format artifact for Defender SDK - need build-info format, not individual artifact
|
|
329
|
+
const buildInfo = await this.getBuildInfoForContract('DiamondCutFacet', diamond);
|
|
330
|
+
|
|
331
|
+
const cutRequest: DeployContractRequest = {
|
|
332
|
+
network,
|
|
333
|
+
contractName: diamondCutContractName,
|
|
334
|
+
contractPath: diamondCutArtifact.sourceName, // Use the actual source name from artifact
|
|
335
|
+
constructorInputs: [],
|
|
336
|
+
verifySourceCode: true, // TODO Verify this should be true or optional
|
|
337
|
+
artifactPayload: JSON.stringify(buildInfo), // Use build-info format for Defender SDK
|
|
338
|
+
salt: ethers.hexlify(ethers.randomBytes(32)), // Add salt for CREATE2 deployment as required by Defender
|
|
339
|
+
};
|
|
340
|
+
|
|
341
|
+
let cutDeployment;
|
|
342
|
+
try {
|
|
343
|
+
cutDeployment = await this.client.deploy.deployContract(cutRequest);
|
|
344
|
+
console.log(chalk.blue(`📊 Initial deployment response:`, JSON.stringify(cutDeployment, null, 2)));
|
|
345
|
+
} catch (error) {
|
|
346
|
+
console.log(chalk.red("❌ Error deploying DiamondCutFacet via Defender:"), error);
|
|
347
|
+
throw error;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
store.saveStep({
|
|
351
|
+
stepName: stepNameCut,
|
|
352
|
+
proposalId: cutDeployment.deploymentId,
|
|
353
|
+
status: 'pending',
|
|
354
|
+
description: 'DiamondCutFacet deployed via Defender DeployClient',
|
|
355
|
+
timestamp: Date.now()
|
|
356
|
+
});
|
|
357
|
+
await this.pollUntilComplete(stepNameCut, diamond);
|
|
358
|
+
|
|
359
|
+
console.log(chalk.blue(`📡 Submitted DiamondCutFacet deploy to Defender: ${cutDeployment.deploymentId}`));
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// ---- Deploy Diamond ----
|
|
363
|
+
const stepNameDiamond = 'deploy-diamond';
|
|
364
|
+
const diamondStep = store.getStep(stepNameDiamond);
|
|
365
|
+
if (!diamondStep || (diamondStep.status !== 'executed' && diamondStep.status !== 'failed')) {
|
|
366
|
+
// First, ensure DiamondCutFacet is fully completed and data is updated
|
|
367
|
+
const cutStep = store.getStep(stepNameCut);
|
|
368
|
+
if (cutStep?.status === 'executed') {
|
|
369
|
+
console.log(chalk.blue(`🔍 Ensuring DiamondCutFacet data is up to date...`));
|
|
370
|
+
|
|
371
|
+
// Force update DiamondCutFacet data if needed
|
|
372
|
+
try {
|
|
373
|
+
const cutDeployment = await this.client.deploy.getDeployedContract(cutStep.proposalId!);
|
|
374
|
+
if (cutDeployment.status === 'completed' && cutDeployment.address) {
|
|
375
|
+
await this.updateDiamondWithDeployment(diamond, stepNameCut, cutDeployment);
|
|
376
|
+
console.log(chalk.green(`✅ DiamondCutFacet data updated: ${cutDeployment.address}`));
|
|
377
|
+
}
|
|
378
|
+
} catch (error) {
|
|
379
|
+
console.warn(chalk.yellow(`⚠️ Could not update DiamondCutFacet data: ${error}`));
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
// Get the deployed DiamondCutFacet address
|
|
384
|
+
const deployedDiamondData = diamond.getDeployedDiamondData();
|
|
385
|
+
const diamondCutFacetAddress = deployedDiamondData.DeployedFacets?.['DiamondCutFacet']?.address;
|
|
386
|
+
|
|
387
|
+
// If still not found, try to get from Defender directly
|
|
388
|
+
if (!diamondCutFacetAddress) {
|
|
389
|
+
console.log(chalk.yellow(`⚠️ DiamondCutFacet address not found in deployment data, checking Defender...`));
|
|
390
|
+
const cutStep = store.getStep(stepNameCut);
|
|
391
|
+
if (cutStep?.proposalId) {
|
|
392
|
+
try {
|
|
393
|
+
const cutDeployment = await this.client.deploy.getDeployedContract(cutStep.proposalId);
|
|
394
|
+
if (cutDeployment.status === 'completed' && cutDeployment.address) {
|
|
395
|
+
console.log(chalk.blue(`🔍 Found DiamondCutFacet address from Defender: ${cutDeployment.address}`));
|
|
396
|
+
// Use this address directly for the Diamond constructor
|
|
397
|
+
const directDiamondCutFacetAddress = cutDeployment.address;
|
|
398
|
+
|
|
399
|
+
const diamondContractName = await getDiamondContractName(diamond.diamondName, diamond);
|
|
400
|
+
const diamondArtifact = await getContractArtifact(diamond.diamondName, diamond);
|
|
401
|
+
const buildInfo = await this.getBuildInfoForContract(diamond.diamondName, diamond);
|
|
402
|
+
|
|
403
|
+
console.log(chalk.blue(`🏗️ Diamond deployment configuration (direct Defender lookup):`));
|
|
404
|
+
console.log(chalk.blue(` Contract Name: ${diamondContractName}`));
|
|
405
|
+
console.log(chalk.blue(` Contract Path: ${diamondArtifact.sourceName}`));
|
|
406
|
+
console.log(chalk.blue(` Constructor Params:`));
|
|
407
|
+
console.log(chalk.blue(` Owner: ${deployerAddress}`));
|
|
408
|
+
console.log(chalk.blue(` DiamondCutFacet: ${directDiamondCutFacetAddress}`));
|
|
409
|
+
|
|
410
|
+
const diamondRequest: DeployContractRequest = {
|
|
411
|
+
network,
|
|
412
|
+
contractName: diamondContractName,
|
|
413
|
+
contractPath: diamondArtifact.sourceName,
|
|
414
|
+
constructorInputs: [deployerAddress, directDiamondCutFacetAddress], // Use address from Defender
|
|
415
|
+
verifySourceCode: true,
|
|
416
|
+
artifactPayload: JSON.stringify(buildInfo),
|
|
417
|
+
salt: ethers.hexlify(ethers.randomBytes(32)),
|
|
418
|
+
};
|
|
419
|
+
|
|
420
|
+
const diamondDeployment = await this.client.deploy.deployContract(diamondRequest);
|
|
421
|
+
console.log(chalk.blue(`📊 Diamond deployment response:`, JSON.stringify(diamondDeployment, null, 2)));
|
|
422
|
+
|
|
423
|
+
store.saveStep({
|
|
424
|
+
stepName: stepNameDiamond,
|
|
425
|
+
proposalId: diamondDeployment.deploymentId,
|
|
426
|
+
status: 'pending',
|
|
427
|
+
description: 'Diamond deployed via Defender DeployClient',
|
|
428
|
+
timestamp: Date.now()
|
|
429
|
+
});
|
|
430
|
+
await this.pollUntilComplete(stepNameDiamond, diamond);
|
|
431
|
+
|
|
432
|
+
console.log(chalk.blue(`📡 Submitted Diamond deploy to Defender: ${diamondDeployment.deploymentId}`));
|
|
433
|
+
return; // Exit early since we handled the deployment
|
|
434
|
+
}
|
|
435
|
+
} catch (error) {
|
|
436
|
+
console.error(chalk.red(`❌ Could not get DiamondCutFacet from Defender: ${error}`));
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
throw new Error('DiamondCutFacet must be deployed before Diamond contract. DiamondCutFacet address not found.');
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
console.log(chalk.blue(`🔗 Using DiamondCutFacet address: ${diamondCutFacetAddress}`));
|
|
444
|
+
|
|
445
|
+
const diamondContractName = await getDiamondContractName(diamond.diamondName, diamond);
|
|
446
|
+
const diamondArtifact = await getContractArtifact(diamond.diamondName, diamond);
|
|
447
|
+
const buildInfo = await this.getBuildInfoForContract(diamond.diamondName, diamond);
|
|
448
|
+
|
|
449
|
+
// Validate the build info structure
|
|
450
|
+
if (!buildInfo.output?.contracts?.[diamondArtifact.sourceName]?.[diamondContractName]) {
|
|
451
|
+
console.warn(chalk.yellow(`⚠️ Build info validation warning for ${diamondContractName}`));
|
|
452
|
+
console.warn(chalk.yellow(` Expected path: output.contracts["${diamondArtifact.sourceName}"]["${diamondContractName}"]`));
|
|
453
|
+
console.warn(chalk.yellow(` Available contracts:`, Object.keys(buildInfo.output?.contracts || {})));
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
console.log(chalk.blue(`🏗️ Diamond deployment configuration:`));
|
|
457
|
+
console.log(chalk.blue(` Contract Name: ${diamondContractName}`));
|
|
458
|
+
console.log(chalk.blue(` Contract Path: ${diamondArtifact.sourceName}`));
|
|
459
|
+
console.log(chalk.blue(` Constructor Params:`));
|
|
460
|
+
console.log(chalk.blue(` Owner: ${deployerAddress}`));
|
|
461
|
+
console.log(chalk.blue(` DiamondCutFacet: ${diamondCutFacetAddress}`));
|
|
462
|
+
|
|
463
|
+
const diamondRequest: DeployContractRequest = {
|
|
464
|
+
network,
|
|
465
|
+
contractName: diamondContractName,
|
|
466
|
+
contractPath: diamondArtifact.sourceName,
|
|
467
|
+
constructorInputs: [deployerAddress, diamondCutFacetAddress], // Use actual DiamondCutFacet address instead of ZeroAddress
|
|
468
|
+
verifySourceCode: true, // TODO Verify this should be true or optional
|
|
469
|
+
artifactPayload: JSON.stringify(buildInfo), // Use build-info format for Defender SDK
|
|
470
|
+
salt: ethers.hexlify(ethers.randomBytes(32)), // Add salt for CREATE2 deployment as required by Defender
|
|
471
|
+
};
|
|
472
|
+
|
|
473
|
+
const diamondDeployment = await this.client.deploy.deployContract(diamondRequest);
|
|
474
|
+
console.log(chalk.blue(`📊 Diamond deployment response:`, JSON.stringify(diamondDeployment, null, 2)));
|
|
475
|
+
|
|
476
|
+
store.saveStep({
|
|
477
|
+
stepName: stepNameDiamond,
|
|
478
|
+
proposalId: diamondDeployment.deploymentId,
|
|
479
|
+
status: 'pending',
|
|
480
|
+
description: 'Diamond deployed via Defender DeployClient',
|
|
481
|
+
timestamp: Date.now()
|
|
482
|
+
});
|
|
483
|
+
await this.pollUntilComplete(stepNameDiamond, diamond);
|
|
484
|
+
|
|
485
|
+
console.log(chalk.blue(`📡 Submitted Diamond deploy to Defender: ${diamondDeployment.deploymentId}`));
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
protected async preDeployFacetsTasks(diamond: Diamond): Promise<void> {
|
|
490
|
+
const facets = Object.keys(diamond.getDeployConfig().facets);
|
|
491
|
+
for (const facet of facets) {
|
|
492
|
+
await this.checkAndUpdateDeployStep(`deploy-${facet}`, diamond);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
/**
|
|
497
|
+
* deployFacetsTasks
|
|
498
|
+
*
|
|
499
|
+
* Deploys the facets of the diamond using OpenZeppelin Defender.
|
|
500
|
+
*
|
|
501
|
+
* @param diamond
|
|
502
|
+
*/
|
|
503
|
+
protected async deployFacetsTasks(diamond: Diamond): Promise<void> {
|
|
504
|
+
const deployConfig = diamond.getDeployConfig();
|
|
505
|
+
const facetsConfig = deployConfig.facets;
|
|
506
|
+
const diamondConfig = diamond.getDiamondConfig();
|
|
507
|
+
const network = diamondConfig.networkName!;
|
|
508
|
+
const deploymentId = `${diamond.diamondName}-${network}-${diamondConfig.chainId}`;
|
|
509
|
+
const store = new DefenderDeploymentStore(diamond.diamondName, deploymentId, diamondConfig.deploymentsPath);
|
|
510
|
+
|
|
511
|
+
const signer = diamond.getSigner()!;
|
|
512
|
+
const facetNamesSorted = Object.keys(facetsConfig).sort((a, b) => {
|
|
513
|
+
return (facetsConfig[a].priority ?? 1000) - (facetsConfig[b].priority ?? 1000);
|
|
514
|
+
});
|
|
515
|
+
|
|
516
|
+
for (const facetName of facetNamesSorted) {
|
|
517
|
+
const stepKey = `deploy-${facetName}`;
|
|
518
|
+
const step = store.getStep(stepKey);
|
|
519
|
+
if (step?.status === 'executed') {
|
|
520
|
+
console.log(chalk.gray(`⏩ Skipping already-deployed facet: ${facetName}`));
|
|
521
|
+
continue;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
const facetConfig = facetsConfig[facetName];
|
|
525
|
+
const deployedVersion = diamond.getDeployedDiamondData().DeployedFacets?.[facetName]?.version ?? -1;
|
|
526
|
+
const availableVersions = Object.keys(facetConfig.versions ?? {}).map(Number);
|
|
527
|
+
const targetVersion = Math.max(...availableVersions);
|
|
528
|
+
|
|
529
|
+
if (targetVersion <= deployedVersion && deployedVersion !== -1) {
|
|
530
|
+
console.log(chalk.gray(`⏩ Skipping facet ${facetName}, already at version ${deployedVersion}`));
|
|
531
|
+
continue;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
console.log(chalk.cyan(`🔧 Deploying facet ${facetName} to version ${targetVersion}...`));
|
|
535
|
+
|
|
536
|
+
const facetContractName = await getContractName(facetName, diamond);
|
|
537
|
+
const facetArtifact = await getContractArtifact(facetName, diamond);
|
|
538
|
+
const buildInfo = await this.getBuildInfoForContract(facetName, diamond);
|
|
539
|
+
const deployRequest: DeployContractRequest = {
|
|
540
|
+
network,
|
|
541
|
+
contractName: facetContractName,
|
|
542
|
+
contractPath: facetArtifact.sourceName,
|
|
543
|
+
constructorInputs: [],
|
|
544
|
+
verifySourceCode: true, // TODO Verify this should be true or optional
|
|
545
|
+
artifactPayload: JSON.stringify(buildInfo), // Use build-info format for Defender SDK
|
|
546
|
+
salt: ethers.hexlify(ethers.randomBytes(32)), // Add salt for CREATE2 deployment as required by Defender // Fixed format to match contracts wrapper structure
|
|
547
|
+
};
|
|
548
|
+
|
|
549
|
+
const deployResult = await this.client.deploy.deployContract(deployRequest);
|
|
550
|
+
|
|
551
|
+
store.saveStep({
|
|
552
|
+
stepName: stepKey,
|
|
553
|
+
proposalId: deployResult.deploymentId,
|
|
554
|
+
status: 'pending',
|
|
555
|
+
description: `Facet ${facetName} deployment submitted`,
|
|
556
|
+
timestamp: Date.now()
|
|
557
|
+
});
|
|
558
|
+
await this.pollUntilComplete(stepKey, diamond);
|
|
559
|
+
|
|
560
|
+
console.log(chalk.blue(`📡 Submitted deployment for facet ${facetName}: ${deployResult.deploymentId}`));
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
|
|
565
|
+
/**
|
|
566
|
+
* Performs the diamond cut tasks using OpenZeppelin Defender.
|
|
567
|
+
* @param diamond The diamond instance.
|
|
568
|
+
*/
|
|
569
|
+
|
|
570
|
+
/**
|
|
571
|
+
* Performs the diamond cut tasks using OpenZeppelin Defender with batching support.
|
|
572
|
+
* @param diamond The diamond instance.
|
|
573
|
+
*/
|
|
574
|
+
protected async performDiamondCutTasks(diamond: Diamond): Promise<void> {
|
|
575
|
+
const deployedDiamondData = diamond.getDeployedDiamondData();
|
|
576
|
+
const diamondAddress = deployedDiamondData.DiamondAddress!;
|
|
577
|
+
const deployConfig = diamond.getDeployConfig();
|
|
578
|
+
const diamondConfig = diamond.getDiamondConfig();
|
|
579
|
+
const network = diamondConfig.networkName!;
|
|
580
|
+
const [initCalldata, initAddress] = await this.getInitCalldata(diamond);
|
|
581
|
+
const facetCuts = await this.getFacetCuts(diamond);
|
|
582
|
+
|
|
583
|
+
await this.validateNoOrphanedSelectors(facetCuts);
|
|
584
|
+
|
|
585
|
+
// If no cuts needed, skip
|
|
586
|
+
if (facetCuts.length === 0) {
|
|
587
|
+
if (this.verbose) {
|
|
588
|
+
console.log(chalk.yellow('⏩ No DiamondCut operations needed - all facets already deployed and up to date'));
|
|
589
|
+
}
|
|
590
|
+
return;
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
// Check for batch size limits
|
|
594
|
+
const MAX_BATCH_SIZE = 10; // Conservative limit for gas and transaction size
|
|
595
|
+
const needsBatching = facetCuts.length > MAX_BATCH_SIZE;
|
|
596
|
+
|
|
597
|
+
if (needsBatching) {
|
|
598
|
+
console.log(chalk.yellow(`⚠️ Large DiamondCut detected (${facetCuts.length} cuts). Splitting into batches...`));
|
|
599
|
+
await this.performBatchedDiamondCut(diamond, facetCuts, initCalldata, initAddress);
|
|
600
|
+
return;
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
// Single batch execution
|
|
604
|
+
await this.performSingleDiamondCut(diamond, facetCuts, initCalldata, initAddress);
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
/**
|
|
608
|
+
* Perform a single DiamondCut operation
|
|
609
|
+
*/
|
|
610
|
+
private async performSingleDiamondCut(
|
|
611
|
+
diamond: Diamond,
|
|
612
|
+
facetCuts: FacetCuts,
|
|
613
|
+
initCalldata: string,
|
|
614
|
+
initAddress: string
|
|
615
|
+
): Promise<void> {
|
|
616
|
+
const deployedDiamondData = diamond.getDeployedDiamondData();
|
|
617
|
+
const diamondAddress = deployedDiamondData.DiamondAddress!;
|
|
618
|
+
const deployConfig = diamond.getDeployConfig();
|
|
619
|
+
const diamondConfig = diamond.getDiamondConfig();
|
|
620
|
+
const network = diamondConfig.networkName!;
|
|
621
|
+
|
|
622
|
+
if (this.verbose) {
|
|
623
|
+
console.log(chalk.yellowBright(`\n🪓 Performing DiamondCut with ${facetCuts.length} cut(s):`));
|
|
624
|
+
for (const cut of facetCuts) {
|
|
625
|
+
console.log(chalk.bold(`- ${FacetCutAction[cut.action]} for facet ${cut.name} at ${cut.facetAddress}`));
|
|
626
|
+
console.log(chalk.gray(` Selectors:`), cut.functionSelectors);
|
|
627
|
+
}
|
|
628
|
+
if (initAddress !== ethers.ZeroAddress) {
|
|
629
|
+
console.log(chalk.cyan(`Initializing with functionSelector ${initCalldata} on ProtocolInitFacet ${deployConfig.protocolInitFacet} @ ${initAddress}`));
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
const proposal: CreateProposalRequest = {
|
|
634
|
+
contract: {
|
|
635
|
+
address: diamondAddress,
|
|
636
|
+
network,
|
|
637
|
+
},
|
|
638
|
+
title: `DiamondCut ${facetCuts.length} facets`,
|
|
639
|
+
description: 'Perform diamondCut via Defender',
|
|
640
|
+
type: 'custom',
|
|
641
|
+
functionInterface: {
|
|
642
|
+
name: 'diamondCut',
|
|
643
|
+
inputs: [
|
|
644
|
+
{
|
|
645
|
+
name: 'facetCuts',
|
|
646
|
+
type: 'tuple[]',
|
|
647
|
+
components: [
|
|
648
|
+
{ name: 'facetAddress', type: 'address' },
|
|
649
|
+
{ name: 'action', type: 'uint8' },
|
|
650
|
+
{ name: 'functionSelectors', type: 'bytes4[]' }
|
|
651
|
+
]
|
|
652
|
+
},
|
|
653
|
+
{ name: 'initAddress', type: 'address' },
|
|
654
|
+
{ name: 'initCalldata', type: 'bytes' },
|
|
655
|
+
],
|
|
656
|
+
},
|
|
657
|
+
functionInputs: [
|
|
658
|
+
JSON.stringify(facetCuts.map(cut => ({
|
|
659
|
+
facetAddress: cut.facetAddress,
|
|
660
|
+
action: cut.action,
|
|
661
|
+
functionSelectors: cut.functionSelectors
|
|
662
|
+
}))),
|
|
663
|
+
initAddress,
|
|
664
|
+
initCalldata
|
|
665
|
+
],
|
|
666
|
+
via: this.via,
|
|
667
|
+
viaType: this.viaType,
|
|
668
|
+
};
|
|
669
|
+
|
|
670
|
+
try {
|
|
671
|
+
const { proposalId, url } = await this.client.proposal.create({ proposal });
|
|
672
|
+
console.log(chalk.blue(`📡 Defender Proposal created: ${url}`));
|
|
673
|
+
|
|
674
|
+
// Store the proposal
|
|
675
|
+
const store = new DefenderDeploymentStore(diamond.diamondName, `${diamond.diamondName}-${network}-${diamondConfig.chainId}`, diamondConfig.deploymentsPath);
|
|
676
|
+
store.saveStep({
|
|
677
|
+
stepName: 'diamond-cut',
|
|
678
|
+
proposalId,
|
|
679
|
+
status: 'pending',
|
|
680
|
+
description: `DiamondCut proposal with ${facetCuts.length} facets`,
|
|
681
|
+
timestamp: Date.now()
|
|
682
|
+
});
|
|
683
|
+
|
|
684
|
+
if (this.autoApprove) {
|
|
685
|
+
await this.handleAutoApproval(proposalId, url);
|
|
686
|
+
} else {
|
|
687
|
+
console.log(chalk.blue(`🔗 Manual approval required: ${url}`));
|
|
688
|
+
}
|
|
689
|
+
} catch (error: any) {
|
|
690
|
+
if (error.response?.status === 402) {
|
|
691
|
+
throw new Error('Defender account billing issue. Please check your Defender account subscription and billing status.');
|
|
692
|
+
} else if (error.response?.status === 400) {
|
|
693
|
+
throw new Error(`DiamondCut request invalid. This may be due to gas limits with ${facetCuts.length} cuts. Consider reducing batch size.`);
|
|
694
|
+
}
|
|
695
|
+
throw error;
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
/**
|
|
700
|
+
* Perform batched DiamondCut operations
|
|
701
|
+
*/
|
|
702
|
+
private async performBatchedDiamondCut(
|
|
703
|
+
diamond: Diamond,
|
|
704
|
+
allFacetCuts: FacetCuts,
|
|
705
|
+
initCalldata: string,
|
|
706
|
+
initAddress: string
|
|
707
|
+
): Promise<void> {
|
|
708
|
+
const MAX_BATCH_SIZE = 10;
|
|
709
|
+
const batches = [];
|
|
710
|
+
|
|
711
|
+
// Split into batches
|
|
712
|
+
for (let i = 0; i < allFacetCuts.length; i += MAX_BATCH_SIZE) {
|
|
713
|
+
batches.push(allFacetCuts.slice(i, i + MAX_BATCH_SIZE));
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
console.log(chalk.blue(`📦 Splitting ${allFacetCuts.length} cuts into ${batches.length} batches`));
|
|
717
|
+
|
|
718
|
+
// Execute batches sequentially
|
|
719
|
+
for (let batchIndex = 0; batchIndex < batches.length; batchIndex++) {
|
|
720
|
+
const batch = batches[batchIndex];
|
|
721
|
+
const isLastBatch = batchIndex === batches.length - 1;
|
|
722
|
+
|
|
723
|
+
// Only use init on the last batch
|
|
724
|
+
const batchInitCalldata = isLastBatch ? initCalldata : '0x';
|
|
725
|
+
const batchInitAddress = isLastBatch ? initAddress : ethers.ZeroAddress;
|
|
726
|
+
|
|
727
|
+
console.log(chalk.blue(`🔄 Processing batch ${batchIndex + 1}/${batches.length} (${batch.length} cuts)`));
|
|
728
|
+
|
|
729
|
+
try {
|
|
730
|
+
await this.performSingleDiamondCut(diamond, batch, batchInitCalldata, batchInitAddress);
|
|
731
|
+
console.log(chalk.green(`✅ Batch ${batchIndex + 1} completed successfully`));
|
|
732
|
+
|
|
733
|
+
// Wait between batches to avoid rate limiting
|
|
734
|
+
if (batchIndex < batches.length - 1) {
|
|
735
|
+
console.log(chalk.gray('⏳ Waiting 5 seconds before next batch...'));
|
|
736
|
+
await new Promise(resolve => setTimeout(resolve, 5000));
|
|
737
|
+
}
|
|
738
|
+
} catch (error) {
|
|
739
|
+
console.error(chalk.red(`❌ Batch ${batchIndex + 1} failed:`), error);
|
|
740
|
+
throw new Error(`DiamondCut batch ${batchIndex + 1} failed: ${error instanceof Error ? error.message : error}`);
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
console.log(chalk.green(`🎉 All ${batches.length} DiamondCut batches completed successfully!`));
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
/**
|
|
748
|
+
* Handle auto-approval for proposals
|
|
749
|
+
*/
|
|
750
|
+
private async handleAutoApproval(proposalId: string, url: string): Promise<void> {
|
|
751
|
+
const maxAttempts = 30;
|
|
752
|
+
const delayMs = 8000;
|
|
753
|
+
let attempts = 0;
|
|
754
|
+
|
|
755
|
+
console.log(chalk.blue(`⚡ Auto-approving proposal ${proposalId}...`));
|
|
756
|
+
|
|
757
|
+
while (attempts < maxAttempts) {
|
|
758
|
+
try {
|
|
759
|
+
// For auto-approval, we'll just log the status
|
|
760
|
+
// Note: The actual execution method may vary by Defender API version
|
|
761
|
+
console.log(chalk.gray(`⌛ Proposal status check ${attempts + 1}/${maxAttempts}. Manual execution may be required.`));
|
|
762
|
+
|
|
763
|
+
} catch (err: any) {
|
|
764
|
+
console.error(chalk.red(`⚠️ Error checking proposal status:`), err);
|
|
765
|
+
if (attempts >= maxAttempts - 1) {
|
|
766
|
+
throw err;
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
await new Promise((res) => setTimeout(res, delayMs));
|
|
771
|
+
attempts++;
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
if (attempts >= maxAttempts) {
|
|
775
|
+
console.warn(chalk.red(`⚠️ Proposal polling completed after ${maxAttempts} attempts.`));
|
|
776
|
+
console.log(chalk.blue(`🔗 Manual execution may be required: ${url}`));
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
/**
|
|
781
|
+
* Get build-info format for a contract that Defender SDK expects
|
|
782
|
+
*/
|
|
783
|
+
private async getBuildInfoForContract(contractName: string, diamond: Diamond): Promise<any> {
|
|
784
|
+
try {
|
|
785
|
+
// Get the contract artifact first to determine the source path
|
|
786
|
+
const artifact = await getContractArtifact(contractName, diamond);
|
|
787
|
+
const sourceName = artifact.sourceName;
|
|
788
|
+
|
|
789
|
+
// Try to find the build-info file that contains this contract
|
|
790
|
+
const buildInfoPath = join(process.cwd(), 'artifacts', 'build-info');
|
|
791
|
+
const buildInfoFiles = fs.readdirSync(buildInfoPath);
|
|
792
|
+
|
|
793
|
+
for (const fileName of buildInfoFiles) {
|
|
794
|
+
if (!fileName.endsWith('.json')) continue;
|
|
795
|
+
|
|
796
|
+
const filePath = join(buildInfoPath, fileName);
|
|
797
|
+
const buildInfo = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
|
798
|
+
|
|
799
|
+
// Check if this build-info contains our contract
|
|
800
|
+
if (buildInfo.output?.contracts?.[sourceName]?.[contractName]) {
|
|
801
|
+
return buildInfo;
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
// Fallback: create a minimal build-info structure
|
|
806
|
+
console.warn(`⚠️ Could not find build-info for ${contractName}, creating minimal structure`);
|
|
807
|
+
return {
|
|
808
|
+
input: {
|
|
809
|
+
language: 'Solidity',
|
|
810
|
+
sources: {
|
|
811
|
+
[sourceName]: {
|
|
812
|
+
content: '// Source not available'
|
|
813
|
+
}
|
|
814
|
+
},
|
|
815
|
+
settings: {
|
|
816
|
+
optimizer: { enabled: true, runs: 1000 },
|
|
817
|
+
outputSelection: {
|
|
818
|
+
'*': {
|
|
819
|
+
'*': ['abi', 'evm.bytecode', 'evm.deployedBytecode']
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
},
|
|
824
|
+
output: {
|
|
825
|
+
contracts: {
|
|
826
|
+
[sourceName]: {
|
|
827
|
+
[contractName]: artifact
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
};
|
|
832
|
+
|
|
833
|
+
} catch (error) {
|
|
834
|
+
console.warn(`⚠️ Error getting build info for ${contractName}:`, error);
|
|
835
|
+
// Return a minimal structure as fallback
|
|
836
|
+
const artifact = await getContractArtifact(contractName, diamond);
|
|
837
|
+
return {
|
|
838
|
+
output: {
|
|
839
|
+
contracts: {
|
|
840
|
+
[artifact.sourceName]: {
|
|
841
|
+
[contractName]: artifact
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
};
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
}
|