Skip to content

qeth - A Multipurpose Script

With the introduction of OT Node version 2.0.50 you need your own RPC Server (link) to interact with the Ethereum Mainnet. This link is set in the OT Node config file ~/.origintrail_noderc.

The nice thing about this link is it can also be used in own scripts to interact with Ethereum Mainnet.

The script (let's call it qeth - query ethereum) will have following features:

Command Explanation
qeth ow Shows ETH balance of operational wallet
qeth mw Shows ETH balance of management wallet
qeth 0x... Shows ETH balance of specific ETH address
qeth gas Shows config file gas price (if set) and current estimated "safe" gas price in gwei
qeth block Query current block number

And there are some additional benefits. If those commands provide correct values, it assures that

  • the syntax of the config file is right
  • the RPC Server link works properly
  • the wallet addresses are correct
  • and you used the correct number of zeros for the gas price

Those checks are especially helpful before setup a node or after editing the config file but also if you just want to check if the RPC link works.

#!/bin/bash

#https://github.com/ethereum/wiki/wiki/JSON-RPC

function queryeth()
{
  RESULT=$(curl -s $RPC -H "Content-Type: application/json" -X POST \
  -d '{"id":1,"jsonrpc":"2.0","method":"'$METHOD'","params":['$PARAM']}'|jq -r ".result")
}

OTCONFIG=~/.origintrail_noderc
if  [[ ! -f $OTCONFIG ]]; then
  echo
  echo "File $OTCONFIG not found!"
  echo
  exit 1
fi

RPC=$(jq -r ".blockchain.rpc_server_url" $OTCONFIG)

if [[ "$RPC" = 'null' ]]; then
  echo
  echo "rpc_server_url in file $OTCONFIG missing!"
  echo
  exit 1
fi

#RPC="https://ethereum.api.nodesmith.io/v1/mainnet/jsonrpc?apiKey=..."

echo
par=${1,,}
case "$par" in

  ow)
  OW=$(jq -r ".node_wallet" $OTCONFIG)
  METHOD="eth_getBalance"
  PARAM='"'$OW'","latest"'
  queryeth
  echo "Operational Wallet: $OW"
  echo $(($RESULT))|awk '{printf "    Balance in ETH: %.8g\n", $1/1000000000000000000}'
  ;;

  mw)
  MW=$(jq -r ".management_wallet" $OTCONFIG)
  METHOD="eth_getBalance"
  PARAM='"'$MW'","latest"'
  queryeth
  echo "Management Wallet: $MW"
  echo $(($RESULT))|awk '{printf "   Balance in ETH: %.8g\n", $1/1000000000000000000}'
  ;;

  gas)
  OWNGAS=$(jq -r ".blockchain.gas_price" $OTCONFIG)
  if [[ "$OWNGAS" != 'null' ]]; then
    echo $OWNGAS|awk '{printf " Config file gas price: %.2g gwei\n", $1/1000000000}'
  fi
  METHOD="eth_gasPrice"
  PARAM=''
  queryeth
  echo $(($RESULT))|awk '{printf "Current safe gas price: %.2g gwei\n", $1/1000000000}'
  ;;

  block)
  METHOD="eth_blockNumber"
  PARAM=''
  queryeth
  echo "Latest Block Number: $(($RESULT))"
  ;;

  0x*)
  METHOD="eth_getBalance"
  PARAM='"'$1'","latest"'
  queryeth
  echo $(($RESULT))|awk '{printf "Balance in ETH: %.8g\n", $1/1000000000000000000}'
  ;;

  *)
  echo "Query Ethereum Mainnet via INFURA"
  echo
  echo "$(basename $0) ow      # Show ETH balance of operational wallet"
  echo "     mw      # Show ETH balance of management wallet"
  echo "     0x...   # Show ETH balance of a specific address"
  echo "     gas     # Show config file gas price (if set) and current safe gas price in gwei"
  echo "     block   # Show current block number"
  ;;

esac
echo

If you haven't already used your own scripts check Some Simple Scripting for the steps to create your own.

Close to the top of the script there's a link to the whole JSON-RPC reference where you probably find other interesting functions to add.

I found another Ethereum RPC service provider (Nodesmith.io) which may act as fallback in case there are issues with Infura. You can play with their interface after you registered. Just remove the comment character (#) at about line 22 (#RPC="...") and use your RPC link for Ethereum Mainnet.