-
Notifications
You must be signed in to change notification settings - Fork 0
/
hardhat-deployer.ts
131 lines (112 loc) · 3.59 KB
/
hardhat-deployer.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
import { ITenderlyContractData, TDeployArgs, TProxyKind } from "../missions/types";
import axios from "axios";
import { IContractV6 } from "../campaign/types";
import { IHardhatBase, ISignerBase, IProviderBase, IHardhatDeployerArgs } from "./types";
export class HardhatDeployer <
H extends IHardhatBase,
S extends ISignerBase,
P extends IProviderBase,
> {
hre : H;
signer : S;
env : string;
provider ?: P;
constructor ({
hre,
signer,
env,
provider,
} : IHardhatDeployerArgs<H, S, P>) {
this.hre = hre;
this.signer = signer;
this.env = env;
this.provider = provider;
}
async getFactory (contractName : string, signer ?: S) {
const attachedSigner = signer ?? this.signer;
return this.hre.ethers.getContractFactory(contractName, attachedSigner);
}
async getContractObject (contractName : string, address : string) {
const factory = await this.getFactory(contractName);
return factory.attach(address);
}
async deployProxy ({
contractName,
args,
kind,
} : {
contractName : string;
args : TDeployArgs;
kind : TProxyKind;
}) : Promise<IContractV6> {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const contractFactory = await this.getFactory(contractName);
const deployment = await this.hre.upgrades.deployProxy(contractFactory, args, {
kind,
});
let receipt;
if (!this.provider) {
return deployment.waitForDeployment();
} else {
const tx = await deployment.deploymentTransaction();
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
receipt = await this.provider.waitForTransaction(tx!.hash, 3);
return contractFactory.attach(receipt.contractAddress);
}
}
async deployContract (contractName : string, args : TDeployArgs) : Promise<IContractV6> {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const contractFactory = await this.getFactory(contractName);
const deployment = await contractFactory.deploy(...args);
let receipt;
if (!this.provider) {
return deployment.waitForDeployment();
} else {
const tx = await deployment.deploymentTransaction();
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
receipt = await this.provider.waitForTransaction(tx!.hash, 3);
return contractFactory.attach(receipt.contractAddress);
}
}
getContractArtifact (contractName : string) {
return this.hre.artifacts.readArtifactSync(contractName);
}
async getProxyImplAddress (proxyContract : string) {
return this.hre.upgrades.erc1967.getImplementationAddress(proxyContract);
}
async getBytecodeFromChain (address : string) {
return this.hre.ethers.provider.getCode(address);
}
async tenderlyPush (contracts : Array<ITenderlyContractData>) {
const inst = axios.create({
baseURL: "https://api.tenderly.co/",
headers: {
"Content-Type": "application/json",
"X-Access-Key": process.env.TENDERLY_ACCESS_KEY,
},
});
const { data } = await inst.post(
`api/v2/accounts/${process.env.TENDERLY_ACCOUNT_ID}/projects/${process.env.TENDERLY_PROJECT_SLUG}/contracts`,
{
contracts,
}
);
return data;
}
async etherscanVerify ({
address,
ctorArgs,
} : {
address : string;
ctorArgs ?: TDeployArgs;
}) {
return this.hre.run("verify:verify", {
address,
// this should only be used for non-proxied contracts
// or proxy impls that have actual constructors
constructorArguments: ctorArgs,
});
}
}