Skip to content

Commit

Permalink
support abi gen
Browse files Browse the repository at this point in the history
  • Loading branch information
0xmaayan committed Dec 5, 2024
1 parent 3c32468 commit b766edd
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 18 deletions.
2 changes: 1 addition & 1 deletion examples/ts-node-app/abis/todolist_abi.ts
Original file line number Diff line number Diff line change
@@ -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;
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;
10 changes: 8 additions & 2 deletions workspace/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,16 @@ npm install --save-dev @thalalabs/surf
```

Surf uses the contract ABI to infer the types of the contract's functions and events.
To generate your contract ABI, you can use the `npx aptos-workspace gen-abi` command.
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.

```bash
npx aptos-workspace gen-abi
# in your Move.toml
[addresses]
alice = "0x1"
bob = "0x2"

# in your terminal
npx aptos-workspace gen-abi --names alice,bob --name alice
```

This function will generate the ABI for your contracts and save it in the `abis` directory.
Expand Down
12 changes: 10 additions & 2 deletions workspace/src/internal/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ program
.description("Run Move unit tests")
.option(
"--package-path <PATH>",
"The path to the Move package with a Move.toml file you want to test, Example: ./contracts/my-contract"
"The path to the Move package with a Move.toml file you want to test, Example: my-contract-folder-name"
)
.action(async (options) => {
await moveUnitTestTask(options);
Expand All @@ -39,10 +39,18 @@ program
program
.command("gen-abi")
.description("Generate the module ABI")
.option(
.requiredOption(
"--names <NAMES>",
"The names you use in the named-addresses for the move binary, Example: alice,bob"
)
.requiredOption(
"--name <NAME>",
"The name from the named-addresses to use to publish the package, Example: alice"
)
.option(
"--package-path <PATH>",
"The path to the Move package with a Move.toml file you want to generate the ABI for, Example: my-contract-folder-name"
)
.action(async (options) => {
/**
* NOTE: The only feasible way to generate the ABI is to publish the package to chain
Expand Down
2 changes: 1 addition & 1 deletion workspace/src/internal/rootHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const mochaHooks: RootHookObject = {
/**
* Spins up a new Aptos node and assigns it to the global `workspace` object.
*/
const createGlobalAptosClientInstance = async () => {
export const createGlobalAptosClientInstance = async () => {
workspace.testNode = await TestNode.spawn();
// inject aptos instance to the global `workspace` object.

Expand Down
30 changes: 18 additions & 12 deletions workspace/src/tasks/gen-abi.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,43 @@
import fs from "fs";
const cli = require("@aptos-labs/ts-sdk/dist/common/cli/index.js");
import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk";

import { publishMovePackageTask } from "./publishMovePackage";
import { generateTestAccount } from "../external";
import { generateTestAccount, workspace } from "../external";
import { createGlobalAptosClientInstance } from "../internal/rootHook";

export const genAbi = async (options: { names: string }) => {
const localNode = new cli.LocalNode();
export type GenAbiOptions = {
names: string;
name: string;
packagePath?: string;
};

export const genAbi = async (options: GenAbiOptions) => {
const { names, name, packagePath } = options;
console.log(`Generating ABI... hold on`);
// spin up a localnet
await localNode.run();
await createGlobalAptosClientInstance();
// create a random account
const publisher = await generateTestAccount();
// build the named addresses object with the random account address
const parsedNamedAddresses = buildAddressObject(
options.names,
names,
publisher.accountAddress.toString()
);
// publish the package to chain
const packageObjectAddress = await publishMovePackageTask({
publisher,
namedAddresses: parsedNamedAddresses,
addressName: Object.keys(parsedNamedAddresses)[0],
addressName: name,
packageFolderName: packagePath,
});
// fetch the abi from the node and generate in a local file
await fetchAbiFromNode(packageObjectAddress);
// stop the localnet
await localNode.stop();
await workspace.testNode.stop();
console.log(`ABI generated successfully in the abis/ folder`);
};

export const fetchAbiFromNode = async (objectAddress: string) => {
const aptosConfig = new AptosConfig({ network: Network.LOCAL });
const aptos = new Aptos(aptosConfig);
const modules = await aptos.getAccountModules({
const modules = await workspace.getAccountModules({
accountAddress: objectAddress,
});
modules.forEach((module) => {
Expand Down

0 comments on commit b766edd

Please sign in to comment.