前几天瓜叔给了我几个小号,叫我帮他设置一下自动领取SCOT收益,如果可以,顺便也领取一下STEEM收益。
自动领取SCOT收益的程序 已经正常运行了,但是还没有添加自动领取STEEM收益的功能,所以就趁这个机会,添加一下自动领取STEEM收益的功能。
看了一遍steemjs的开发手册,一如既往的简单并且无用。简单的搜索一下,需要使用一下2个函数:getAccounts和claimRewardBalance
获取账号信息 要领取收益,首先要知道需要领取多少的收益。要获得这些信息那要使用getAccounts函数。
1 2 3 steem.api.getAccounts([ACCOUNT_NAME], function(err, result){ console.log(err,result); })
通过getAccounts获取账号信息:
其中,我们只需要3个数值用于领取收益。他们是: reward_sbd_balance reward_steem_balance reward_vesting_balance
从上图可以看到,账号有555461.145480 VESTS(27.925 SP)待领取。
读取3个数值:
1 2 3 4 5 6 7 8 function claimSteemRewards() { steem.api.getAccounts([ACCOUNT_NAME], function (err, result) { console.log(err, result) let sbdReward = result[0].reward_sbd_balance; let steemReward = result[0].reward_steem_balance; let spInVests = result[0].reward_vesting_balance; }); }
领取收益 已经知道需要领取多少收益后,就可以使用claimRewardBalance的函数领取收益了。
1 2 3 steem.broadcast.claimRewardBalance(ACCOUNT_KEY, ACCOUNT_NAME, steemReward, sbdReward, spInVests , function(err, result) { console.log(err, result); });
结合上面的getAccounts,一个简单的领取收益程序就完成了:
1 2 3 4 5 6 7 8 9 10 11 function claimSteemRewards() { steem.api.getAccounts([ACCOUNT_NAME], function (err, result) { console.log(err, result) let sbdReward = result[0].reward_sbd_balance; let steemReward = result[0].reward_steem_balance; let spInVests = result[0].reward_vesting_balance; steem.broadcast.claimRewardBalance(ACCOUNT_KEY, ACCOUNT_NAME, steemReward, sbdReward, spInVests, function (err, result) { console.log(err, result); }); }); }
但是这个程序还有一个小问题,那就是,如果待领取的收益为0时,就会报错。所以需要加个逻辑,如果待领取收益全部是0的时候,不领取收益。代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 function claimSteemRewards() { steem.api.getAccounts([ACCOUNT_NAME], function (err, result) { let sbdReward = result[0].reward_sbd_balance; let steemReward = result[0].reward_steem_balance; let spInVests = result[0].reward_vesting_balance; if (sbdReward.split(' ')[0] > 0 || steemReward.split(' ')[0] > 0 || spInVests.split(' ')[0] > 0) { steem.broadcast.claimRewardBalance(ACCOUNT_KEY, ACCOUNT_NAME, steemReward, sbdReward, spInVests, function (err, result) { console.log(err, result); }); } }); }
设置自动领取 程序基本完成,目前唯一的问题是,你需要每次运行这个程序来领取一次收益。如果你想每个小时自动领取一次的话,那就需要使用setInterval. 代码如下:
1 2 3 4 5 const INTERVAL = 60 * 60 * 1000; //60 minutes setInterval(function () { claimSteemRewards() }, INTERVAL);
完整代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 const steem = require('steem'); const ACCOUNT_NAME = ''; const ACCOUNT_KEY = ''; const INTERVAL = 60 * 60 * 1000; //60 minutes setInterval(function () { claimSteemRewards() }, INTERVAL); function claimSteemRewards() { steem.api.getAccounts([ACCOUNT_NAME], function (err, result) { let sbdReward = result[0].reward_sbd_balance; let steemReward = result[0].reward_steem_balance; let spInVests= result[0].reward_vesting_balance; if (sbdReward.split(' ')[0] > 0 || steemReward.split(' ')[0] > 0 || spInVests.split(' ')[0] > 0) { steem.broadcast.claimRewardBalance(ACCOUNT_KEY, ACCOUNT_NAME, steemReward, sbdReward, spInVests, function (err, result) { if (result && !err) { console.log(`${ACCOUNT_NAME} claimed ${sbdReward}, ${steemReward} and ${spInVests}`); } else { console.log(err); } }); } }); }
这样一个完整的自动领取收益的程序就完成了~
完整代码也可以在我的github上找到:https://github.com/ericet/steemjs-fun/blob/master/claimRewardBalance.js
STEEM编程系列:
This page is synchronized from the post: ‘怎么用JS自动领取STEEM收益?’