What Will I Learn?
In this third tutorial we will finally use a JSON config file, we will only allow upvotes with an exact amount and refund the others.
- Understand and use config files correctly
- Refund transactions
- Read the transferred amount out of the transaction data
Requirements
Have a basic understanding of JavaScript
- Read my last parts (Click here)
- Have a TextEditor (e.g Atom.io)
- A working brain and some time
Difficulty
- Basic
Tutorial Contents
The Config File
What is a config file and what is JSON ? Before we continue developing our upvote bot we will first create a JSON Config file.
Config File
A config file is used to store parameters and the initial parameters for out bot, also it is a useful way to define specific variables on one place. For example we could store our private posting key in a config file, after that we can use the key everywhere in our code via the config file. If the Key changes we only need to edit ONE place instead of everywhere we used the key.
JSON
JavaScript Object Notation (JSON) is a lightweight data format. It is easy to read and write for humans and easy to parse and generate for machines. JSON is complete language independent and is perfect for creating config files.
First create a config file named config.json inside of your projects directory and add following content
Inside of your index.js add and change following code:
1 - Add to the beginning
const fs = require("fs");
var config = JSON.parse(fs.readFileSync("config.json"));
2 - Change following lines:
...
steem.api.setOptions({ url: config.steem_node });
...
steem.api.getAccounts([config.account_name], function (err, result) {
...
steem.broadcast.vote(config.private_posting_key, account.name, vote.author, vote.permlink, 10000, function (err, result) {
Now it is very easy for use to change the settings of our bot (e.g change the steem-account for the bot)
Only allow correct transaction amounts
Next we will check if a transactions was sended with a valid amount of SBD or STEEM. To do so change your code from
if (op[0] == 'transfer' && op[1].to == account.name) {
if (validUrl.isUri(op[1].memo)) {
checkValidMemo(op);
}
}
To:
Let's break it down:
var amount = op[1].amount;
var currency = amount.substr(amount.indexOf(' ') + 1);
amount = parseFloat(amount);
First we are reading from the transaction data A) the amount which was send and B) the currency which was send - we are using the substring and indexOf function here. Why ? - Because normally the amount is send for example like this: '1.00 SBD'. So first we get the Index of the whitespace and create a substring starting and the next index after the substring. We will get SBD from this call.
After this we parse the amount to a valid float value.
if(config.accepted_currencies && config.accepted_currencies.indexOf(currency) < 0) {
console.log("INVALID CURRENCY SENT - SHOULD REFUND");
refund(op[1].from, op[1].amount);
return;
} else if(amount < config.min_bid) {
console.log("BID TO LOW - SHOULD REFUND");
refund(op[1].from, op[1].amount);
return;
} else if (amount > config.max_bid) {
console.log("BID TO HIGH - SHOULD REFUND");
refund(op[1].from, op[1].amount);
return;
}
Here we have our exit-early clauses. Exit early means we first check all possible clauses which we DO NOT want. If they appear we want to EXIT our code EARLY before doing the real stuff our program should do.
What we check are A) Is the currency a valid currency we accept ? B) Is the amount send higher than our minimum amount ? C) Is the amount send lower than our maximal amount?
If all three succeed our bot will check the post if it is a valid post and so on...
If NOT our bot stops... But wait we still have the amount on our account balance - we should refund it !
refund(op[1].from, op[1].amount);
Calls our refund method , which we will create in a second, with following parameters:
Who sended the amount, the amount.
Refund funcion
steem.broadcast.transfer(config.private_active_key, config.account_name, sender, amount, '', function (err, response) {}
Takes as parameters your private active key, the sender account, the receiver account, the amount, the memo field.
Testing ?
Beware you should only test this with 2 different accounts (the best would be to now use a bot account!) - why ? Well when you try to refund to your own account it will get stuck in a refund loop!
What's up next ?
In the next part we will learn:
- Using refund memo messages from our config file
- Save the last checked transaction, so our bot does not always check the same transactions
- Create default values for our config
- Check post age
Source Code
Curriculum
Posted on Utopian.io - Rewarding Open Source Contributors