-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a new api for building script payload (#565)
* import aptos-intent * Fix some lints * Fix comments * Fix deps, re-export CallArgument * fixup! Fix deps, re-export CallArgument * Fix comments * fix lint * fixup! fix lint * Update package
- Loading branch information
1 parent
f644e61
commit a301268
Showing
10 changed files
with
297 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright © Aptos Foundation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { TransactionComposer, initSync, create_wasm } from "@wgb5445/aptos-intent-npm"; | ||
import { AptosApiType } from "../../utils"; | ||
import { AptosConfig } from "../../api/aptosConfig"; | ||
import { InputBatchedFunctionData } from "../types"; | ||
import { fetchMoveFunctionAbi, getFunctionParts, standardizeTypeTags } from "../transactionBuilder"; | ||
import { CallArgument } from "../../types"; | ||
import { convertCallArgument } from "../transactionBuilder/remoteAbi"; | ||
|
||
(async () => { | ||
initSync(await create_wasm()); | ||
})(); | ||
|
||
// A wrapper class around TransactionComposer, which is a WASM library compiled | ||
// from aptos-core/aptos-move/script-composer. | ||
// | ||
// This class allows the SDK caller to build a transaction that invokes multiple Move functions | ||
// and allow for arguments to be passed around. | ||
export class AptosScriptComposer { | ||
private builder: TransactionComposer; | ||
|
||
private config: AptosConfig; | ||
|
||
constructor(aptosConfig: AptosConfig) { | ||
this.builder = TransactionComposer.single_signer(); | ||
this.config = aptosConfig; | ||
} | ||
|
||
// Add a move function invocation to the TransactionComposer. | ||
// | ||
// Similar to how to create an entry function, the difference is that input arguments could | ||
// either be a `CallArgument` which represents an abstract value returned from a previous Move call | ||
// or the regular entry function arguments. | ||
// | ||
// The function would also return a list of `CallArgument` that can be passed on to future calls. | ||
async addBatchedCalls(input: InputBatchedFunctionData): Promise<CallArgument[]> { | ||
const { moduleAddress, moduleName, functionName } = getFunctionParts(input.function); | ||
const nodeUrl = this.config.getRequestUrl(AptosApiType.FULLNODE); | ||
|
||
// Load the calling module into the builder. | ||
await this.builder.load_module(nodeUrl, `${moduleAddress}::${moduleName}`); | ||
|
||
// Load the calling type arguments into the loader. | ||
if (input.typeArguments !== undefined) { | ||
for (const typeTag of input.typeArguments) { | ||
// eslint-disable-next-line no-await-in-loop | ||
await this.builder.load_type_tag(nodeUrl, typeTag.toString()); | ||
} | ||
} | ||
const typeArguments = standardizeTypeTags(input.typeArguments); | ||
const functionAbi = await fetchMoveFunctionAbi(moduleAddress, moduleName, functionName, this.config); | ||
// Check the type argument count against the ABI | ||
if (typeArguments.length !== functionAbi.typeParameters.length) { | ||
throw new Error( | ||
`Type argument count mismatch, expected ${functionAbi.typeParameters.length}, received ${typeArguments.length}`, | ||
); | ||
} | ||
|
||
const functionArguments: CallArgument[] = input.functionArguments.map((arg, i) => | ||
convertCallArgument(arg, functionName, functionAbi, i, typeArguments), | ||
); | ||
|
||
return this.builder.add_batched_call( | ||
`${moduleAddress}::${moduleName}`, | ||
functionName, | ||
typeArguments.map((arg) => arg.toString()), | ||
functionArguments, | ||
); | ||
} | ||
|
||
build(): Uint8Array { | ||
return this.builder.generate_batched_calls(true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./indexer"; | ||
export * from "./types"; | ||
export { CallArgument } from "@wgb5445/aptos-intent-npm"; |
Oops, something went wrong.