-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbase-deploy-mission.ts
215 lines (179 loc) · 5.74 KB
/
base-deploy-mission.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* eslint-disable camelcase */
import {
TDeployArgs,
IProxyData,
IDeployMissionArgs,
ITenderlyContractData,
} from "./types";
import { DeployCampaign } from "../campaign/deploy-campaign";
import {
IContractState,
IContractV6,
IDeployCampaignConfig,
ITransactionResponseBase,
TLogger,
} from "../campaign/types";
import { IContractDbData } from "../db/types";
import { EnvironmentLevels, NetworkData } from "../deployer/constants";
import { IHardhatBase, ISignerBase } from "../deployer/types";
export class BaseDeployMission <
H extends IHardhatBase,
S extends ISignerBase,
C extends IDeployCampaignConfig<S>,
St extends IContractState,
> {
contractName! : string;
instanceName! : string;
proxyData! : IProxyData;
campaign : DeployCampaign<H, S, C, St>;
logger : TLogger;
config : C;
implAddress! : string | null;
constructor ({
campaign,
logger,
config,
} : IDeployMissionArgs<H, S, C, St>) {
this.campaign = campaign;
this.logger = logger;
this.config = config;
}
get dbName () : string {
return this.contractName;
}
async getFromDB () {
return this.campaign.dbAdapter.getContract(this.dbName);
}
async saveToDB (contract : IContractV6) {
this.logger.debug(`Writing ${this.dbName} to DB...`);
this.implAddress = this.proxyData.isProxy
? await this.campaign.deployer.getProxyImplAddress(await contract.getAddress())
: null;
const contractDbDoc = await this.buildDbObject(contract, this.implAddress);
return this.campaign.dbAdapter.writeContract(this.dbName, contractDbDoc);
}
async needsDeploy () {
const dbContract = await this.getFromDB();
if (!dbContract) {
this.logger.info(`${this.dbName} not found in DB, proceeding to deploy...`);
} else {
this.logger.info(`${this.dbName} found in DB at ${dbContract.address}, no deployment needed.`);
const contract = await this.campaign.deployer.getContractObject(
this.contractName,
dbContract.address,
);
// eslint-disable-next-line max-len
this.logger.debug(`Updating ${this.contractName} in state from DB data with address ${await contract.getAddress()}`);
this.campaign.updateStateContract(this.instanceName, this.contractName, contract);
}
return !dbContract;
}
async deployArgs () : Promise<TDeployArgs> {
return [];
}
getArtifact () {
return this.campaign.deployer.getContractArtifact(this.contractName);
}
async buildDbObject (
hhContract : IContractV6,
implAddress : string | null
) : Promise<Omit<IContractDbData, "version">> {
const { abi, bytecode } = this.getArtifact();
return {
name: this.dbName,
address: await hhContract.getAddress(),
abi: JSON.stringify(abi),
bytecode,
implementation: implAddress,
};
}
async deploy () {
const deployArgs = await this.deployArgs();
this.logger.info(`Deploying ${this.contractName} with arguments: ${deployArgs}`);
let contract : IContractV6;
if (this.proxyData.isProxy) {
contract = await this.campaign.deployer.deployProxy({
contractName: this.contractName,
args: deployArgs,
kind: this.proxyData.kind,
});
} else {
contract = await this.campaign.deployer.deployContract(this.contractName, deployArgs);
}
await this.saveToDB(contract);
this.campaign.updateStateContract(this.instanceName, this.contractName, contract);
this.logger.info(`Deployment success for ${this.contractName} at ${await contract.getAddress()}`);
}
async needsPostDeploy () {
return Promise.resolve(false);
}
async postDeploy () {
return Promise.resolve();
}
async awaitConfirmation (tx : ITransactionResponseBase | null) {
const {
config: {
env,
confirmationsN,
},
} = this.campaign;
if (env !== EnvironmentLevels.dev) {
if (tx) await tx.wait(confirmationsN);
}
}
async execute () {
if (await this.needsDeploy()) {
await this.deploy();
} else {
this.logger.info(`Skipping ${this.contractName} deployment...`);
}
if (await this.needsPostDeploy()) {
await this.postDeploy();
}
}
async verify () {
this.logger.debug(`Verifying ${this.contractName} on Etherscan...`);
const address = await this.campaign[this.instanceName].getAddress();
const ctorArgs = !this.proxyData.isProxy ? await this.deployArgs() : undefined;
try {
await this.campaign.deployer.etherscanVerify({
address,
ctorArgs,
});
this.logger.debug(`Etherscan verification for ${this.contractName} finished successfully.`);
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
} catch (e : any) {
this.logger.error(`Etherscan verification for ${this.contractName} failed.`);
this.logger.error(e.message);
this.logger.debug("Continuing...");
}
}
async getMonitoringData () : Promise<Array<ITenderlyContractData>> {
const networkId = NetworkData[this.campaign.config.env].id;
const implName = this.contractName;
let implAddress = await this.campaign[this.instanceName].getAddress();
if (this.proxyData.isProxy) {
const proxyAddress = await this.campaign[this.instanceName].getAddress();
implAddress = this.implAddress || await this.campaign.deployer.getProxyImplAddress(proxyAddress);
return [
{
display_name: `${this.contractName}Proxy`,
address: proxyAddress,
network_id: networkId,
},
{
display_name: `${implName}Impl`,
address: implAddress,
network_id: networkId,
},
];
}
return [
{
display_name: implName,
address: implAddress,
network_id: networkId,
},
];
}
}