Skip to content

Commit

Permalink
[Types, Wallet Adapter] Export transaction builder types for the wall…
Browse files Browse the repository at this point in the history
…et adapter (#139)

* Adding the TransactionBuilder class/helpers to the API

* Exporting transaction builder types for the wallet adapter

* Formatting and adding omitted file

* Exporting types and renaming input types to be prefixed with Input

* Renaming a few exported types to be more explicit

* Fixing dependency cycle and linting/formatting

* Rename typeTag.ts to index.ts and change order of exports to avoid double export/cyclical dependency

* Fix linter useless path segment
  • Loading branch information
xbtmatt authored Oct 31, 2023
1 parent cf5ef70 commit 6cb2d16
Show file tree
Hide file tree
Showing 20 changed files with 193 additions and 161 deletions.
6 changes: 3 additions & 3 deletions src/api/coin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { AptosConfig } from "./aptosConfig";
import { Account } from "../core";
import { transferCoinTransaction } from "../internal/coin";
import { SingleSignerTransaction, GenerateTransactionOptions } from "../transactions/types";
import { InputSingleSignerTransaction, InputGenerateTransactionOptions } from "../transactions/types";
import { AnyNumber, HexInput, MoveStructType } from "../types";

/**
Expand Down Expand Up @@ -32,8 +32,8 @@ export class Coin {
recipient: HexInput;
amount: AnyNumber;
coinType?: MoveStructType;
options?: GenerateTransactionOptions;
}): Promise<SingleSignerTransaction> {
options?: InputGenerateTransactionOptions;
}): Promise<InputSingleSignerTransaction> {
return transferCoinTransaction({ aptosConfig: this.config, ...args });
}
}
10 changes: 5 additions & 5 deletions src/api/digitalAsset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from "../types";
import { AptosConfig } from "./aptosConfig";
import { Account } from "../core";
import { GenerateTransactionOptions, SingleSignerTransaction } from "../transactions/types";
import { InputGenerateTransactionOptions, InputSingleSignerTransaction } from "../transactions/types";
import {
CreateCollectionOptions,
createCollectionTransaction,
Expand Down Expand Up @@ -68,9 +68,9 @@ export class DigitalAsset {
description: string;
name: string;
uri: string;
options?: GenerateTransactionOptions;
options?: InputGenerateTransactionOptions;
} & CreateCollectionOptions,
): Promise<SingleSignerTransaction> {
): Promise<InputSingleSignerTransaction> {
return createCollectionTransaction({ aptosConfig: this.config, ...args });
}

Expand Down Expand Up @@ -133,8 +133,8 @@ export class DigitalAsset {
description: string;
name: string;
uri: string;
options?: GenerateTransactionOptions;
}): Promise<SingleSignerTransaction> {
options?: InputGenerateTransactionOptions;
}): Promise<InputSingleSignerTransaction> {
return mintTokenTransaction({ aptosConfig: this.config, ...args });
}

Expand Down
42 changes: 21 additions & 21 deletions src/api/transactionSubmission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import { AptosConfig } from "./aptosConfig";
import { Account } from "../core";
import { AccountAuthenticator } from "../transactions/authenticator/account";
import {
AnyRawTransaction,
FeePayerTransaction,
GenerateMultiAgentRawTransactionInput,
GenerateTransactionInput,
GenerateFeePayerRawTransactionInput,
GenerateSingleSignerRawTransactionInput,
MultiAgentTransaction,
SingleSignerTransaction,
SimulateTransactionData,
GenerateTransactionOptions,
InputAnyRawTransaction,
InputFeePayerTransaction,
InputGenerateMultiAgentRawTransactionData,
InputGenerateTransactionData,
InputGenerateFeePayerRawTransactionData,
InputGenerateSingleSignerRawTransactionData,
InputMultiAgentTransaction,
InputSingleSignerTransaction,
InputSimulateTransactionData,
InputGenerateTransactionOptions,
} from "../transactions/types";
import { UserTransactionResponse, PendingTransactionResponse, HexInput } from "../types";
import {
Expand All @@ -39,10 +39,10 @@ export class TransactionSubmission {
* When we call `generateTransaction` function with the relevant type properties,
* Typescript can infer the return type based on the appropriate function overload.
*/
async generateTransaction(args: GenerateSingleSignerRawTransactionInput): Promise<SingleSignerTransaction>;
async generateTransaction(args: GenerateFeePayerRawTransactionInput): Promise<FeePayerTransaction>;
async generateTransaction(args: GenerateMultiAgentRawTransactionInput): Promise<MultiAgentTransaction>;
async generateTransaction(args: GenerateTransactionInput): Promise<AnyRawTransaction>;
async generateTransaction(args: InputGenerateSingleSignerRawTransactionData): Promise<InputSingleSignerTransaction>;
async generateTransaction(args: InputGenerateFeePayerRawTransactionData): Promise<InputFeePayerTransaction>;
async generateTransaction(args: InputGenerateMultiAgentRawTransactionData): Promise<InputMultiAgentTransaction>;
async generateTransaction(args: InputGenerateTransactionData): Promise<InputAnyRawTransaction>;

/**
* Generates any transaction by passing in the required arguments
Expand Down Expand Up @@ -84,7 +84,7 @@ export class TransactionSubmission {
* }
* ```
*/
async generateTransaction(args: GenerateTransactionInput): Promise<AnyRawTransaction> {
async generateTransaction(args: InputGenerateTransactionData): Promise<InputAnyRawTransaction> {
return generateTransaction({ aptosConfig: this.config, ...args });
}

Expand All @@ -104,7 +104,7 @@ export class TransactionSubmission {
* @return The signer AccountAuthenticator
*/
/* eslint-disable class-methods-use-this */
signTransaction(args: { signer: Account; transaction: AnyRawTransaction }): AccountAuthenticator {
signTransaction(args: { signer: Account; transaction: InputAnyRawTransaction }): AccountAuthenticator {
return signTransaction({ ...args });
}

Expand All @@ -117,7 +117,7 @@ export class TransactionSubmission {
* @param args.feePayerPublicKey optional. For when the transaction is a fee payer (aka sponsored) transaction
* @param args.options optional. A config to simulate the transaction with
*/
async simulateTransaction(args: SimulateTransactionData): Promise<Array<UserTransactionResponse>> {
async simulateTransaction(args: InputSimulateTransactionData): Promise<Array<UserTransactionResponse>> {
return simulateTransaction({ aptosConfig: this.config, ...args });
}

Expand All @@ -131,7 +131,7 @@ export class TransactionSubmission {
* @return PendingTransactionResponse
*/
async submitTransaction(args: {
transaction: AnyRawTransaction;
transaction: InputAnyRawTransaction;
senderAuthenticator: AccountAuthenticator;
secondarySignerAuthenticators?: {
feePayerAuthenticator?: AccountAuthenticator;
Expand All @@ -158,7 +158,7 @@ export class TransactionSubmission {
*/
async signAndSubmitTransaction(args: {
signer: Account;
transaction: AnyRawTransaction;
transaction: InputAnyRawTransaction;
}): Promise<PendingTransactionResponse> {
const { signer, transaction } = args;
return signAndSubmitTransaction({
Expand All @@ -185,8 +185,8 @@ export class TransactionSubmission {
account: HexInput;
metadataBytes: HexInput;
moduleBytecode: Array<HexInput>;
options?: GenerateTransactionOptions;
}): Promise<SingleSignerTransaction> {
options?: InputGenerateTransactionOptions;
}): Promise<InputSingleSignerTransaction> {
return publicPackageTransaction({ aptosConfig: this.config, ...args });
}
}
4 changes: 1 addition & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ export * from "./api";
export * from "./bcs";
export * from "./client";
export * from "./core";
export * from "./transactions/types";
export * from "./transactions/typeTag";
export * from "./transactions/typeTag/parser";
export * from "./transactions";
export * from "./types";
export * from "./utils/apiEndpoints";
export * from "./utils/hdKey";
8 changes: 4 additions & 4 deletions src/internal/coin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AptosConfig } from "../api/aptosConfig";
import { U64 } from "../bcs/serializable/movePrimitives";
import { Account, AccountAddress } from "../core";
import { GenerateTransactionOptions, SingleSignerTransaction } from "../transactions/types";
import { InputGenerateTransactionOptions, InputSingleSignerTransaction } from "../transactions/types";
import { HexInput, AnyNumber, MoveStructType } from "../types";
import { APTOS_COIN } from "../utils/const";
import { generateTransaction } from "./transactionSubmission";
Expand All @@ -13,8 +13,8 @@ export async function transferCoinTransaction(args: {
recipient: HexInput;
amount: AnyNumber;
coinType?: MoveStructType;
options?: GenerateTransactionOptions;
}): Promise<SingleSignerTransaction> {
options?: InputGenerateTransactionOptions;
}): Promise<InputSingleSignerTransaction> {
const { aptosConfig, sender, recipient, amount, coinType, options } = args;
const coinStructType = coinType ?? APTOS_COIN;
const transaction = await generateTransaction({
Expand All @@ -28,5 +28,5 @@ export async function transferCoinTransaction(args: {
options,
});

return transaction as SingleSignerTransaction;
return transaction as InputSingleSignerTransaction;
}
14 changes: 7 additions & 7 deletions src/internal/digitalAsset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import { AptosConfig } from "../api/aptosConfig";
import { MoveString, MoveVector, Bool, U64, U8 } from "../bcs";
import { Account, Hex } from "../core";
import { GenerateTransactionOptions, SingleSignerTransaction } from "../transactions/types";
import { InputGenerateTransactionOptions, InputSingleSignerTransaction } from "../transactions/types";
import {
AnyNumber,
GetCollectionDataResponse,
Expand Down Expand Up @@ -55,8 +55,8 @@ export async function mintTokenTransaction(args: {
description: string;
name: string;
uri: string;
options?: GenerateTransactionOptions;
}): Promise<SingleSignerTransaction> {
options?: InputGenerateTransactionOptions;
}): Promise<InputSingleSignerTransaction> {
const { aptosConfig, options, creator } = args;
const transaction = await generateTransaction({
aptosConfig,
Expand All @@ -75,7 +75,7 @@ export async function mintTokenTransaction(args: {
},
options,
});
return transaction as SingleSignerTransaction;
return transaction as InputSingleSignerTransaction;
}

export async function getTokenData(args: {
Expand Down Expand Up @@ -218,9 +218,9 @@ export async function createCollectionTransaction(
description: string;
name: string;
uri: string;
options?: GenerateTransactionOptions;
options?: InputGenerateTransactionOptions;
} & CreateCollectionOptions,
): Promise<SingleSignerTransaction> {
): Promise<InputSingleSignerTransaction> {
const { aptosConfig, options, creator } = args;
const transaction = await generateTransaction({
aptosConfig,
Expand Down Expand Up @@ -248,7 +248,7 @@ export async function createCollectionTransaction(
},
options,
});
return transaction as SingleSignerTransaction;
return transaction as InputSingleSignerTransaction;
}

export async function getCollectionData(args: {
Expand Down
34 changes: 17 additions & 17 deletions src/internal/transactionSubmission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ import {
generateSignedTransactionForSimulation,
generateSignedTransaction,
sign,
} from "../transactions/transaction_builder/transaction_builder";
} from "../transactions/transactionBuilder/transactionBuilder";
import {
GenerateTransactionInput,
AnyRawTransaction,
SimulateTransactionData,
GenerateTransactionOptions,
SingleSignerTransaction,
GenerateTransactionPayloadDataWithRemoteABI,
InputGenerateTransactionData,
InputAnyRawTransaction,
InputSimulateTransactionData,
InputGenerateTransactionOptions,
InputSingleSignerTransaction,
InputGenerateTransactionPayloadDataWithRemoteABI,
} from "../transactions/types";
import { UserTransactionResponse, PendingTransactionResponse, MimeType, HexInput } from "../types";

Expand Down Expand Up @@ -68,12 +68,12 @@ import { UserTransactionResponse, PendingTransactionResponse, MimeType, HexInput
* ```
*/
export async function generateTransaction(
args: { aptosConfig: AptosConfig } & GenerateTransactionInput,
): Promise<AnyRawTransaction> {
args: { aptosConfig: AptosConfig } & InputGenerateTransactionData,
): Promise<InputAnyRawTransaction> {
const { aptosConfig, sender, data, options, secondarySignerAddresses, feePayerAddress } = args;

// Merge in aptosConfig for remote ABI on non-script payloads
let generateTransactionPayloadData: GenerateTransactionPayloadDataWithRemoteABI;
let generateTransactionPayloadData: InputGenerateTransactionPayloadDataWithRemoteABI;
if ("bytecode" in data) {
generateTransactionPayloadData = data;
} else if ("multisigAddress" in data) {
Expand Down Expand Up @@ -118,7 +118,7 @@ export async function generateTransaction(
*
* @return The signer AccountAuthenticator
*/
export function signTransaction(args: { signer: Account; transaction: AnyRawTransaction }): AccountAuthenticator {
export function signTransaction(args: { signer: Account; transaction: InputAnyRawTransaction }): AccountAuthenticator {
const accountAuthenticator = sign({ ...args });
return accountAuthenticator;
}
Expand All @@ -133,7 +133,7 @@ export function signTransaction(args: { signer: Account; transaction: AnyRawTran
* @param args.options optional. A config to simulate the transaction with
*/
export async function simulateTransaction(
args: { aptosConfig: AptosConfig } & SimulateTransactionData,
args: { aptosConfig: AptosConfig } & InputSimulateTransactionData,
): Promise<Array<UserTransactionResponse>> {
const { aptosConfig, transaction, signerPublicKey, secondarySignersPublicKeys, feePayerPublicKey, options } = args;

Expand Down Expand Up @@ -171,7 +171,7 @@ export async function simulateTransaction(
*/
export async function submitTransaction(args: {
aptosConfig: AptosConfig;
transaction: AnyRawTransaction;
transaction: InputAnyRawTransaction;
senderAuthenticator: AccountAuthenticator;
secondarySignerAuthenticators?: {
feePayerAuthenticator?: AccountAuthenticator;
Expand All @@ -193,7 +193,7 @@ export async function submitTransaction(args: {
export async function signAndSubmitTransaction(args: {
aptosConfig: AptosConfig;
signer: Account;
transaction: AnyRawTransaction;
transaction: InputAnyRawTransaction;
}): Promise<PendingTransactionResponse> {
const { aptosConfig, signer, transaction } = args;
const authenticator = signTransaction({ signer, transaction });
Expand All @@ -209,8 +209,8 @@ export async function publicPackageTransaction(args: {
account: HexInput;
metadataBytes: HexInput;
moduleBytecode: Array<HexInput>;
options?: GenerateTransactionOptions;
}): Promise<SingleSignerTransaction> {
options?: InputGenerateTransactionOptions;
}): Promise<InputSingleSignerTransaction> {
const { aptosConfig, account, metadataBytes, moduleBytecode, options } = args;

const totalByteCode = moduleBytecode.map((bytecode) => MoveVector.U8(bytecode));
Expand All @@ -224,5 +224,5 @@ export async function publicPackageTransaction(args: {
},
options,
});
return transaction as SingleSignerTransaction;
return transaction as InputSingleSignerTransaction;
}
5 changes: 5 additions & 0 deletions src/transactions/authenticator/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0

export * from "./account";
export * from "./transaction";
9 changes: 9 additions & 0 deletions src/transactions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0

export * from "./authenticator";
export * from "./instances";
export * from "./transactionBuilder";
export * from "./typeTag";
export * from "./typeTag/parser";
export * from "./types";
1 change: 1 addition & 0 deletions src/transactions/instances/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export * from "./chainId";
export * from "./identifier";
export * from "./moduleId";
export * from "./rawTransaction";
export * from "./signedTransaction";
export * from "./transactionArgument";
export * from "./transactionPayload";
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

import {
EntryFunctionArgumentTypes,
GenerateTransactionPayloadData,
GenerateTransactionPayloadDataWithRemoteABI,
ScriptData,
InputGenerateTransactionPayloadData,
InputGenerateTransactionPayloadDataWithRemoteABI,
InputScriptData,
SimpleEntryFunctionArgumentTypes,
} from "../types";
import { Bool, FixedBytes, MoveString, U128, U16, U256, U32, U64, U8 } from "../../bcs";
Expand Down Expand Up @@ -67,8 +67,8 @@ export function isBcsU256(arg: EntryFunctionArgumentTypes | SimpleEntryFunctionA
}

export function isScriptDataInput(
arg: GenerateTransactionPayloadDataWithRemoteABI | GenerateTransactionPayloadData,
): arg is ScriptData {
arg: InputGenerateTransactionPayloadDataWithRemoteABI | InputGenerateTransactionPayloadData,
): arg is InputScriptData {
return "bytecode" in arg;
}

Expand Down
6 changes: 6 additions & 0 deletions src/transactions/transactionBuilder/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0

export * from "./helpers";
export * from "./transactionBuilder";
export * from "./remoteAbi";
Loading

0 comments on commit 6cb2d16

Please sign in to comment.