Skip to content

Commit 74e91ab

Browse files
committed
Use compile_and_publish_move_package method in large_package_publisher example
1 parent 123255f commit 74e91ab

File tree

3 files changed

+36
-86
lines changed

3 files changed

+36
-86
lines changed

aptos_sdk/package_publisher.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ async def upgrade_package_object(
321321
)
322322
return await self.client.submit_bcs_transaction(signed_transaction)
323323

324-
async def publish_move_package(
324+
async def compile_and_publish_move_package(
325325
self,
326326
sender: Account,
327327
package_dir: str,
@@ -339,25 +339,25 @@ async def publish_move_package(
339339
340340
Note: This method requires the local Aptos CLI for compilation and will not work without it.
341341
"""
342-
343-
if not AptosCLIWrapper.does_cli_exist():
344-
raise Exception("AptosCLI does not exist.")
342+
AptosCLIWrapper.assert_cli_exists()
345343

346344
# Determine the account or object address for publishing the package.
347345
if publish_mode == PublishMode.ACCOUNT_DEPLOY:
348346
deploy_address = sender.address()
349347

350348
elif publish_mode == PublishMode.OBJECT_DEPLOY:
349+
# Calculate the number of transactions needed for the chunked publish to predict the code object address.
350+
# Start by assuming a single transaction for a preliminary build to estimate the artifact size.
351351
deploy_address = await CompileHelper.derive_object_address(
352352
self.client, sender.address()
353353
)
354354

355-
# Compile the package as a preliminary build to check if chunking is required.
356355
AptosCLIWrapper.compile_package(package_dir, {module_name: deploy_address})
357356
metadata, modules = PublishHelper.load_package_artifacts(package_dir)
358357

359358
# If the package size requires chunked publishing, recalculate the deploy address.
360359
if PublishHelper.is_large_package(metadata, modules):
360+
# Number of transactions required for the chunked publish.
361361
required_txns = len(
362362
PublishHelper.prepare_chunked_payloads(
363363
metadata,
@@ -385,6 +385,8 @@ async def publish_move_package(
385385
# Compile the package with the correct deployment address.
386386
AptosCLIWrapper.compile_package(package_dir, {module_name: deploy_address})
387387

388+
print(f"Deploying {module_name} to {deploy_address}...")
389+
388390
return await self.publish_package_in_path(
389391
sender,
390392
package_dir,

examples/large_package_publisher.py

Lines changed: 20 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
# Copyright © Aptos Foundation
22
# SPDX-License-Identifier: Apache-2.0
3+
4+
"""
5+
This example demonstrates publishing large Move packages which cannot fit in a single transaction, using the most
6+
abstract method `compile_and_publish_move_package` from the `PackagePublisher` class. This method handles all necessary
7+
steps for compiling and publishing both regular and large packages.
8+
9+
Note: This method requires the presence of the Aptos CLI in `APTOS_CLI_PATH`. As an alternative, if you want finer
10+
control over the process or do not want to rely on the CLI, you may use `publish_package_in_path`, which is
11+
demonstrated in the `object_code_deployment.py` example.
12+
"""
13+
314
import asyncio
415
import os
516
import sys
617

718
import aptos_sdk.cli as aptos_sdk_cli
819
from aptos_sdk.account import Account
920
from aptos_sdk.account_address import AccountAddress
10-
from aptos_sdk.aptos_cli_wrapper import AptosCLIWrapper
1121
from aptos_sdk.async_client import ClientConfig, FaucetClient, RestClient
12-
from aptos_sdk.package_publisher import (
13-
MODULE_ADDRESS,
14-
CompileHelper,
15-
PackagePublisher,
16-
PublishHelper,
17-
PublishMode,
18-
)
22+
from aptos_sdk.package_publisher import MODULE_ADDRESS, PackagePublisher, PublishMode
1923

2024
from .common import APTOS_CORE_PATH, FAUCET_URL, NODE_URL
2125

@@ -58,97 +62,32 @@ async def main(
5862
# Name of the move module for the package to be published, containing artifacts larger than the MAX_CHUNK_SIZE
5963
module_name = "large_package_example"
6064

61-
# Example 1. Account deployment
65+
# -- Example 1. Account deployment
6266
print("=== Publishing large package to account ===")
6367

64-
if AptosCLIWrapper.does_cli_exist():
65-
AptosCLIWrapper.compile_package(
66-
large_package_example_dir, {module_name: alice.address()}
67-
)
68-
else:
69-
input("\nUpdate the module with Alice's address, compile, and press Enter.")
70-
71-
account_deploy_txn_hash = await publisher.publish_package_in_path(
72-
alice, large_package_example_dir, large_packages_module_address
68+
account_deploy_txn_hash = await publisher.compile_and_publish_move_package(
69+
alice, large_package_example_dir, module_name, large_packages_module_address
7370
)
7471

7572
print(f"Tx submitted: {account_deploy_txn_hash[0]}")
7673
await rest_client.wait_for_transaction(account_deploy_txn_hash[0])
77-
print(f"Package deployed to account {alice.address()}")
74+
print("Transaction completed.")
7875

79-
# Example 2. Object code deployment
80-
# Note: Here we assume that we already know we should use the chunked publish mode, so we run a preliminary build.
76+
# ----- Example 2. Object code deployment
8177
print("=== Publishing large package to object ===")
8278

83-
# Calculate the number of transactions needed for the chunked publish to predict the code object address.
84-
# Start by deriving the address assuming a single transaction for a preliminary build to estimate artifact size.
85-
code_object_address = await CompileHelper.derive_object_address(
86-
rest_client, alice.address()
87-
)
88-
89-
print("\nCompiling package as a preliminary build...")
90-
if AptosCLIWrapper.does_cli_exist():
91-
AptosCLIWrapper.compile_package(
92-
large_package_example_dir, {module_name: code_object_address}
93-
)
94-
else:
95-
print(f"Address of the object to be created: {code_object_address}")
96-
input(
97-
"\nUpdate the module with the derived code object address, compile, and press enter."
98-
)
99-
100-
metadata, modules = PublishHelper.load_package_artifacts(large_package_example_dir)
101-
102-
# Number of transactions required for the chunked publish.
103-
required_txns = len(
104-
PublishHelper.prepare_chunked_payloads(
105-
metadata,
106-
modules,
107-
large_packages_module_address,
108-
PublishMode.OBJECT_DEPLOY,
109-
)
110-
)
111-
112-
if required_txns > 1:
113-
code_object_address = await CompileHelper.derive_object_address(
114-
rest_client, alice.address(), required_txns
115-
)
116-
print("\nCompiling the package with updated object address...")
117-
if AptosCLIWrapper.does_cli_exist():
118-
AptosCLIWrapper.compile_package(
119-
large_package_example_dir, {module_name: code_object_address}
120-
)
121-
else:
122-
print(f"Address of the object to be created: {code_object_address}")
123-
input(
124-
"\nUpdate the module with the derived code object address, compile, and press enter."
125-
)
126-
127-
object_deploy_txn_hash = await publisher.publish_package_in_path(
79+
object_deploy_txn_hash = await publisher.compile_and_publish_move_package(
12880
alice,
12981
large_package_example_dir,
82+
module_name,
13083
large_packages_module_address,
13184
PublishMode.OBJECT_DEPLOY,
13285
)
13386

13487
print(f"The last tx submitted: {object_deploy_txn_hash[-1]}")
13588
await rest_client.wait_for_transaction(object_deploy_txn_hash[-1])
136-
print(f"Package deployed to object {code_object_address}")
137-
138-
# Example 3. Object code upgrade
139-
print("=== Upgrading large package object ===")
140-
141-
object_upgrade_txn_hash = await publisher.publish_package_in_path(
142-
alice,
143-
large_package_example_dir,
144-
large_packages_module_address,
145-
PublishMode.OBJECT_UPGRADE,
146-
code_object_address,
147-
)
89+
print("Transaction completed.")
14890

149-
print(f"The last tx submitted: {object_upgrade_txn_hash[-1]}")
150-
await rest_client.wait_for_transaction(object_upgrade_txn_hash[-1])
151-
print(f"Package in object {code_object_address} upgraded")
15291
await rest_client.close()
15392

15493

examples/object_code_deployment.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
# Copyright © Aptos Foundation
22
# SPDX-License-Identifier: Apache-2.0
33

4+
"""
5+
This example demonstrates publishing Move packages using the `publish_package_in_path` method from the
6+
`PackagePublisher` class. This method provides more control over the package publishing process, directly loading
7+
artifacts from a pre-compiled package directory and handling both Account and Object deployment.
8+
9+
Note: For a higher-level abstraction that handles compilation and deployment automatically, you may use
10+
`compile_and_publish_move_package`, as demonstrated in the `large_package_publisher.py` example.
11+
"""
12+
413
import asyncio
514
import os
615
import sys

0 commit comments

Comments
 (0)