Last active
April 11, 2019 10:03
-
-
Save VasiliyS/881b28c0d33e8198c3a6a8433c17a84d to your computer and use it in GitHub Desktop.
Polkadot JS playground - Watch rewards on Controller account and print various diagnostics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Run your polkadot node | |
// Copy and paste this snippet to https://polkadot.js.org/apps/#/js | |
// update myStash with ID of the Stash account you'd like to watch | |
// You may leave this example running and it'll print updated balances of your RewardDestination. | |
const stash1 = '5GbphVim5aSCKVwPtruCKHfy4zmTMEUsm13hXcHn7n3y4geM'; | |
const myStash = stash1; | |
//retrive AccountID the stash is bonded to, if any | |
const controllerAccID = await bondedTo(myStash); | |
if( controllerAccID === "" ){ | |
console.log('Account : ' + myStash,' is not bonded to a Controller!'); | |
return; | |
} | |
console.log('Your Stash account: ' + util.stringShorten(myStash,6), ' :'); | |
console.log('... is bonded to Controller: ' + util.stringShorten(controllerAccID,6) ); | |
await printStakeInfo(controllerAccID); | |
console.log('Rewards for the stake will be paid to: '); | |
//retrieve accountID that'll receive rewards at the end of an Era | |
const payee = await api.query.staking.payee(myStash); | |
//staking.payee() returns Enum | |
let payeeType = payee.toString(); | |
let rewardsDestAccID = myStash; | |
switch(payeeType){ | |
case 'Controller': | |
rewardsDestAccID = controllerAccID; | |
break; | |
case 'Stash': | |
payeeType = 'Stash, and the stake will not be increased'; | |
break; | |
case 'Staked': | |
payeeType = 'Stash, and the stake will be increased with each reward'; | |
break; | |
default: | |
console.error('Unknown payee destination returned: ' + payee.toString()); | |
return; | |
} | |
console.log('... ' + payeeType); | |
const isValidating = await isValidator(controllerAccID); | |
if(isValidating){ | |
await watchBalanceOf(rewardsDestAccID); | |
} else { | |
console.log('Controller :',' associated with Stash: ',); | |
console.log('... is not an active Validator'); | |
console.log('Session key for Controller is :'); | |
const sessionKey = await api.query.session.nextKeyFor(controllerAccID); | |
console.log('... ' + (sessionKey.isSome ? sessionKey.value : "not set!")); | |
} | |
// --- helper functions ------------ | |
async function watchBalanceOf(accountID){ | |
// Retrieve the initial balance. | |
const startingBalance = await api.query.balances.freeBalance(accountID); | |
console.log('Started monitoring at: ' + (new Date()).toString() ); | |
console.log('Account : ' + util.stringShorten(accountID,6),' had initial balance balance of: ' + util.formatBalance(startingBalance)); | |
let prevBalance = startingBalance; | |
// Subscribe and listen to balance changes | |
api.query.balances.freeBalance(accountID, (balance) => { | |
// Calculate the delta | |
const change = balance.sub(prevBalance); | |
if(change != 0){ | |
console.log('Updated at: ' + (new Date()).toString() ); | |
console.log('new balance is: ' + util.formatBalance(balance),' delta is: ' + util.formatBalance(change)); | |
prevBalance = balance; | |
} | |
}); | |
} | |
// checks to see if accountID is currently an active Validator | |
async function isValidator(accountID){ | |
const validators = await api.query.session.validators(); | |
let accFound = false; | |
for (let acc of validators) { | |
if( acc.eq(accountID)){ | |
accFound = true; | |
break; | |
} | |
} | |
return accFound; | |
} | |
// print some info on stake, assumes that a bonded controller ID is provided | |
async function printStakeInfo(controllerID){ | |
const stLedgerOpt = await api.query.staking.ledger(controllerID); | |
if(stLedgerOpt.isSome){ | |
const ledger = stLedgerOpt.value; | |
const stashBalance = await api.query.balances.freeBalance(ledger.stash); | |
console.log('... has total balance of: ' + util.formatBalance(stashBalance)); | |
console.log('... of which currently at stake are: ' + util.formatBalance(ledger.total)); | |
} | |
} | |
//checks whether accoundID is bonded to a Controller | |
async function bondedTo(accountID){ | |
let controller = await api.query.staking.bonded(accountID); | |
//convert from Option & AccountId | |
return controller.isSome ? controller.value.toString() : ""; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment