Application Integration Guide
This guide is currently in progress of being replaced. Please check out the exchange integration guide for more up-to-date information.
If you provide blockchain services to your customers and wish to add the Aptos blockchain to your platform, then this guide is for you. This system integrators guide will walk you through all you need to integrate the Aptos blockchain into your platform.
Overview
This document will guide you through the following tasks to integrate with Aptos:
- Prepare an environment for testing.
- Create an account on the blockchain.
- Exchange account identifiers with another entity on the blockchain, for example, to perform swaps.
- Create a transaction.
- Obtain a gas estimate and validate the transaction for correctness.
- Submit the transaction to the blockchain.
- Wait for the outcome of the transaction.
- Query historical transactions and interactions for a given account with a specific account, i.e., withdraws and deposits.
Getting Started
In order to get started you’ll need to select a network and pick your set of tools. There are also a handful of SDKs to help accelerate development.
SDKs and tools
Aptos has multiple SDKs across many different languages and platforms, please check out SDKs for more information.
Almost all developers will benefit from exploring the CLI. Using the CLI demonstrates how the CLI can be used to create accounts, transfer coins, publish Move modules, and more.
Accounts on Aptos
An account represents
an entity on the Aptos blockchain that can send transactions. Each account is
identified by a particular 32-byte account address and is a container for
Move modules and resources.
On Aptos, accounts must be created on-chain prior to any blockchain operations
involving that account. The Aptos framework supports implicitly creating
accounts when transferring Aptos coin via aptos_account::transfer
or explicitly via aptos_account::create_account
.
At creation, an Aptos account contains:
- A resource containing Aptos Coin and deposit and withdrawal of coins from that resource.
- An authentication key associated with their current public, private key(s).
- A strictly increasing sequence number that represents the account’s next transaction’s sequence number to prevent replay attacks.
- A strictly increasing number that represents the next distinct GUID creation number.
- An event handle for all new types of coins added to the account.
- An event handle for all key rotations for the account.
Read more about Accounts and set one up.
Transactions
Aptos transactions are encoded in Binary Canonical Serialization (BCS). Transactions contain information such as the sender’s account address, authentication from the sender, the desired operation to be performed on the Aptos blockchain, and the amount of gas the sender is willing to pay to execute the transaction.
Read more in Transactions and States.
Generating transactions
Aptos supports two methods for constructing transactions:
- Using the Aptos client libraries to generate native BCS transactions.
- Constructing JSON-encoded objects and interacting with the REST API to generate native transactions.
The preferred approach is to directly generate native BCS transactions. Generating them via the REST API enables rapid development at the cost of trusting the fullnode to generate the transaction correctly.
BCS-encoded transactions
BCS-encoded transactions can be submitted to the /transactions
endpoint but
must specify Content-Type: application/x.aptos.signed_transaction+bcs
in the
HTTP headers. This will return a transaction submission result that, if
successful, contains a transaction hash in
the hash
field.
Types of transactions
Within a given transaction, the target of execution can be one of two types:
- An entry function
- A Move script
All official SDKs support the generation of transactions that target entry functions. This guide
points out many of those entry functions, such as aptos_account::transfer
and aptos_account::create_account
.
Most basic operations on the Aptos blockchain should be available via entry point calls. While one could submit multiple transactions calling entry points in series, such operations benefit from being called atomically from a single transaction. A script payload transaction can call any public (entry) function defined within any module. Here’s an example Move script that uses a MultiAgent transaction to extract funds from two accounts and deposit them into two other accounts. This is a Python example that uses the bytecode generated by compiling that script.
Status of a transaction
Obtain transaction status by querying the
API /transactions/by_hash/{hash}
with the hash returned during the submission of the transaction.
A reasonable strategy for submitting transactions is to limit their lifetime to 30 to 60 seconds, and polling that API at regular intervals until success or several seconds after that time has elapsed. If there is no commitment on-chain, the transaction was likely discarded.
All SDKs support this automatically for waiting for transactions.
Testing transactions or transaction pre-execution
To facilitate evaluation of transactions as well as gas estimation, Aptos supports a simulation API that does not require and should not contain valid signatures on transactions.
The simulation API is a synchronous API that executes a transaction and returns
the output inclusive of gas usage. The simulation API can be accessed by
submitting a transaction
to /transactions/simulate
.
Both the Typescript SDK and Python SDK support the simulation API. Note the output and gas used may change based upon the state of the account. For gas estimations, we recommend that the maximum gas amount be larger than the amount quoted by this API.
Viewing current and historical state
Most integrations into the Aptos blockchain benefit from a holistic and comprehensive overview of the current and historical state of the blockchain. Aptos provides historical transactions, state, and events, all the result of transaction execution.
- Historical transactions specify the execution status, output, and tie to related events. Each transaction has a unique version number associated with it that dictates its global sequential ordering in the history of the blockchain ledger.
- The state is the representation of all transaction outputs up to a specific version. In other words, a state version is the accumulation of all transactions inclusive of that transaction version.
- As transactions execute, they may emit events. Events are hints about changes in on-chain data.
The storage service on a node employs two forms of pruning that erase data from nodes:
- state
- events, transactions, and everything else
While either of these may be disabled, storing the state versions is not particularly sustainable.
Events and transactions pruning can be disabled via setting
the enable_ledger_pruner
to false
. This is default behavior in Mainnet. In the near future, Aptos will
provide indexers that mitigate the need to directly query from a node.
The REST API offers querying transactions and events in these ways:
Exchanging and tracking fungible assets
Aptos has a standard Fungible Asset. Different types of fungible asset (FA) can be represented in this standard through the use of distinct metadata object.
A user’s FA is stored in FungibleStore
objects owned by them. For each type of
FA, every account has one primary store for that FA and
optional multiple secondary stores. The difference between primary and secondary
stores is the address of primary store
is deterministic based on the addresses of user account and metadata object.
Transferring FAs between users
FAs, including APT, can be transferred between users’ primary stores via
the primary_fungible_store::transfer
function.
For any FungibleStore
s, fungible_asset::transfer
would be invoked with FungibleStore
object addresses.
Current balance for Fungible Asset
The current balance for an APT FA of FungibleStore is available at the account resources URL: https://{rest_api_server}/accounts/{fungible_store_object_address}/resource/0x1::fungible_asset::FungibleStore
. The balance is stored as balance
. The resource also contains a metadata object of the FA type and the frozen status. The address of the primary fungible store can be calculated as sha3_256(32-byte account address | 32-byte metadata object address | 0xFC)
. The metadata object address of APT FA is 0xA
.
Aptos users have the option to upgrade to concurrent fungible balance to allow parallelization of balance updates, improving the performance of a single account. When a user has upgraded a fungible store balance to support concurrent update, the fungible store object will have another resource ConcurrentFungibleBalance
that contains the balance of the store, and the balance
field of FungibleStore will be set to 0. The current balance for an APT FA of ConcurrentFungibleBalance
(if exists) is available at the account resources URL: https://{rest_api_server}/accounts/{fungible_store_object_address}/resource/0x1::fungible_asset::ConcurrentFungibleBalance
.
Therefore, to get the total balance of a fungible asset, it is either the non-zero balance of FungibleStore
or the balance
field of ConcurrentFungibleBalance
if it exists and the balance of FungibleStore
is 0.
{
"type": "0x1::fungible_asset::FungibleStore",
"data": {
"balance": "233910778869",
"frozen": false,
"metadata": {
"inner": "0xedc2704f2cef417a06d1756a04a16a9fa6faaed13af469be9cdfcac5a21a8e2e"
}
}
}
{
"type": "0x1::fungible_asset::ConcurrentFungibleBalance",
"data": {
"balance": "233910778869"
}
}
Exchanging and tracking coins
Aptos has a standard
Coin type.
Different types of coins can be represented in this type through the use of
distinct structs that represent the type parameter or generic for Coin<T>
.
Coins are stored within an account under the resource CoinStore<T>
. At account
creation, each user has the resource CoinStore<0x1::aptos_coin::AptosCoin>
or CoinStore<AptosCoin>
, for short. Within this resource is the Aptos
coin: Coin<AptosCoin>
.
Transferring coins between users
Coins, including APT, can be transferred between users via
the aptos_account::transfer_coins
function for all coins
and aptos_account::transfer
for Aptos coins.
It is important to note that if an account has not registered a
CoinStore<T />
for a given T
, then any transfer of type T
to that account will fail.
Current balance for a coin
To retrieve the balance of a coin, or a coin that was migrated to a fungible asset, you can use
the 0x1::coin::balance<CoinType>(account address)
view function. This will combine the coin and coin migrated to fungible asset balances.
import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk";
const config = new AptosConfig({ network: Network.DEVNET });
const aptos = new Aptos(config);
const coinType = "0x1::aptos_coin::AptosCoin";
const account = "0x00000000000000000000000000000001";
const [balanceStr] = await aptos.view<[string]>({
payload: {
function: "0x1::coin::balance",
typeArguments: [coinType],
functionArguments: [account]
}
});
const balance = parseInt(balanceStr, 10);
Querying transactions
In Aptos, each transaction is committed as a distinct version to the
blockchain. This allows for the convenience of sharing committed transactions
by their version number; to do so, query:
https://{rest_server_api}/transactions/by_version/{version}
Transactions submitted by an account can also be queried via the following URL
where the sequence_number
matches the sequence number of the transaction:
https://{rest_server_api}/account/{address}/transactions?start={sequence_number}&limit=1
A transfer transaction would appear as follows:
{
"version": "13629679",
"gas_used": "4",
"success": true,
"vm_status": "Executed successfully",
"changes": [
{
"address": "0xb258b91eee04111039320a85b0c24a2dd433909e14a6b5c32ee722e0fdecfddc",
"data": {
"type": "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>",
"data": {
"coin": {
"value": "1000"
},
"deposit_events": {
"counter": "1",
"guid": {
"id": {
"addr": "0x5098df8e7969b58ab3bd2d440c6203f64c60a1fd5c08b9d4abe6ae4216246c3e",
"creation_num": "2"
}
}
},
...
}
},
"type": "write_resource"
},
...
],
"sender": "0x810026ca8291dd88b5b30a1d3ca2edd683d33d06c4a7f7c451d96f6d47bc5e8b",
"sequence_number": "0",
"max_gas_amount": "2000",
"gas_unit_price": "1",
"expiration_timestamp_secs": "1660616127",
"payload": {
"function": "0x1::aptos_account::transfer",
"arguments": [
"0x5098df8e7969b58ab3bd2d440c6203f64c60a1fd5c08b9d4abe6ae4216246c3e",
"1000"
],
"type": "entry_function_payload"
},
"events": [
{
"key": "0x0300000000000000810026ca8291dd88b5b30a1d3ca2edd683d33d06c4a7f7c451d96f6d47bc5e8b",
"guid": {
"id": {
"addr": "0x810026ca8291dd88b5b30a1d3ca2edd683d33d06c4a7f7c451d96f6d47bc5e8b",
"creation_num": "3"
}
},
"sequence_number": "0",
"type": "0x1::coin::WithdrawEvent",
"data": {
"amount": "1000"
}
},
{
"key": "0x02000000000000005098df8e7969b58ab3bd2d440c6203f64c60a1fd5c08b9d4abe6ae4216246c3e",
"guid": {
"id": {
"addr": "0x5098df8e7969b58ab3bd2d440c6203f64c60a1fd5c08b9d4abe6ae4216246c3e",
"creation_num": "2"
}
},
"sequence_number": "0",
"type": "0x1::coin::DepositEvent",
"data": {
"amount": "1000"
}
}
],
"timestamp": "1660615531147935",
"type": "user_transaction"
}
Here is a breakdown of the information in a transaction:
version
indicates the globally unique identifier for this transaction, its ordered position in all the committed transactions on the blockchainsender
is the account address of the entity that submitted the transactiongas_used
is the units paid for executing the transactionsuccess
andvm_status
indicate whether the transaction successfully executed and any reasons why it might not havechanges
include the final values for any state resources that have been modified during the execution of the transactionevents
contain all the events emitted during the transaction executiontimestamp
is the near real-time timestamp of the transaction’s execution
If success
is false, then vm_status
will contain an error code or message
that resulted in the transaction failing to succeed. When success
is
false, changes
will be limited to gas deducted from the account and the
sequence number incrementing. There will be no events
.
Each event in events
is differentiated by a key
. The key
is derived from
the guid
in changes
. Specifically, the key
is a 40-byte hex string where
the first eight bytes (or 16 characters) are the little-endian representation
of the creation_num
in the guid
of the changes
event, and the remaining
characters are the account address.
As events do not dictate what emitted them, it is imperative to track the path
in changes
to determine the source of an event. In particular,
each CoinStore<T>
has both a WithdrawEvent
and a DepositEvent
, based
upon the type of coin. In order to determine which coin type is used in a
transaction, an indexer can compare the guid::creation_num
in a changes
event combined with the address to the key
for events in events
.
Using the above example, events[1].guid
is equivalent
to changes[0].data.data.deposit_events.guid
, which
is
{"addr": "0x5098df8e7969b58ab3bd2d440c6203f64c60a1fd5c08b9d4abe6ae4216246c3e", "creation_num": "2"}
The key
field will be going away in favor of guid
Querying events
Aptos provides clear and canonical events for all withdraw and deposit of coins. This can be used in coordination with the associated transactions to present to a user the change of their account balance over time, when that happened, and what caused it. With some amount of additional parsing, metadata such as the transaction type and the other parties involved can also be shared.
Query events by handle
URL: https://{rest_api_server}/accounts/{address}/events/0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>/withdraw_events
[
{
"version":"13629679",
"key": "0x0300000000000000cb2f940705c44ba110cd3b4f6540c96f2634938bd5f2aabd6946abf12ed88457",
"guid": {
"id": {
"addr": "0x810026ca8291dd88b5b30a1d3ca2edd683d33d06c4a7f7c451d96f6d47bc5e8b",
"creation_num": "3"
}
},
"sequence_number": "0",
"type": "0x1::coin::WithdrawEvent",
"data": {
"amount": "1000"
}
}
]
Gather more information from the transaction that generated the event by
querying https://{rest_server_api}/transactions/by_version/{version}
where {version}
is the same value as the {version}
in the event query.
When tracking full movement of coins, normally events are
sufficient. 0x1::aptos_coin::AptosCoin
, however, requires
considering gas_used
for each transaction sent from the given account
since it represents gas in Aptos. To reduce unnecessary overhead, extracting
gas fees due to transactions does not emit an event. All transactions for an
account can be retrieved from this API:
https://{rest_server_api}/accounts/{address}/transactions
Tracking coin balance changes
Consider the transaction from the earlier section, but now with an arbitrary
coin 0x1337::my_coin::MyCoin
and some gas parameters changed:
{
"version": "13629679",
"gas_used": "20",
"success": true,
"vm_status": "Executed successfully",
"changes": [
{
"address": "0xb258b91eee04111039320a85b0c24a2dd433909e14a6b5c32ee722e0fdecfddc",
"data": {
"type": "0x1::coin::CoinStore<0x1337::my_coin::MyCoin>",
"data": {
"coin": {
"value": "1000"
},
"deposit_events": {
"counter": "1",
"guid": {
"id": {
"addr": "0x5098df8e7969b58ab3bd2d440c6203f64c60a1fd5c08b9d4abe6ae4216246c3e",
"creation_num": "2"
}
}
},
...
}
},
"type": "write_resource"
},
...
],
"sender": "0x810026ca8291dd88b5b30a1d3ca2edd683d33d06c4a7f7c451d96f6d47bc5e8b",
"sequence_number": "0",
"max_gas_amount": "2000",
"gas_unit_price": "110",
"expiration_timestamp_secs": "1660616127",
"payload": {
"function": "0x1::aptos_account::transfer_coins",
"type_arguments": [
"0x1337::my_coin::MyCoin"
],
"arguments": [
"0x5098df8e7969b58ab3bd2d440c6203f64c60a1fd5c08b9d4abe6ae4216246c3e",
"1000"
],
"type": "entry_function_payload"
},
"events": [
{
"key": "0x0300000000000000810026ca8291dd88b5b30a1d3ca2edd683d33d06c4a7f7c451d96f6d47bc5e8b",
"guid": {
"id": {
"addr": "0x810026ca8291dd88b5b30a1d3ca2edd683d33d06c4a7f7c451d96f6d47bc5e8b",
"creation_num": "3"
}
},
"sequence_number": "0",
"type": "0x1::coin::WithdrawEvent",
"data": {
"amount": "1000"
}
},
{
"key": "0x02000000000000005098df8e7969b58ab3bd2d440c6203f64c60a1fd5c08b9d4abe6ae4216246c3e",
"guid": {
"id": {
"addr": "0x5098df8e7969b58ab3bd2d440c6203f64c60a1fd5c08b9d4abe6ae4216246c3e",
"creation_num": "2"
}
},
"sequence_number": "0",
"type": "0x1::coin::DepositEvent",
"data": {
"amount": "1000"
}
}
],
"timestamp": "1660615531147935",
"type": "user_transaction"
}
There are three balance changes in this transaction:
- A withdrawal of
1000
of0x1337::my_coin::MyCoin
from the transaction sending account0x810026ca8291dd88b5b30a1d3ca2edd683d33d06c4a7f7c451d96f6d47bc5e8b
- A deposit of
1000
of0x1337::my_coin::MyCoin
to receiving account0x5098df8e7969b58ab3bd2d440c6203f64c60a1fd5c08b9d4abe6ae4216246c3e
- A gas fee
2200
of0x1::aptos_coin::AptosCoin
from the sending account0x810026ca8291dd88b5b30a1d3ca2edd683d33d06c4a7f7c451d96f6d47bc5e8b
To retrieve the withdrawal information:
- Scan the
changes
for0x1::coin::CoinStore<CoinType>
. Note theCoinType
is a generic signifying which coin is stored in the store. In this example, theCoinType
is0x1337::my_coin::MyCoin
. - Retrieve the
guid
forwithdraw_events
. In this example, theguid
containsaddr
0x810026ca8291dd88b5b30a1d3ca2edd683d33d06c4a7f7c451d96f6d47bc5e8b
andcreation_num
3
. - Scan for events with this
guid
and extract the event associated with it. In this example, it is the0x1::coin::WithdrawEvent
. - Note the
amount
field will be the number ofCoinType
removed from the account in theguid
. In this example, it is1000
.
To retrieve the deposit information, it’s the same as withdrawal except:
- The
guid
used is underdeposit_events
- The
amount
will be a positive increase on the account’s balance. - The event’s name will be:
0x1::coin::DepositEvent
To retrieve the gas fee:
- The
gas_used
field must be multiplied times thegas_unit_price
. In this example,gas_used=20
andgas_unit_price=110
so the total gas coins withdrawn is2200
. - Gas is always:
0x1::aptos_coin::AptosCoin
To retrieve information about the number of decimals of the coin:
- You can retrieve the number of decimals for a coin via
its:
0x1::coin::CoinInfo<CoinType>
- This will be located at the address of the coin type. In this example, you
would need to look up
0x1::coin::CoinInfo<0x1337::my_coin::MyCoin>
at address0x1337
.
If you always use the events in this manner, you won’t miss any balance
changes
for an account.
By monitoring the events, you will find all balance changes in
the 0x1::coin::CoinStore
:
- Coin mints
- Coin burns
- Coin transfers
- Staking coins
- Withdrawing staked coins
- Transfers not derived from
coin::transfer
To create some sample data to explore, conduct “Your first transaction”. To learn more about coin creation, make “Your First Coin”.