4

In the Aptos Move docs, it explains how to interact with a smart contract which has exposed "entry functions".In the hello_blockchain example, set_message is used.

Move modules expose access points, also referred as entry functions. These access points can be called via transactions. The CLI allows for seamless access to these access points. The example Move module hello_blockchain exposes a set_message entry function that takes in a string. This can be called via the CLI:

However, there is no explanation on how to query the get_message function which to my understanding is akin to a read function.

Furthermore, there is no explanation of how to query read/write functions using the Python SDK.

Two questions:

  1. Is it possible to use the Python SDK to query read/write functions in a Move module?
  2. How do you define a read function in a Move module?
TylerH
  • 20,799
  • 66
  • 75
  • 101
Jolow
  • 41
  • 1

3 Answers3

5

If you want to read a resource on an account, you would submit a read request to the API. For example, with curl:

curl https://fullnode.mainnet.aptoslabs.com/v1/accounts/<addr>/resource/<resource>

A concrete example of this:

curl https://fullnode.mainnet.aptoslabs.com/v1/accounts/0x00ffe770ccae2e373bc1f217585a1f97b5fa003cc169a27e1b4d6bfc8d3b243b/resource/0x3::token::TokenStore

This is equivalent to:

Read the resource 0x3::token::TokenStore at account 0x00ffe770ccae2e373bc1f217585a1f97b5fa003cc169a27e1b4d6bfc8d3b243b.

In the Python SDK, you would do something like this:

client.account_resource(
    "0x00ffe770ccae2e373bc1f217585a1f97b5fa003cc169a27e1b4d6bfc8d3b243b",
    "0x3::token::TokenStore",
)

This uses this client method: https://github.com/aptos-labs/aptos-core/blob/05d04ecc511f572380e1e8fe0bbc234f30645f0d/ecosystem/python/sdk/aptos_sdk/client.py#L63

The get_message function in the hello_blockchain example is somewhat misleading (we can improve this). There is a hint though, note that only entry functions can be run from external calls (e.g. using the CLI command aptos move run). All other functions can only be called from within a Move module.

To be even clearer: In order to read from the Aptos blockchain, you must make requests to the read API endpoints, not to "read functions" in Move modules.

For more info, check out these docs: https://aptos.dev/tutorials/your-first-transaction.

Daniel Porteous
  • 5,536
  • 3
  • 25
  • 44
  • Hi Daniel thanks for the response and I apologise for not being more clear. I understand that you can use the API to query a resource of an Aptos account. However, I want to execute a function defined in a move module for read purposes i.e. off-chain analysis (without paying gas fees). My question is whether this is even possible in Aptos. Thanks! – Jolow Oct 28 '22 at 21:35
  • 1
    In order to call a function in a Move module, you need to submit a transaction. Transactions on mainnet do have a gas cost. Note that the point of a transaction is strictly to create some kind of output that alters the state of the blockchain. Specifically, when a Move module entry function is run, it generates WriteSets that then get applied to storage. Even if you had a "read" function in a Move module, there is no way for you to get the output of calling that function. Simply put, transactions are for writing, not reading. – Daniel Porteous Oct 28 '22 at 22:10
  • What if the information relating different resources together is in a function? Let me give a simple example to illustrate my point. The goal is to find `user_shares` which is `total_shares` divided by `num_tokens` which the user owns. `user_shares` is not stored as a resource, but it can be computed from the other two resources. however, an off-chain user may want to use a function defined by the contract as there may be cases where the relationship between multiple resources is not obvious. In this case, is there anyway to simulate the transaction to get the output without gas? – Jolow Nov 02 '22 at 01:22
  • Unfortunately there is no way to do this via the contract. Instead you'd probably want to define some standard API that offers this information for the user. I realize this seems to add centralization to the otherwise decentralized system, but note that really this is no different from reading information from the blockchain via a fullnode, since in both cases, you're trusting that the server you're talking to is legitimate (both could easily return faked data). The alternative to this approach is to roll an SDK in some common languages that could provide this functionality. – Daniel Porteous Nov 02 '22 at 02:44
  • Feel free to reach out to discuss this issue further by the way! – Daniel Porteous Nov 02 '22 at 02:45
  • I opened an issue for us to clear this up: https://github.com/aptos-labs/aptos-core/issues/5420. – Daniel Porteous Nov 02 '22 at 21:48
0

Looks like you're looking for view functions. Currently, there is no way to query a read function from a move module.

There is an open Github feature request for this on the Aptos repo: https://github.com/aptos-labs/aptos-core/issues/4915

Greg
  • 1
  • 1
0

To simulate a read-only function on the Aptos, we recently built a tool: https://github.com/sentioxyz/sentio-composer. If there's a view function defined in your module, no matter whether it is an entry function or not, you can call it using this tool with the real on-chain data.

For example, to view the balance of an account, you can simulate the balance function using the CLI tool:

# command
view-function \
--function-id 0x1::coin::balance \
--type-args 0x1::aptos_coin::AptosCoin \
--args 0x21ddba785f3ae9c6f03664ab07e9ad83595a0fa5ca556cec2b9d9e7100db0f07 \
--ledger-version 35842267 \
--network mainnet
# output
{
  "log_path": "",
  "return_values": [
    3120544100
  ]
}

Quick web demo at: http://composer.sentio.xyz/