blog

How to Get Started with Aptos: Build, Sign, and Broadcast

Written by Dan Kazenoff | Apr 22, 2024 5:34:58 PM

This how-to guide will give you an overview of Aptos and help you get started building with Validation Cloud’s Node API. 

What is Aptos? An Overview

Aptos addresses the critical demands of decentralized applications with an emphasis on scalability, safety, reliability, and upgradeability. Created from a collaborative effort involving over 350 developers worldwide, Aptos incorporates state-of-the-art innovations in its foundation. The platform is powered by an optimistic concurrency control engine known as Block-STM, as well as a BFT Proof-of-Stake mechanism known as Jolteon. The introduction of the Move programming language is a defining characteristic, offering a secure and adaptable environment for application development. The recent native integration of Aptos Keyless Accounts is a revolutionary enabling familiar Web2 logins to support transacting on the Aptos blockchain. This novel architecture, emphasizing rigorous and rapid enhancement, positions Aptos as a clear leader in making Web3 technologies accessible on a global scale.

Why Choose Aptos?
Aptos technology and the Move programming language ensure a seamless and secure integration of projects. Here are a few reasons why developers and enterprises are choosing to build on Aptos:
  • Scalability and Performance: Aptos is engineered for high throughput and low latency, employing a batched, pipelined, and parallelized approach to transaction processing. This architecture fully leverages physical resources, enabling highly parallel execution without sacrificing transaction atomicity, which is critical for real-world applications.
  • Security and Safety: With the introduction of the Move programming language and the Aptos Prover, developers are equipped with tools for creating secure and reliable smart contracts. This focus on security helps protect against malicious threats and ensures the integrity of the blockchain.
  • Flexibility and Upgradeability: Aptos features a modular architecture design that supports frequent and instant upgrades. This flexibility allows for the rapid deployment of new technology innovations and supports emerging Web3 use cases, making it a dynamic and future-proof platform for developers and users alike​.
Getting Started with Validation Cloud's Aptos API

For developers eager to explore the benefits of the Aptos blockchain, Validation Cloud's Aptos API offers a powerful and seamless entry point.

To get started,  our comprehensive documentation provides a clear roadmap for setting up your development environment and connecting to the Aptos Network. In the next section, we’ll walk you through setup, querying the chain, importing your key to an Aptos account, simulating a transaction, and then sending a transaction. First, sign up for free at validationcloud.io, and create your Aptos API key.

Setup
Let’s get your TypeScript environment prepared. Ensure you have Node.js and Yarn installed, and then set up a new TypeScript project:

Create a ‘.env’ file in your project root to store your API key from app.validationcloud.io.

The ts-sdk version for this tutorial is ^1.13. Additionally, we will store a pre-funded wallet’s private key here as well. Storing private keys in plaintext is strongly not advised for wallets containing any sort of significant balance. In those scenarios, a more secure solution such as AWS Secrets Manager, Azure Key Vault, HashiCorp Vault, or others would be expected.
1. Connecting to Aptos, and Retrieving Ledger Info

Let’s start by connecting to Aptos and fetching some ledger info. The returned values will include the chainID, epoch, ledger version, timestamp, the git hash, and the blockheight, among a couple other properties.

2. Get the Latest Block by Height

For this method call, we’ll build off of the information from #1 to grab the latest block, and then run another call to get that latest block’s details.

3. Get an Account by Address

Retrieving an account by its address on the Aptos blockchain, even if it only provides basic information such as the authentication key and sequence number, serves several practical purposes. The authentication key is crucial for confirming the identity and the cryptographic credentials associated with the account, which is fundamental for security and transaction verification processes. This key helps ensure that any transaction submitted is genuinely from the account holder. The sequence number of an account, on the other hand, is vital for transaction ordering. Each transaction from an account must have a sequence number greater than the last confirmed transaction.

4. Get an Account Resource

An example of an account resource on Aptos could be the coin balance resource. 

For instance, the 0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin> resource defines the balance of Aptos Coins (the native currency) held in an account, including methods for managing these funds like depositing and withdrawing. Fetching an account resource on the Aptos blockchain provides valuable insights into the state of a specific account. This operation is crucial for developers and users wanting to understand the properties and current state of an account, such as its balances, sequence numbers, and associated modules and resources.

Note, getting the coin resource defaults to the unit of “microAptos,” so we convert it to APT for readability below.

5. Simulating a Transaction

If you’ve ever made a mistake issuing a transaction on Web3 before and wished you could have known the mistake in advance, you’re not alone! 

Simulating a transaction on Aptos is a powerful tool for developers and users to predict and analyze the outcome of a transaction without committing it to the blockchain. This simulation reveals whether a transaction will succeed or fail, and provides details about the changes it would make if committed, such as gas costs and state mutations. This is invaluable for debugging and optimizing smart contracts and transactions before they go live, which can save time and reduce costs by preventing failed transactions and their associated fees. Moreover, simulation helps in stress-testing smart contracts under various conditions to ensure their robustness and security, enhancing trust in the applications built on top of the Aptos platform.

We will now be using the Aptos official TS SDK (which contains utilities for transaction signing and broadcasting) to simulate sending 0.01 APT from one account to another.

Note: Before issuing tokens to any Aptos account via the SDK, the account must have registered to receive tokens first. Creating a CoinStore resource on an account for storing coins will resolve any errors encountered here, alternatively, a wallet extension can be used to send APT tokens to the account in question, creating the coin resource on the account automatically.

Note 2: The private key exported for these examples was from the Martian wallet. As of writing, the key format exported was legacy, and Ed25519 format.

6. Sending a Transaction

Keeping everything above the function in the prior example the same, the following code essentially alters the transaction.simulate to a transaction.submit, now calling the appropriate client resources to commit the transaction to the blockchain itself.

Through these examples, you've learned how to effectively interact with the Aptos blockchain using TypeScript and Axios. This tutorial should serve as a foundation for building more complex applications on top of the Aptos platform. Stay tuned for future posts where we'll dive deeper into smart contract interactions and node API possibilities.

About Validation Cloud
Validation Cloud is a Web3 data streaming and infrastructure company that connects organizations into Web3 through a fast, scalable, and intelligent platform. Headquartered in Zug, Switzerland, Validation Cloud offers highly performant and customizable products in staking, node, and data-as-a-service. Learn more at validationcloud.io| LinkedIn | X