How to run a script in the hive blockchain with npm and node.js
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:
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
Leave How to run a script in the hive blockchain with npm and node.js to:
Read more #hive posts
Best Posts From Fefe
We have not curated any of fefe99's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From Fefe
- Hello World!
- TAG (The Adventure Guild): A game in the Wax Blockchain
- A clicker that gives you free nfts in the testnet
- How to create a python script that runs automatically at startup (WINDOWS)
- How to run a script in the hive blockchain with npm and node.js
- How to create a table with javascript from hive engine's data.
- Rewards from Pizza staking
- How to use sscjs library to find info in hive-engine:
- HK-University Experience #1
- [Zapfic 40] TEAM