Endless
  • 🚀README
  • Discovery
    • 🚀Endless Web3 Genesis Cloud
    • 💎Business Model
    • 🎯Vision
    • ✈️Roadmap
    • 🪙Economics
    • 👤Team
      • Yu Xiong
      • Amit Kumar Jaiswal
      • Ned
      • 0xfun
      • Scott Trowbridge
      • Neeraj Sharma LLB
      • Amjad Suleman
      • Binu Paul
      • Eduard Romulus GOEAN
    • ❤️Developer Community
  • Endless Chain
    • Tech Docs
      • Account Address Format
      • Endless Account
      • Endless Coin(EDS)
      • Sponsored Transaction
      • On-Chain Multisig
      • Randomness
      • Safety Transaction
      • Token Locking & Distribution
    • Start
      • Learn about Endless
        • Accounts
        • Resources
        • Events
        • Transactions and States
        • Gas and Storage Fees
        • Computing Transaction Gas
        • Blocks
        • Staking
          • Delegated Staking
        • Governance
        • Endless Blockchain Deep Dive
          • Validator Nodes Overview
          • Fullnodes Overview
          • Node Networks and Synchronization
        • Move - A Web3 Language and Runtime
      • Explore Endless
      • Latest Endless Releases
      • Networks
    • Build
      • Tutorials
        • Your First Transaction
        • Your First Fungible Asset
        • Your First NFT
        • Your First Move Module
        • Your First Multisig
      • Learn the Move Language
        • The Move Book
          • Getting Started
            • Introduction
            • Modules and Scripts
          • Primitive Types
            • Move Tutorial
            • Integers
            • Bool
            • Address
            • Vector
            • Signer
            • References
            • Tuples and Unit
          • Basic Concepts
            • Local Variables and Scope
            • Equality
            • Abort and Assert
            • Conditionals
            • While, For, and Loop
            • Functions
            • Structs and Resources
            • Constants
            • Generics
            • Abilities
            • Uses and Aliases
            • Friends
            • Packages
            • Package Upgrades
            • Unit Tests
          • Global Storage
            • Global Storage - Structure
            • Global Storage - Operators
          • Reference
            • Libraries
            • Move Coding Conventions
        • Advanced Move Guides
          • Objects
            • Creating Objects
            • Configuring objects
            • Using objects
          • Move Scripts
            • Writing Move Scripts
            • Compiling Move Scripts
            • Running Move Scripts
            • Move Scripts Tutorial
          • Resource Accounts
          • Modules on Endless
          • Cryptography
          • Gas Profiling
          • Security
      • Endless Standards
        • Object
        • Endless Fungible Asset Standard
        • Endless Digital Asset Standard
        • Endless Wallet Standard
      • Endless APIs
        • Fullnode Rest API
        • Indexer Restful API
          • Indexer Installation
        • GRPC Transaction Stream
          • Running Locally
          • Custom Processors
            • End-to-End Tutorial
            • Parsing Transactions
          • Self-Hosted Transaction Stream Service
      • Endless SDKs
        • TypeScript SDK
          • Account
          • SDK Configuration
          • Fetch data from chain
          • Transaction Builder
          • HTTP Client
          • Move Types
          • Testing
          • Typescript
        • Rust SDK
        • Go SDK
      • Endless CLI
        • Install the Endless CLI
          • Install On Mac
          • Install On Alibaba Cloud
          • Install On Linux
          • Install On Windows
        • CLI Configuration
        • Use Endless CLI
          • Working With Move Contracts
            • Arguments in JSON Tutorial
          • Trying Things On-Chain
            • Look Up On-Chain Account Info
            • Create Test Accounts
          • Running A Local Network
            • Running a Public Network
          • Managing a Network Node
      • Integrate with Endless
        • Endless Token Overview
        • Application Integration Guide
      • Endless VSCode extension
      • Advanced Builder Guides
        • Develop Locally
          • Running a Local Network
          • Run a Localnet with Validator
    • Nodes
      • Learn about Nodes
      • Run a Validator and VFN
        • Node Requirements
        • Deploy Nodes
          • Using Docker
          • Using AWS
          • Using Azure
          • Using GCP
        • Connect Nodes
          • Connect to a Network
        • Verify Nodes
          • Node Health
          • Validator Leaderboard
      • Run a Public Fullnode
        • PFN Requirements
        • Deploy a PFN
          • Using Pre-compiled Binary
          • Using Docker
          • Using GCP 🚧 (under_construction)
        • Verify a PFN
        • Modify a PFN
          • Upgrade your PFN
          • Generate a PFN Identity
          • Customize PFN Networks
      • Bootstrap a Node
        • Bootstrap from a Snapshot
        • Bootstrap from a Backup
      • Configure a Node
        • State Synchronization
        • Data Pruning
        • Telemetry
        • Locating Node Files
          • Files For Mainnet
          • Files For Testnet
          • Files For Devnet
      • Monitor a Node
        • Node Inspection Service
        • Important Node Metrics
        • Node Health Checker
    • Reference
      • Endless Error Codes
      • Move Reference Documentation
      • Endless Glossary
    • FAQs
  • Endless Bridge
    • Intro to Endless Bridge
    • How to use bridge
    • Liquidity Management
    • Faucet
    • Developer Integration
      • Contract Integration
        • Message Contract
        • Execute Contract
      • Server-Side Integration
        • Message Sender
        • Example of Message Listener Service (Rust)
        • Example of Token Cross-Chain (JS)
  • Endless Wallet
    • User Guide
    • Basic Tutorial
    • FAQs
    • MultiAccount
    • SDK
      • Functions
      • Events
  • GameFi
    • Intro
    • GameFi & Endless
  • Endless Modules
    • Stacks
    • Storage
    • Module List
  • Endless Ecosystem
    • Intro
    • Show Cases
    • App Demo
  • Whitepaper
  • Endless SCAN
    • User Guide
  • MULTI-SIGNATURE
    • Multi-Signature User Guide
  • Regulations
    • Privacy Policy
    • Terms of Service
    • Funding Terms - Disclaimer
Powered by GitBook
On this page
  • TS SDK Fetch data from chain
  • Queries with generics
  • options input argument
  • Wait for Indexer to sync up
  • Use namespace
Export as PDF
  1. Endless Chain
  2. Build
  3. Endless SDKs
  4. TypeScript SDK

Fetch data from chain

PreviousSDK ConfigurationNextTransaction Builder

Last updated 6 months ago

TS SDK Fetch data from chain

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 or as needed and ease the burden on the developer to know and understand what service they need to query.

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

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.

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

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 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

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.

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

To get the latest ledger version we can

  1. Query for the ledger info

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

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

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

Use namespace

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.

const endless = new Endless()
endless.< list of available API functions and namespaces >
RPC service
Indexer