Skip to content

Commit b3fafa5

Browse files
authored
Hosted fungible example (#39)
* Hosted fungible example * `examples/hosted-counter`: fix up style for shared stylesheet * `hosted-fungible`: improve error handling * `examples/hosted-fungible`: add copy button * `client`: update manifest for publishing * `linera-protocol`: bring to trunk
1 parent 87f28be commit b3fafa5

File tree

13 files changed

+397
-19
lines changed

13 files changed

+397
-19
lines changed

client/package.json

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@linera/client",
3-
"version": "1.0.0",
4-
"description": "",
3+
"version": "0.0.3",
4+
"description": "Web client for the Linera blockchain",
55
"main": "dist/linera_web.js",
66
"types": "dist/linera_web.d.ts",
77
"files": "dist",
@@ -15,5 +15,13 @@
1515
},
1616
"keywords": [],
1717
"author": "Linera <[email protected]>",
18-
"license": "Apache-2.0"
18+
"license": "Apache-2.0",
19+
"repository": {
20+
"type": "git",
21+
"url": "git+https://github.com/linera-io/linera-web.git"
22+
},
23+
"bugs": {
24+
"url": "https://github.com/linera-io/linera-web/issues"
25+
},
26+
"homepage": "https://github.com/linera-io/linera-web#readme"
1927
}

client/src/lib.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,21 @@ impl JsFaucet {
137137
.mutate(|wallet| wallet.set_default_chain(outcome.chain_id))
138138
.await??;
139139
context.client.track_chain(outcome.chain_id);
140+
let chain_client = context.make_chain_client(outcome.chain_id)?;
141+
wasm_bindgen_futures::spawn_local(async move {
142+
let (listener, listen_handle, mut notifications) = chain_client.listen().await.unwrap();
143+
wasm_bindgen_futures::spawn_local(async move {
144+
while let Some(notification) = notifications.next().await {
145+
if let linera_core::worker::Reason::NewIncomingBundle { .. } =
146+
notification.reason
147+
{
148+
chain_client.process_inbox().await.unwrap();
149+
}
150+
}
151+
});
152+
listener.await;
153+
drop(listen_handle);
154+
});
140155
Ok(outcome.chain_id.to_string())
141156
}
142157
}
@@ -186,6 +201,13 @@ pub struct Client {
186201
#[derive(Clone)]
187202
pub struct Frontend(Client);
188203

204+
#[derive(serde::Deserialize)]
205+
struct TransferParams {
206+
donor: Option<linera_base::identifiers::Owner>,
207+
amount: u64,
208+
recipient: linera_base::identifiers::Account,
209+
}
210+
189211
#[wasm_bindgen]
190212
impl Client {
191213
/// Creates a new client and connects to the network.
@@ -279,6 +301,30 @@ impl Client {
279301
result
280302
}
281303

304+
#[wasm_bindgen]
305+
pub async fn transfer(&self, options: wasm_bindgen::JsValue) -> JsResult<()> {
306+
let params: TransferParams = serde_wasm_bindgen::from_value(options)?;
307+
let chain_client = self.default_chain_client().await?;
308+
309+
let _hash = self
310+
.apply_client_command(&chain_client, || {
311+
chain_client.transfer(
312+
params.donor,
313+
linera_base::data_types::Amount::from_tokens(params.amount.into()),
314+
linera_execution::system::Recipient::Account(params.recipient),
315+
)
316+
})
317+
.await??;
318+
319+
Ok(())
320+
}
321+
322+
pub async fn identity(&self) -> JsResult<JsValue> {
323+
Ok(serde_wasm_bindgen::to_value(
324+
&self.default_chain_client().await?.identity().await?,
325+
)?)
326+
}
327+
282328
/// Gets an object implementing the API for Web frontends.
283329
#[wasm_bindgen]
284330
#[must_use]

examples/assets/style.css

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ body {
2222
height: 100%;
2323
padding: 0.5rem;
2424
display: grid;
25-
grid-template-columns: repeat(auto-fit, minmax(496px, 1fr));
25+
grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
2626
}
2727

2828
.content {
@@ -45,11 +45,7 @@ p {
4545
max-width: 20rem;
4646
}
4747

48-
.counter {
49-
font-size: 1.25rem;
50-
}
51-
52-
button {
48+
button, input[type="submit"] {
5349
background-color: rgb(222, 42, 2);
5450
color: #f3eee2;
5551
border: none;
@@ -65,11 +61,19 @@ button {
6561
height: fit-content;
6662
}
6763

64+
button[disabled], input[type="submit"][disabled] {
65+
background-color: gray;
66+
}
67+
68+
input[type="text"] {
69+
padding: 0.3rem;
70+
}
71+
6872
button:hover {
6973
background-color: #d62f1a;
7074
}
7175

72-
.log-panel {
76+
.logs {
7377
background-color: rgb(222, 42, 2);
7478
color: #f3eee2;
7579
border-radius: 24px;
@@ -80,18 +84,18 @@ button:hover {
8084
overflow: hidden;
8185
}
8286

83-
.log-panel>* {
87+
.logs>* {
8488
white-space: nowrap;
8589
}
8690

87-
.log-entry {
91+
#logs li {
8892
font-family: monospace;
8993
font-size: 0.9rem;
9094
padding: 0.5rem;
9195
overflow: hidden;
9296
}
9397

94-
.log-entry .height {
98+
#logs li .height {
9599
min-width: 3em;
96100
}
97101

@@ -110,6 +114,11 @@ h1::before {
110114
padding-bottom: 0.1em;
111115
}
112116

117+
input {
118+
background-color: #fffef2;
119+
border-style: solid;
120+
}
121+
113122
@keyframes breathe {
114123
from {
115124
scale: 100%;

examples/hosted-counter/index.html

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
display: flex;
1212
flex-direction: column;
1313
}
14+
15+
.ui .counter {
16+
font-size: 1.25rem;
17+
}
1418
</style>
1519
</head>
1620
<body>
@@ -31,13 +35,13 @@ <h1>Counter</h1>
3135
</div>
3236
</div>
3337

34-
<div class="log-panel">
38+
<div class="logs">
3539
<h2>Chain history for <code id="chain-id" class="hex">requesting chain…</code></h2>
36-
<div class="logs" id="logs">
37-
<template id="block-template">
38-
<div class="log-entry">
40+
<ul id="logs">
41+
<template>
42+
<li>
3943
<span class="height"></span>: <span class="code hash"></span>
40-
</div>
44+
</li>
4145
</template>
4246
</div>
4347
</div>
@@ -74,6 +78,7 @@ <h2>Chain history for <code id="chain-id" class="hex">requesting chain…</code>
7478
logs.insertBefore(entry, logs.firstChild);
7579
}
7680

81+
7782
async function updateCount() {
7883
const response = await counter.query('{ "query": "query { value }" }');
7984
document.getElementById('count').innerText

0 commit comments

Comments
 (0)