@layerzerolabs/lz-initia-cli 3.0.19 → 3.0.20

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/dist/index.cjs CHANGED
@@ -1,13 +1,15 @@
1
1
  'use strict';
2
2
 
3
- var fs3 = require('fs');
4
- var path2 = require('path');
3
+ var fs4 = require('fs');
4
+ var path3 = require('path');
5
5
  var initia_js = require('@initia/initia.js');
6
+ var sha3 = require('@noble/hashes/sha3');
7
+ var lzDefinitions = require('@layerzerolabs/lz-definitions');
8
+ var lzUtilities = require('@layerzerolabs/lz-utilities');
6
9
  var smolToml = require('smol-toml');
7
10
  var glob = require('glob');
8
11
  var zx = require('zx');
9
12
  var crypto = require('crypto');
10
- var lzDefinitions = require('@layerzerolabs/lz-definitions');
11
13
 
12
14
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
13
15
 
@@ -29,8 +31,8 @@ function _interopNamespace(e) {
29
31
  return Object.freeze(n);
30
32
  }
31
33
 
32
- var fs3__default = /*#__PURE__*/_interopDefault(fs3);
33
- var path2__default = /*#__PURE__*/_interopDefault(path2);
34
+ var fs4__default = /*#__PURE__*/_interopDefault(fs4);
35
+ var path3__default = /*#__PURE__*/_interopDefault(path3);
34
36
  var crypto__namespace = /*#__PURE__*/_interopNamespace(crypto);
35
37
 
36
38
  var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
@@ -39,6 +41,44 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
39
41
  if (typeof require !== "undefined") return require.apply(this, arguments);
40
42
  throw Error('Dynamic require of "' + x + '" is not supported');
41
43
  });
44
+ function getInitiaClient(env, config) {
45
+ const url = config.network?.[env];
46
+ if (url === void 0) {
47
+ throw new Error(`No network url for ${env} found in lz-initia.config.ts`);
48
+ }
49
+ const lcd = new initia_js.LCDClient(url);
50
+ return lcd;
51
+ }
52
+
53
+ // src/constant.ts
54
+ var InitiaObjectDeployerAddress = "0x1";
55
+
56
+ // src/config-parser.ts
57
+ function getConfigByNetwork(config, network) {
58
+ if (config === null) {
59
+ throw new Error("Config is null");
60
+ }
61
+ if (typeof config === "object") {
62
+ return config[network];
63
+ }
64
+ return config;
65
+ }
66
+ function getObjectDeployer(configValue, network) {
67
+ return getConfigByNetwork(configValue, network);
68
+ }
69
+ function getObjectDeployerFromConfig(config, network, moduleName) {
70
+ const module = config.modules[moduleName];
71
+ if (!module) {
72
+ throw new Error(`module ${moduleName} not found when getting object deployer`);
73
+ }
74
+ const moduleObjectDeployer = getObjectDeployer(module.objectDeployer, network);
75
+ const defaultObjectDeployer = getObjectDeployer(config.defaultObjectDeployer, network);
76
+ return moduleObjectDeployer ?? defaultObjectDeployer ?? InitiaObjectDeployerAddress;
77
+ }
78
+ function getFullName(config, moduleName) {
79
+ const variant = config.modules[moduleName]?.variant;
80
+ return variant !== void 0 && variant.length > 0 ? `${moduleName}-${variant}` : moduleName;
81
+ }
42
82
 
43
83
  // src/loader.ts
44
84
  function loadConfig(configPath) {
@@ -63,11 +103,11 @@ function loadConfig(configPath) {
63
103
  function isInitiaAccount(account) {
64
104
  return account !== void 0 && account instanceof initia_js.MnemonicKey;
65
105
  }
66
- function getDeployer(moduleName, lzInitiaConfig, network) {
67
- const module = lzInitiaConfig.modules[moduleName];
106
+ function getDeployer(moduleName, lzAptosConfig, network) {
107
+ const module = lzAptosConfig.modules[moduleName];
68
108
  if (!module) {
69
- if (lzInitiaConfig.baseModules && lzInitiaConfig.baseModules.length > 0) {
70
- for (const baseModule of lzInitiaConfig.baseModules) {
109
+ if (lzAptosConfig.baseModules && lzAptosConfig.baseModules.length > 0) {
110
+ for (const baseModule of lzAptosConfig.baseModules) {
71
111
  const baseConfig = loadConfig(baseModule);
72
112
  try {
73
113
  return getDeployer(moduleName, baseConfig, network);
@@ -76,23 +116,102 @@ function getDeployer(moduleName, lzInitiaConfig, network) {
76
116
  }
77
117
  }
78
118
  throw new Error(
79
- `Module ${moduleName} not found. Make sure it is defined in lz-initia.config.ts and double check the key is the package name in Move.toml.`
119
+ `Module ${moduleName} not found. Make sure it is defined in lz-aptos.config.ts and double check the key is the package name in Move.toml.`
80
120
  );
81
121
  }
82
122
  const moduleDeployer = isInitiaAccount(module.deployer) ? module.deployer : module.deployer?.[network];
83
- const defaultDeployer = isInitiaAccount(lzInitiaConfig.defaultDeployer) ? lzInitiaConfig.defaultDeployer : lzInitiaConfig.defaultDeployer?.[network];
123
+ const defaultDeployer = isInitiaAccount(lzAptosConfig.defaultDeployer) ? lzAptosConfig.defaultDeployer : lzAptosConfig.defaultDeployer?.[network];
84
124
  const deployer = moduleDeployer ?? defaultDeployer;
85
125
  if (!deployer) {
86
126
  throw new Error(`deployer for module ${moduleName} not found`);
87
127
  }
88
128
  return deployer;
89
129
  }
130
+ function getDeploymentPath(lzAptosConfig, network, moduleName) {
131
+ return path3__default.default.join(lzAptosConfig.deploymentPath, network, `${moduleName}.json`);
132
+ }
133
+ async function checkIfObjectAddressExists(lzAptosConfig, network, address) {
134
+ try {
135
+ const client = getInitiaClient(lzDefinitions.networkToEnv(network, lzDefinitions.EndpointVersion.V2), lzAptosConfig);
136
+ const object = await client.auth.accountInfo(address);
137
+ return object !== void 0;
138
+ } catch (e) {
139
+ return false;
140
+ }
141
+ }
142
+ function getObjectAddressFromDeployment(lzAptosConfig, network, moduleName) {
143
+ const deploymentPath2 = getDeploymentPath(lzAptosConfig, network, moduleName);
144
+ if (!fs4__default.default.existsSync(deploymentPath2)) {
145
+ return void 0;
146
+ }
147
+ const deployment = JSON.parse(fs4__default.default.readFileSync(deploymentPath2, "utf-8"));
148
+ return deployment.address;
149
+ }
150
+ async function resolveAddress(moduleName, lzAptosConfig, network, isOngoingModule) {
151
+ const deployToObject = lzAptosConfig.modules[moduleName]?.deployToObject ?? lzAptosConfig.defaultDeployToObject ?? false;
152
+ if (!deployToObject) {
153
+ const deployer = getDeployer(moduleName, lzAptosConfig, network);
154
+ return initia_js.AccAddress.toHex(deployer.accAddress);
155
+ }
156
+ const objectDeployer = getObjectDeployerFromConfig(lzAptosConfig, network, moduleName);
157
+ if (objectDeployer !== InitiaObjectDeployerAddress) {
158
+ return genObjectAddressForLayerzero(network, lzAptosConfig, moduleName);
159
+ }
160
+ const objectAddress = getObjectAddressFromDeployment(lzAptosConfig, network, moduleName);
161
+ if (objectAddress !== void 0) {
162
+ if (await checkIfObjectAddressExists(lzAptosConfig, network, objectAddress)) {
163
+ return objectAddress;
164
+ }
165
+ if (!isOngoingModule) {
166
+ throw new Error(
167
+ `Deployment file found at: ${getDeploymentPath(lzAptosConfig, network, moduleName)} but object address ${objectAddress} does not exist. Dependent module ${moduleName} should be deployed if using ${InitiaObjectDeployerAddress}::object_code_deployment.`
168
+ );
169
+ }
170
+ } else if (!isOngoingModule) {
171
+ throw new Error(
172
+ `Deployment file not found at: ${getDeploymentPath(lzAptosConfig, network, moduleName)}. Make sure ${moduleName} is deployed.`
173
+ );
174
+ }
175
+ return genObjectAddress(moduleName, lzAptosConfig, network);
176
+ }
177
+ var createObjectAddress = (creatorAddress, seed) => {
178
+ const hexAddress = initia_js.AccAddress.validate(creatorAddress) ? initia_js.AccAddress.toHex(creatorAddress) : creatorAddress;
179
+ const creatorBytes = initia_js.bcs.address().serialize(hexAddress).toBytes();
180
+ const seedBytes = typeof seed === "string" ? Buffer.from(seed, "utf8") : seed;
181
+ const bytes = new Uint8Array([...creatorBytes, ...seedBytes, 254]);
182
+ return lzUtilities.ensure0x(Buffer.from(sha3.sha3_256(bytes)).toString("hex"));
183
+ };
184
+ async function genObjectAddressForOfficial(network, lzAptosConfig, moduleName) {
185
+ const client = getInitiaClient(lzDefinitions.networkToEnv(network, lzDefinitions.EndpointVersion.V2), lzAptosConfig);
186
+ const deployer = getDeployer(moduleName, lzAptosConfig, network);
187
+ const OBJECT_CODE_DEPLOYMENT_DOMAIN_SEPARATOR = "aptos_framework::object_code_deployment";
188
+ const account = await client.auth.accountInfo(deployer.accAddress);
189
+ const sequenceNumber = account.getSequenceNumber();
190
+ const firstPart = initia_js.bcs.string().serialize(OBJECT_CODE_DEPLOYMENT_DOMAIN_SEPARATOR);
191
+ const secondPart = initia_js.bcs.u64().serialize(BigInt(sequenceNumber) + BigInt(1));
192
+ const seed = new Uint8Array([...firstPart.toBytes(), ...secondPart.toBytes()]);
193
+ const objectAddress = createObjectAddress(deployer.accAddress, seed);
194
+ return objectAddress;
195
+ }
196
+ function genObjectAddressForLayerzero(network, lzAptosConfig, moduleName) {
197
+ const deployer = getDeployer(moduleName, lzAptosConfig, network);
198
+ const seed = getFullName(lzAptosConfig, moduleName);
199
+ const objectAddress = createObjectAddress(deployer.accAddress, new Uint8Array(Buffer.from(seed, "utf8")));
200
+ return objectAddress.toString();
201
+ }
202
+ async function genObjectAddress(moduleName, lzAptosConfig, network) {
203
+ const objectDeployer = getObjectDeployerFromConfig(lzAptosConfig, network, moduleName);
204
+ if (objectDeployer === InitiaObjectDeployerAddress) {
205
+ return genObjectAddressForOfficial(network, lzAptosConfig, moduleName);
206
+ }
207
+ return genObjectAddressForLayerzero(network, lzAptosConfig, moduleName);
208
+ }
90
209
  function getRawMoveContextByPath(modulePath) {
91
- const tomlPath = path2__default.default.join(modulePath, "Move.toml");
92
- if (!fs3__default.default.existsSync(tomlPath)) {
210
+ const tomlPath = path3__default.default.join(modulePath, "Move.toml");
211
+ if (!fs4__default.default.existsSync(tomlPath)) {
93
212
  throw new Error(`Move.toml not found in ${tomlPath}`);
94
213
  }
95
- const toml = smolToml.parse(fs3__default.default.readFileSync(tomlPath, "utf-8"));
214
+ const toml = smolToml.parse(fs4__default.default.readFileSync(tomlPath, "utf-8"));
96
215
  const { dependencies } = toml;
97
216
  let result = { [toml.package.name]: toml };
98
217
  if (dependencies !== void 0) {
@@ -100,7 +219,7 @@ function getRawMoveContextByPath(modulePath) {
100
219
  return dependencies[key]?.local !== void 0;
101
220
  }).forEach((key) => {
102
221
  const dependency = dependencies[key];
103
- const dependencyPath = path2__default.default.join(modulePath, dependency.local);
222
+ const dependencyPath = path3__default.default.join(modulePath, dependency.local);
104
223
  result = { ...getRawMoveContextByPath(dependencyPath), ...result };
105
224
  });
106
225
  }
@@ -125,7 +244,7 @@ function parseAddresses(addresses, network) {
125
244
  }
126
245
  return result;
127
246
  }
128
- function getMoveContext(moduleName, lzInitiaConfig, network) {
247
+ async function getMoveContext(moduleName, lzInitiaConfig, network) {
129
248
  const module = lzInitiaConfig.modules[moduleName];
130
249
  if (!module) {
131
250
  throw new Error(`module ${moduleName} not found when getting Move context`);
@@ -137,15 +256,21 @@ function getMoveContext(moduleName, lzInitiaConfig, network) {
137
256
  const address = toml.addresses[addressKey];
138
257
  const presetAddresses = {
139
258
  ...parseAddresses(lzInitiaConfig.modules[key]?.addresses, network),
259
+ // dependency or current module config address
140
260
  ...parseAddresses(lzInitiaConfig.modules[moduleName]?.addresses, network)
261
+ // current module config address
141
262
  };
142
263
  if (address === "_") {
143
264
  if (presetAddresses[addressKey] !== void 0 && presetAddresses[addressKey] !== "") {
144
265
  toml.addresses[addressKey] = presetAddresses[addressKey];
145
266
  } else {
146
267
  try {
147
- toml.addresses[addressKey] = initia_js.AccAddress.toHex(
148
- getDeployer(addressKey, lzInitiaConfig, network).accAddress
268
+ const isOnGoingModule = moduleName === addressKey;
269
+ toml.addresses[addressKey] = await resolveAddress(
270
+ addressKey,
271
+ lzInitiaConfig,
272
+ network,
273
+ isOnGoingModule
149
274
  );
150
275
  } catch (e) {
151
276
  if (key === moduleName) {
@@ -193,21 +318,21 @@ function buildProcess(modulePath, addresses, toml, customOptions = {}) {
193
318
  })`initiad move build ${args}`;
194
319
  }
195
320
  async function copyArtifacts(src, dest) {
196
- if (fs3__default.default.existsSync(dest)) {
321
+ if (fs4__default.default.existsSync(dest)) {
197
322
  console.log(`Removing existing artifacts at ${dest}`);
198
- fs3__default.default.rmSync(dest, { recursive: true });
323
+ fs4__default.default.rmSync(dest, { recursive: true });
199
324
  }
200
- fs3__default.default.mkdirSync(dest, { recursive: true });
201
- const moduleDir = path2__default.default.join(src, "bytecode_modules");
325
+ fs4__default.default.mkdirSync(dest, { recursive: true });
326
+ const moduleDir = path3__default.default.join(src, "bytecode_modules");
202
327
  const modules = glob.glob.sync("*.mv", { cwd: moduleDir });
203
328
  for (const module of modules) {
204
- const destByteCodePath = path2__default.default.join(dest, "bytecode_modules");
205
- if (!fs3__default.default.existsSync(destByteCodePath)) {
206
- fs3__default.default.mkdirSync(destByteCodePath, { recursive: true });
329
+ const destByteCodePath = path3__default.default.join(dest, "bytecode_modules");
330
+ if (!fs4__default.default.existsSync(destByteCodePath)) {
331
+ fs4__default.default.mkdirSync(destByteCodePath, { recursive: true });
207
332
  }
208
333
  await zx.$({
209
334
  verbose: true
210
- })`cp ${path2__default.default.join(moduleDir, module)} ${path2__default.default.join(destByteCodePath, module)}`;
335
+ })`cp ${path3__default.default.join(moduleDir, module)} ${path3__default.default.join(destByteCodePath, module)}`;
211
336
  }
212
337
  }
213
338
  async function build(moduleName, lzInitiaConfig, network, skipBuild = false, variant) {
@@ -216,11 +341,11 @@ async function build(moduleName, lzInitiaConfig, network, skipBuild = false, var
216
341
  throw new Error(`module ${moduleName} not found`);
217
342
  }
218
343
  const { modulePath } = module;
219
- const context = getMoveContext(moduleName, lzInitiaConfig, network);
344
+ const context = await getMoveContext(moduleName, lzInitiaConfig, network);
220
345
  const pkgName = context[moduleName].package.name;
221
- const srcPath = path2__default.default.join(modulePath, "build", pkgName);
346
+ const srcPath = path3__default.default.join(modulePath, "build", pkgName);
222
347
  const outputDir = lzInitiaConfig.modules[moduleName]?.alias ?? pkgName;
223
- const outputPath = path2__default.default.join(
348
+ const outputPath = path3__default.default.join(
224
349
  lzInitiaConfig.artifactsPath,
225
350
  variant !== void 0 && variant.length > 0 ? `${outputDir}-${variant}` : outputDir
226
351
  );
@@ -241,14 +366,6 @@ async function build(moduleName, lzInitiaConfig, network, skipBuild = false, var
241
366
  }
242
367
  await copyArtifacts(srcPath, outputPath);
243
368
  }
244
- function getInitiaClient(env, config) {
245
- const url = config.network?.[env];
246
- if (url === void 0) {
247
- throw new Error(`No network url for ${env} found in lz-initia.config.ts`);
248
- }
249
- const lcd = new initia_js.LCDClient(url);
250
- return lcd;
251
- }
252
369
  function isAxiosError(error) {
253
370
  return error?.response?.data?.message !== void 0;
254
371
  }
@@ -292,20 +409,21 @@ function handleError(error, moduleName, context, network) {
292
409
  throw error;
293
410
  }
294
411
  function deploymentPath(pkgName, dest, network, variant) {
295
- const deploymentPath2 = path2__default.default.join(dest, network);
296
- return path2__default.default.join(deploymentPath2, `${pkgWithVariant(pkgName, variant)}.json`);
412
+ const deploymentPath2 = path3__default.default.join(dest, network);
413
+ return path3__default.default.join(deploymentPath2, `${pkgWithVariant(pkgName, variant)}.json`);
297
414
  }
298
- function saveDeployment(moduleName, pkgName, address, dest, network, bytecodeHash, hash, variant, compatibleVersions = [lzDefinitions.EndpointVersion.V1, lzDefinitions.EndpointVersion.V2]) {
415
+ function saveDeployment(deployer, moduleName, pkgName, address, dest, network, bytecodeHash, hash, variant, compatibleVersions = [lzDefinitions.EndpointVersion.V1, lzDefinitions.EndpointVersion.V2]) {
299
416
  const destPath = deploymentPath(pkgName, dest, network, variant);
300
- const deploymentDir = path2__default.default.dirname(destPath);
301
- if (!fs3__default.default.existsSync(deploymentDir)) {
302
- fs3__default.default.mkdirSync(deploymentDir, { recursive: true });
417
+ const deploymentDir = path3__default.default.dirname(destPath);
418
+ if (!fs4__default.default.existsSync(deploymentDir)) {
419
+ fs4__default.default.mkdirSync(deploymentDir, { recursive: true });
303
420
  }
304
- fs3__default.default.writeFileSync(
421
+ fs4__default.default.writeFileSync(
305
422
  destPath,
306
423
  JSON.stringify(
307
424
  {
308
425
  address,
426
+ deployer: initia_js.AccAddress.toHex(deployer.accAddress),
309
427
  name: pkgWithVariant(pkgName, variant),
310
428
  moduleName,
311
429
  network,
@@ -319,16 +437,35 @@ function saveDeployment(moduleName, pkgName, address, dest, network, bytecodeHas
319
437
  );
320
438
  console.log(`Deployment saved to ${destPath}`);
321
439
  }
322
- function needDeploy(pkgName, dest, network, bytecodeHash, variant) {
323
- if (lzDefinitions.networkToStage(network) === lzDefinitions.Stage.SANDBOX) {
324
- return true;
440
+ async function checkIfModuleExistedOnChain(client, moduleNames, address) {
441
+ try {
442
+ const modules = (await client.move.modules(address))[0];
443
+ return moduleNames.some(
444
+ (moduleName) => modules.map((module) => module.module_name).includes(moduleName.replace(".mv", ""))
445
+ );
446
+ } catch (e) {
447
+ try {
448
+ const object = await client.auth.accountInfo(address);
449
+ return object !== void 0;
450
+ } catch (e2) {
451
+ return false;
452
+ }
325
453
  }
454
+ }
455
+ async function getDeployActionType(client, pkgName, moduleNames, dest, network, bytecodeHash, variant) {
326
456
  const destPath = deploymentPath(pkgName, dest, network, variant);
327
- if (fs3__default.default.existsSync(destPath)) {
328
- const deployment = JSON.parse(fs3__default.default.readFileSync(destPath, "utf-8"));
329
- return deployment.bytecodeHash !== bytecodeHash;
457
+ if (fs4__default.default.existsSync(destPath)) {
458
+ const deployment = JSON.parse(fs4__default.default.readFileSync(destPath, "utf-8"));
459
+ const moduleExisted = await checkIfModuleExistedOnChain(client, moduleNames, deployment.address);
460
+ if (moduleExisted) {
461
+ if (deployment.bytecodeHash !== bytecodeHash || network.includes("local")) {
462
+ return { actionType: "Upgrade" /* Upgrade */, address: deployment.address };
463
+ } else {
464
+ return { actionType: "Skip" /* Skip */, address: deployment.address };
465
+ }
466
+ }
330
467
  }
331
- return true;
468
+ return { actionType: "Deploy" /* Deploy */, address: void 0 };
332
469
  }
333
470
  function pkgWithVariant(pkgName, variant) {
334
471
  return variant !== void 0 && variant.length > 0 ? `${pkgName}-${variant}` : pkgName;
@@ -340,7 +477,44 @@ function getBytecodesHash(bytecodes) {
340
477
  });
341
478
  return hash.digest("hex");
342
479
  }
343
- async function deploy(moduleName, lzInitiaConfig, network, variant) {
480
+ async function createAccountDeploymentTx(client, deployer, modules, lzInitiaConfig, network) {
481
+ const msgs = [
482
+ new initia_js.MsgPublish(
483
+ deployer.accAddress,
484
+ modules.map((codeBytes) => codeBytes.toString("base64")),
485
+ initia_js.MsgPublish.Policy.COMPATIBLE
486
+ )
487
+ ];
488
+ const wallet = new initia_js.Wallet(client, deployer);
489
+ const gasPrices = lzInitiaConfig.gasPrice?.[network] === void 0 ? void 0 : `${lzInitiaConfig.gasPrice[network]}uinit`;
490
+ const signedTx = await wallet.createAndSignTx({ msgs, gasPrices });
491
+ return signedTx;
492
+ }
493
+ async function createObjectDeploymentTx(client, deployer, modules, lzInitiaConfig, network, moduleName, objectAddress) {
494
+ const objectDeployer = getObjectDeployerFromConfig(lzInitiaConfig, network, moduleName);
495
+ const metadataArg = initia_js.bcs.vector(initia_js.bcs.u8()).serialize(Uint8Array.from([])).toBase64();
496
+ const modulesArg = initia_js.bcs.vector(initia_js.bcs.vector(initia_js.bcs.u8())).serialize(modules).toBase64();
497
+ const objectAddressArg = objectAddress === void 0 ? void 0 : initia_js.bcs.address().serialize(objectAddress).toBase64();
498
+ let functionArguments = objectAddressArg === void 0 ? [metadataArg, modulesArg] : [metadataArg, modulesArg, objectAddressArg];
499
+ if (objectDeployer !== InitiaObjectDeployerAddress) {
500
+ const seed = getFullName(lzInitiaConfig, moduleName);
501
+ const seedArg = initia_js.bcs.vector(initia_js.bcs.u8()).serialize(Uint8Array.from(Buffer.from(seed))).toBase64();
502
+ functionArguments = objectAddressArg === void 0 ? [seedArg, metadataArg, modulesArg] : [metadataArg, modulesArg, objectAddressArg];
503
+ }
504
+ const msg = new initia_js.MsgExecute(
505
+ deployer.accAddress,
506
+ initia_js.AccAddress.fromHex(objectDeployer),
507
+ "object_code_deployment",
508
+ objectAddress === void 0 ? `publish` : `upgrade`,
509
+ [],
510
+ functionArguments
511
+ );
512
+ const gasPrices = lzInitiaConfig.gasPrice?.[network] === void 0 ? void 0 : `${lzInitiaConfig.gasPrice[network]}uinit`;
513
+ const wallet = new initia_js.Wallet(client, deployer);
514
+ const signedTx = await wallet.createAndSignTx({ msgs: [msg], gasPrices });
515
+ return signedTx;
516
+ }
517
+ async function deploy(moduleName, lzInitiaConfig, network, variant, deployToObject = false) {
344
518
  const module = lzInitiaConfig.modules[moduleName];
345
519
  if (!module) {
346
520
  throw new Error(`module ${moduleName} not found`);
@@ -348,36 +522,52 @@ async function deploy(moduleName, lzInitiaConfig, network, variant) {
348
522
  const env = lzDefinitions.networkToEnv(network, lzDefinitions.EndpointVersion.V2);
349
523
  const client = getInitiaClient(env, lzInitiaConfig);
350
524
  const deployer = getDeployer(moduleName, lzInitiaConfig, network);
351
- const context = getMoveContext(moduleName, lzInitiaConfig, network);
525
+ const context = await getMoveContext(moduleName, lzInitiaConfig, network);
352
526
  const pkgName = lzInitiaConfig.modules[moduleName]?.alias ?? context[moduleName].package.name;
353
- const moduleDir = path2__default.default.join(lzInitiaConfig.artifactsPath, `${pkgWithVariant(pkgName, variant)}/bytecode_modules`);
354
- const moduleNames = glob.glob.sync("*.mv", { cwd: moduleDir });
355
- const moduleBuffers = moduleNames.map((moduleName2) => fs3__default.default.readFileSync(path2__default.default.join(moduleDir, moduleName2)));
527
+ const moduleDir = path3__default.default.join(lzInitiaConfig.artifactsPath, `${pkgWithVariant(pkgName, variant)}/bytecode_modules`);
528
+ const mvNames = glob.glob.sync("*.mv", { cwd: moduleDir });
529
+ const moduleBuffers = mvNames.map((moduleName2) => fs4__default.default.readFileSync(path3__default.default.join(moduleDir, moduleName2)));
356
530
  const bytecodeHash = getBytecodesHash(moduleBuffers);
357
- if (!needDeploy(pkgName, lzInitiaConfig.deploymentPath, network, bytecodeHash, variant)) {
531
+ const { actionType, address: deployedAddress } = await getDeployActionType(
532
+ client,
533
+ moduleName,
534
+ mvNames,
535
+ lzInitiaConfig.deploymentPath,
536
+ network,
537
+ bytecodeHash,
538
+ variant
539
+ );
540
+ if (actionType === "Skip" /* Skip */) {
358
541
  console.warn(`Code of ${moduleName} has not changed, skipping deploy`);
359
542
  return;
360
543
  }
361
544
  try {
362
- const msgs = [
363
- new initia_js.MsgPublish(
364
- deployer.accAddress,
365
- moduleBuffers.map((codeBytes) => codeBytes.toString("base64")),
366
- initia_js.MsgPublish.Policy.COMPATIBLE
367
- )
368
- ];
369
- const wallet = new initia_js.Wallet(client, deployer);
370
- const gasPrices = lzInitiaConfig.gasPrice?.[network] === void 0 ? void 0 : `${lzInitiaConfig.gasPrice[network]}uinit`;
371
- const signedTx = await wallet.createAndSignTx({ msgs, gasPrices });
545
+ const signedTx = deployToObject ? await createObjectDeploymentTx(
546
+ client,
547
+ deployer,
548
+ moduleBuffers,
549
+ lzInitiaConfig,
550
+ network,
551
+ moduleName,
552
+ deployedAddress
553
+ ) : await createAccountDeploymentTx(client, deployer, moduleBuffers, lzInitiaConfig, network);
372
554
  const tx = await client.tx.broadcast(signedTx);
555
+ const info = await client.tx.txInfo(tx.txhash);
556
+ const publishedEvent = info.events.find(
557
+ (event) => event.type === "move" && event.attributes.find((attr) => attr.key === "type_tag")?.value === "0x1::code::ModulePublishedEvent"
558
+ );
559
+ const publishModuleAttr = publishedEvent?.attributes.find((attr) => attr.key === "data");
560
+ const { module_id } = JSON.parse(publishModuleAttr.value);
561
+ const codeAddress = module_id.split("::")[0];
373
562
  if (tx.code !== 0) {
374
563
  throw new Error(tx.raw_log);
375
564
  }
376
565
  console.log(`Deploy transaction ${tx.txhash} successfully`);
377
566
  saveDeployment(
567
+ deployer,
378
568
  moduleName,
379
569
  pkgName,
380
- initia_js.AccAddress.toHex(deployer.accAddress),
570
+ codeAddress,
381
571
  lzInitiaConfig.deploymentPath,
382
572
  network,
383
573
  bytecodeHash,