infra-role-geth/templates/rpc.sh.j2

37 lines
718 B
Plaintext
Raw Normal View History

#!/usr/bin/env bash
# vim: set ft=sh:
set -euo pipefail
URL="http://{{ geth_rpc_addr }}:{{ geth_rpc_port }}/"
METHOD="$1"
shift
PARAMS=("$@")
if [[ -z "${METHOD}" ]]; then
echo "No method specified!" >&2
exit 1
fi
if [[ -n "${@}" ]]; then
PARAMS=$(printf '%s,' "${@}")
PARAMS="${@%%,}"
else
PARAMS=''
fi
PAYLOAD="{
\"id\": 1,
\"jsonrpc\": \"2.0\",
\"method\": \"${METHOD}\",
\"params\": [${PARAMS}]
}"
# The jq script checks if error exists and adjusts exit code.
curl --request POST \
--silent \
--fail-with-body \
--header 'Content-type:application/json' \
--data "${PAYLOAD}" \
"${URL}" | \
jq -e '., if .error != null then null|halt_error(2) else halt end'