|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | + <head> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 6 | + <title>Linera | Counter</title> |
| 7 | + <link href="/style.css" rel="stylesheet"> |
| 8 | + <link href="/icon.png" rel="icon"> |
| 9 | + <style type="text/css"> |
| 10 | + .ui { |
| 11 | + display: flex; |
| 12 | + flex-direction: column; |
| 13 | + } |
| 14 | + |
| 15 | + .ui .counter { |
| 16 | + font-size: 1.25rem; |
| 17 | + } |
| 18 | + </style> |
| 19 | + </head> |
| 20 | + <body> |
| 21 | + <div class="container"> |
| 22 | + <div class="content"> |
| 23 | + <div class="description"> |
| 24 | + <h1>Counter</h1> |
| 25 | + <p> |
| 26 | + This is a simple application tracking some on-chain state that remembers the value of an integer counter. |
| 27 | + </p> |
| 28 | + <p> |
| 29 | + Click the button to submit a block that increments the counter, and watch your local node's state update in real-time. |
| 30 | + </p> |
| 31 | + </div> |
| 32 | + <div class="ui"> |
| 33 | + <p class="counter">Clicks: <span id="count">0</span></p> |
| 34 | + <button id="increment-btn">Click me!</button> |
| 35 | + </div> |
| 36 | + </div> |
| 37 | + |
| 38 | + <div class="logs"> |
| 39 | + <h2>Connected as <code id="owner" class="hex">requesting owner…</code> </h2> |
| 40 | + <h2>Chain history for <code id="chain-id" class="hex">requesting chain…</code></h2> |
| 41 | + <ul id="logs"> |
| 42 | + <template> |
| 43 | + <li> |
| 44 | + <span class="height"></span>: <span class="code hash"></span> |
| 45 | + </li> |
| 46 | + </template> |
| 47 | + </div> |
| 48 | + </div> |
| 49 | + </div> |
| 50 | + |
| 51 | + <script type="importmap"> |
| 52 | + { |
| 53 | + "imports": { |
| 54 | + "@linera/client": "./js/@linera/client/linera_web.js", |
| 55 | + "@linera/signer": "./js/@linera/signer/index.js" |
| 56 | + } |
| 57 | + } |
| 58 | + </script> |
| 59 | + |
| 60 | + <script type="module"> |
| 61 | + import * as linera from '@linera/client'; |
| 62 | + import * as linera_signer from '@linera/signer'; |
| 63 | + |
| 64 | + const COUNTER_APP_ID = import.meta.env.VITE_COUNTER_APP_ID; |
| 65 | + |
| 66 | + async function run() { |
| 67 | + await linera.default(); |
| 68 | + const faucet = await new linera.Faucet(import.meta.env.VITE_FAUCET_URL); |
| 69 | + const signer = await new linera_signer.MetaMaskEIP191Signer(); |
| 70 | + const wallet = await faucet.createWallet(); |
| 71 | + const owner = await signer.address(); |
| 72 | + document.getElementById('owner').innerText = owner; |
| 73 | + document.getElementById('chain-id').innerText = await faucet.claimChain(wallet, owner); |
| 74 | + const client = await new linera.Client(wallet, signer); |
| 75 | + const counter = await client.frontend().application(COUNTER_APP_ID); |
| 76 | + const logs = document.getElementById('logs'); |
| 77 | + const incrementButton = document.getElementById('increment-btn'); |
| 78 | + const blockTemplate = document.getElementById('block-template'); |
| 79 | + |
| 80 | + function addLogEntry(block) { |
| 81 | + const entry = logs.getElementsByTagName('template')[0].content.cloneNode(true); |
| 82 | + entry.querySelector('.height').textContent = block.height; |
| 83 | + entry.querySelector('.hash').textContent = block.hash; |
| 84 | + logs.insertBefore(entry, logs.firstChild); |
| 85 | + } |
| 86 | + |
| 87 | + async function updateCount() { |
| 88 | + const response = await counter.query('{ "query": "query { value }" }'); |
| 89 | + document.getElementById('count').innerText |
| 90 | + = JSON.parse(response).data.value; |
| 91 | + } |
| 92 | + |
| 93 | + updateCount(); |
| 94 | + client.onNotification(notification => { |
| 95 | + let newBlock = notification.reason.NewBlock; |
| 96 | + if (!newBlock) return; |
| 97 | + addLogEntry(newBlock); |
| 98 | + updateCount(); |
| 99 | + }); |
| 100 | + |
| 101 | + incrementButton.addEventListener('click', () => { |
| 102 | + counter.query('{ "query": "mutation { increment(value: 1) }" }'); |
| 103 | + }); |
| 104 | + } |
| 105 | + |
| 106 | + if (document.readyState === 'loading') |
| 107 | + document.addEventListener('DOMContentLoaded', run); |
| 108 | + else |
| 109 | + run(); |
| 110 | + </script> |
| 111 | + </body> |
| 112 | +</html> |
0 commit comments