Aptos transaction

1. Generate Transaction

1.1. Generate Transaction (Single signer)

Generate transaction from readable JSON format payload.

Method type

async generateTransaction(
    payload: EntryFunctionPayload,
    options?: Partial<SubmitTransactionRequest>
): Promise<Response<Uint8Array>>;

type SubmitTransactionRequest = {
    sender: string;
    sequence_number: string; // U64
    max_gas_amount: string; // U64
    gas_unit_price: string; // U64
    expiration_timestamp_secs: string; // U64
    payload: object; // Transaction Payload (entry, script, module_bundle)
    signature: object; // ed25519, multi_ed25519, multi_agent
}

type EntryFunctionPayload = {
    type: string;
    function: string;
    type_arguments: Array<string>;
    arguments: Array<any>;
}

Example request

const receiverAddress = "0xcca3338dfda1b5e9bab0d744c3b50a9a24e3fe55bba48917307e813a4535e034";
const amount = 1000;

const payload = {
  type: "entry_function_payload",
  function: "0x1::coin::transfer",
  type_arguments: ["0x1::aptos_coin::AptosCoin"],
  arguments: [receiverAddress, amount],
};

const rawTransaction = await window.fewcha.generateTransaction(payload);

Example response

{
    "method": "generateTransaction",
    "status": 200,
    "data": [...]
}

Response data: TxnBuilderTypes.RawTransaction as Uint8Array

Covert Uint8Array to TxnBuilderTypes.RawTransaction (optional)

import { TxnBuilderTypes } from "aptos";

const d = new BCS.Deserializer(rawTransaction.data);
const tx = TxnBuilderTypes.RawTransaction.deserialize(d);

// type of tx is TxnBuilderTypes.RawTransaction

1.2. Generate Raw Transaction (Single signer)

Generate unsigned transaction from BCS serialized payload

Method type

async generateRawTransaction(
    payload: Uint8Array,
    extraArgs?: {
        maxGasAmount?: BCS.Uint64;
        gasUnitPrice?: BCS.Uint64;
        expireTimestamp?: BCS.Uint64
    }
): Promise<Response<Uint8Array>>;

Example request

const receiverAddress = TxnBuilderTypes.AccountAddress.fromHex(
  "0xcca3338dfda1b5e9bab0d744c3b50a9a24e3fe55bba48917307e813a4535e034",
);
const sendBalance = 1000;

const token = new TxnBuilderTypes.TypeTagStruct(
  TxnBuilderTypes.StructTag.fromString("0x1::aptos_coin::AptosCoin"),
);
const entryFunctionPayload = new TxnBuilderTypes.TransactionPayloadEntryFunction(
  TxnBuilderTypes.EntryFunction.natural(
    "0x1::coin",
    "transfer",
    [token],
    [BCS.bcsToBytes(receiverAddress), BCS.bcsSerializeUint64(sendBalance)],
  ),
);

const s = new BCS.Serializer();
entryFunctionPayload.serialize(s);

const rawTxn = await window.fewcha.generateRawTransaction(s.getBytes());

console.log(rawTxn);

Example response

{
    "method": "generateRawTransaction",
    "status": 200,
    "data": [...]
}

Response data: TxnBuilderTypes.RawTransaction as Uint8Array

Covert Uint8Array to TxnBuilderTypes.RawTransaction (optional)

import { TxnBuilderTypes } from "aptos";

const d = new BCS.Deserializer(txnRequest.data);
const tx = TxnBuilderTypes.RawTransaction.deserialize(d);

// type of tx is TxnBuilderTypes.RawTransaction

1.3. Generate BCS Transaction (Single signer)

Generate signed BCS serialized transaction from BCS serialized payload. Connected account will sign this BCS serialized payload.

Method type

async generateBCSTransaction(
    rawTransaction: Uint8Array
): Promise<Response<Uint8Array>>;

Example request

const receiverAddress = TxnBuilderTypes.AccountAddress.fromHex(
  "0xcca3338dfda1b5e9bab0d744c3b50a9a24e3fe55bba48917307e813a4535e034",
);
const sendBalance = 1000;

const token = new TxnBuilderTypes.TypeTagStruct(
  TxnBuilderTypes.StructTag.fromString("0x1::aptos_coin::AptosCoin"),
);
const entryFunctionPayload = new TxnBuilderTypes.TransactionPayloadEntryFunction(
  TxnBuilderTypes.EntryFunction.natural(
    "0x1::coin",
    "transfer",
    [token],
    [BCS.bcsToBytes(receiverAddress), BCS.bcsSerializeUint64(sendBalance)],
  ),
);

const s = new BCS.Serializer();
entryFunctionPayload.serialize(s);

const rawTxn = await web3.action.generateRawTransaction(s.getBytes());
if (!parseError(rawTxn.status)) return;

const bcsTxn = await web3.action.generateBCSTransaction(rawTxn.data);

console.log(bcsTxn);

Example response

{
    "method": "generateBCSTransaction",
    "status": 200,
    "data": [...]
}

Response data: TxnBuilderTypes.RawTransaction as Uint8Array

Covert Uint8Array to TxnBuilderTypes.RawTransaction (optional)

import { TxnBuilderTypes } from "aptos";

const d = new BCS.Deserializer(txnRequest.data);
const tx = TxnBuilderTypes.RawTransaction.deserialize(d);

// type of tx is TxnBuilderTypes.RawTransaction

2. Sign Transaction

2.1. Sign and Submit Transaction (Single signer)

We recommend you use this method

Method type

async signAndSubmitTransaction(
    rawTransaction: Uint8Array
): Promise<Response<Gen.HexEncodedBytes>>;

Example request

const receiverAddress = "0xcca3338dfda1b5e9bab0d744c3b50a9a24e3fe55bba48917307e813a4535e034";
const amount = 1000;

const payload = {
  type: "entry_function_payload",
  function: "0x1::coin::transfer",
  type_arguments: ["0x1::aptos_coin::AptosCoin"],
  arguments: [receiverAddress, amount],
};

const rawTransaction = await window.fewcha.generateTransaction(payload);
if (rawTransaction.status !== 200) return;

const txnHash = await window.fewcha.signAndSubmitTransaction(rawTransaction.data);

console.log("txnHash", txnHash);

Example response

{
    "method": "signAndSubmitTransaction",
    "status": 200,
    "data": "0x67d7b7ccee8530659b91ad284ba2c24bd87fb70c5b4411737f4f314826b5cc97"
}

2.2. Sign Transaction (Single signer)

Sign a transaction and do not submit to the Aptos blockchain.

Method type

async signTransaction(
    rawTransaction: Uint8Array
): Promise<Response<Uint8Array>>;

Example request

const receiverAddress = "0xcca3338dfda1b5e9bab0d744c3b50a9a24e3fe55bba48917307e813a4535e034";
const amount = 1000;

const payload = {
  type: "entry_function_payload",
  function: "0x1::coin::transfer",
  type_arguments: ["0x1::aptos_coin::AptosCoin"],
  arguments: [receiverAddress, amount],
};

const rawTransaction = await window.fewcha.generateTransaction(payload);
if (rawTransaction.status !== 200) return;

const signedTx = await window.fewcha.signTransaction(rawTransaction.data);

console.log("signedTx", signedTx);

Example response

{
    "method": "signTransaction",
    "status": 200,
    "data": [...]
}

Response data: Uint8Array

2.3. Sign Message (Single signer)

export interface SignMessagePayload {
  address?: boolean; // Should we include the address of the account in the message
  application?: boolean; // Should we include the domain of the dapp
  chainId?: boolean; // Should we include the current chain id the wallet is connected to
  message: string; // The message to be signed and displayed to the user
  nonce: string; // A nonce the dapp should generate
}

export interface SignMessageResponse {
  address: string;
  application: string;
  chainId: number;
  fullMessage: string; // The message that was generated to sign
  message: string; // The message passed in by the user
  nonce: string,
  prefix: string, // Should always be APTOS
  signature: string; // The signed full message
}

async signMessage(message: SignMessagePayload):
    Promise<Response<SignMessageResponse>>

3. Submit Transaction

3.1 Submit Transaction (Single signer)

Method type

async submitTransaction(
    signedTxn: Uint8Array
): Promise<Response<Gen.HexEncodedBytes>>

Example request

const receiverAddress = "0xcca3338dfda1b5e9bab0d744c3b50a9a24e3fe55bba48917307e813a4535e034";
const amount = 1000;

const payload = {
  type: "entry_function_payload",
  function: "0x1::coin::transfer",
  type_arguments: ["0x1::aptos_coin::AptosCoin"],
  arguments: [receiverAddress, amount],
};

const txnRequest = await window.fewcha.generateTransaction(payload);
if (!parseError(txnRequest.status)) return;

const signedTx = await window.fewcha.signTransaction(txnRequest.data);
if (!parseError(signedTx.status)) return;

const txnHash = await window.fewcha.submitTransaction(signedTx.data);
if (!parseError(txnHash.status)) return;

console.log("tx", txnHash);

Example response

{
    "method": "submitTransaction",
    "status": 200,
    "data": "0x4af85fb48400715fda9c4ea9c3f2bad0c1d6995f6aa0be7f5ea6d74d4f5f7682"
}

Response data: Gen.HexEncodedBytes

4. Simulate Transaction

Verify if the transaction is valid. You can use this function to estimate gas consumer.

4.1. simulateTransaction (Single Signer, Multi-Sign)

Method type

async simulateTransaction(
    rawTransaction: Uint8Array
): Promise<Response<Gen.UserTransaction[]>>

Example request

const receiverAddress = "0xcca3338dfda1b5e9bab0d744c3b50a9a24e3fe55bba48917307e813a4535e034";
const sendBalance = 1000;

const payload = {
  type: "entry_function_payload",
  function: "0x1::coin::transfer",
  type_arguments: ["0x1::aptos_coin::AptosCoin"],
  arguments: [receiverAddress, sendBalance],
};

const txnRequest = await window.fewcha.generateTransaction(payload);
if (!parseError(txnRequest.status)) return;

const siTx = await window.fewcha.simulateTransaction(txnRequest.data);
if (!parseError(siTx.status)) return;

console.log("siTx", siTx);

Example response

{
    "method": "simulateTransaction",
    "status": 200,
    "data": [
        {
            "version": "19832615",
            "hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
            "state_change_hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
            "event_root_hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
            "state_checkpoint_hash": null,
            "gas_used": "51",
            "success": true,
            "vm_status": "Executed successfully",
            "accumulator_root_hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
            "changes": [...],
            "sender": "0x20364f4121f608f2a09830bc0ab6980fdccff45c2f5df6c41c17f40e511fe80e",
            "sequence_number": "12",
            "max_gas_amount": "2000",
            "gas_unit_price": "1",
            "expiration_timestamp_secs": "1661767977",
            "payload": {
                "function": "0x1::coin::transfer",
                "type_arguments": [
                    "0x1::aptos_coin::AptosCoin"
                ],
                "arguments": [
                    "0xcca3338dfda1b5e9bab0d744c3b50a9a24e3fe55bba48917307e813a4535e034",
                    "1000"
                ],
                "type": "entry_function_payload"
            },
            "signature": {
                "public_key": "0x89a10bded6d812d21299ee129410d53a74f12f91183989c19282fa24ce629b49",
                "signature": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
                "type": "ed25519_signature"
            },
            "events": [
                {
                    "key": "0x030000000000000020364f4121f608f2a09830bc0ab6980fdccff45c2f5df6c41c17f40e511fe80e",
                    "sequence_number": "9",
                    "type": "0x1::coin::WithdrawEvent",
                    "data": {
                        "amount": "1000"
                    }
                },
                {
                    "key": "0x0200000000000000cca3338dfda1b5e9bab0d744c3b50a9a24e3fe55bba48917307e813a4535e034",
                    "sequence_number": "10",
                    "type": "0x1::coin::DepositEvent",
                    "data": {
                        "amount": "1000"
                    }
                }
            ],
            "timestamp": "1661767957293402"
        }
    ]
}

Response data: Gen.UserTransaction[] (array of Gen.UserTransaction)

Learn more about response data types at Response Format and Types

5. Supported methods

generateTransaction(
  payload: Gen.EntryFunctionPayload,
  options?: Partial<Gen.SubmitTransactionRequest>,
): Promise<Uint8Array>;
generateRawTransaction(payload: Uint8Array, extraArgs?: OptionalTransactionArgs): Promise<Uint8Array>;
generateMultiAgentTransaction(
  payload: Gen.EntryFunctionPayload,
  secondarySenders: MaybeHexString[],
  options?: Partial<Gen.SubmitTransactionRequest>,
): Promise<Uint8Array>;
generateSignSubmitTransaction(
  payload: Gen.EntryFunctionPayload,
  extraArgs?: Partial<Gen.SubmitTransactionRequest>,
): Promise<Gen.HexEncodedBytes>;
generateSignSubmitRawTransaction(
  payload: Uint8Array,
  extraArgs?: {
    maxGasAmount?: BCS.Uint64;
    gasUnitPrice?: BCS.Uint64;
    expireTimestamp?: BCS.Uint64;
    checkSuccess?: boolean;
    timeoutSecs?: number;
  },
): Promise<Gen.HexEncodedBytes>;
generateSignSubmitWaitForTransaction(
  payload: Uint8Array,
  extraArgs?: {
    maxGasAmount?: BCS.Uint64;
    gasUnitPrice?: BCS.Uint64;
    expireTimestamp?: BCS.Uint64;
    checkSuccess?: boolean;
    timeoutSecs?: number;
  },
): Promise<Gen.Transaction>;
signMessage(message: SignMessagePayload): Promise<SignMessageResponse>;
signBuffer(buffer: Uint8Array): Promise<HexString>;
simulateTransaction(
  rawTransaction: Uint8Array,
  query?: {
    estimateGasUnitPrice?: boolean;
    estimateMaxGasAmount?: boolean;
    estimatePrioritizedGasUnitPrice: boolean;
  },
): Promise<Gen.UserTransaction[]>;
simulateMultiAgentTransaction(
  multiAgentTxn: Uint8Array,
  addresses: MaybeHexString[],
  publicKeys: MaybeHexString[],
  query?: {
    estimateGasUnitPrice?: boolean;
    estimateMaxGasAmount?: boolean;
    estimatePrioritizedGasUnitPrice?: boolean;
  },
): Promise<Gen.UserTransaction[]>;
signTransaction(rawTransaction: Uint8Array): Promise<Uint8Array>;
signMultiAgentTransaction(rawMultiAgentTransaction: Uint8Array): Promise<Uint8Array>;
signMultiSignTransaction(rawTransaction: Uint8Array): Promise<Uint8Array>;
submitTransaction(signedTxn: Uint8Array): Promise<Gen.HexEncodedBytes>;
submitSignedBCSMultiAgentTransaction(
  multiAgentTxn: Uint8Array,
  addresses: MaybeHexString[],
  publicKeys: MaybeHexString[],
  _signatures: Uint8Array[],
): Promise<Gen.HexEncodedBytes>;
submitSignedBCSMultiSignTransaction(
  rawTxn: Uint8Array,
  bitmap: number[],
  multiSignPubKey: MaybeHexString,
  _signatures: Uint8Array[],
): Promise<Gen.HexEncodedBytes>;
signAndSubmitTransaction(rawTransaction: Uint8Array): Promise<Gen.HexEncodedBytes>;
generateBCSTransaction(rawTransaction: Uint8Array): Promise<Uint8Array>;
generateBCSSimulation(rawTransaction: Uint8Array): Promise<Uint8Array>;
submitSignedBCSTransaction(signedTxn: Uint8Array): Promise<Gen.HexEncodedBytes>;
submitBCSSimulation(
  bcsBody: Uint8Array,
  query?: {
    estimateGasUnitPrice?: boolean;
    estimateMaxGasAmount?: boolean;
    estimatePrioritizedGasUnitPrice: boolean;
  },
): Promise<Gen.UserTransaction[]>;

Last updated