# Fetch data from chain

## TS SDK Fetch data from chain <a href="#ts-sdk-fetch-data-from-chain" id="ts-sdk-fetch-data-from-chain"></a>

Once we created a new endless 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 Endless [RPC service](https://rpc.endless.link/v1/spec#/) or [Indexer](https://idx-test.endless.link/api/v1) as needed and ease the burden on the developer to know and understand what service they need to query.

```js
const endless = new Endless();
const alice = Account.generate();

// fund alice, make sure account is created on chain

const info = await endless.getAccountInfo({ accountAddress: alice.accountAddress });
const txs = await endless.getAccountTransactions({ accountAddress: alice.accountAddress });
const resources = await endless.getAccountResources({ accountAddress: alice.accountAddress });
```

### Queries with generics <a href="#queries-with-generics" id="queries-with-generics"></a>

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.

```js
const resource = await endless.getAccountResource<Coin>({
  accountAddress: alice.accountAddress,
  resourceType: "0x1::account::Account",
});

// Now we have access to the response type property
const authKey = resource.authentication_key;
```

### `options` input argument <a href="#options-input-argument" id="options-input-argument"></a>

We can provide queries with an `options` input as query parameters. For those queries that support this option, an `option` input param is available

```js
const resource = await endless.getAccountResource({
  accountAddress: alice.accountAddress,
  resourceType: "0x1::account::Account",
  options: { ledgerVersion: 12 },
});

const tokens = await endless.getAccountOwnedTokens({
  accountAddress: alice.accountAddress,
  options: {
    tokenStandard: "v2",
    pagination: { offset: 0, limit: 10 },
    orderBy: [{ last_transaction_version: "desc" }],
  },
});
```

### Wait for Indexer to sync up <a href="#wait-for-indexer-to-sync-up" id="wait-for-indexer-to-sync-up"></a>

Sometimes we use Indexer service to fetch data, this is because we can not get complex data direct from fullnode or some queries are not supported with the fullnode API. Since Indexer indexes the chain, it might take it some time to catch up with the latest ledger version, and we can end up not getting the real time data.

For that, the SDK supports an optional input argument `minimumLedgerVersion`. We can pass a ledger version to sync up to, before querying. If no version provided, the SDK will not wait for Indexer to sync up.

```js
const tokens = await endless.getAccountOwnedTokens({
  accountAddress: alice.accountAddress,
  minimumLedgerVersion: 1234,
});
```

To get the latest ledger version we can

1. Query for the ledger info

```js
const ledgerInfo = await endless.getLedgerInfo();

const ledgerVersion = ledgerInfo.ledger_version;
```

1. If we just committed a transaction with the SDK, we can use `waitForTransaction` method, that would return us a `CommittedTransactionResponse` that holds the latest ledger version

```js
const response = await endless.waitForTransaction({
  transactionHash: pendingTransaction.hash,
});

const tokens = await endless.getAccountOwnedTokens({
  accountAddress: alice.accountAddress,
  minimumLedgerVersion: BigInt(response.version),
});
```

### Use namespace <a href="#use-namespace" id="use-namespace"></a>

The Endless class holds different namespaces related to the query operation we seek to do. For example, all `account` related queries are under the `endless.account` namespace. Once we initiate the `Endless` class, all namespaces will be available for as with autocomplete along with all the possible API functions.

Thought we don't need to specify the namespace when making a query, it can be beneficial while developing.

```js
const endless = new Endless()
endless.< list of available API functions and namespaces >
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.endless.link/endless/devbuild/build/endless-sdks/typescript-sdk/fetch-data-from-chain.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
