Have you ever wondered how to automate some tasks you do daily on WAX?
For example, after voting, you may be familiar with going to MyCloudWallet.com, logging on, clicking the Staking link and Claiming GBM Rewards (voting rewards). But what if there was an easier way? Well, this tutorial will introduce you to NodeJS and Javascript code that is relatively short and uses your private keys locally, letting you sign and retrieve your WAX rewards.
Unfortunately, this method works if you have your private keys. While it is possible to claim them via MyCloudWallet.com advanced options, we recommend creating a new account that is not a Wax Cloud Wallet and where you know your private keys. The scope of this tutorial will not cover creating a Wax account where you own the private keys - here is a video tutorial on that topic.
(Just a quick note: Anchor Wallet is a piece of software that lets you interface with dozens and dozens of your own private keys, sign transactions and more - but its standalone from Wax Cloud Wallet so don't get those confused)
What you'll need:
Install NodeJS - https://nodejs.org/en
Notepad++ - https://notepad-plus-plus.org/downloads/v8.6.2/
Dependencies you'll need:
"@waxio/waxjs": "^1.0.4",
"axios": "^0.27.2",
"eosjs": "^21.0.4",
"request": "^2.88.2"
The base smart contract call:
``` async function claimVotingRewards(user){
try {
const result = await api.transact({
actions: [{
account: 'eosio',
name: 'claimgbmvote',
authorization: [{
actor: user,
permission: 'active',
}],
data: {
owner: user
}
},
{
account: 'eosio',
name: 'claimgenesis',
authorization: [{
actor: user,
permission: 'active',
}],
data: {
claimer: user
}
}
]
}, {
blocksBehind: 3,
expireSeconds: 30,
});
console.log("Claimed GBM Voting rewards successfully for " + user);
return true;
} catch(e) {
console.log("Could not claim GBM voting rewards: " + e.message);
return e.message;
}
}```
Alright, what can you tell from the code above? Look familiar?
That's right! This is the same smart contract call information you may have seen whenever the WAX Cloud Wallet pops up on your screen on Atomic Hub and asks for your to APPROVE a transaction. If you were to expand some of the sections in that pop up, you'd see similiar fields like 'account', 'actor', 'permission', 'data' and information for the game or app you were using.
This is the smart contract call in Javascript; we can use it with something called the eosjs library which is EOS' javascripting library of commands. Since WAX utilizes EOS, we can use this library conveniently to pass a smart contract call. But you know what we're missing right?
Our signing keys!
Yes, first we're gonna get our private keys and use them to sign the transaction. Here is the rest of the code:
```
const user = 'YOUR WAX ACCOUNT NAME HERE';
const privateKeys = ["YOUR PKEY HERE"];
const request = require("request");
const {
Api,
JsonRpc
} = require('eosjs');
const {
JsSignatureProvider
} = require('eosjs/dist/eosjs-jssig');
const fetch = require('node-fetch');
const {
TextDecoder,
TextEncoder
} = require('util');
const signatureProvider = new JsSignatureProvider(privateKeys); // this line uses the private keys to create a signature provider so we can sign our smart contract call!
const rpc = new JsonRpc("https://wax.greymass.com", {
fetch
});
const api = new Api({
rpc,
signatureProvider,
textDecoder: new TextDecoder(),
textEncoder: new TextEncoder()
});
const axios = require('axios').default;
```
Okay, so copy the above code into Notepad++ and you'll have the "base" of most nodejs scripts we run to automate and simplify our WAX processes. Then paste the smart contract call to claim voting rewards (the snippet of code from earlier) below it. And finally, let's add this:
```
claimVotingRewards(user)
.then(result => {
if (result === true) {
console.log("Voting rewards claimed successfully.");
} else {
console.log("Error while claiming voting rewards:", result);
}
})
.catch(error => {
console.error("Error occurred:", error);
});
```
This last section is an asynchronous call to use the smart contract call. Basically, it puts everything together and tells us if the function has succeeded or failed. Now we can save this in Notepad++ as 'claimrewards.js'. Now find the folder where you saved this file. Click on the address bar in the explorer window and erase whatever is in there (Windows 7-11 support this):
(Here's an example of what this looks like above. We erased the directory breadcrumbs in the input field here and typed in 'CMD' and then clicked enter. This opens the command line in Windows.
Type in 'node claimrewards.js' to run your script from this directory.
( This is just an easier way to focus the command line software to pay attention to the directory where your nodejs script is located. )
You should see the script running. If you had an error, it may close or list the errors it had. You can throw those errors into ChatGPT to walk you through all the other issues you might have, such as needing to install dependencies.
Here is the full script if you'd like to expiriment with your very first nodejs script locally:
```
const user = 'YOUR WAX ACCOUNT NAME HERE';
const privateKeys = ["YOUR PKEY HERE"];
const request = require("request");
const {
Api,
JsonRpc
} = require('eosjs');
const {
JsSignatureProvider
} = require('eosjs/dist/eosjs-jssig');
const fetch = require('node-fetch');
const {
TextDecoder,
TextEncoder
} = require('util');
const signatureProvider = new JsSignatureProvider(privateKeys);
const rpc = new JsonRpc("https://wax.greymass.com", {
fetch
});
const api = new Api({
rpc,
signatureProvider,
textDecoder: new TextDecoder(),
textEncoder: new TextEncoder()
});
const axios = require('axios').default;
async function claimVotingRewards(user){
try {
const result = await api.transact({
actions: [{
account: 'eosio',
name: 'claimgbmvote',
authorization: [{
actor: user,
permission: 'active',
}],
data: {
owner: user
}
},
{
account: 'eosio',
name: 'claimgenesis',
authorization: [{
actor: user,
permission: 'active',
}],
data: {
claimer: user
}
}
]
}, {
blocksBehind: 3,
expireSeconds: 30,
});
console.log("Claimed GBM Voting rewards successfully for " + user);
return true;
} catch(e) {
console.log("Could not claim GBM voting rewards: " + e.message);
return e.message;
}
}
claimVotingRewards(user)
.then(result => {
if (result === true) {
console.log("Voting rewards claimed successfully.");
} else {
console.log("Error while claiming voting rewards:", result);
}
})
.catch(error => {
console.error("Error occurred:", error);
});
```
What's next, how do we truly automate it to run every 3 or 6 days?
- Go to the Start Menu, type in Task Scheduler.
- Click Create Basic Task, choose a name for your task: "Run WAX Claim Voting Rewards Script", select how frequently (weekly is great for this)
- Choose 'Start Program' and paste in the Action field:
- /c "cd C:\myfolderwhereikeepmyscript && node C:\myfolderwhereikeepmyscript\claimrewards.js"
- This will make Task Scheduler run this nodejs script every week once you create the task
- (You can make edits to the script by clicking the 'Task Scheduler Library' in the left-hand menu and finding the name of your task)
If you had any particular issues that got you stuck, please comment. We want to make this post a resource for new users. For all kinds of simple errors you may face that you don't want to wait for, just use ChatGPT and plug your error in there. For most basic WAX eosjs/waxjs issues ChatGPT seems to be able to assist fairly well.