Cover Image for Cosmos API: Access Blockchain Data Seamlessly

Cosmos API: Access Blockchain Data Seamlessly

ERC
SPL
Solana
Ethereum

Cosmos is a decentralized network of independent parallel blockchains, each powered by consensus algorithms like Tendermint. It's often called the "Internet of Blockchains" because its primary goal is to enable different blockchains to communicate in a decentralized way, thereby solving the interoperability problem in the blockchain space. Cosmos aims to create an ecosystem of blockchains that can programmatically share data and tokens without intermediaries.

How Is Cosmos Different from Bitcoin and Ethereum?

The Cosmos API, as part of the Cosmos blockchain ecosystem, differs significantly from Bitcoin and Ethereum in several key aspects, mainly due to its underlying architecture, purpose, and capabilities. Here’s a breakdown of these differences:

  • Purpose and Design Philosophy

    • Cosmos: Designed to solve scalability and interoperability issues in the blockchain space. Cosmos enables different blockchains to communicate and transact with each other seamlessly through the Inter-Blockchain Communication (IBC) protocol. It’s a network of blockchains that can scale and interoperate while maintaining sovereignty.
    • Bitcoin: Primarily serves as a decentralized digital currency. Its main goal is to enable peer-to-peer transactions without a central authority. Bitcoin uses a proof-of-work (PoW) consensus mechanism, focusing on security and decentralization.
    • Ethereum: Aims to be a global, decentralized platform for smart contracts and decentralized applications (DApps). Ethereum introduces the concept of a programmable blockchain that can execute scripts using an international network of public nodes.
  • Consensus Mechanism

    • Cosmos: Utilizes Tendermint BFT, a proof-of-stake (PoS) based consensus mechanism that is fast, scalable, and energy-efficient. Tendermint allows for instant finality, meaning it cannot be reversed once a block is created.
    • Bitcoin: Uses a proof-of-work (PoW) consensus mechanism, which requires miners to solve complex mathematical puzzles to validate transactions and create new blocks. This process is energy-intensive and can lead to slower transaction times during periods of congestion.
    • Ethereum: Currently transitioning from PoW to a PoS consensus mechanism with its Ethereum 2.0 upgrade. The shift aims to improve scalability, security, and sustainability.
  • Scalability

    • Cosmos: Designed with scalability in mind. By allowing each blockchain in the network (Zones) to process transactions independently and communicate via IBC, Cosmos can handle more transactions across its ecosystem without the bottlenecks in singular, monolithic blockchains.
    • Bitcoin and Ethereum: Both suffered from scalability issues initially due to their consensus mechanisms and the single-chain architecture. Ethereum’s transition to Ethereum 2.0 with shard chains attempts to address these issues.
  • Interoperability

    • Cosmos: The core feature of Cosmos is its built-in interoperability, facilitated by IBC. This allows different blockchains within the Cosmos ecosystem to transfer data and tokens directly without needing a central exchange or intermediary.
    • Bitcoin and Ethereum: Native interoperability is not built into Bitcoin or the current version of Ethereum, though third-party solutions and sidechains have been developed to facilitate some level of cross-chain interaction.
  • Development Framework

    • Cosmos: Offers the Cosmos SDK, a framework for building bespoke blockchains with specific use cases. This SDK simplifies the process of creating sovereign yet interoperable blockchains.
    • Ethereum: Provides a Solidity programming language and smart contract capabilities, enabling developers to build a wide range of DApps and decentralized autonomous organizations (DAOs) on a single blockchain platform.

Building With Cosmos API

Building applications with the Cosmos API involves interacting with the Cosmos blockchain and its various functionalities. Cosmos is known for its interoperability, scalability, and customizability, offering developers the tools to create interconnected blockchains. The Cosmos SDK and Cosmos Hub (Gaia) are central components of the Cosmos ecosystem, with the SDK providing the framework for building blockchain applications and Gaia serving as the first blockchain built with the SDK, acting as the main hub of the Cosmos network.

Here's a high-level guide on how to start building with the Cosmos API:

  • Cosmos SDK: Familiarize yourself with the Cosmos SDK, a framework for building blockchain applications in Go. It allows developers to create modular and interoperable blockchains.
  • Tendermint Core: Learn about Tendermint Core, the consensus engine that underpins the Cosmos network, enabling high-performance, secure, and consistent blockchain states across the Cosmos ecosystem.
  • Inter-Blockchain Communication (IBC): Understand the IBC protocol, which allows blockchains to communicate and transfer assets between each other, creating a network of interoperable blockchains.

Cosmos Blockchain Explorer API

To view the key statistics of the Cosmos on the Cosmos Hub Network, use the Bitquery Explorer. It provides a user-friendly interface for retrieving data including Adress, Blocks, Attributes, and a lot more.

Bitquery Cosmos Explorer Screenshot

The query is displayed in the GraphQL IDE as follows:

query ($network: CosmosNetwork!, $limit: Int!, $offset: Int!, $from: ISO8601DateTime, $till: ISO8601DateTime) {
  cosmos(network: $network) {
    blocks(
      options: {desc: "height", limit: $limit, offset: $offset}
      time: {since: $from, till: $till}
    ) {
      timestamp {
        time(format: "%Y-%m-%d %H:%M:%S")
      }
      height
      hash
      proposer {
        address
      }
    }
  }
}

In the upcoming sections, let’s discuss how we can use the BitQuery API to retrieve more information about the Cosmos API.

Cosmos API - Address

The Cosmos Address API from the BitQuery API provides information about a specific address on the Cosmos blockchain, like balance, token info, annotation etc.

Open the query in the IDE, and replace the address with a specific address, let’s say, cosmos1cx82d7pm4dgffy7a93rl6ul5g84vjgxk8xxrnv, and the network variable with “cosmoshub” as shown.

query MyQuery {
  cosmos(network: cosmoshub) {
    address(address: {in: "cosmos1cx82d7pm4dgffy7a93rl6ul5g84vjgxk8xxrnv"}) {
      address
      balance
      annotation
    }
  }
}

Cosmos API - Attributes

You can retrieve all the information about the attributes from the cosmos network using the attribute field in the query. Let’s try getting the attribute information over the cosmos newtork after the date, for example 1st January 2024.

Open the query in the IDE, and add the date in the date parameter and add the “desc” parameter with “"block.timestamp.iso8601". You can also filter the attributes by the even type as well.

query MyQuery {
  cosmos(network: cosmoshub) {
    attributes(date: {after: "2024-01-01"}, options: {desc: "block.timestamp.iso8601", limit: 10}) {
      attribute
      block {
        timestamp {
          iso8601
        }
      }
      eventType
      message {
        index
        senders
        success
        type
      }
      transaction {
        hash
        signer {
          address
        }
      }
    }
  }
}

The query is gives the details about specific attributes from the Cosmos blockchain (Cosmos Hub) for events that occurred after January 1, 2024. It returns information such as the attribute's type, related block timestamp in ISO 8601 format, event type, and the associated message and transaction details, including message index, senders, success status, message type, transaction hash, and signer's address.

Cosmos API - Blocks

You can easily retrieve specific details according to your needs about the blocks produced over the Cosmos Netwrok, from the Cosmos API.

Opne the IDE and write the query as shown below. Let’s search the block data after 1st January 2024 and limit our search to 10 results by putting the “limit” argument to 10.

query MyQuery {
  cosmos(network: cosmoshub) {
    blocks(
      date: {after: "2024-01-01"}
      options: {desc: "timestamp.iso8601", limit: 10}
    ) {
      hash
      header
      metadata
      proposer {
        address
      }
      timestamp {
        iso8601
      }
    }
  }
}

This query provides a snapshot of the most recent activity on the Cosmos blockchain, focusing on the latest 10 blocks added after January 1, 2024, including their hashes, headers, heights, metadata, the addresses of their proposers, etc. This information can be useful for tracking blockchain activity, understanding the sequence of block proposals, and analyzing the blockchain's current state.

Cosmos API - Coinpath

This Cosmos API helps you to study the money flow of a specific address on the Cosmos Blockchain. If you want to study the money flow according to your projects, you can make your own query.

Open the query in the IDE, and replace the address with the one shown.

query MyQuery {
  cosmos(network: cosmoshub) {
    coinpath(initialAddress: {is: "cosmos1cx82d7pm4dgffy7a93rl6ul5g84vjgxk8xxrnv"}) {
      amount(in: USD)
      any(of: time)
      block {
        height
        timestamp {
          iso8601
        }
      }
      receiver {
        address
      }
      sender {
        address
      }
    }
  }
}

This query is designed to extract detailed transaction data for a specific address on the Cosmos Hub, including the transaction amounts in USD, block height and timestamp, as well as the sender and receiver addresses for each transaction. This could be useful for analyzing a specific address's transaction history and patterns, tracking fund flows, or auditing purposes.

Cosmos API - Messages

The Cosmos Message API in Bitquery allows you to query message-related data on the Cosmos blockchain. It enables access to detailed information about messages, including senders, types, success status, and other attributes associated with transactions and events on the Cosmos network. This API is crucial for developers and analysts looking to extract and analyze specific message data, facilitating a deeper understanding of transaction dynamics and interactions within the Cosmos ecosystem.

To receive the messages within a specific block, open the IDE and use the following query, as shown below.

query MyQuery {
  cosmos(network: cosmoshub) {
    messages(blockHeight: {is: 16494924}) {
      index
      json
      senders
      success
      transaction {
        hash
        signer {
          address
        }
      }
      type
    }
  }
}

You can also retrieve messages based on the specific address, by entering your desired address and using the “transactionSigner” argument of the query.

Cosmos API - Transactions

Use the Cosmos Transactions API to retrieve the information related to the transactions created on the Cosmos Blockchain.

For example, let’s retrieve the details on the specific hash. Open the IDE, and use the following query.

query MyQuery {
  cosmos(network: cosmoshub) {
    transactions(
      hash: {is: "028b49185b0be6adf7b24804a3c28cd3a0a01c9b0ce0c6eb2e7dce1f5c0dc6b2"}
    ) {
      block {
        height
        timestamp {
          iso8601
        }
      }
      fee
      feeCurrency {
        name
        address
      }
      gasUsed
      index
      rawTx
      signer {
        address
      }
      type
    }
  }
}

This query is structured to provide a detailed view of recent transactions on the Cosmos Hub, focusing on their block placement, timing, fees, and the currency used for those fees. It's a targeted way to understand transaction dynamics, costs, and activities on the Cosmos Hub for a specific recent timeframe.

Cosmos API - Transfers

The Transfers API returns information about the transfers taking place over the Cosmos Network according to your needs. To retrieve information about the latest transfers that have taken place over the Cosmos Network, aftera particular date, let’s say 2nd February, consider the following query, in the IDE.

query MyQuery {
  cosmos(network: cosmoshub) {
    transfers(
      date: {after: "2024-02-05"}
      options: {desc: "block.timestamp.iso8601", limit: 10}
    ) {
      block {
        height
        timestamp {
          iso8601
        }
      }
      currency {
        address
        name
      }
    }
  }
}

Conclusion

Building with the Cosmos API using BitQuery involves utilizing GraphQL queries to access a wide range of data from the Cosmos blockchain, such as transactions, messages, blocks, and attributes. This process enables developers to create applications with real-time and historical blockchain data, enhancing their capabilities with detailed insights into blockchain events and entities. The API supports the development of various blockchain-based products by providing structured, reliable data from the Cosmos network, essential for analysis, monitoring, and integration into decentralized applications (dApps).

About Bitquery

Bitquery is a set of software tools that parse, index, access, search, and use information across blockchain networks in a unified way. Our products are:

  • Coinpath® APIs provide blockchain money flow analysis for more than 24 blockchains. With Coinpath’s APIs, you can monitor blockchain transactions, investigate crypto crimes such as bitcoin money laundering, and create crypto forensics tools. Read this to get started with Coinpath®.
  • Digital Assets API provides index information related to all major cryptocurrencies, coins, and tokens.
  • DEX API provides real-time deposits and transactions, trades, and other related data on different DEX protocols like Uniswap, Kyber Network, Airswap, Matching Network, etc.

If you have any questions about our products, ask them on our Telegram channel or email us at sales@bitquery.io. Also, subscribe to our newsletter below, we will keep you updated with the latest in the cryptocurrency world.

Subscribe to our newsletter

Subscribe and never miss any updates related to our APIs, new developments & latest news etc. Our newsletter is sent once a week on Monday.