Skip to main content

Querying smart contract data

In the EVM blockchains, there are two distinct types of functions: "View Functions" and "Transaction Functions." In this section, we'll focus exclusively on view functions. These are read-only operations that enable developers to read blockchain data, which do not incurr gas costs or make any change to the blockchain state. In this section, we will focus on these operations and teach you how to read blockchain data with our APIs.

The main characteristics of the view functions are:

  • Gas-Free Interaction: Perhaps the most significant advantage of view functions is that they do not consume gas. In blockchain ecosystems, gas refers to the computational power required to execute transactions or perform operations. Traditional transactions that alter the blockchain's state, such as transferring tokens or updating records, typically incur gas costs. View functions, on the other hand, provide a cost-effective solution for users who want to retrieve information without worrying about transaction fees.

  • Immutable State: View functions operate in a read-only manner, meaning they do not modify the blockchain's state. This immutability ensures data integrity and transparency, making them a reliable choice for querying information.

  • Enhanced Security: By design, view functions do not introduce any security risks or vulnerabilities since they cannot alter the blockchain's underlying data. This makes them a safe choice for developers to explore the blockchain's vast potential without the need for extensive security audits.

Executing view functions with the Vottun APIs​

Using the Vottun APIs, developers can execute view functions of any smart contract in order to retrieve their data. This can be achieved with the /view endpoint from our Web3 Core API. For a detailed example of how to perform this operation with the Vottun APIs, head to Query smart contract information

How to leverage the power of view functions​

Now that we have talked about WHY you should use these functions and HOW to perform them, let's see some real examples of the use of view functions:

1. Authentication: Verify account permissions​

In platforms that use token-gated authentication, users posess a token that grants them access to certain services. Thus, when a user tries to access a private/gated section, the platform must check that the user possesses the corresponding token or credential before granting them access to ceratin features. This can be achieved by using the balanceOf(address ownerAccount) method of the Smart Contract of the token used as access credential. An example of this operation would look like this:

curl --location 'https://api.vottun.tech/core/v1/evm/transact/view' \
--header 'Authorization: Bearer <YOUR_API_KEY>' \
--header 'x-application-vkn: <YOUR_APPLICATION_ID>' \
--header 'Content-Type: application/json' \
--data-raw '{
"contractAddress": "0xa0b86991c621..BA20e5A3Aa0C",
"blockchainNetwork": 97,
"method": "balanceOf",
"params": [
"0xC02A3F525f2..597B2ffcaE633a"
]
}'

2. Oracles: Retrieve real-time market prices​

Market-related dApps often require real-time data. With view functions, you can access live market prices, order book information, or trading volumes directly from the blockchain, creating efficient and up-to-date trading interfaces. Imagine you are using a Chainlink Oracle Smart Contract for retrieving real-time prices, you could call the latestRoundData() function to get the latest price data. An example of this operation would look like this:

curl --location 'https://api.vottun.tech/core/v1/evm/transact/view' \
--header 'Authorization: Bearer <YOUR_API_KEY>' \
--header 'x-application-vkn: <YOUR_APPLICATION_ID>' \
--header 'Content-Type: application/json' \
--data-raw '{
"contractAddress": "0x1b44F3514812d8..BA20e5A3Aa0C",
"blockchainNetwork": 97,
"method": "latestRoundData",
"params": []
}'

3. Supply chain: Trace product history​

For supply chain management applications, view functions enable the retrieval of product history and provenance data. Imagine the smart contract offers a method called getProductHistory(string productId): calling this view function would allow users to trace the origin, journey, and authenticity of a specific product. An example of this operation would look like this:

curl --location 'https://api.vottun.tech/core/v1/evm/transact/view' \
--header 'Authorization: Bearer <YOUR_API_KEY>' \
--header 'x-application-vkn: <YOUR_APPLICATION_ID>' \
--header 'Content-Type: application/json' \
--data-raw '{
"contractAddress": "0x0A212363e559D..BA20e5A3Aa0C",
"blockchainNetwork": 97,
"method": "getProductHistory",
"params": [
"4ca568a4-ae92-4cf8-bf69-626b2030a39c"
]
}'