Developer Resources
Block ExplorerGitHubRun a NodeMigration GuideETN Testnet Faucet
  • Overview
  • Foundational topics
    • Intro to the Electroneum Smart Chain
    • Intro to ETN
    • Intro to dapps
    • Web2 vs Web3
    • Accounts
    • Transactions
    • Blocks
    • Electroneum Virtual Machine (EVM)
      • Opcodes
    • Gas and Fees
    • Nodes and clients
    • Networks
    • Consensus mechanisms
      • IBFT
  • Electroneum Stack
    • Intro to the stack
    • Smart contracts
    • Development networks
    • Development frameworks
    • Electroneum client APIs
      • JavaScript APIs
      • JSON-RPC
    • Storage
    • Integrated Development Environments (IDEs)
    • Metamask
    • Rabby Wallet
    • Trust Wallet
  • Advanced
    • Bridges
    • Standards
      • Token standards
        • ERC-20 Fungible Tokens
        • ERC-721 NFTs
    • Oracles
    • Networking layer
      • Network addresses
    • Data structures and encoding
      • Patricia Merkle Trie
      • Recursive-length prefix (RLP)
      • Web3 secret storage definition
  • Design fundamentals
    • Intro to design and UX
  • ETN-SC Client
    • Getting started
      • Introduction
      • Hardware requirements
      • Instaling ETN-SC
    • Fundamentals
      • Command-line options
      • Security
      • Sync-modes
      • Account management
      • Databases
      • Backup & restore
      • Logs
      • Connecting to peers
      • Pruning
      • Private networks
      • Config files
      • Light client
    • Interacting with ETN-SC
      • JSON-RPC Server
        • Batch requests
        • Real-time events
      • JSON-RPC Namespaces
        • admin
        • clique
        • debug
        • eth
        • istanbul
        • les
        • miner
        • net
        • personal
        • txpool
      • JS Console
      • JS Console 2: Contracts
      • GraphQL Server
    • Developers
      • Introduction
      • Dapp developers
        • Dev mode
        • Go API
        • Go Account Management
        • Go Contract Bindings
      • EVM tracing
        • Introduction
        • Basic traces
        • Built-in tracers
        • Custom EVM tracer
        • Tutorial for JavaScript tracing
      • ETN-SC developer
        • Developer guide
        • Disclosures
        • DNS discovery setup guide
        • Code review guidelines
      • Contributing
    • Monitoring
      • Creating Dashboards
      • Understanding Dashboards
      • Ethstats
      • Metrics
    • Tools
      • Clef
        • Introduction
        • APIs
        • Rules
        • Setup
        • Datatypes
        • Tutorial
        • Clique-signing
      • abigen
      • devp2p
    • FAQs
  • Migration to Smart Chain
    • Overview
    • How to Migrate
      • ETN Online Wallets
      • Paper Wallets
      • CLI Wallet Users
      • Exchange Holders
      • Exchanges
    • Bridge Smart Contract
  • Misc. Guides
    • Set up Ledger + Metamask
  • MY.ELECTRONEUM.COM
    • Vendor API
Powered by GitBook
On this page
  • GraphiQL
  • Query
  • Accessing state

Was this helpful?

  1. ETN-SC Client
  2. Interacting with ETN-SC

GraphQL Server

PreviousJS Console 2: ContractsNextDevelopers

Last updated 1 year ago

Was this helpful?

In addition to the , Etn-sc supports the GraphQL API as specified by . GraphQL lets you specify which fields of an objects you need as part of the query, eliminating the extra load on the client for filling in fields which are not needed. It also allows for combining several traditional JSON-RPC requests into one query which translates into less overhead and more performance.

The GraphQL endpoint piggybacks on the HTTP transport used by JSON-RPC. Hence the relevant --http flags and the --graphql flag should be passed to Etn-sc:

etn-sc --http --graphql

Now queries can be raised against http://localhost:8545/graphql. To change the port, provide a custom port number to --http.port, e.g.:

etn-sc --http --http.port 9545 --graphql

GraphiQL

An easy way to try out queries is the GraphiQL interface shipped with Etn-sc. To open it visit http://localhost:8545/graphql/ui. To see how this works let's read the sender, recipient and value of all transactions in block number 6000000. In GraphiQL:

query txInfo {
  block(number: 6000000) {
    transactions {
      hash
      from {
        address
      }
      to {
        address
      }
      value
    }
  }
}

GraphiQL also provides a way to explore the schema Etn-sc provides to help you formulate your queries, which you can see on the right sidebar. Under the title Root Types click on Query to see the high-level types and their fields.

Query

For example, the code snippet below shows how to obtain the latest block number using Curl. Note the use of a JSON object for the data section:

❯ curl -X POST http://localhost:8545/graphql -H "Content-Type: application/json" --data '{ "query": "query { block { number } }" }'
{"data":{"block":{"number":"0x5b9d65"}}}

Alternatively store the JSON-ified query in a file (let's call it block-num.query) and do:

❯ curl -X POST http://localhost:8545/graphql -H "Content-Type: application/json" --data '@block-num.query'

Executing a simple query in JS looks as follows. Here the lightweight library graphql-request is used to perform the request. Note the use of variables instead of hardcoding the block number in the query:

const { request, gql } = require('graphql-request');

const query = gql`
  query blockInfo($number: Long) {
    block(number: $number) {
      hash
      stateRoot
    }
  }
`;
request('http://localhost:8545/graphql', query, { number: '6004067' })
  .then(res => {
    console.log(res);
  })
  .catch(err => {
    console.log(err);
  });

Accessing state

The schema allows for querying parts of state, i.e. accounts and their storage slots. E.g. it is possible to get the balance of the sender of a tx via:

transaction(hash: "0xdad") {
    from {
        balance
    }
}

It is important to note however that the balance returned here is not the balance at the given transaction, rather it is the latest balance of the sender account, i.e. at the head of the chain. It is possible to query the state of this account at a particular block N via:

transaction(hash: "0xdad") {
    from(block: 6004067) {
        balance
    }
}

Reading out data from Etn-sc is the biggest use-case for GraphQL. In addition to using the UI queries can also be sent programmatically. The official GraphQL explain how to find bindings for many languages, or send http requests from the terminal using tools such as Curl.

As you can see this effect takes in a block number parameter which instructs Etn-sc to return the state of this account from an older block. The node needs to have the state for that block persisted, otherwise this query will result in an error. To see how Etn-sc persists state please see this .

JSON-RPC APIs
EIP-1767
docs
page