DogeChain is an Ethereum Virtual Machine (EVM) compatible network that has a JSON RPC API you can use to retrieve transaction data, blocks, and much more from its blockchain. If you are an Ethereum developer, these commands will look familiar. If not, have no fear. This article has you covered!

How does the DogeChain JSON RPC API work? A DogeChain node stores a copy of the blockchain and provides methods you can use to retrieve the data using standard HTTP POST requests that you would send to the RPC API. POST requests are used for this instead of GET requests because they are more secure.

The flow of data looks like this: DogeChain Node’s Copy Of The Blockchain >> JSON RPC API >> Your App.

In this article, i’ll show you how to send requests like those mentioned above using Node.js. Afterwards, you can make your Node.js app provide the data to a mobile app or website if you wish.

Prerequisites

Install Node.js on Ubuntu or Debian (the example code herein was tested with Node.js version 10.19.0, although it will work with many other versions):

sudo apt install nodejs

If you use freeBSD:

pkg install node

Instructions for RedHat

Install npm:

sudo apt install npm

Create a directory called dogerpc and enter it, then install the required dependency called ‘request-promise’ which we will use to build our HTTP requests and send them off to the DogeChain RPC API:

npm install request-promise

Retrieve The Details Of A DogeChain Transaction

Create a file in the same dogerpc directory called mydogeapp.js, and start it off with the following sample code:

const rp = require('request-promise');

const rpcuri = "https://rpc-testnet.dogechain.dog"; //This is the node we're sending the HTTP request to

function getTransactionByHash(hash) {
    const requestOptions = {
        method: 'POST',
        uri: rpcuri,
        body: {
            "jsonrpc":"2.0","method":"eth_getTransactionByHash","params":[hash],"id":1
        },
        json: true,
        gzip: true
    };
    rp(requestOptions).then(response => {
        console.log(response); //Response contains the blockchain data you requested, if it exists.
    }).catch((err) => {
        console.log('API call error:', err.message);
    });
}

getTransactionByHash('0x5de411b96b3e264227d690d9af7e98e2738cff50c879b77793028e614b8b948e'); //A real transaction hash on the testnet, so you won't have to find one.

The ‘rpcuri’ variable contains whichever node you’ve chosen to use. You can change it to whichever node you like, including your own.

Note the method used, which is ‘eth_getTransactionByHash’. The method field tells the RPC server what type of data you are requesting. In this case, you are requesting the details of a transaction, and ‘by hash’ means that you’re stating your intent to search for the transaction using its hash. The server will send you details about the quantity sent in that transaction (the ‘value’ field), the gas fee paid, and much more.

The ‘params’ field tells the server exactly which data you are searching for, and in this case it must be a transaction hash — as stated by the ‘method’ field.

Learn how functions work

The server should respond with the following data:

{ jsonrpc: '2.0',
  id: 1,
  result:
   { nonce: '0xe4f',
     gasPrice: '0x174876e800',
     gas: '0x1c9c380',
     to: '0xE7FfFd87c82a84167DbEa69915a5A7e6B772e3E4',
     value: '0x8ac7230489e80000',
     input: '0x',
     v: '0x493',
     r:
      '0x4f65e8f3b0c942ce645d4410718ec2193a51e84d1970a8e51de9793f8d5f1804',
     s:
      '0x8b6ac48f5ca1e14ab04b74e105f06dffb9196fde29da6dc7647c099fc4e2abd',
     hash:
      '0x5de411b96b3e264227d690d9af7e98e2738cff50c879b77793028e614b8b948e',
     from: '0x4F87e677D1244Fc776E62289e8E4d44Ff51b969f',
     blockHash:
      '0x922e80cf4f376abfbe4d62d08681c70970aa6d347cf8ad2908045093bd0a670a',
     blockNumber: '0x16f158',
     transactionIndex: '0x0' } }

Parsing: If, for example you just wanted to find the amount sent in the transaction, you could change the ‘console.log(response);‘ line to: ‘console.log(response.result.value);‘.

How To Retrieve A DogeChain Block

You can retrieve the data contained in a DogeChain block (eg. transactions, miner details, size, difficulty, and much more) using the ‘eth_getBlockByHash’ method, as shown below:

const rp = require('request-promise');

const rpcuri = "https://rpc-testnet.dogechain.dog"; //This is the node we're sending the HTTP request to

function getBlockByHash(hash) {
    const requestOptions = {
        method: 'POST',
        uri: rpcuri,
        body: {
            "jsonrpc":"2.0","method":"eth_getBlockByHash","params":[hash, false],"id":67
        },
        json: true,
        gzip: true
    };
    rp(requestOptions).then(response => {
        console.log(response);
    }).catch((err) => {
        console.log('API call error:', err.message);
    });
}

getBlockByHash('0x922e80cf4f376abfbe4d62d08681c70970aa6d347cf8ad2908045093bd0a670a');

The code samples above are very similar because they are simply sending your desired methods and parameters to the JSON RPC server. There are other methods you can use to retrieve other kinds of blockchain data using the code samples above as a template, including but not limited to:

  • eth_getBlockByNumber
  • eth_gasPrice
  • eth_getBalance
  • eth_sendTransaction

The server’s response should look like this:

{ jsonrpc: '2.0',
  id: 67,
  result:
   { parentHash:
      '0x5f0f22a2aa5c228e04680ab4d819947b7f513d162f52778987feb288a8acd0e4',
     sha3Uncles:
      '0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347',
     miner: '0x7945EB690589dCfd424457E030234AD8f7204FC4',
     stateRoot:
      '0xac209a6df6c6c2f23afb03cc8a7e3925c81920035bb2bcec451ea409a73d9720',
     transactionsRoot:
      '0x57a5e4b4beea95f45d0f397957b5b1444b173cdfaa4f7657fceb74e254259760',
     receiptsRoot:
      '0x056b23fbba480696b65fe5a59b8f2148a1299103c4f57df839233af2cf4ca2d2',
     logsBloom:
      '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
     difficulty: '0x16f158',
     totalDifficulty: '0x16f158',
     size: '0x403',
     number: '0x16f158',
     gasLimit: '0x1c9c380',
     gasUsed: '0x5208',
     timestamp: '0x62994f43',
     extraData:
      '0x0000000000000000000000000000000000000000000000000000000000000000f90164f8549421da36cb4fe7b11ee84c560084c45c0e8886e42b94b113213506bfc320ad0ab9f2992d3129224b22b8947945eb690589dcfd424457e030234ad8f7204fc494a2ad008adedbe39b2b1b06fe5040e11bef0c26afb841b1e9a881f2708a9eb29461cd68b155ed131cfe5e892f6ce9ec6f857a5b2b8d4f6be8e85123fc120668ed4a584e1eff2d2d89aad8e30c59f12e0f8ad86ebb6fe301f8c9b841ef86cd8e67309bfd4ccf0d0b1c6a548658d1fd0521673bc7cb9c3fa804fa717f0e8bba47d7904f081e2ddcc23bdaa5d0b606a3a4ff936cf09bddd827df3285ca00b841ec49b896d4b489624cfaf769e3397a99dd65f6438041e75040925874ed82d5d91c0e144b6beddfd99eeb60289d550371009370423ac1d6df75362cf253a1395901b841c0d3b8de2a8ea107e29c045e695af679088a78e0da9575963b6b0f531bbf5f4a0eb2b0f72e42dd4d0c9117aff22f393883e4a66cb71e4671adc8ffceef4033d901',
     mixHash:
      '0x63746963616c2062797a616e74696e65206661756c7420746f6c6572616e6365',
     nonce: '0x0000000000000000',
     hash:
      '0x922e80cf4f376abfbe4d62d08681c70970aa6d347cf8ad2908045093bd0a670a',
     transactions:
      [ '0x5de411b96b3e264227d690d9af7e98e2738cff50c879b77793028e614b8b948e' ],
     uncles: [] } }

The code examples above can be easily implemented in the form of a module to use in your apps or bots.

Node.js Code Examples Tested with:

  • Node.js version 10.19.0.
  • request-promise version 4.2.5.

Python Code Sample: Get The Contents Of A DogeChain Block

Create a file named dctest.py, paste the following into it and run it with Python 3. The ‘method’ and ‘params’ fields work the same as they do in the examples above. Feel free to change them to use whichever other method you’d like to use from the DogeChain JSON RPC API.

import requests

nodeurl = 'https://rpc-testnet.dogechain.dog' #Address/url of the node
requestbody = {"jsonrpc":"2.0","method":"eth_getBlockByHash","params":["0x922e80cf4f376abfbe4d62d08681c70970aa6d347cf8ad2908045093bd0a670a"],"id":67}
#Replace the '0x' string highlighted in blue above with the hash of the block that you're seeking.
response = requests.post(nodeurl, json = requestbody) #Sends off the request to the node and saves the response in 'response'

print(response.text) #Prints the response sent by the DogeChain node to the command line

Python Code Example Tested With:

  • Python version 3.10.4.
  • Requests version 2.25.1.