forked from Scaffold-Stark/scaffold-stark-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy-contract.ts
223 lines (201 loc) · 5.71 KB
/
deploy-contract.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
216
217
218
219
220
221
222
223
import fs from "fs";
import path from "path";
import { networks } from "./helpers/networks";
import yargs from "yargs";
import {
CallData,
stark,
RawArgs,
transaction,
extractContractHashes,
DeclareContractPayload
} from "starknet";
import { Network } from "./types";
const argv = yargs(process.argv.slice(2)).argv;
const networkName: string = argv["network"];
let deployments = {};
let deployCalls = [];
const { provider, deployer }: Network = networks[networkName];
const declareIfNot_NotWait = async (payload: DeclareContractPayload, options?: { maxFee?: bigint}) => {
const declareContractPayload = extractContractHashes(payload);
try {
await provider.getClassByHash(declareContractPayload.classHash);
} catch (error) {
try {
const declareOptions = options?.maxFee ? { maxFee: options.maxFee } : {};
const { transaction_hash } = await deployer.declare(payload, declareOptions);
if (networkName === "sepolia" || networkName === "mainnet") {
await provider.waitForTransaction(transaction_hash);
}
} catch (e) {
console.error("Error declaring contract:", e);
throw e;
}
}
return {
classHash: declareContractPayload.classHash,
};
};
const deployContract_NotWait = async (payload: {
salt: string;
classHash: string;
constructorCalldata: RawArgs;
}) => {
try {
const { calls, addresses } = transaction.buildUDCCall(
payload,
deployer.address
);
deployCalls.push(...calls);
return {
contractAddress: addresses[0],
};
} catch (error) {
console.error("Error building UDC call:", error);
throw error;
}
};
const deployContract = async (
contractName: string,
exportContractName?: string,
constructorArgs?: RawArgs,
options?: {
maxFee?: bigint;
}
): Promise<{
classHash: string;
address: string;
}> => {
try {
await deployer.getContractVersion(deployer.address);
} catch (e) {
if (e.toString().includes("Contract not found")) {
const errorMessage = `The wallet you're using to deploy the contract is not deployed in the ${networkName} network.`;
console.error(errorMessage);
throw new Error(errorMessage);
} else {
console.error("Error getting contract version:", e);
throw e;
}
}
let compiledContractCasm;
let compiledContractSierra;
try {
compiledContractCasm = JSON.parse(
fs
.readFileSync(
path.resolve(
__dirname,
`../contracts/target/dev/contracts_${contractName}.compiled_contract_class.json`
)
)
.toString("ascii")
);
} catch (error) {
if (
typeof error.message === "string" &&
error.message.includes("no such file") &&
error.message.includes("compiled_contract_class")
) {
const match = error.message.match(
/\/dev\/(.+?)\.compiled_contract_class/
);
const missingContractName = match ? match[1].split("_").pop() : "Unknown";
console.error(
`The contract "${missingContractName}" doesn't exist or is not compiled`
);
} else {
console.error("Error reading compiled contract class file:", error);
}
return {
classHash: "",
address: "",
};
}
try {
compiledContractSierra = JSON.parse(
fs
.readFileSync(
path.resolve(
__dirname,
`../contracts/target/dev/contracts_${contractName}.contract_class.json`
)
)
.toString("ascii")
);
} catch (error) {
console.error("Error reading contract class file:", error);
return {
classHash: "",
address: "",
};
}
const contractCalldata = new CallData(compiledContractSierra.abi);
const constructorCalldata = constructorArgs
? contractCalldata.compile("constructor", constructorArgs)
: [];
console.log("Deploying Contract ", contractName);
let { classHash } = await declareIfNot_NotWait({
contract: compiledContractSierra,
casm: compiledContractCasm,
}, options);
let randomSalt = stark.randomAddress();
let { contractAddress } = await deployContract_NotWait({
salt: randomSalt,
classHash,
constructorCalldata,
});
console.log("Contract Deployed at ", contractAddress);
let finalContractName = exportContractName || contractName;
deployments[finalContractName] = {
classHash: classHash,
address: contractAddress,
contract: contractName,
};
return {
classHash: classHash,
address: contractAddress,
};
};
const executeDeployCalls = async () => {
try {
let { transaction_hash } = await deployer.execute(deployCalls);
console.log("Deploy Calls Executed at ", transaction_hash);
if (networkName === "sepolia" || networkName === "mainnet") {
await provider.waitForTransaction(transaction_hash);
}
} catch (error) {
console.error("Error executing deploy calls:", error);
// split the calls in half and try again recursively
if (deployCalls.length > 1) {
let half = Math.ceil(deployCalls.length / 2);
let firstHalf = deployCalls.slice(0, half);
let secondHalf = deployCalls.slice(half);
deployCalls = firstHalf;
await executeDeployCalls();
deployCalls = secondHalf;
await executeDeployCalls();
}
}
};
const exportDeployments = () => {
const networkPath = path.resolve(
__dirname,
`../deployments/${networkName}_latest.json`
);
if (fs.existsSync(networkPath)) {
const currentTimestamp = new Date().getTime();
fs.renameSync(
networkPath,
networkPath.replace("_latest.json", `_${currentTimestamp}.json`)
);
}
fs.writeFileSync(networkPath, JSON.stringify(deployments, null, 2));
};
export {
deployContract,
provider,
deployer,
exportDeployments,
executeDeployCalls,
};