TS SDK Fetch data from chain
Once we created a new Aptos instance, we get access to all the sdk functionality. We now can query the chain for data.
The SDK provides built in queries to easily query the chain with most used or popular queries. The SDK resolves those queries to Aptos fullnode or Indexer as needed and ease the burden on the developer to know and understand what service they need to query.
const aptos = new Aptos();
const fund = await aptos.getAccountInfo({ accountAddress: "0x123" });
const modules = await aptos.getAccountTransactions({ accountAddress: "0x123" });
const tokens = await aptos.getAccountOwnedTokens({ accountAddress: "0x123" });
Queries with generics
Some query responses do not provide the full response type as the SDK can't infer the actual type. For that we might want to provide a generic type for the response type, so we can access the response properties that are not included in the API type.
For example, for the getAccountResource
query we can define the resource
to query but the SDK can't infer the response type, and we can't have access to the response properties.
For that we support generic response types for different queries.
type Coin = { coin: { value: string } };
const resource = await aptos.getAccountResource<Coin>({
accountAddress: testAccount.accountAddress,
resourceType: "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>",
});
// Now we have access to the response type property
const value = resource.coin.value;
options
input argument
We can provide queries with an options
input as query parameters. For those queries that support this option, an option
input param is available
const resource = await aptos.getAccountResource({
accountAddress: alice.accountAddress,
resourceType: "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>",
options: { ledgerVersion: 12 },
});
const tokens = await aptos.getAccountOwnedTokens({
accountAddress: alice.accountAddress,
options: {
tokenStandard: "v2",
pagination: { offset: 0, limit: 10 },
orderBy: [{ last_transaction_version: "desc" }],
},
});