Massive Updates, Steem RPC Calls, Ticker Scripts

in #utopian-io7 years ago (edited)

Much progress was made in adding support for various JSON RPC calls and a few bug fixes were made.

Github Repo here: steem-bash

Bug Fixes

There was a minor bug in fetching the value of a given coin, it would attempt to parse the USD value of the coin from the response even if a different currency was provided.
Fixed in commit #8741ac5d.

There was a bug in my use of the math function that I added, it was scaling improperly. Fixed in 994b53e1.

New Features

Added support for the following RPC methods (and documented them when documentation was available, or just based on what they seem to return):

  • rpc_get_account_count()
  • rpc_get_account_history()
  • rpc_get_account_votes()
  • rpc_get_accounts()
  • rpc_get_active_votes()
  • rpc_get_active_witnesses()
  • rpc_get_block()
  • rpc_get_chain_properties()
  • rpc_get_config()
  • rpc_get_content()
  • rpc_get_content_replies()
  • rpc_get_conversion_requests()
  • rpc_get_current_median_history_price()
  • rpc_get_discussions_by_active()
  • rpc_get_discussions_by_author_before_date()
  • rpc_get_discussions_by_cashout()
  • rpc_get_dynamic_global_properties()
  • rpc_get_expiring_vesting_delegations()
  • rpc_get_discussions_by_blog()
  • rpc_get_discussions_by_children()
  • rpc_get_discussions_by_comments()
  • rpc_get_discussions_by_created()
  • rpc_get_discussions_by_feed()
  • rpc_get_discussions_by_hot()
  • rpc_get_discussions_by_payout()
  • rpc_get_discussions_by_promoted()
  • rpc_get_discussions_by_trending()
  • rpc_get_discussions_by_votes()
  • rpc_get_discussions_by_payout()
  • rpc_get_expiring_vesting_delegtions()
  • rpc_get_feed_history()
  • rpc_get_hardfork_version()
  • rpc_get_escrow()

Created a generic function for invoking any RPC service:

  • rpc_invoke()

Added rpc_raw for invoking RPC methods without trying to pull out the result for debugging purposes (and probably to take over for rpc_invoke when I add actual error handling.

These were implemented using a base function that invokes a provided method and its arguments against a provided RPC endpoint and parses the resulting JSON with the jq command. This is a nice leap forward in the usefulness of the project. It will be possible to implement even more useful features with these. For example, I plan to create a script that will find posts for a specific user and set of tags and generate a list of markdown links that can be put at the end of a post for backlinks. Much like what coffeesource.net does, but in Bash instead of a web browser.

All functions have an optional endpoint argument, if no endpoint is provided they default to https://steemd.privex.io. Mostly because it was the first one I tried that worked.

I painstakingly invoked each method by hand, recording arguments and quirks and coded these up as convenience functions. Some of the functions were very easy. For example, the get_config method was coded up like so:

##
#    rpc_get_config <number> [ENDPOINT]
#
# Original documentation from [web](http://steem.readthedocs.io/en/latest/steem.html):
# Get internal chain configuration.
rpc_get_config(){
    local ENDPOINT=${1:-${RPC_ENDPOINT}}
    rpc_invoke get_config '' "${ENDPOINT}"
}

Note that's it's basically a wrapper for the workhorse function rpc_invoke. That function looks like this:

##
#     rpc_invoke <method> <args> [endpoint]
# Invokes the specified RPC method giving it the provided arguments and going against the provided RPC endpoint.  Defaults to $RPC_ENDPOINT global.
rpc_invoke(){
    local METHOD=${1}
    local ARGS=${2:-null}
    local ENDPOINT=${3:-${RPC_ENDPOINT}}
    local DATA="{ \"jsonrpc\": \"2.0\", \"method\": \"${METHOD}\", \"params\": [${ARGS},] \"id\": 1 }"
    wget --method=PUT --body-data "${DATA}"  -O - "${ENDPOINT}" 2>/dev/null | jq '.result'
}

It uses wget and jq to push JSON to the RPC endpoint and then pull out the resulting "result" element. This will appropriately return a non-zero status if there is a problem, but the JSON error message is lost. I'm looking into using the raw function instead, and having that attempt to extract the error messages so that in addition to returning non-zero it could send the error message to STDERR.

Some functions, like those for fetching discussions need more complicated objects. For these I am hand rolling much of the JSON, but I'm looking into having jq format them for me. For example, in the get_accounts function wraps the work horse like this:

rpc_get_accounts(){
    local ARGS=${@}
    rpc_invoke get_accounts "$(jq -c -n -M --arg v "${ARGS}" '($v|split(" "))')"
}

But I found this to be hard to read, so I'm still up in the air over whether I should keep hand rolling or use this format. If this actualy does the appropriate escaping then I'll be switching to thsi format because otherwise I will need to code up logic in Bash for making the values compliant JSON.

Speed improvements

Reduced the number of calls made to web resources.

  • Modified get_steem_per_mvest to make use of the RPC functions instead of scraping steemd.com (it's usually faster).
  • Made a get_prices method that fetches multiple currency values from cryptocompare.com at a time to speed things up.
  • Modified get_bank to make use of the faster single get_prices call instead of multiple get_price calls. It's quite a bit faster now that the external invocations have been minimized.

Reduced the math to be done in external shells.

  • Modified get_steempower_for_vests and created get_steem_per_vest to use vests instead of millions of vests to avoid extra division and multiplication when calculating in get_steempower_for_vests.

Sample Scripts

The sample scripts show off a tiny tiny fraction of the actual functionality that I've packed into this update, but I do find them useful.

Added two scrolling ticker scripts:

  • balance_ticker.sh - Displays various account balances.
  • worth_ticker.sh - Displays total account value.

The balance_ticker.sh can take more than one account name and it makes an attempt at functionality and aesthetic. It displays the user name, the worth of their account in USD, followed by the value of their SBD, the current rate of SBD, the value of their STEEM, the current rate of STEEM, and it does it for each user name provided.

Invoke it thusly:

./balance_ticker.sh not-a-bird not-a-gamer

balance_ticker.sh invocation takes any number of user names.

Output from it looks like this:
balance_ticker.gif

balanc_ticker.sh output

The other script, worth_ticker.sh just scrolls the value of the account provided.
./balance_ticker.sh not-a-bird not-a-gamer

worth_ticker.sh invocation takes any number of user names.

Output looks like this:
worth_ticker.gif

worth_ticker.sh output

Both of the tickers update after a full scroll of the values and continue running until they are killed.

Try it Out!

If you have a Linux system with git, jq, and bc installed then just check out my project and invoke the sample scripts.

For example:

git clone https://www.github.com/not-a-bird/steem-bash
cd steem-bash
./balance_ticker.sh ned

Or download the zip, unzip it, and try the scripts.

If you want to write your own scripts against any of the API calls, just source the functions.sh at the top of your script and go to town.

Or, you can run functions at the command:

source functions.sh
get_price LTC
get_bank ned

Feedback

I invite any and all feedback. I've spent a lot of time working on this over the last few days and I may have been a little tunnel blind in my focus to get this parts of it ready enough to be played with. If anyone spots anything dumb, just curious about a choice I made, or anything, feel free to leave a comment.



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for your hard worked ! =).
It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @stoodkev, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!

Hey @not-a-bird I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.21
TRX 0.13
JST 0.030
BTC 67334.64
ETH 3519.34
USDT 1.00
SBD 3.10