-
Notifications
You must be signed in to change notification settings - Fork 0
/
uploadAndInstantiateGateway.ts
292 lines (240 loc) · 10.2 KB
/
uploadAndInstantiateGateway.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import { BroadcastMode, SecretNetworkClient, Wallet } from "secretjs";
import * as fs from "fs";
import path from 'path';
import { fileURLToPath } from 'url';
import config from './config/config.js';
import { getSecretGatewayContractKeys } from "./functions/query/getSecretGatewayContractKeys.js";
import { loadDeployed } from "./loadDeployed.js";
import { writeDeployed } from "./writeDeployed.js";
async function main () {
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
console.log('__dirname: ', __dirname);
const walletOptions = {
hdAccountIndex: 0,
coinType: 529,
bech32Prefix: 'secret',
}
const isLocal = config.networkSettings.secret.network == "localhost";
const { walletMnemonic, isOptimizedContractWasm, secretNunya: { nunyaContractWasmPath }, secretGateway: { gatewayContractAdminAddress, gatewayContractCodeId, gatewayContractCodeHash, gatewayContractWasmPath, gatewayContractWasmPathOptimized }, chainId: secretChainId, endpoint: secretEndpoint } =
isLocal == false
? config.networkSettings.secret.testnet
: config.networkSettings.secret.localhost;
const relayerPath = config.networkSettings.relayer.path;
if (walletMnemonic == "") {
throw Error("Unable to obtain mnemonic phrase");
}
const wallet = new Wallet(walletMnemonic, walletOptions);
console.log('wallet address: ', wallet.address);
const rootPath = path.join(__dirname, '../../../'); // relative to ./dist
console.log('rootPath', rootPath)
const secretGatewayContractWASMPath = `${rootPath}packages/secret-contracts/secret-gateway/${isOptimizedContractWasm ? "optimized-wasm/" + gatewayContractWasmPathOptimized : gatewayContractWasmPath}`;
console.log('secretGatewayContractWASMPath: ', secretGatewayContractWASMPath);
const secret_gateway_contract_wasm: any = fs.readFileSync(secretGatewayContractWASMPath);
// console.log('secret_gateway_contract_wasm: ', secret_gateway_contract_wasm);
const secretjs = new SecretNetworkClient({
chainId: secretChainId,
url: secretEndpoint || "",
wallet: wallet,
walletAddress: wallet.address,
});
console.log('secretEndpoint: ', secretEndpoint);
// console.log('secretjs: ', secretjs);
let deployed = await loadDeployed();
deployed.data.evm.network = config.networkSettings.evm.network;
deployed.data.secret.network = config.networkSettings.secret.network;
if (isLocal) {
console.log("isLocal - relayerPath: ", relayerPath);
deployed.data.relayer.path = relayerPath;
deployed.data.secret.localhost.chainId = secretChainId;
deployed.data.secret.localhost.endpoint = secretEndpoint;
} else {
console.log("isLocal - relayerPath: ", relayerPath);
deployed.data.relayer.path = relayerPath;
deployed.data.secret.testnet.chainId = secretChainId;
deployed.data.secret.testnet.endpoint = secretEndpoint;
}
await writeDeployed(deployed);
const { balance } = await secretjs.query.bank.balance({
address: wallet.address,
denom: "uscrt",
});
console.log('balance: ', balance);
type INIT_MSG = {
admin: string,
};
type CODE_PARAMS = {
codeId: string,
contractCodeHash: string,
};
type CONTRACT_PARAMS = {
contractAddress: string,
contractCodeHash: string,
};
// TODO: Refactor into its own file
let uploadContract = async () => {
console.log("Starting deployment...");
let codeId: string;
let contractCodeHash: string;
let tx: any;
let txParams = {
sender: wallet.address,
wasm_byte_code: secret_gateway_contract_wasm,
source: "",
builder: "",
};
// ../../packages/secret-contracts-scripts/node_modules/secretjs/src/secret_network_client.ts
let txOptions = {
// requires gasLimit of 5844449 to deploy locally
gasLimit: 6_000_000, // default 25_000
gasPriceInFeeDenom: 1, // default 0.1
feeDenom: "uscrt",
feeGranter: wallet.address,
waitForCommit: true, // default true
broadcastTimeoutMs: 240_000, // default 60_000
broadcastCheckIntervalMs: 24_000, // default 6_000 for 6 second block
broadcastMode: BroadcastMode.Async,
};
try {
tx = await secretjs.tx.compute.storeCode(txParams, txOptions);
} catch (e) {
console.log('error: ', e);
}
if (typeof tx == undefined) {
throw Error("Unable to obtain codeId");
}
// View tx in block explorer - https://docs.scrt.network/secret-network-documentation/overview-ecosystem-and-technology/ecosystem-overview/explorers-and-tools
codeId = tx?.arrayLog?.find((log: any) => log?.type === "message" && log?.key === "code_id")?.value;
if (tx?.rawLog) {
console.log("tx.rawLog: ", tx?.rawLog);
} else {
console.log("tx: ", tx);
}
if (codeId == "") {
throw Error("Unable to obtain codeId");
}
contractCodeHash = (
await secretjs.query.compute.codeHashByCodeId({ code_id: codeId.toString() })
).code_hash || "";
if (contractCodeHash == "") {
throw Error("Unable to obtain contractCodeHash");
}
return {
codeId,
contractCodeHash
}
};
// TODO: Refactor into its own file
let instantiateContract = async (params: CODE_PARAMS) => {
console.log("Instantiating contract...");
console.log('params: ', params)
if (typeof params.codeId == undefined || typeof params.contractCodeHash == undefined) {
throw Error("Unable to instantiate without codeId and contractCodeHash");
}
let contractAddress: string;
if (!params.codeId || !params.contractCodeHash) {
throw new Error("codeId or contractCodeHash is not set.");
}
let initMsg: INIT_MSG = {
admin: gatewayContractAdminAddress,
};
let txParams = {
code_id: params.codeId.toString(),
sender: wallet.address,
code_hash: params.contractCodeHash.toString(),
init_msg: initMsg,
label: "SnakePath Encrypt " + Math.ceil(Math.random() * 10000),
};
let txOptions = {
gasLimit: 5_000_000, // default 25_000
gasPriceInFeeDenom: 1, // default 0.1
feeDenom: "uscrt",
feeGranter: wallet.address,
waitForCommit: true, // default true
broadcastTimeoutMs: 240_000, // default 60_000
broadcastCheckIntervalMs: 24_000, // default 6_000 for 6 second block
broadcastMode: BroadcastMode.Async,
};
let tx = await secretjs.tx.compute.instantiateContract(txParams, txOptions);
console.log('tx: ', tx)
// Find the contract_address in the logs
contractAddress = tx?.arrayLog?.find(
(log) => log?.type === "message" && log?.key === "contract_address"
)?.value || "";
if (contractAddress == "") {
throw Error("Unable to find the contract_address");
}
let contractParams: CONTRACT_PARAMS = {
contractAddress,
contractCodeHash: params.contractCodeHash,
};
return contractParams;
};
// Chain the execution using promises
await uploadContract()
.then(async (res: CODE_PARAMS) => {
console.log(`CODE_ID: ${res.codeId}`);
console.log(`CODE_HASH: ${res.contractCodeHash}`);
let deployed = await loadDeployed();
if (isLocal) {
deployed.data.secret.localhost.secretGateway.gatewayContractAdminAddress = gatewayContractAdminAddress;
deployed.data.secret.localhost.secretGateway.gatewayContractCodeId = res.codeId;
deployed.data.secret.localhost.secretGateway.gatewayContractCodeHash = res.contractCodeHash;
} else {
deployed.data.secret.testnet.secretGateway.gatewayContractAdminAddress = gatewayContractAdminAddress;
deployed.data.secret.testnet.secretGateway.gatewayContractCodeId = res.codeId;
deployed.data.secret.testnet.secretGateway.gatewayContractCodeHash = res.contractCodeHash;
}
await writeDeployed(deployed);
const codeParams = {
codeId: res.codeId,
contractCodeHash: res.contractCodeHash,
};
await instantiateContract(codeParams)
.then(async (contractParams: CONTRACT_PARAMS) => {
console.log("SECRET_ADDRESS: ", contractParams.contractAddress);
deployed = await loadDeployed();
if (isLocal) {
deployed.data.secret.localhost.secretGateway.gatewayContractAddress = contractParams.contractAddress;
} else {
deployed.data.secret.testnet.secretGateway.gatewayContractAddress = contractParams.contractAddress;
}
await writeDeployed(deployed);
// TODO: Refactor and put common types in types.ts
type EphemeralKeys = {
encryption_key: string,
verification_key: string,
}
let params = {
endpoint: secretEndpoint,
chainId: secretChainId,
contractAddress: contractParams.contractAddress,
contractCodeHash: res.contractCodeHash,
};
// Fetch the latest from the Secret Gateway since it is generated randomly when instantiated rather than rely on user having populated it in config.ts already
const secretGatewayContractKeys: EphemeralKeys = await getSecretGatewayContractKeys(params);
console.log('secretGatewayContractKeys.encryption_key: ', secretGatewayContractKeys.encryption_key);
console.log('secretGatewayContractKeys.verification_key: ', secretGatewayContractKeys.verification_key);
deployed = await loadDeployed();
if (isLocal) {
deployed.data.secret.localhost.secretGateway.gatewayContractEncryptionKeyForChaChaPoly1305 = secretGatewayContractKeys.encryption_key;
deployed.data.secret.localhost.secretGateway.gatewayContractPublicKey = secretGatewayContractKeys.verification_key;
} else {
deployed.data.secret.testnet.secretGateway.gatewayContractEncryptionKeyForChaChaPoly1305 = secretGatewayContractKeys.encryption_key;
deployed.data.secret.testnet.secretGateway.gatewayContractPublicKey = secretGatewayContractKeys.verification_key;
}
await writeDeployed(deployed);
})
.catch((error: any) => {
console.error("Error:", error);
});
})
.catch((error) => {
console.error("Error:", error);
});
process.exit()
}
main().catch((error) => {
console.error(error);
process.exit(-1);
});