Reputation: 51
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:
Upvotes: 5
Views: 1404
Reputation: 36
View functions are fully supported on mainnet today, you can use ts-sdk to call it like this
const result = await aptos.view({
payload: {
function: "module_address::module_name::view_function_name",
typeArguments: [],
functionArguments: [arg1],
},
});
Upvotes: 1
Reputation: 11
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/
Upvotes: 0
Reputation: 1
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
Upvotes: 0
Reputation: 6333
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 account0x00ffe770ccae2e373bc1f217585a1f97b5fa003cc169a27e1b4d6bfc8d3b243b
.
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.
Upvotes: 5