How To Create Auto-Transfer Script With Steem.js JavaScript Library (Steem.js #4)

in utopian-io •  25 days ago

Auto-Transfer Script

Repository

https://github.com/steemit/steem-js

What Will I Learn?

In this tutorial you will learn how to create auto-transfer script. This can be useful if you need to transfer STEEM/SBD at specific time (e.g. Payment for service like VPS server from Privex or loan repayment).

  • You will learn how to claim rewards with Steem.js.
  • You will learn how to transfer STEEM/SBD with Steem.js.
  • You will learn how to schedule script with Node Cron.

Requirements

You need:

  • Linux Ubuntu 16.04 VPS (It can be also done in Windows or other Linux but I didn't test this) - Get it at Privex.io for 8$ per month (STEEM & SBD & ETH & BTC & LTC)
  • Node.js: sudo apt-get install nodejs
  • Node.js package manager npm: sudo apt-get install npm
  • Node-Cron: npm install --save node-cron
  • Steem.js: npm install steem --save

Difficulty

  • Intermediate

Tutorial

First connect to your VPS or open terminal.

Than create file transfer.js: touch transfer.js and open it in text editor: nano transfer.js.

Once you have transfer.js open, set variables steem and cron.

var steem = require('steem');
var cron = require('node-cron');

Now we need to claim rewards. But first we need to check if there are any rewards to claim.

steem.api.getAccounts(['your-account'], function(err, result){
    console.log(err, result)
    let sbdReward = result[0].reward_sbd_balance
    let steemReward = result[0].reward_steem_balance
    let steemPowerInVests = result[0].reward_vesting_balance
})

Don't forget to replace your-account with your Steem username.

This will check if there is any reward to claim and set variables sbdRewards, steemRewards and steemPowerInVests to amount of available amount of SBD/STEEM/VESTS.

Once you know how much rewards you have, you just need to claim them.

steem.broadcast.claimRewardBalance('private-posting-key', 'your-account', steemReward, sbdReward, PowerInVests, function(err, result) {
      console.log(err, result);
    });

This will claim all available rewards. Private posting key and account username need quotes (' ').

Now we need to transfer STEEM/SBD to another account. Let's set variables wif (active key), from (your account), to (another account), amount (amount to 3 decimals and symbol SBD or STEEM), memo (any memo)

var wif = 'your-active-key'
var from = 'my-account'
var to = 'another-account'
var amount = '100.000 SBD'
var memo = 'memo'
steem.broadcast.transfer(wif, from, to, amount, memo, function(err, result) {
 console.log(err, result);
});

This is working script, but you need to run it manually. We want automation!!

Let's use node-cron:

var steem = require('steem');
var cron = require('node-cron');

cron.schedule('* * * * *', function() {
//all code here
}

How to choose time when script will start? Change * * * * * with correct expressions. Use https://crontab.guru/ to create right expression. (e.g. 5 0 * 8 * will run script at 00:05 in August)

Full Code:

var steem = require('steem');
var cron = require('node-cron');

cron.schedule('* * * * *', function() {

steem.api.getAccounts(['your-account'], function(err, result){
            console.log(err, result)
            let sbdReward = result[0].reward_sbd_balance
            let steemReward = result[0].reward_steem_balance
            let steemPowerInVests = result[0].reward_vesting_balance

            steem.broadcast.claimRewardBalance('private-posting-key', 'your-account', steemReward, sbdReward, steemPowerInVests, function(err, result) {
                console.log(err, result);
            });
    })

var wif = 'your-active-key'
var from = 'my-account'
var to = 'another-account'
var amount = '100.000 SBD'
var memo = 'memo'

steem.broadcast.transfer(wif, from, to, amount, memo, function(err, result) {
 console.log(err, result);
});

})

Curriculum

Proof of Work Done

https://github.com/fbslo/Steem.js/blob/master/transfer.js

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Hey @fbslo
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Contributing on Utopian
Learn how to contribute on our website or by watching this tutorial on Youtube.

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

what a amazing Create Auto-Transfer Script system man.

Very good work, keep it up!

·

Thank you :)

Thank you for your contribution.
While I liked the content of your contribution, I would still like to extend few advices for your upcoming contributions:

  • There are parts of the code that have little explanation, try to explain as much as possible.
  • Put more resources.
  • Improve the structure of the tutorial.
  • Where is the video?

Looking forward to your upcoming tutorials.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Hello,
Do you need a official website for your witness account? I am offering to build and deploy a single/multi page website which will display your recent posts in very nice UI.
Check Creative Crypto Website for reference.
I am offering the website for SBD 5 only.
Please get in touch with on on my discord : aneilpatel#4929

·

Sorry, I already have website http://fbslo.net
Please don't spam!

·
·

This comment is only test!

·
·
·

This comment is only test, please ignore it!

You got a 34.31% upvote from @upme thanks to @fbslo! Send at least 3 SBD or 3 STEEM to get upvote for next round. Delegate STEEM POWER and start earning 100% daily payouts ( no commission ).

Thanks @fbslo for the tutorial, however I would like to suggest you to edit the 'your-active-key' and other static details to environment variables to prevent them from being leaked anywhere. Using environment variables for such properties is one of the best practices in terms of security.