4. Fetch Data from Chain
In the fourth chapter of the tutorial on building an end-to-end dapp on Aptos, you will be learning to fetch data from chain.
Our UI logic relies on whether the connected account has created a todo list. If the account has created a todo list, our app should display that list; if not, the app should display a button offering the option to create a new list.
For that, we first need to check if the connected account has a TodoList
resource. In our smart contract, whenever someone creates a todo list we create and assign a TodoList
resource to their account.
To fetch data from chain, we can use the Aptos TypeScript SDK. The SDK provides classes and functions for us to easily interact and query the Aptos chain.
To get started:
- Stop the local server if running.
- In the
client
directory, run:npm i @aptos-labs/ts-sdk
- In the
App.tsx
file, import theAptos
class like so:
import { Aptos } from "@aptos-labs/ts-sdk";
The TypeScript SDK provides us with a Aptos
class which is the main entry point into Aptos's API. By initializing Aptos
we can query the Aptos chain.
Read more about the Aptos
class in the Aptos TypeScript SDK docs.
- In the
App.tsx
file, add:
const aptos = new Aptos();