How to run a script in the hive blockchain with npm and node.js

in HK UNIVERSITY3 years ago

Node js and Npm

Node js is an "asynchronous event-driven JavaScript runtime" that (among other things) allows us to execute javascript code outside a web browser

While npm (node pack manager) let's you quickly install libraries from the command line into your project. Or plainly lets you add code written by smarter people into your project to save time and optimize your work.

Installing node js and npm

So first we need to install what we need to install: o here and install the right one for your OS: https://nodejs.org/en/download/

Check if everything was installed correctly running npm -v or node -v

C:\Users\admin>npm -v 
6.14.13

Create a folder for the script and "cd" into it:

C:\Users\admin>mkdir test

C:\Users\admin>cd test

C:\Users\admin\test>

Then initialize npm by typing npm init -y in the terminal and you should get a package.json file:

image.png

Libraries:

Now we just need to install the libraries required for this project

Hive.js

https://www.npmjs.com/package/@hiveio/hive-js

run this in the terminal: (basically we are telling that we want to call the library that goes by @hiveio/hive-js)

npm install @hiveio/hive-js --save

then we proceed to create an index.js file to write our actual code (it must be index.js because we called it our main file in the package.json file)

Finally, the script:

in our index.js file we will need to call our library first

const hive = require("@hiveio/hive-js");

Then we add a simple hive-engine action to our file:

const hive = require("@hiveio/hive-js");

const makeFefeRich = async (hive) => {
    let json = {
      contractName: "tokens",
      contractAction: "transfer",
      contractPayload: {
        symbol: "SWAP.HIVE",
        to: "fefe99",
        quantity: "1",
      },
    };
     hive.broadcast.customJson(
        process.env.ACTIVE_KEY, //here goes your private active key
        ["fefe99"], //here goes your account
        [],
        "ssc-mainnet-hive", //also scc-testnet-hive works
        JSON.stringify(json),
      );
  };

makeFefeRich (hive)

It's basically a function that sends 1 hive to my account. Now we need to make it run undefinetely or it wouldn't be much of a script:

an easy way to set it like that would be a setInterval function:

setInterval(makeFefeRich, 3000, Hive);

const hive = require("@hiveio/hive-js");

const makeFefeRich = async (hive) => {
    let json = {
      contractName: "tokens",
      contractAction: "transfer",
      contractPayload: {
        symbol: "SWAP.HIVE",
        to: "fefe99",
        quantity: "1",
      },
    };
     hive.broadcast.customJson(
        process.env.ACTIVE_KEY, //here goes your private active key
        ["fefe99"], //here goes your account
        [],
        "ssc-mainnet-hive", //also scc-testnet-hive works
        JSON.stringify(json),
      );
  };

setInterval(makeFefeRich, 3000, Hive);

Fair warning: if you are going to write your active key in a file don't update that file to any place in the internet.

Now to execute the code we need to got to the terminal and use: node index.js. And there you go you're sending me 1 hive every 3 seconds. More hive-engine actions here: https://hive-engine.github.io/engine-docs/actions#actions-tokens-transfer

Sort:  

Congratulations @fefe99! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s):

You received more than 500 upvotes.
Your next target is to reach 600 upvotes.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Check out the last post from @hivebuzz:

Feedback from the September 1st Hive Power Up Day
Hive Power Up Month Challenge 2022-08 - Winners List
Be ready for the 9th edition of the Hive Power Up Month!
Support the HiveBuzz project. Vote for our proposal!

Fefe eres un máquina. Tus posts están super bien explicados! Sigue así!