Skip to content

Commit b766edd

Browse files
committed
support abi gen
1 parent 3c32468 commit b766edd

File tree

5 files changed

+38
-18
lines changed

5 files changed

+38
-18
lines changed
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: 8 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.

workspace/src/internal/cli.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ program
3030
.description("Run Move unit tests")
3131
.option(
3232
"--package-path <PATH>",
33-
"The path to the Move package with a Move.toml file you want to test, Example: ./contracts/my-contract"
33+
"The path to the Move package with a Move.toml file you want to test, Example: my-contract-folder-name"
3434
)
3535
.action(async (options) => {
3636
await moveUnitTestTask(options);
@@ -39,10 +39,18 @@ program
3939
program
4040
.command("gen-abi")
4141
.description("Generate the module ABI")
42-
.option(
42+
.requiredOption(
4343
"--names <NAMES>",
4444
"The names you use in the named-addresses for the move binary, Example: alice,bob"
4545
)
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+
)
4654
.action(async (options) => {
4755
/**
4856
* 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) => {

0 commit comments

Comments
 (0)