diff --git a/intro/4-ipfs-code-execution/web/src/App.tsx b/intro/4-ipfs-code-execution/web/src/App.tsx index c144ff4..345949a 100644 --- a/intro/4-ipfs-code-execution/web/src/App.tsx +++ b/intro/4-ipfs-code-execution/web/src/App.tsx @@ -5,9 +5,14 @@ import "./App.scss"; import { createClient, FluenceClient } from "@fluencelabs/fluence"; import { get_external_api_multiaddr } from "@fluencelabs/aqua-ipfs"; import { stage } from "@fluencelabs/fluence-network-environment"; -import { deploy_service, put_file_size, remove_service, provideFile } from "@fluencelabs/ipfs-execution"; -import { Multiaddr, protocols } from 'multiaddr'; -const { create, globSource, urlSource, CID } = require('ipfs-http-client'); +import { + deploy_service, + put_file_size, + remove_service, + provideFile, +} from "@fluencelabs/ipfs-execution"; +import { Multiaddr, protocols } from "multiaddr"; +const { create, globSource, urlSource, CID } = require("ipfs-http-client"); const relayNodes = [stage[0], stage[1], stage[2]]; @@ -16,29 +21,37 @@ const copyToClipboard = (text: string) => { }; function fromOption(opt: T | T[] | null): T | null { - if (Array.isArray(opt)) { - if (opt.length === 0) { return null; } - - opt = opt[0]; + if (Array.isArray(opt)) { + if (opt.length === 0) { + return null; } - if (opt === null) { return null; } - - return opt; + + opt = opt[0]; + } + if (opt === null) { + return null; + } + + return opt; } function decapsulateP2P(rpcAddr: string): string { - return new Multiaddr(rpcAddr).decapsulateCode(protocols.names.p2p.code).toString(); + return new Multiaddr(rpcAddr) + .decapsulateCode(protocols.names.p2p.code) + .toString(); } function App() { const [client, setClient] = useState(null); const [serviceId, setServiceId] = useState(null); - const [wasm, setWasm] = useState("Qmf8fH2cDZXGKS9uDGBcHxv5uQ51ChrigdZKe3QxS2C1AF"); - const [rpcAddr, setRpcAddr] = useState(""); + const [wasm, setWasm] = useState( + "Qmf8fH2cDZXGKS9uDGBcHxv5uQ51ChrigdZKe3QxS2C1AF" + ); + const [rpcAddr, setRpcAddr] = useState(""); const [fileCID, setFileCID] = useState(""); - const [fileSize, setFileSize] = useState(null); - const [fileSizeCID, setFileSizeCID] = useState(null); + const [fileSize, setFileSize] = useState(""); + const [fileSizeCID, setFileSizeCID] = useState(""); const isConnected = client !== null; const gotRpcAddr = rpcAddr !== null; @@ -64,7 +77,7 @@ function App() { console.log("getRpcAddr result", result); let rpcAddr = result.multiaddr; setRpcAddr(decapsulateP2P(rpcAddr)); - } + }; const deployService = async () => { console.log("wasm %s rpcAddr %s", wasm, rpcAddr); @@ -72,10 +85,12 @@ function App() { return; } var service_id = await deploy_service( - client, - client.relayPeerId!, wasm, rpcAddr, - (msg, value) => console.log(msg, value), - { ttl: 10000 } + client, + client.relayPeerId!, + wasm, + rpcAddr, + (msg, value) => console.log(msg, value), + { ttl: 10000 } ); service_id = fromOption(service_id); setServiceId(service_id); @@ -87,9 +102,12 @@ function App() { } var putResult = await put_file_size( - client, - client.relayPeerId!, fileCID, rpcAddr, serviceId, - size => setFileSize(size.toString()), + client, + client.relayPeerId!, + fileCID, + rpcAddr, + serviceId, + (size) => setFileSize(size.toString()), (label, error) => setFileSize("Error: " + label + ": " + error), { ttl: 10000 } ); @@ -109,238 +127,253 @@ function App() { return; } - await remove_service(client, client.relayPeerId!, serviceId, { ttl: 10000 }); + await remove_service(client, client.relayPeerId!, serviceId, { + ttl: 10000, + }); setServiceId(null); }; - console.log("isConnected gotRpcAddr deployed\n", isConnected, gotRpcAddr, deployed); + console.log( + "isConnected gotRpcAddr deployed\n", + isConnected, + gotRpcAddr, + deployed + ); - if (!isConnected) { - return (
-
- logo -
+ return ( +
+
+ logo +
-
- <> -

Pick a relay

-
    - {relayNodes.map((x) => ( -
  • - {x.peerId} - -
  • - ))} -
- -
-
- ); - } else if (isConnected && gotRpcAddr && !deployed) { - return ( -
-
- logo -
+
+ {!isConnected && } + {isConnected && } + {isConnected && gotRpcAddr && !deployed && ( + + )} + {deployed && ( + + )} +
+
+ ); +} -
- <> -

Connected

- - - - - - - - - - - -
Peer id:{client!.selfPeerId} - -
Relay peer id:{client!.relayPeerId} - -
-
-
-

Set IPFS RPC address:

-

- Specify IPFS to download process_files.wasm from -

- setRpcAddr(e.target.value)} - value={rpcAddr!} - /> -
-
-

Set process_files.wasm module CID

-

- To deploy a service, specify CID of WebAssembly module. -

- setWasm(e.target.value)} - value={wasm!} - /> -
-
-
-

Deploy ProcessFiles service

-

- process_files.wasm will be downloaded via IPFS to the Fluence node, - and then a service will be dynamically created from it! - - After that, you will be able to use that service to get sizes of IPFS files! -

-
- -
-
- -
+const Deployed = (props: { + setRpcAddr: React.Dispatch>; + rpcAddr: string; + setFileCID: React.Dispatch>; + fileCID: string; + fileSize: string; + fileSizeCID: string; + getFileSize: () => Promise; + wasm: string; + serviceId: string | null; + removeService: () => {}; +}) => { + return ( + <> +

Deployed

+ + + + + + + + + + + + + + + +
process_files.wasm CID:{props.wasm} + +
ProcessFiles service ID:{props.serviceId}
File Size:{props.fileSize}
+
+
+

Set IPFS RPC address:

+

Specify IPFS to download file from

+ props.setRpcAddr(e.target.value)} + value={props.rpcAddr} + />
- ) - } else if (deployed) { - return ( -
-
- logo -
+

Get file size

+

+ Upload any file to IPFS node +

+ {props.rpcAddr} +

+ paste CID here and get the size of that file via ProcessFiles service + you have just deployed +

+
+ + props.setFileCID(e.target.value)} + value={props.fileCID} + /> +
+
+ +
+
+
+ + +
+
+ + +
+ + ); +}; -
- <> -

Deployed

- - - - - - - - - - - - - - - - - - - - - - - - - -
Peer id:{client!.selfPeerId} - -
Relay peer id:{client!.relayPeerId} - -
process_files.wasm CID:{wasm} - -
ProcessFiles service ID:{serviceId}
File Size:{fileSize}
-
-
-

Set IPFS RPC address:

-

- Specify IPFS to download file from -

- setRpcAddr(e.target.value)} - value={rpcAddr!} - /> -
-

Get file size

-

- Upload any file to IPFS node -

{ rpcAddr }

- paste CID here and get the size of that file via ProcessFiles service you have just deployed -

-
- - setFileCID(e.target.value)} - value={fileCID} - /> -
-
- -
-
-
- - -
-
- - -
- +const IpfsForm = (props: { + rpcAddr: string; + setRpcAddr: React.Dispatch>; + wasm: string; + setWasm: React.Dispatch>; + deployService: () => Promise; +}) => { + return ( + <> +
+
+

Set IPFS RPC address:

+

Specify IPFS to download process_files.wasm from

+ props.setRpcAddr(e.target.value)} + value={props.rpcAddr} + />
+
+

Set process_files.wasm module CID

+

+ To deploy a service, specify CID of WebAssembly module. +

+ props.setWasm(e.target.value)} + value={props.wasm} + />
- ) - } else { - return ( -
-
- logo -
+
+
+

Deploy ProcessFiles service

+

+ process_files.wasm will be downloaded via IPFS to the Fluence node, + and then a service will be dynamically created from it! After that, + you will be able to use that service to get sizes of IPFS files! +

+
+ +
+
+ + ); +}; -
- <> -

Unimplemented! isConnected {isConnected} deployed {deployed} wasm {wasm}

- -
-
- ) - } - } +const ConnectionForm = (props: { + connect: (multiaddr: string) => Promise; +}) => { + return ( + <> +

Pick a relay

+
    + {relayNodes.map((x) => ( +
  • + {x.peerId} + +
  • + ))} +
+ + ); +}; + +const Connected = (props: { client: FluenceClient }) => { + return ( + <> +

Connected

+ + + + + + + + + + + +
Peer id:{props.client.selfPeerId} + +
Relay peer id:{props.client.relayPeerId} + +
+ + ); +}; export default App;