Skip to content

Commit 9754c05

Browse files
authored
Merge pull request #43 from aptos-labs/support_multiple_contracts_2
Support multiple contracts 2
2 parents 6612a34 + b766edd commit 9754c05

File tree

8 files changed

+660
-25
lines changed

8 files changed

+660
-25
lines changed

examples/move-program/package-lock.json

Lines changed: 588 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/move-program/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
"author": "",
1111
"license": "ISC",
1212
"description": "",
13+
"dependencies": {
14+
"@aptos-labs/ts-sdk": "^1.30.0"
15+
},
1316
"devDependencies": {
1417
"@aptos-labs/workspace": "file:../../workspace",
1518
"chai": "^4.5.0"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export const TODOLIST_ABI = {"address":"0x3933100e09d9dded0e68365c8870e481a4ee403d531bd299a4ef2053738ae6a3","name":"todolist","friends":[],"exposed_functions":[{"name":"complete_task","visibility":"public","is_entry":true,"is_view":false,"generic_type_params":[],"params":["&signer","u64"],"return":[]},{"name":"create_list","visibility":"public","is_entry":true,"is_view":false,"generic_type_params":[],"params":["&signer"],"return":[]},{"name":"create_task","visibility":"public","is_entry":true,"is_view":false,"generic_type_params":[],"params":["&signer","0x1::string::String"],"return":[]}],"structs":[{"name":"Task","is_native":false,"is_event":true,"abilities":["copy","drop","store"],"generic_type_params":[],"fields":[{"name":"task_id","type":"u64"},{"name":"address","type":"address"},{"name":"content","type":"0x1::string::String"},{"name":"completed","type":"bool"}]},{"name":"TodoList","is_native":false,"is_event":false,"abilities":["key"],"generic_type_params":[],"fields":[{"name":"tasks","type":"0x1::table::Table<u64, 0x3933100e09d9dded0e68365c8870e481a4ee403d531bd299a4ef2053738ae6a3::todolist::Task>"},{"name":"task_counter","type":"u64"}]}]} as const;
1+
export const TODOLIST_ABI = {"address":"0x73be7292ae896a31b98c3b07ad439a0a746edce3e20094d2f239a64917798db9","name":"todolist","friends":[],"exposed_functions":[{"name":"complete_task","visibility":"public","is_entry":true,"is_view":false,"generic_type_params":[],"params":["&signer","u64"],"return":[]},{"name":"create_list","visibility":"public","is_entry":true,"is_view":false,"generic_type_params":[],"params":["&signer"],"return":[]},{"name":"create_task","visibility":"public","is_entry":true,"is_view":false,"generic_type_params":[],"params":["&signer","0x1::string::String"],"return":[]}],"structs":[{"name":"Task","is_native":false,"is_event":true,"abilities":["copy","drop","store"],"generic_type_params":[],"fields":[{"name":"task_id","type":"u64"},{"name":"address","type":"address"},{"name":"content","type":"0x1::string::String"},{"name":"completed","type":"bool"}]},{"name":"TodoList","is_native":false,"is_event":false,"abilities":["key"],"generic_type_params":[],"fields":[{"name":"tasks","type":"0x1::table::Table<u64, 0x73be7292ae896a31b98c3b07ad439a0a746edce3e20094d2f239a64917798db9::todolist::Task>"},{"name":"task_counter","type":"u64"}]}]} as const;

workspace/README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,16 @@ npm install --save-dev @thalalabs/surf
206206
```
207207

208208
Surf uses the contract ABI to infer the types of the contract's functions and events.
209-
To generate your contract ABI, you can use the `npx aptos-workspace gen-abi` command.
209+
To generate your contract ABI, you can use the `npx aptos-workspace gen-abi` command and specify the names you used in the `named-addresses` for the Move binary along with the name of the address you want to generate the ABI for.
210210

211211
```bash
212-
npx aptos-workspace gen-abi
212+
# in your Move.toml
213+
[addresses]
214+
alice = "0x1"
215+
bob = "0x2"
216+
217+
# in your terminal
218+
npx aptos-workspace gen-abi --names alice,bob --name alice
213219
```
214220

215221
This function will generate the ABI for your contracts and save it in the `abis` directory.
@@ -227,6 +233,12 @@ To run your Move unit tests, you can use the `npx aptos-workspace move-unit-tes`
227233
npx aptos-workspace move-unit-test
228234
```
229235

236+
By default, Workspace will look for the Move package under the folder specified in the `workspace.config` file. If your Move package is under a sub folder (e.g. `contract/MessageBoard` - for cases you have multiple move packages in your project), you can specify a `--package-path` flag.
237+
238+
```bash
239+
npx aptos-workspace move-unit-test --package-path MessageBoard
240+
```
241+
230242
### Using `pnpm` or `yarn`?
231243

232244
#### Using yarn?

workspace/src/internal/cli.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,29 @@ program
2828
program
2929
.command("move-unit-test")
3030
.description("Run Move unit tests")
31-
.action(async () => {
32-
await moveUnitTestTask();
31+
.option(
32+
"--package-path <PATH>",
33+
"The path to the Move package with a Move.toml file you want to test, Example: my-contract-folder-name"
34+
)
35+
.action(async (options) => {
36+
await moveUnitTestTask(options);
3337
});
3438

3539
program
3640
.command("gen-abi")
3741
.description("Generate the module ABI")
38-
.option(
42+
.requiredOption(
3943
"--names <NAMES>",
4044
"The names you use in the named-addresses for the move binary, Example: alice,bob"
4145
)
46+
.requiredOption(
47+
"--name <NAME>",
48+
"The name from the named-addresses to use to publish the package, Example: alice"
49+
)
50+
.option(
51+
"--package-path <PATH>",
52+
"The path to the Move package with a Move.toml file you want to generate the ABI for, Example: my-contract-folder-name"
53+
)
4254
.action(async (options) => {
4355
/**
4456
* NOTE: The only feasible way to generate the ABI is to publish the package to chain

workspace/src/internal/rootHook.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const mochaHooks: RootHookObject = {
1919
/**
2020
* Spins up a new Aptos node and assigns it to the global `workspace` object.
2121
*/
22-
const createGlobalAptosClientInstance = async () => {
22+
export const createGlobalAptosClientInstance = async () => {
2323
workspace.testNode = await TestNode.spawn();
2424
// inject aptos instance to the global `workspace` object.
2525

workspace/src/tasks/gen-abi.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,43 @@
11
import fs from "fs";
2-
const cli = require("@aptos-labs/ts-sdk/dist/common/cli/index.js");
3-
import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk";
42

53
import { publishMovePackageTask } from "./publishMovePackage";
6-
import { generateTestAccount } from "../external";
4+
import { generateTestAccount, workspace } from "../external";
5+
import { createGlobalAptosClientInstance } from "../internal/rootHook";
76

8-
export const genAbi = async (options: { names: string }) => {
9-
const localNode = new cli.LocalNode();
7+
export type GenAbiOptions = {
8+
names: string;
9+
name: string;
10+
packagePath?: string;
11+
};
12+
13+
export const genAbi = async (options: GenAbiOptions) => {
14+
const { names, name, packagePath } = options;
15+
console.log(`Generating ABI... hold on`);
1016
// spin up a localnet
11-
await localNode.run();
17+
await createGlobalAptosClientInstance();
1218
// create a random account
1319
const publisher = await generateTestAccount();
1420
// build the named addresses object with the random account address
1521
const parsedNamedAddresses = buildAddressObject(
16-
options.names,
22+
names,
1723
publisher.accountAddress.toString()
1824
);
1925
// publish the package to chain
2026
const packageObjectAddress = await publishMovePackageTask({
2127
publisher,
2228
namedAddresses: parsedNamedAddresses,
23-
addressName: Object.keys(parsedNamedAddresses)[0],
29+
addressName: name,
30+
packageFolderName: packagePath,
2431
});
2532
// fetch the abi from the node and generate in a local file
2633
await fetchAbiFromNode(packageObjectAddress);
2734
// stop the localnet
28-
await localNode.stop();
35+
await workspace.testNode.stop();
36+
console.log(`ABI generated successfully in the abis/ folder`);
2937
};
3038

3139
export const fetchAbiFromNode = async (objectAddress: string) => {
32-
const aptosConfig = new AptosConfig({ network: Network.LOCAL });
33-
const aptos = new Aptos(aptosConfig);
34-
const modules = await aptos.getAccountModules({
40+
const modules = await workspace.getAccountModules({
3541
accountAddress: objectAddress,
3642
});
3743
modules.forEach((module) => {

workspace/src/tasks/unit-test.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
1+
import path from "path";
12
import { Move } from "@aptos-labs/ts-sdk/dist/common/cli/index.js";
23
import fs from "fs";
34
import toml from "toml";
4-
import { getUserConfigContractDir } from "../internal/utils/userConfig";
5+
import {
6+
getUserConfigContractDir,
7+
getUserConfigVerbose,
8+
} from "../internal/utils/userConfig";
9+
10+
export type MoveUnitTestOptions = {
11+
packagePath?: string;
12+
};
13+
14+
export const moveUnitTestTask = async (options: MoveUnitTestOptions) => {
15+
const { packagePath } = options;
516

6-
export const moveUnitTestTask = async () => {
717
// get the configured contract dir
818
const contractDir = getUserConfigContractDir();
19+
20+
const contractPackagePath = packagePath
21+
? path.join(contractDir, packagePath)
22+
: contractDir;
23+
924
// read the Move.toml file
10-
var str = fs.readFileSync(contractDir + "/Move.toml", "utf-8");
25+
var str = fs.readFileSync(contractPackagePath + "/Move.toml", "utf-8");
1126
// parse the Move.toml file and extract the named addresses
1227
const namedAddresses = toml.parse(str).addresses;
1328

@@ -18,8 +33,8 @@ export const moveUnitTestTask = async () => {
1833
});
1934

2035
await new Move().test({
21-
packageDirectoryPath: contractDir,
36+
packageDirectoryPath: contractPackagePath,
2237
namedAddresses,
23-
showStdout: false,
38+
showStdout: true,
2439
});
2540
};

0 commit comments

Comments
 (0)