135 subscribers
join
Rating
Login
Logout

Revolutionize Your Aptos Smart Contract

Crypto Education

Table of Contents

Since the launch of Aptos blockchain’s main-net on October 12th, 2022, several new features have been added to improve its functionality. Among these updates, we will focus on one of the most significant features implemented on Aptos to-date that will substantially transform the process of developing smart contracts.

View Functions

“View functions do not modify blockchain state. A View function and its input can be used to read potentially complex on-chain states using Move.

The View function operates like the Aptos Simulation API, though with no side effects and an accessible output path. The function is immutable if tagged as #[view], the compiler will confirm it so and if fail otherwise. View functions can be called via the /view endpoint. Calls to view functions require the module and function names along with input type parameters and values.” - Aptos (Aptos.dev View Functions Documentation).

View functions are a recent addition to the Aptos blockchain that allows developers to create a GET API structure to display complex states of smart contracts. In the past, developers had to fetch all the module’s resource data and perform calculations on the client-side, which was time-consuming and inefficient. However, with view functions, developers can now retrieve complex states through a simpler and more streamlined process that saves time and resources. This development has significantly improved the usability of the Aptos blockchain and made it more accessible to developers.

module view_functions::example { use std::string::String; use std::vector; use std::signer; const ERROR_TODO_STORE_EXISTS: u64 = 1; struct TodoStore has key { todos: vector } public entry fun add_todos(account: &signer, todos: vector) acquires TodoStore { let account_address = signer::address_of(account); if (exists(account_address)) { let todo_store = &mut borrow_global_mut(account_address).todos; vector::push_back(todo_store.todos, todos); } else { move_to(account, TodoStore { todos }); } } }

This is an example Aptos smart contract that allows users to store and retrieve to-do items. The code defines a data structure called “TodoStore,” which has a vector of strings to store the to-do items. The “add_todos” function is the public entry point to add new to-do items to the TodoStore. It first checks if a TodoStore exists for the user’s account and then adds the new to-do items to the vector in the TodoStore.

In the Aptos blockchain, before the introduction of view functions, there was no straightforward way to retrieve cohesive data from a specific smart contract and expose it to the invoker. If a developer needed data from a smart contract, they had to query the module’s resources and then perform any necessary compilations to the data on the client-side. This process was often time-consuming and inefficient, requiring developers to write custom code to retrieve and compile data from smart contracts.

const client = new AptosClient(NODE_URL); const resources = await client.getAccountResource(address, `${CONTRACT_ADDRESS}::view_functions::example`).data; // Returns the entire module's resources

The example above demonstrates how to retrieve resource data for a specific account and contract on the Aptos blockchain prior to the introduction of view functions. In the absence of view functions, you were required to retrieve all the data associated with a given smart contract, regardless of the specific request being made. This resulted in a problem of over-fetching, where unnecessary data was retrieved and transmitted, leading to inefficiency and slower processing times.

In a traditional web/server context, this would be similar to having to retrieve the entire database every time you wanted to make a query.

#[view] public fun get_todos(todo_address: address): vector acquires TodoStore { borrow_global(todo_address).todos }
const client = new AptosClient(NODE_URL); const payload = { function: `${CONTRACT_ADDRESS}::view_functions::example`, type_arguments: [], arguments: [TODO_ADDRESS], }; const resources = await client.view(payload); // Returns the cleaned data in a standard, digestible format

With the introduction of view functions, developers can now retrieve complex states from a smart contract in a more streamlined and efficient manner. View functions allow developers to define functions that can return specific data from a smart contract, providing a simple API that can be used by external invokers to retrieve data from the blockchain. This advancement has significantly improved the usability and accessibility of the Aptos blockchain, making it more developer-friendly and easier to use.

Instead of receiving the entire database every time you query it, you can now receive the specific data you need in the desired format using the new view functions.

About Pontem

Pontem Network is a product studio building foundational dApps for Aptos. Our products  include Pontem Wallet; Liquidswap, the first DEX (AMM) for Aptos; browser code editor Move Playground; the Move IntelliJ IDE plugin for developers; and the Solidity to Move translator ByteBabel -- the first implementation he Ethereum Virtual Machine for Aptos.

Install our wallet and try DEX

Related posts

revolutionize-your-aptos-smart-contract
6446c84ca8b43e1d73ab4641
amb-revolutionize-your-aptos-smart-contract