-
Notifications
You must be signed in to change notification settings - Fork 37
/
index.html
147 lines (124 loc) · 5.11 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Nimiq Demo</title>
<script type="module">
import * as Nimiq from '/nimiq/web/index.js';
window.Nimiq = Nimiq;
function _onConsensusChanged(state) {
document.getElementById('message').innerText = `Consensus ${state}.`;
if (state === 'established') {
_updateBalance();
}
}
async function _updateBalance() {
// Get current balance.
const account = await $.client.getAccount($.keypair.toAddress());
_onBalanceChanged(account);
}
function _onBalanceChanged(account) {
console.log(`New balance of ${$.keypair.toAddress().toUserFriendlyAddress()} is ${account.balance}.`);
document.getElementById('balance').innerText = (account.balance / 1e5).toFixed(2);
}
async function _onHeadChanged() {
const height = await $.client.getHeadHeight();
console.log(`Now at height ${height}.`);
document.getElementById('height').innerText = height;
// Recheck balance at every macro block (60s).
if (Nimiq.Policy.isMacroBlockAt(height)) {
_updateBalance();
}
}
async function _updatePeerCount(_peerId, _reason, peerCount, _peerInfo) {
// console.log(`Now connected to ${peerCount} peers.`);
document.getElementById('peers').innerText = peerCount;
}
const nimiqInitPromise = Nimiq.default().then(() => {
// Create client configuration so it can be manipulated before creating the client.
window.config = new Nimiq.ClientConfiguration();
}).catch(error => {
alert(`Error: ${error.message}`);
throw error;
});
window.init = async function(clientType = 'light') {
await nimiqInitPromise;
document.getElementById('message').innerText = 'Nimiq loaded. Connecting and establishing consensus.';
const $ = {};
window.$ = $;
// Instantiate the client based on the configuration.
// The client automatically connects to the network when instantiated.
$.client = await Nimiq.Client.create(window.config.build());
$.keypair = await Nimiq.KeyPair.generate();
document.getElementById('address').innerText = $.keypair.toAddress().toUserFriendlyAddress();
$.client.addConsensusChangedListener(_onConsensusChanged);
$.client.addHeadChangedListener(_onHeadChanged);
$.client.addPeerChangedListener(_updatePeerCount);
}
window.mainnet = function() {
window.config.network('mainalbatross');
window.config.seedNodes([
'/dns4/aurora.seed.nimiq.com/tcp/443/wss',
'/dns4/catalyst.seed.nimiq.network/tcp/443/wss',
'/dns4/cipher.seed.nimiq-network.com/tcp/443/wss',
'/dns4/eclipse.seed.nimiq.cloud/tcp/443/wss',
'/dns4/lumina.seed.nimiq.systems/tcp/443/wss',
'/dns4/nebula.seed.nimiq.com/tcp/443/wss',
'/dns4/nexus.seed.nimiq.network/tcp/443/wss',
'/dns4/polaris.seed.nimiq-network.com/tcp/443/wss',
'/dns4/photon.seed.nimiq.cloud/tcp/443/wss',
'/dns4/pulsar.seed.nimiq.systems/tcp/443/wss',
'/dns4/quasar.seed.nimiq.com/tcp/443/wss',
'/dns4/solstice.seed.nimiq.network/tcp/443/wss',
'/dns4/vortex.seed.nimiq.cloud/tcp/443/wss',
'/dns4/zenith.seed.nimiq.systems/tcp/443/wss',
]);
console.log('Configured client for mainnet.');
}
</script>
<!-- Use Nimiq's brand font (regular and bold weights). -->
<link href="https://fonts.googleapis.com/css?family=Mulish:400,700" rel="stylesheet">
</head>
<body>
<h1>Getting started with the Nimiq Client API</h1>
This HTML file demonstrates the most simple way to build web applications on top of the <a target="_blank" href="https://nimiq.com">Nimiq Blockchain</a>.
<h2>Most Simple Client</h2>
<p id="client-selector">
Client type:
<!-- <a href="#" client-type="pico">pico</a> -->
<a href="#" client-type="light">light</a>
</p>
<div id="client-info" style="display:none">
<p>Status: <span id="message">Loading Nimiq.</span></p>
<p>Account address: <span id="address"><em>loading</em></span></p>
<p>Balance: ⬣ <span id="balance"><em>loading</em></span></p>
<p>Current block height: <span id="height"><em>loading</em></span></p>
<p>Current number of peers: <span id="peers"><em>loading</em></span></p>
</div>
<h2>Source Code</h2>
<a target="_blank" href="https://github.com/nimiq/nimiq-demo/blob/master/index.html">View Source Code on Github.</a>
<style>
body {
background: #F4F4F4;
color: #1F2348; /* Nimiq Indigo */
font-family: 'Mulish', sans-serif;
padding: 6% 8%;
}
a {
color: #0582CA; /* Nimiq Blue */
}
</style>
<script>
/* Attach UI event handlers. */
document.querySelectorAll('[client-type]').forEach(el => {
el.addEventListener('click', (event) => {
event.preventDefault();
const type = el.getAttribute('client-type');
document.getElementById('client-selector').textContent = 'Client type: ' + type;
document.getElementById('client-info').style.display = 'block';
init(type);
});
});
</script>
</body>
</html>