-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Revert "[script-composer] Revert script composers (#574)" This reverts commit bf8afa1. * update deps * Update package.json * Update build.ts * Fix: Add default FeePayer address for sponsored transactions in Script Composer (#612) * fix lock * Use lazy loading * Fix comments * fixup! Fix comments * fixup! fixup! Fix comments * upgrade @aptos-labs/script-composer-pack and fix init * fixup! upgrade @aptos-labs/script-composer-pack and fix init * fixup! fixup! upgrade @aptos-labs/script-composer-pack and fix init * fixup! fixup! fixup! upgrade @aptos-labs/script-composer-pack and fix init --------- Co-authored-by: Anto <[email protected]> Co-authored-by: WGB5445 <[email protected]>
- Loading branch information
1 parent
2386c07
commit d5f7124
Showing
10 changed files
with
355 additions
and
4 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,87 @@ | ||
// Copyright © Aptos Foundation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { ScriptComposerWasm } from "@aptos-labs/script-composer-pack"; | ||
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"; | ||
|
||
/** | ||
* 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 config: AptosConfig; | ||
|
||
private builder?: any; | ||
|
||
private static transactionComposer?: any; | ||
|
||
constructor(aptosConfig: AptosConfig) { | ||
this.config = aptosConfig; | ||
this.builder = undefined; | ||
} | ||
|
||
// Initializing the wasm needed for the script composer, must be called | ||
// before using the composer. | ||
async init() { | ||
if (!AptosScriptComposer.transactionComposer) { | ||
const module = await import("@aptos-labs/script-composer-pack"); | ||
const { TransactionComposer, initSync } = module; | ||
if (!ScriptComposerWasm.isInitialized) { | ||
ScriptComposerWasm.init(); | ||
} | ||
initSync({ module: ScriptComposerWasm.wasm }); | ||
AptosScriptComposer.transactionComposer = TransactionComposer; | ||
} | ||
this.builder = AptosScriptComposer.transactionComposer.single_signer(); | ||
} | ||
|
||
// 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) { | ||
await Promise.all(input.typeArguments.map((typeTag) => 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 "@aptos-labs/script-composer-pack"; |
Oops, something went wrong.