Skip to content

Commit 4a54d2d

Browse files
committed
Merge branch 'master' into fix-sub-account-generator
2 parents 2ee9138 + 6ad314f commit 4a54d2d

File tree

12 files changed

+276
-44
lines changed

12 files changed

+276
-44
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.vscode

codec/codec.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const { TypeRegistry, createType } = polkadotTypes;
2+
13
let rawBytes = document.getElementById('rawBytes');
24
let customTypes = document.getElementById('customTypes');
35
let output = document.getElementById('output');
@@ -17,7 +19,7 @@ editor.set(initialJson);
1719

1820
/* CUSTOM TYPES EDITOR END */
1921

20-
const registry = new types.TypeRegistry();
22+
const registry = new TypeRegistry();
2123

2224
function parseCustomType() {
2325
try {
@@ -38,7 +40,7 @@ function parseCustomType() {
3840
}
3941

4042
output.innerText = JSON.stringify(
41-
types.createType(registry, lastTypeKey, rawBytes.value.trim())
43+
createType(registry, lastTypeKey, rawBytes.value.trim())
4244
);
4345
} catch (e) {
4446
output.innerText = e;

codec/index.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ <h4>SCALE Decoder</h4>
7373
</div>
7474
</footer>
7575
<script src="https://cdnjs.cloudflare.com/ajax/libs/jsoneditor/7.0.4/jsoneditor.min.js"></script>
76-
<script src="//unpkg.com/polkadot-js-bundle/polkadot.js"></script>
76+
<script src="//unpkg.com/@polkadot/util/bundle-polkadot-util.js"></script>
77+
<script src="//unpkg.com/@polkadot/util-crypto/bundle-polkadot-util-crypto.js"></script>
78+
<script src="//unpkg.com/@polkadot/types/bundle-polkadot-types.js"></script>
7779
<script src="codec.js"></script>
7880
</body>
7981

index.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ <h4>More Utilities</h4>
2727
<li><a href="./codec/">SCALE Decoder</a></li>
2828
<li><a href="./indices/">Query Indices</a></li>
2929
<li><a href="./versions/">Network Versions</a></li>
30+
<li><a href="./nominations/">Query Nominations</a></li>
3031
<li><a href="https://www.shawntabrizi.com/substrate-balance-graph/">Graph Balances</a></li>
3132
<li><a href="https://www.shawntabrizi.com/substrate-weight-graph/">Graph Weights</a></li>
3233
<li><a href="https://www.shawntabrizi.com/substrate-balance-calculator/">Calculate Reserved
@@ -159,7 +160,10 @@ <h4>Sub-Account Generator</h4>
159160
</span>
160161
</div>
161162
</footer>
162-
<script src="//unpkg.com/polkadot-js-bundle/polkadot.js"></script>
163+
<script src="//unpkg.com/@polkadot/util/bundle-polkadot-util.js"></script>
164+
<script src="//unpkg.com/@polkadot/util-crypto/bundle-polkadot-util-crypto.js"></script>
165+
<script src="//unpkg.com/@polkadot/keyring/bundle-polkadot-keyring.js"></script>
166+
163167
<script src="utilities.js"></script>
164168
</body>
165169

indices/index.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ <h4>Query Indices on a Substrate Chain</h4>
6969
</span>
7070
</div>
7171
</footer>
72-
<script src="https://cdnjs.cloudflare.com/ajax/libs/jsoneditor/7.0.4/jsoneditor.min.js"></script>
73-
<script src="//unpkg.com/polkadot-js-bundle/polkadot.js"></script>
72+
<script src="//unpkg.com/@polkadot/util/bundle-polkadot-util.js"></script>
73+
<script src="//unpkg.com/@polkadot/util-crypto/bundle-polkadot-util-crypto.js"></script>
74+
<script src="//unpkg.com/@polkadot/types/bundle-polkadot-types.js"></script>
75+
<script src="//unpkg.com/@polkadot/api/bundle-polkadot-api.js"></script>
7476
<script src="indices.js"></script>
7577
</body>
7678

indices/indices.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const { WsProvider, ApiPromise } = polkadotApi;
2+
13
// Some global variables used by this code.
24
let global = {
35
indices: {},
@@ -8,9 +10,9 @@ let global = {
810
async function connect() {
911
let endpoint = document.getElementById('endpoint').value;
1012
if (!window.substrate || global.endpoint != endpoint) {
11-
const provider = new api.WsProvider(endpoint);
13+
const provider = new WsProvider(endpoint);
1214
document.getElementById('output').innerHTML = 'Connecting to Endpoint...';
13-
window.substrate = await api.ApiPromise.create({ provider });
15+
window.substrate = await ApiPromise.create({ provider });
1416
global.endpoint = endpoint;
1517
document.getElementById('output').innerHTML = 'Connected';
1618
clearIndices();
@@ -33,8 +35,8 @@ async function findIndices(a, b) {
3335

3436
for (let i = 0; i < results.length; i += 2) {
3537
let index = results[i];
36-
let account = results[i+1];
37-
let info =[];
38+
let account = results[i + 1];
39+
let info = [];
3840
info.push(index);
3941
let ss58 = substrate.createType('AccountIndex', index).toString();
4042
info.push(ss58);
@@ -68,11 +70,8 @@ function createTable() {
6870

6971
for (index of Object.keys(global.indices)) {
7072
let tr2 = document.createElement('tr');
71-
console.log(index);
72-
console.log(global.indices[index])
7373

7474
for (key in keys) {
75-
console.log(key)
7675
let td = document.createElement('td');
7776
td.innerText = global.indices[index][key];
7877
tr2.appendChild(td);

nominations/index.html

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<!doctype html>
2+
<html lang="en" class="h-100">
3+
4+
<head>
5+
<meta charset="utf-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
7+
<meta name="description" content="Simple Utilities for Substrate">
8+
<meta name="author" content="Shawn Tabrizi">
9+
<title>Substrate JS Utilities: Nominations</title>
10+
11+
<!-- Bootstrap core CSS -->
12+
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
13+
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
14+
<!-- Custom styles for this template -->
15+
<link href="../custom.css" rel="stylesheet">
16+
<link href="https://cdnjs.cloudflare.com/ajax/libs/jsoneditor/7.0.4/jsoneditor.min.css" rel="stylesheet">
17+
</head>
18+
19+
<body class="d-flex flex-column h-100">
20+
<main role="main" class="flex-shrink-0">
21+
<div class="container">
22+
<h1 class="mt-5"> <a href="../">&#8592; Substrate Utilities</a></h1>
23+
<p class="lead">Hopefully this page helps you do something.</p>
24+
<h4>Query Nominations on a Substrate Chain</h4>
25+
<div class="input-group">
26+
<div class="input-group-prepend">
27+
<span class="input-group-text">Address:</span>
28+
</div>
29+
<input type="text" class="form-control" id="address"
30+
value="12hAtDZJGt4of3m2GqZcUCVAjZPALfvPwvtUTFZPQUbdX1Ud" placeholder="Address" />
31+
<div class="input-group-append">
32+
<a class="btn btn-lg btn-primary" onclick="queryNominations();" role="button">Query Nominations
33+
&raquo;</a>
34+
</div>
35+
</div>
36+
<div class="input-group">
37+
<div class="input-group-prepend">
38+
<span class="input-group-text">Endpoint:</span>
39+
</div>
40+
<input type="text" class="form-control" id="endpoint" value="wss://kusama-rpc.polkadot.io/"
41+
placeholder="wss://..." />
42+
43+
<div class="input-group-prepend">
44+
<span class="input-group-text">Inflation (%):</span>
45+
</div>
46+
<input type="number" class="form-control" id="inflation" value="12" placeholder="12" />
47+
</div>
48+
<div id="output"></div>
49+
<br />
50+
<div class="alert alert-success" role="alert" id="estimate" style="display: none"></div>
51+
<br />
52+
<div class="table-responsive">
53+
<table id="validators-table" class="table table-striped table-sm">
54+
</table>
55+
</div>
56+
</div>
57+
</main>
58+
59+
<footer class="footer mt-auto py-3">
60+
<div class="container">
61+
<span class="text-muted">
62+
<a href="../">Substrate JS Utilities</a>
63+
- Created by <a href="https://shawntabrizi.com">Shawn Tabrizi</a>
64+
- Source on <a href="https://github.com/shawntabrizi/substrate-js-utilities">GitHub</a>
65+
- Using <a href="https://polkadot.js.org/common/">polkadot-js/common</a>
66+
</span>
67+
</div>
68+
</footer>
69+
<script src="//unpkg.com/@polkadot/util/bundle-polkadot-util.js"></script>
70+
<script src="//unpkg.com/@polkadot/util-crypto/bundle-polkadot-util-crypto.js"></script>
71+
<script src="//unpkg.com/@polkadot/types/bundle-polkadot-types.js"></script>
72+
<script src="//unpkg.com/@polkadot/api/bundle-polkadot-api.js"></script>
73+
<script src="nominations.js"></script>
74+
</body>
75+
76+
</html>

nominations/nominations.js

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
const { WsProvider, ApiPromise } = polkadotApi;
2+
const { BN, BN_HUNDRED } = polkadotUtil;
3+
4+
// Some global variables used by this code.
5+
let global = {
6+
endpoint: "",
7+
account: {},
8+
validators: [],
9+
};
10+
11+
function output(text) {
12+
document.getElementById('output').innerHTML = text;
13+
}
14+
15+
// Connect to Substrate endpoint
16+
async function connect() {
17+
let endpoint = document.getElementById('endpoint').value;
18+
if (!window.substrate || global.endpoint != endpoint) {
19+
const provider = new WsProvider(endpoint);
20+
output('Connecting to Endpoint...');
21+
window.substrate = await ApiPromise.create({ provider });
22+
global.endpoint = endpoint;
23+
output('Connected');
24+
}
25+
}
26+
27+
async function getNominations(address) {
28+
output("Querying...");
29+
if (!substrate.derive.staking) {
30+
output("Could not find derive.staking API.");
31+
return;
32+
}
33+
34+
global.account = await substrate.derive.staking.account(address);
35+
if (global.account.nominators.length == 0) {
36+
output(`No nominations found for ${address}.`);
37+
return;
38+
}
39+
global.validators = await substrate.derive.staking.accounts(global.account.nominators);
40+
// pull out commission and get identity
41+
for ([index, validator] of global.validators.entries()) {
42+
validator["identity"] = await substrate.derive.accounts.identity(validator.accountId);
43+
validator["commission"] = validator.validatorPrefs.commission.toHuman();
44+
validator["display"] = validator.identity.displayParent ? validator.identity.displayParent + "/" : "";
45+
validator["display"] += validator.identity.display;
46+
validator["#"] = index;
47+
validator["lastClaimed"] = validator.stakingLedger.claimedRewards.slice(-1)[0];
48+
validator["selfStake"] = substrate.createType("Balance", validator.stakingLedger.active).toHuman();
49+
}
50+
return global.validators;
51+
}
52+
53+
// Create a table with the information
54+
function createTable(validators) {
55+
if (!validators || validator.length == 0) {
56+
output("No nominations found.");
57+
return;
58+
}
59+
output("Creating Table...");
60+
61+
let keys = ["#", "display", "accountId", "commission", "lastClaimed", "selfStake"];
62+
let table = document.getElementById('validators-table');
63+
64+
// Clear table
65+
while (table.firstChild) {
66+
table.removeChild(table.firstChild);
67+
}
68+
69+
let thead = document.createElement('thead');
70+
let tbody = document.createElement('tbody');
71+
72+
let tr = document.createElement('tr');
73+
for (key of keys) {
74+
let th = document.createElement('th');
75+
th.innerText = key;
76+
tr.appendChild(th);
77+
}
78+
79+
for (account of validators) {
80+
let tr2 = document.createElement('tr');
81+
82+
for (key of keys) {
83+
let td = document.createElement('td');
84+
td.innerText = account[key];
85+
tr2.appendChild(td);
86+
}
87+
tbody.appendChild(tr2);
88+
}
89+
90+
thead.appendChild(tr);
91+
table.appendChild(thead);
92+
table.appendChild(tbody);
93+
94+
output("Done.");
95+
}
96+
97+
function estimateReward() {
98+
if (global.account && global.account.stakingLedger) {
99+
let active = global.account.stakingLedger.active.toBn();
100+
let inflation = document.getElementById('inflation').value;
101+
let yearlyInflation = new BN(inflation, 10);
102+
let yearlyReward = active.mul(yearlyInflation).div(BN_HUNDRED);
103+
let daysInYear = new BN("365", 10);
104+
105+
let dailyReward = yearlyReward.div(daysInYear);
106+
global.account["selfStake"] = substrate.createType("Balance", dailyReward).toHuman();
107+
108+
document.getElementById('estimate').innerText = `Active Stake: ${substrate.createType("Balance", active).toHuman()}
109+
Assuming Yearly Inflation: ${yearlyInflation}%
110+
Estimated Yearly Rewards: ${substrate.createType("Balance", yearlyReward).toHuman()}
111+
Estimated Daily Rewards: ${substrate.createType("Balance", dailyReward).toHuman()}
112+
`;
113+
114+
document.getElementById('estimate').style.display = "block";
115+
}
116+
}
117+
118+
// Main function
119+
async function queryNominations() {
120+
try {
121+
await connect();
122+
123+
let address = document.getElementById("address").value;
124+
125+
let validators = await getNominations(address);
126+
estimateReward();
127+
createTable(validators);
128+
129+
} catch (error) {
130+
console.error(error);
131+
output(error);
132+
}
133+
}
134+
135+
document.getElementById('inflation').addEventListener("change", estimateReward);
136+
137+
connect();

0 commit comments

Comments
 (0)