Learn how to claim rewards from unclaimed reward balance using Steemconnect as well as client signing method.
This tutorial runs on the main Steem blockchain. And accounts queried are real users with unclaimed balances.
This tutorial will show few functions such as querying account by name and getting unclaimed rewards. We are using the call
function provided by the dsteem
library to pull accounts from the Steem blockchain. A simple HTML interface is used to capture the account and its unclaimed balance as well as allowing interactively claim rewards.
- App setup Setup
dsteem
to use the proper connection and network. - Search account Get account details after input has account name
- Fill form Fill form with account reward balances
- Claim reward Claim reward with Steemconnect or Client signing options
Below we have dsteem
pointing to the production network with the proper chainId, addressPrefix, and endpoint. There is a public/app.js
file which holds the Javascript segment of this tutorial. In the first few lines we define the configured library and packages:
const dsteem = require('dsteem');
let opts = {};
//connect to production server
opts.addressPrefix = 'STM';
opts.chainId =
'0000000000000000000000000000000000000000000000000000000000000000';
//connect to server which is connected to the network/production
const client = new dsteem.Client('https://api.steemit.com');
After account name field is filled with some name, we do automatic search for account by name when input is focused out. HTML input forms can be found in the index.html
file. The values are pulled from that screen with the below:
const accSearch = document.getElementById('username').value;
const _accounts = await client.database.call('get_accounts', [[accSearch]]);
console.log(`_accounts:`, _accounts);
After we fetched account data, we will fill form with reward balance and show current reward balance details.
const name = _accounts[0].name;
const reward_steem = _accounts[0].reward_steem_balance.split(' ')[0];
const reward_sbd = _accounts[0].reward_sbd_balance.split(' ')[0];
const reward_sp = _accounts[0].reward_vesting_steem.split(' ')[0];
const reward_vests = _accounts[0].reward_vesting_balance.split(' ')[0];
const unclaimed_balance = `Unclaimed balance for ${name}: ${reward_steem} STEEM, ${reward_sbd} SBD, ${reward_sp} SP = ${reward_vests} VESTS<br/>`;
document.getElementById('accList').innerHTML = unclaimed_balance;
document.getElementById('steem').value = reward_steem;
document.getElementById('sbd').value = reward_sbd;
document.getElementById('sp').value = reward_vests;
We have 2 options on how to claim rewards. Steemconnect and Client signing options. We generate Steemconnect link to claim rewards, but you can also choose client signing option to claim rewards right inside tutorial.
In order to enable client signing, we will generate operation and also show Posting Private key (wif) field to sign transaction right there client side. Below you can see example of operation and signing transaction, after successful operation broadcast result will be shown in user interface. It will be block number that transaction was included.
window.submitTx = async () => {
const privateKey = dsteem.PrivateKey.fromString(
document.getElementById('wif').value
);
const op = [
'claim_reward_balance',
{
account: document.getElementById('username').value,
reward_steem: document.getElementById('steem').value + ' STEEM',
reward_sbd: document.getElementById('sbd').value + ' SBD',
reward_vests: document.getElementById('sp').value + ' VESTS',
},
];
client.broadcast.sendOperations([op], privateKey).then(
function(result) {
document.getElementById('result').style.display = 'block';
document.getElementById(
'result'
).innerHTML = `<br/><p>Included in block: ${
result.block_num
}</p><br/><br/>`;
},
function(error) {
console.error(error);
}
);
};
That's it!
- clone this repo
cd tutorials/21_claim_rewards
npm i
npm run dev-server
ornpm run start
- After a few moments, the server should be running at http://localhost:3000/