Coding Practice on Hive - Grabbing Token Prices with Javascript

code.jpg

Like many of you, I'm sure, I have been keeping spreadsheets tracking my Hive holdings for many years. To get the prices for all the tokens I own, I use Keyboard Maestro, a very advanced macro application for the Mac, to essentially scrape the price data from Hive-Engine.com. It goes through page by page, capturing everything, then parses the data using Perl, a very powerful but incredibly cryptic scripting language that uses regular expressions, then calls some Applescripts that automatically insert all the captured prices into my spreadsheet. It's not really as complex as it seems—basically it is just a collection of scripts held together and managed by Keyboard Maestro—it's just hard to explain because there are quite a few steps there.

This works well, but there is one weak point to this system—the data scraping. As anyone who has even scraped data from the web before knows, it is very fragile! As soon as the webpage changes one line of formatting, it can ruin everything, requiring a rewrite of the scraping script. And over the years, that has happened to me many times. Not to mention, Hive-Engine.com often goes down, so I also have scraping scripts for tribaldex and beeswap.

I've lived with the system, because I really am not much of a coder and moving beyond the simple scripting languages I know has always seemed like it would require more effort than I can devote. It's not that I don't have the ability to learn—I studied computer science at university, intensively dealing with C++ and Java for all four years there as well as other languages like Assembler, which I wrote a C++ complier in for my graduation project—I assume I still have the mental capability to learn more languages, I just lack the time to pick up more than bits and pieces of scripting languages like AppleScript here and there as I need to do things. Part of aging is realizing we just don't have enough time to do everything, and I would much rather devote my limited time to my kids, writing, and haiku writing/research.

But then @raymondspeaks did something that inspired me to change this a bit.

If you missed it, Ray decided to use ChatGPT to write a trading bot. You can read his post and watch his video about it here. I'm sure we have all seen the stories floating around about people with no coding experience using ChatGPT to write apps for the iPhone app store. There are no shortage of these stories. I've read a few of them. Basically they seem to boil down to kids who have unlimited time and can go back and forth with ChatGPT for hours and days, working with the prompt enough to get it to write them a workable app. Again, I don't really have that time.

But watching Ray work on his trading bot gave me the idea that maybe I can have ChatGPT work me up some simpler code, not quite an iPhone app, but something more complex than I know how to do.

Although I didn't know it was possible, after watching Ray's video, I got the idea that I could use Javascript and two libraries to grab the token prices: sscjs and @hiveio/dhive.

To make a long story short, I opened ChatGPT and used a much simpler prompt than Ray used in his video. I just wrote: "I want you to write me a script using sscjs and @hiveio/dhive javascript libraries to grab a specific token price from hive-engine. The token symbol will be given as the first argument when I run this script, and it will simply return the price only."

That's it. And it returned a working script that I later used without modification to get the data I need.

It took more time to tell ChatGPT to back up and tell me how to install a javascript environment (1. Use a curl command to download and install nvm, 2. install the latest lts version of node.js, 3. download and install the libraries I want, then 4. set up a new project. Fairly simple, just a lot of waiting for things to download and install.).

Next I pointed keyboard maestro at this javascript, and instructed it to run it multiple times with different token symbols, sticking each price in a variable.

Boom, within an hour I had a much more reliable system to get the token prices for my spreadsheets. As a bonus, it's much much faster as well than my old process of scraping Hive-Engine.com.

If you are curious, by the way, here's the code:

const SSC = require('sscjs');

// Initialize SSC
const ssc = new SSC('https://api.hive-engine.com/rpc/');

// Function to fetch the latest price of a token
const fetchTokenPrice = async (symbol) => {
  const limit = 1;  // We only want the latest trade
  const query = { symbol: symbol };
  const sort = { timestamp: -1 };

  try {
    const result = await ssc.find('market', 'tradesHistory', query, limit, 0, sort);
    if (result.length > 0) {
      console.log(`${result[0].price}`);
    } else {
      console.log(`No trades found for ${symbol}`);
    }
  } catch (error) {
    console.error('Error fetching token price:', error);
  }
};

// Check if a token symbol is provided
if (process.argv.length < 3) {
  console.log('Usage: node fetchPrice.js <TOKEN_SYMBOL>');
} else {
  const tokenSymbol = process.argv[2]; // Get token symbol from command-line argument
  fetchTokenPrice(tokenSymbol);
}

I'd be surprised if it is well written. You Hive coders can tell me if it's good or not. I don't know javascript at all, so I can't tell. As time goes on I may pick that apart and try to figure out what is going on, and if things break I may be forced to learn faster, but for now I just know it works!

So yeah, there we go, ChatGPT coding for the win. I'm going to have to turn to this more often. Thanks for the inspiration to do so, @raymondspeaks!

Hi there! David LaSpina is an American photographer and translator lost in Japan, trying to capture the beauty of this country one photo at a time and searching for the perfect haiku. He blogs here and at laspina.org. Write him on Twitter or Mastodon.
1.55617579 BEE
5 comments

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support. 
 

0.00000345 BEE

I must be one of the few who doesn't keep spreadsheets of his holdings. LOL

I just buy and let it fly.

0.00000348 BEE

Probably more folks who aren't really investing a lot don't need to keep them. I started to do so when I started to invest some of my own money. At first it was for tax purposes, but as I did so I liked having all the data to see various APRs on coins and so on.

0E-8 BEE

I probably should but don’t. I really don’t have enough in crypto at this point but getting there.

0E-8 BEE

That's pretty awesome. I was leveraging Gemini to write some powershell scripts myself just the other day.

0.00000346 BEE

Seems like I'm late to this using AI to write code game. This is a really great use of AI!

0.00037699 BEE

I totally agree. I have only used it that once, I haven't found anything else I need done yet.

0E-8 BEE

What the...!!! ChatGPT can write codes, and it works??!!

I've never really kept track of my assets. I mean when I was flipping HBD and Hive for a while I had an excel spreadsheet, then I tried to be clever and added bits of formula to calculate random stuff and ended up with a really messy spreadsheet so I gave up in the end

0.00000349 BEE

It's pretty incredible, isn't it? I didn't realize the languages of these language model AIs would extend so well to coding, but seems like they do. If you have time, you might play around with it too.

haha yeah I think all my spreadsheets are messy. I'm not the best at designing well-organized structures for presenting data. I originally started tracking all my assets in Hive for tax reasons. For US taxes I never know what I need so better safe than sorry and for Japan taxes, I can add Hive holdings to my income to make my Visa easier to renew every few years. Since then I have come to appreciate how nice it is to have all the data in one place.

0E-8 BEE

Hello dbooster!

It's nice to let you know that your article will take 9th place.
Your post is among 15 Best articles voted 7 days ago by the @hive-lu | King Lucoin Curator by keithtaylor

You receive 🎖 1.2 unique LUBEST tokens as a reward. You can support Lu world and your curator, then he and you will receive 10x more of the winning token. There is a buyout offer waiting for him on the stock exchange. All you need to do is reblog Daily Report 367 with your winnings.

2.png


Invest in the Lu token (Lucoin) and get paid. With 50 Lu in your wallet, you also become the curator of the @hive-lu which follows your upvote.
Buy Lu on the Hive-Engine exchange | World of Lu created by szejq

If you no longer want to receive notifications, reply to this comment with the word STOP or to resume write a word START

0E-8 BEE