Your First Transaction
This tutorial describes how to generate and submit transactions to the Aptos blockchain, and verify these submitted transactions. The transfer-coin
example used in this tutorial is built with the Aptos SDKs.
Step 1: Pick an SDK
Install your preferred SDK from the below list:
Step 2: Run the example
Clone the aptos-core
repo:
Typescript
Python
Rust
Navigate to the Typescript SDK examples directory:
Install the necessary dependencies:
Run the transfer_coin
example:
Step 3: Understand the output
An output very similar to the following will appear after executing the above command:
The above output demonstrates that the transfer-coin
example executes the following steps:
Initializing the REST and faucet clients.
The creation of two accounts: Alice and Bob.
The funding and creation of Alice's account from a faucet.
The creation of Bob's account from a faucet.
The transferring of 1000 coins from Alice to Bob.
The 54100 coins of gas paid for by Alice to make that transfer.
Another transfer of 1000 coins from Alice to Bob.
The additional 54100 coins of gas paid for by Alice to make that transfer.
Now see the below walkthrough of the SDK functions used to accomplish the above steps.
Step 4: The SDK in depth
The transfer-coin
example code uses helper functions to interact with the REST API. This section reviews each of the calls and gives insights into functionality.
Typescript
Python
Rust
SEE THE FULL CODE
See the TypeScript transfer-coin
for the complete code as you follow the below steps.
Step 4.1: Initializing the clients
In the first step, the transfer-coin
example initializes both the REST and faucet clients:
The REST client interacts with the REST API.
The faucet client interacts with the devnet Faucet service for creating and funding accounts.
Typescript
Python
Rust
Using the API client we can create a CoinClient
that we use for common coin operations such as transferring coins and checking balances.
common.ts
initializes the URL values as such:
TIP
By default, the URLs for both the services point to Aptos devnet services. However, they can be configured with the following environment variables:
APTOS_NODE_URL
APTOS_FAUCET_URL
Step 4.2: Creating local accounts
The next step is to create two accounts locally. Accounts represent both on and off-chain state. Off-chain state consists of an address and the public/private key pair used to authenticate ownership. This step demonstrates how to generate that off-chain state.
Typescript
Python
Rust
Step 4.3: Creating blockchain accounts
In Aptos, each account must have an on-chain representation in order to receive tokens and coins and interact with other dApps. An account represents a medium for storing assets; hence, it must be explicitly created. This example leverages the Faucet to create and fund Alice's account and to create but not fund Bob's account:
Typescript
Python
Rust
Step 4.4: Reading balances
In this step, the SDK translates a single call into the process of querying a resource and reading a field from that resource.
Typescript
Python
Rust
Behind the scenes, the checkBalance
function in CoinClient
in the SDK queries the CoinStore resource for the AptosCoin and reads the current stored value:
Step 4.5: Transferring
Like the previous step, this is another helper step that constructs a transaction transferring the coins from Alice to Bob. For correctly generated transactions, the API will return a transaction hash that can be used in the subsequent step to check on the transaction status. The Aptos blockchain does perform a handful of validation checks on submission; and if any of those fail, the user will instead be given an error. These validations use the transaction signature and unused sequence number, and submitting the transaction to the appropriate chain.
Typescript
Python
Rust
Behind the scenes, the transfer
function generates a transaction payload and has the client sign, send, and wait for it:
Within the client, generateSignSubmitTransaction
is doing this:
Breaking the above down into pieces:
transfer
internally is aEntryFunction
in the Coin Move module, i.e. an entry function in Move that is directly callable.The Move function is stored on the coin module:
0x1::coin
.Because the Coin module can be used by other coins, the transfer must explicitly specify which coin type to transfer. If not specified with
coinType
it defaults to0x1::aptos_coin::AptosCoin
.
Step 4.6: Waiting for transaction resolution
Typescript
Python
Rust
In TypeScript, just calling coinClient.transfer
is sufficient to wait for the transaction to complete. The function will return the Transaction
returned by the API once it is processed (either successfully or unsuccessfully) or throw an error if processing time exceeds the timeout.
You can set checkSuccess
to true when calling transfer
if you'd like it to throw an error if the transaction was not committed successfully:
Supporting documentation
Last updated