mirror of
https://github.com/fluencelabs/examples
synced 2025-06-26 08:11:37 +00:00
Getting started intro examples (#13)
This commit is contained in:
160
intro/3-hello-world-web3/src/App.scss
Normal file
160
intro/3-hello-world-web3/src/App.scss
Normal file
@ -0,0 +1,160 @@
|
||||
$color1: black;
|
||||
$color2: rgb(214, 214, 214);
|
||||
$accent-color: rgb(225, 30, 90);
|
||||
|
||||
.logo {
|
||||
height: 15vmin;
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
.mono {
|
||||
font-family: monospace, monospace;
|
||||
}
|
||||
|
||||
.bold {
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
header {
|
||||
margin-top: 10vmin;
|
||||
}
|
||||
|
||||
header,
|
||||
h1,
|
||||
h2,
|
||||
h3 {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.content {
|
||||
width: 800px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
align-content: center;
|
||||
margin-left: auto;
|
||||
margin-right: auto;
|
||||
}
|
||||
|
||||
ul,
|
||||
li {
|
||||
list-style-type: none;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.p {
|
||||
width: 550px;
|
||||
}
|
||||
|
||||
.btn-clipboard {
|
||||
border: none;
|
||||
background-color: transparent;
|
||||
cursor: pointer;
|
||||
|
||||
&:hover,
|
||||
&:focus {
|
||||
color: $accent-color;
|
||||
}
|
||||
|
||||
&:focus {
|
||||
outline: none;
|
||||
}
|
||||
}
|
||||
|
||||
.btn {
|
||||
height: 26px;
|
||||
border: 1px solid;
|
||||
border-color: $color2;
|
||||
|
||||
background-color: transparent;
|
||||
|
||||
margin: 5px;
|
||||
|
||||
font-size: 16px;
|
||||
|
||||
color: $color1;
|
||||
|
||||
&::placeholder {
|
||||
color: $color2;
|
||||
}
|
||||
|
||||
&:hover,
|
||||
&:focus {
|
||||
outline: 1px solid white;
|
||||
border-color: $accent-color;
|
||||
color: $accent-color;
|
||||
}
|
||||
}
|
||||
|
||||
.btn-hello {
|
||||
width: 200px;
|
||||
display: inline;
|
||||
float: right;
|
||||
}
|
||||
|
||||
table {
|
||||
text-align: right;
|
||||
}
|
||||
|
||||
.label {
|
||||
width: 70px;
|
||||
display: inline-block;
|
||||
}
|
||||
|
||||
.input {
|
||||
width: 500px;
|
||||
height: 26px;
|
||||
box-sizing: border-box;
|
||||
|
||||
margin: 5px;
|
||||
border: 1px solid;
|
||||
border-color: $color2;
|
||||
|
||||
color: $color1;
|
||||
|
||||
&::placeholder {
|
||||
color: $color2;
|
||||
}
|
||||
|
||||
&:hover,
|
||||
&:focus {
|
||||
outline: 1px solid white;
|
||||
border-color: $accent-color;
|
||||
}
|
||||
}
|
||||
|
||||
.gg-clipboard {
|
||||
box-sizing: border-box;
|
||||
position: relative;
|
||||
display: block;
|
||||
transform: scale(var(--ggs, 1));
|
||||
width: 18px;
|
||||
height: 18px;
|
||||
border: 2px solid;
|
||||
border-radius: 2px;
|
||||
}
|
||||
.gg-clipboard::after,
|
||||
.gg-clipboard::before {
|
||||
content: "";
|
||||
display: block;
|
||||
box-sizing: border-box;
|
||||
position: absolute;
|
||||
border-radius: 2px;
|
||||
width: 10px;
|
||||
left: 2px;
|
||||
}
|
||||
.gg-clipboard::before {
|
||||
border: 2px solid;
|
||||
border-bottom-left-radius: 3px;
|
||||
border-bottom-right-radius: 3px;
|
||||
top: -2px;
|
||||
height: 6px;
|
||||
}
|
||||
.gg-clipboard::after {
|
||||
height: 2px;
|
||||
background: currentColor;
|
||||
box-shadow: 0 -4px 0 0;
|
||||
bottom: 2px;
|
||||
}
|
145
intro/3-hello-world-web3/src/App.tsx
Normal file
145
intro/3-hello-world-web3/src/App.tsx
Normal file
@ -0,0 +1,145 @@
|
||||
import React, { useState } from "react";
|
||||
import logo from "./logo.svg";
|
||||
import "./App.scss";
|
||||
|
||||
import { createClient, FluenceClient } from "@fluencelabs/fluence";
|
||||
import { krasnodar } from "@fluencelabs/fluence-network-environment";
|
||||
import { sayHello } from "./_aqua/getting-started";
|
||||
|
||||
const relayNodes = [krasnodar[0], krasnodar[1], krasnodar[2]];
|
||||
|
||||
const copyToClipboard = (text: string) => {
|
||||
navigator.clipboard.writeText(text);
|
||||
};
|
||||
|
||||
function App() {
|
||||
const [client, setClient] = useState<FluenceClient | null>(null);
|
||||
const [helloFrom, setHelloFrom] = useState<string | null>(null);
|
||||
|
||||
const [peerIdInput, setPeerIdInput] = useState<string>("");
|
||||
const [relayPeerIdInput, setRelayPeerIdInput] = useState<string>("");
|
||||
|
||||
const isConnected = client !== null;
|
||||
|
||||
const connect = (relayPeerId: string) => {
|
||||
createClient(relayPeerId)
|
||||
.then((client) => {
|
||||
// Register handler for this call in aqua:
|
||||
// HelloWorld.recieveHello(%init_peer_id%)
|
||||
client.callServiceHandler.onEvent(
|
||||
"HelloWorld",
|
||||
"recieveHello",
|
||||
(args) => {
|
||||
const [msg] = args;
|
||||
setHelloFrom(msg);
|
||||
}
|
||||
);
|
||||
setClient(client);
|
||||
})
|
||||
.catch((err) => console.log("Client initialization failed", err));
|
||||
};
|
||||
|
||||
const doSayHello = async () => {
|
||||
if (client === null) {
|
||||
return;
|
||||
}
|
||||
const res = await sayHello(client!, peerIdInput, relayPeerIdInput);
|
||||
setHelloFrom(res);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="App">
|
||||
<header>
|
||||
<img src={logo} className="logo" alt="logo" />
|
||||
</header>
|
||||
|
||||
<div className="content">
|
||||
{isConnected ? (
|
||||
<>
|
||||
<h1>Connected</h1>
|
||||
<table>
|
||||
<tr>
|
||||
<td className="bold">Peer id:</td>
|
||||
<td className="mono">{client!.selfPeerId}</td>
|
||||
<td>
|
||||
<button
|
||||
className="btn-clipboard"
|
||||
onClick={() => copyToClipboard(client!.selfPeerId)}
|
||||
>
|
||||
<i className="gg-clipboard"></i>
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td className="bold">Relay peer id:</td>
|
||||
<td className="mono">{client!.relayPeerId}</td>
|
||||
<td>
|
||||
<button
|
||||
className="btn-clipboard"
|
||||
onClick={() => copyToClipboard(client!.relayPeerId!)}
|
||||
>
|
||||
<i className="gg-clipboard"></i>
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<div>
|
||||
<h2>Say hello!</h2>
|
||||
<p className="p">
|
||||
Now try opening a new tab with the same application. Copy paste
|
||||
the peer id and relay from the second tab and say hello!
|
||||
</p>
|
||||
<div className="row">
|
||||
<label className="label bold">Peer id</label>
|
||||
<input
|
||||
className="input"
|
||||
type="text"
|
||||
onChange={(e) => setPeerIdInput(e.target.value)}
|
||||
value={peerIdInput}
|
||||
/>
|
||||
</div>
|
||||
<div className="row">
|
||||
<label className="label bold">Relay</label>
|
||||
<input
|
||||
className="input"
|
||||
type="text"
|
||||
onChange={(e) => setRelayPeerIdInput(e.target.value)}
|
||||
value={relayPeerIdInput}
|
||||
/>
|
||||
</div>
|
||||
<div className="row">
|
||||
<button className="btn btn-hello" onClick={doSayHello}>
|
||||
say hello
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<h1>Pick a relay</h1>
|
||||
<ul>
|
||||
{relayNodes.map((x) => (
|
||||
<li key={x.peerId}>
|
||||
<span className="mono">{x.peerId}</span>
|
||||
<button className="btn" onClick={() => connect(x.multiaddr)}>
|
||||
Connect
|
||||
</button>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</>
|
||||
)}
|
||||
|
||||
{helloFrom && (
|
||||
<>
|
||||
<h2>Hello from</h2>
|
||||
<div> {helloFrom} </div>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default App;
|
106
intro/3-hello-world-web3/src/_aqua/getting-started.ts
Normal file
106
intro/3-hello-world-web3/src/_aqua/getting-started.ts
Normal file
@ -0,0 +1,106 @@
|
||||
/**
|
||||
*
|
||||
* This file is auto-generated. Do not edit manually: changes may be erased.
|
||||
* Generated by Aqua compiler: https://github.com/fluencelabs/aqua/.
|
||||
* If you find any bugs, please write an issue on GitHub: https://github.com/fluencelabs/aqua/issues
|
||||
* Aqua version: 0.1.9-162
|
||||
*
|
||||
*/
|
||||
import { FluenceClient, PeerIdB58 } from '@fluencelabs/fluence';
|
||||
import { RequestFlowBuilder } from '@fluencelabs/fluence/dist/api.unstable';
|
||||
import { RequestFlow } from '@fluencelabs/fluence/dist/internal/RequestFlow';
|
||||
|
||||
|
||||
|
||||
export async function sayHello(client: FluenceClient, peerId: string, relayPeerId: string, config?: {ttl?: number}): Promise<string> {
|
||||
let request: RequestFlow;
|
||||
const promise = new Promise<string>((resolve, reject) => {
|
||||
const r = new RequestFlowBuilder()
|
||||
.disableInjections()
|
||||
.withRawScript(
|
||||
`
|
||||
(xor
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||
(call %init_peer_id% ("getDataSrv" "peerId") [] peerId)
|
||||
)
|
||||
(call %init_peer_id% ("getDataSrv" "relayPeerId") [] relayPeerId)
|
||||
)
|
||||
(call -relay- ("op" "noop") [])
|
||||
)
|
||||
(xor
|
||||
(seq
|
||||
(call -relay- ("op" "noop") [])
|
||||
(call "12D3KooWHLxVhUQyAuZe6AHMB29P7wkvTNMn7eDMcsqimJYLKREf" ("ba24be5b-9789-48ac-b38a-82c9d3eb0d34" "hello_world") [%init_peer_id%] comp)
|
||||
)
|
||||
(seq
|
||||
(call -relay- ("op" "noop") [])
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||
)
|
||||
)
|
||||
)
|
||||
(call -relay- ("op" "noop") [])
|
||||
)
|
||||
(par
|
||||
(seq
|
||||
(call relayPeerId ("op" "noop") [])
|
||||
(xor
|
||||
(call peerId ("HelloWorld" "recieveHello") [comp.$.msg!])
|
||||
(seq
|
||||
(seq
|
||||
(call relayPeerId ("op" "noop") [])
|
||||
(call -relay- ("op" "noop") [])
|
||||
)
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||
)
|
||||
)
|
||||
)
|
||||
(null)
|
||||
)
|
||||
)
|
||||
(xor
|
||||
(call %init_peer_id% ("callbackSrv" "response") [comp.$.reply!])
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
|
||||
)
|
||||
)
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 4])
|
||||
)
|
||||
|
||||
`,
|
||||
)
|
||||
.configHandler((h) => {
|
||||
h.on('getDataSrv', '-relay-', () => {
|
||||
return client.relayPeerId!;
|
||||
});
|
||||
h.on('getDataSrv', 'peerId', () => {return peerId;});
|
||||
h.on('getDataSrv', 'relayPeerId', () => {return relayPeerId;});
|
||||
h.onEvent('callbackSrv', 'response', (args) => {
|
||||
const [res] = args;
|
||||
resolve(res);
|
||||
});
|
||||
|
||||
h.onEvent('errorHandlingSrv', 'error', (args) => {
|
||||
// assuming error is the single argument
|
||||
const [err] = args;
|
||||
reject(err);
|
||||
});
|
||||
})
|
||||
.handleScriptError(reject)
|
||||
.handleTimeout(() => {
|
||||
reject('Request timed out for sayHello');
|
||||
})
|
||||
if(config?.ttl) {
|
||||
r.withTTL(config.ttl)
|
||||
}
|
||||
request = r.build();
|
||||
});
|
||||
await client.initiateFlow(request!);
|
||||
return promise;
|
||||
}
|
||||
|
13
intro/3-hello-world-web3/src/index.css
Normal file
13
intro/3-hello-world-web3/src/index.css
Normal file
@ -0,0 +1,13 @@
|
||||
body {
|
||||
margin: 0;
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
|
||||
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
|
||||
sans-serif;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
-moz-osx-font-smoothing: grayscale;
|
||||
}
|
||||
|
||||
code {
|
||||
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
|
||||
monospace;
|
||||
}
|
12
intro/3-hello-world-web3/src/index.tsx
Normal file
12
intro/3-hello-world-web3/src/index.tsx
Normal file
@ -0,0 +1,12 @@
|
||||
import React from 'react';
|
||||
import ReactDOM from 'react-dom';
|
||||
import './index.css';
|
||||
import App from './App';
|
||||
|
||||
ReactDOM.render(
|
||||
<React.StrictMode>
|
||||
<App />
|
||||
</React.StrictMode>,
|
||||
document.getElementById('root')
|
||||
);
|
||||
|
17
intro/3-hello-world-web3/src/logo.svg
Normal file
17
intro/3-hello-world-web3/src/logo.svg
Normal file
File diff suppressed because one or more lines are too long
After Width: | Height: | Size: 11 KiB |
1
intro/3-hello-world-web3/src/react-app-env.d.ts
vendored
Normal file
1
intro/3-hello-world-web3/src/react-app-env.d.ts
vendored
Normal file
@ -0,0 +1 @@
|
||||
/// <reference types="react-scripts" />
|
Reference in New Issue
Block a user