mirror of
https://github.com/fluencelabs/examples
synced 2025-06-13 01:51:20 +00:00
Update aqua-ipfs web demo
This commit is contained in:
@ -4,8 +4,7 @@
|
|||||||
"private": true,
|
"private": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@fluencelabs/ipfs-execution-aqua": "file:../aqua",
|
"@fluencelabs/ipfs-execution-aqua": "file:../aqua",
|
||||||
"@fluencelabs/aqua-ipfs-ts": "0.4.2",
|
"@fluencelabs/fluence": "0.11.0",
|
||||||
"@fluencelabs/fluence": "0.10.1",
|
|
||||||
"@fluencelabs/fluence-network-environment": "1.0.10",
|
"@fluencelabs/fluence-network-environment": "1.0.10",
|
||||||
"@fluencelabs/aqua-lib": "0.1.14",
|
"@fluencelabs/aqua-lib": "0.1.14",
|
||||||
"@testing-library/jest-dom": "^5.14.1",
|
"@testing-library/jest-dom": "^5.14.1",
|
||||||
@ -49,7 +48,6 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"chokidar-cli": "^2.1.0",
|
|
||||||
"node-sass": "^6.0.1"
|
"node-sass": "^6.0.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,13 +22,6 @@ function App() {
|
|||||||
const isDeployed = useRecoilValue(isDeployedState);
|
const isDeployed = useRecoilValue(isDeployedState);
|
||||||
const hasResult = useRecoilValue(hasResultState);
|
const hasResult = useRecoilValue(hasResultState);
|
||||||
|
|
||||||
console.log(
|
|
||||||
"isConnected gotRpcAddr deployed\n",
|
|
||||||
isConnected,
|
|
||||||
gotRpcAddr,
|
|
||||||
isDeployed
|
|
||||||
);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="App">
|
<div className="App">
|
||||||
<header>
|
<header>
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
import { useRecoilValue } from "recoil";
|
import { useRecoilValue } from "recoil";
|
||||||
import { clientState } from "../appState";
|
import { isConnectedState, selfPeerIdState } from "../appState";
|
||||||
import { TextWithLabel } from "./TextInput";
|
import { TextWithLabel } from "./TextInput";
|
||||||
|
|
||||||
export const ConnectedInfo = () => {
|
export const ConnectedInfo = () => {
|
||||||
const client = useRecoilValue(clientState);
|
const selfPeerId = useRecoilValue(selfPeerIdState);
|
||||||
|
const client = useRecoilValue(isConnectedState);
|
||||||
if (client === null) {
|
if (client === null) {
|
||||||
return <></>;
|
return <></>;
|
||||||
}
|
}
|
||||||
@ -11,8 +12,8 @@ export const ConnectedInfo = () => {
|
|||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<h1>Connected</h1>
|
<h1>Connected</h1>
|
||||||
<TextWithLabel text={"Peer id:"} value={client.selfPeerId} />
|
<TextWithLabel text={"Peer id:"} value={selfPeerId} />
|
||||||
<TextWithLabel text={"Relay peer id:"} value={client.selfPeerId} />
|
<TextWithLabel text={"Relay peer id:"} value={selfPeerId} />
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { useRecoilState, useRecoilValue } from "recoil";
|
import { useRecoilState } from "recoil";
|
||||||
import { useGetFileSize } from "../appLogic";
|
import { useGetFileSize } from "../appLogic";
|
||||||
import { fileCIDState, rpcAddrState } from "../appState";
|
import { fileCIDState, rpcAddrState } from "../appState";
|
||||||
import { TextInput } from "./TextInput";
|
import { TextInput } from "./TextInput";
|
||||||
|
@ -1,47 +1,50 @@
|
|||||||
import { get_external_api_multiaddr } from "@fluencelabs/aqua-ipfs-ts";
|
import { FluencePeer } from "@fluencelabs/fluence";
|
||||||
import { FluenceClient, createClient } from "@fluencelabs/fluence";
|
|
||||||
import { krasnodar } from "@fluencelabs/fluence-network-environment";
|
import { krasnodar } from "@fluencelabs/fluence-network-environment";
|
||||||
import { useSetRecoilState, useRecoilValue, useRecoilState } from "recoil";
|
import { useSetRecoilState, useRecoilValue, useRecoilState } from "recoil";
|
||||||
import {
|
import {
|
||||||
deploy_service,
|
deploy_service,
|
||||||
put_file_size,
|
put_file_size,
|
||||||
remove_service,
|
remove_service,
|
||||||
|
get_external_api_multiaddr,
|
||||||
} from "@fluencelabs/ipfs-execution-aqua";
|
} from "@fluencelabs/ipfs-execution-aqua";
|
||||||
import {
|
import {
|
||||||
clientState,
|
isConnectedState,
|
||||||
rpcAddrState,
|
rpcAddrState,
|
||||||
wasmState,
|
wasmState,
|
||||||
serviceIdState,
|
serviceIdState,
|
||||||
fileCIDState,
|
fileCIDState,
|
||||||
fileSizeState,
|
fileSizeState,
|
||||||
fileSizeCIDState,
|
fileSizeCIDState,
|
||||||
|
relayState,
|
||||||
|
selfPeerIdState,
|
||||||
} from "./appState";
|
} from "./appState";
|
||||||
import { decapsulateP2P, fromOption } from "./util";
|
import { decapsulateP2P, fromOption } from "./util";
|
||||||
|
|
||||||
export const relayNodes = [krasnodar[0], krasnodar[1], krasnodar[2]];
|
export const relayNodes = [krasnodar[0], krasnodar[1], krasnodar[2]];
|
||||||
|
|
||||||
const getRpcAddr = async (client: FluenceClient) => {
|
const requestRpcAddr = async () => {
|
||||||
if (client === null) {
|
let result = await get_external_api_multiaddr(
|
||||||
console.log("getRpcAddr client is null");
|
FluencePeer.default.connectionInfo.connectedRelay!
|
||||||
return;
|
);
|
||||||
}
|
|
||||||
|
|
||||||
let result = await get_external_api_multiaddr(client, client.relayPeerId!);
|
|
||||||
console.log("getRpcAddr result", result);
|
console.log("getRpcAddr result", result);
|
||||||
let rpcAddr = result.multiaddr;
|
let rpcAddr = result.multiaddr;
|
||||||
return decapsulateP2P(rpcAddr);
|
return decapsulateP2P(rpcAddr);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const useClientConnect = () => {
|
export const useClientConnect = () => {
|
||||||
const setClient = useSetRecoilState(clientState);
|
const setIsConnected = useSetRecoilState(isConnectedState);
|
||||||
|
const setRelay = useSetRecoilState(relayState);
|
||||||
|
const setSelfPeerId = useSetRecoilState(selfPeerIdState);
|
||||||
const setRpcAddr = useSetRecoilState(rpcAddrState);
|
const setRpcAddr = useSetRecoilState(rpcAddrState);
|
||||||
|
|
||||||
const connect = async (relayPeerId: string) => {
|
const connect = async (relayPeerId: string) => {
|
||||||
try {
|
try {
|
||||||
const client = await createClient(relayPeerId);
|
await FluencePeer.default.init({ connectTo: relayPeerId });
|
||||||
const addr = await getRpcAddr(client);
|
setIsConnected(true);
|
||||||
|
setRelay(FluencePeer.default.connectionInfo.connectedRelay);
|
||||||
|
setSelfPeerId(FluencePeer.default.connectionInfo.selfPeerId);
|
||||||
|
const addr = await requestRpcAddr();
|
||||||
setRpcAddr(addr!);
|
setRpcAddr(addr!);
|
||||||
setClient(client);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log("Client initialization failed", err);
|
console.log("Client initialization failed", err);
|
||||||
}
|
}
|
||||||
@ -51,9 +54,10 @@ export const useClientConnect = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const useDeployService = () => {
|
export const useDeployService = () => {
|
||||||
|
const relay = useRecoilValue(relayState);
|
||||||
const wasm = useRecoilValue(wasmState);
|
const wasm = useRecoilValue(wasmState);
|
||||||
const rpcAddr = useRecoilValue(rpcAddrState);
|
const rpcAddr = useRecoilValue(rpcAddrState);
|
||||||
const client = useRecoilValue(clientState);
|
const client = useRecoilValue(isConnectedState);
|
||||||
const setServiceId = useSetRecoilState(serviceIdState);
|
const setServiceId = useSetRecoilState(serviceIdState);
|
||||||
|
|
||||||
return async () => {
|
return async () => {
|
||||||
@ -63,8 +67,7 @@ export const useDeployService = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var service_id = await deploy_service(
|
var service_id = await deploy_service(
|
||||||
client,
|
relay!,
|
||||||
client.relayPeerId!,
|
|
||||||
wasm,
|
wasm,
|
||||||
rpcAddr,
|
rpcAddr,
|
||||||
(msg, value) => console.log(msg, value),
|
(msg, value) => console.log(msg, value),
|
||||||
@ -76,21 +79,21 @@ export const useDeployService = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const useGetFileSize = () => {
|
export const useGetFileSize = () => {
|
||||||
|
const relay = useRecoilValue(relayState);
|
||||||
const rpcAddr = useRecoilValue(rpcAddrState);
|
const rpcAddr = useRecoilValue(rpcAddrState);
|
||||||
const client = useRecoilValue(clientState);
|
const isConnected = useRecoilValue(isConnectedState);
|
||||||
const serviceId = useRecoilValue(serviceIdState);
|
const serviceId = useRecoilValue(serviceIdState);
|
||||||
const fileCID = useRecoilValue(fileCIDState);
|
const fileCID = useRecoilValue(fileCIDState);
|
||||||
const setFileSize = useSetRecoilState(fileSizeState);
|
const setFileSize = useSetRecoilState(fileSizeState);
|
||||||
const setFileSizeCID = useSetRecoilState(fileSizeCIDState);
|
const setFileSizeCID = useSetRecoilState(fileSizeCIDState);
|
||||||
|
|
||||||
return async () => {
|
return async () => {
|
||||||
if (client === null || serviceId === null || rpcAddr === null) {
|
if (!isConnected || serviceId === null || rpcAddr === null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var putResult = await put_file_size(
|
var putResult = await put_file_size(
|
||||||
client,
|
relay!,
|
||||||
client.relayPeerId!,
|
|
||||||
fileCID!,
|
fileCID!,
|
||||||
rpcAddr,
|
rpcAddr,
|
||||||
serviceId,
|
serviceId,
|
||||||
@ -111,18 +114,19 @@ export const useGetFileSize = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const useRemoveService = () => {
|
export const useRemoveService = () => {
|
||||||
const client = useRecoilValue(clientState);
|
const relay = useRecoilValue(relayState);
|
||||||
|
const isConnected = useRecoilValue(isConnectedState);
|
||||||
const [serviceId, setServiceId] = useRecoilState(serviceIdState);
|
const [serviceId, setServiceId] = useRecoilState(serviceIdState);
|
||||||
const setFileCID = useSetRecoilState(fileCIDState);
|
const setFileCID = useSetRecoilState(fileCIDState);
|
||||||
const setFileSize = useSetRecoilState(fileSizeState);
|
const setFileSize = useSetRecoilState(fileSizeState);
|
||||||
const setFileSizeCID = useSetRecoilState(fileSizeCIDState);
|
const setFileSizeCID = useSetRecoilState(fileSizeCIDState);
|
||||||
|
|
||||||
return async () => {
|
return async () => {
|
||||||
if (client === null || serviceId === null) {
|
if (isConnected || serviceId === null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await remove_service(client, client.relayPeerId!, serviceId, {
|
await remove_service(relay!, serviceId, {
|
||||||
ttl: 10000,
|
ttl: 10000,
|
||||||
});
|
});
|
||||||
setServiceId(null);
|
setServiceId(null);
|
||||||
|
@ -1,10 +1,18 @@
|
|||||||
import { FluenceClient } from "@fluencelabs/fluence";
|
|
||||||
import { atom, selector } from "recoil";
|
import { atom, selector } from "recoil";
|
||||||
|
|
||||||
export const clientState = atom<FluenceClient | null>({
|
export const isConnectedState = atom<boolean>({
|
||||||
key: "clientState",
|
key: "isConnectedState",
|
||||||
|
default: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
export const selfPeerIdState = atom<string | null>({
|
||||||
|
key: "selfPeerIdState",
|
||||||
|
default: null,
|
||||||
|
});
|
||||||
|
|
||||||
|
export const relayState = atom<string | null>({
|
||||||
|
key: "relayState",
|
||||||
default: null,
|
default: null,
|
||||||
dangerouslyAllowMutability: true,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
export const serviceIdState = atom<string | null>({
|
export const serviceIdState = atom<string | null>({
|
||||||
@ -37,16 +45,6 @@ export const fileSizeCIDState = atom<string | null>({
|
|||||||
default: null,
|
default: null,
|
||||||
});
|
});
|
||||||
|
|
||||||
export const isConnectedState = selector({
|
|
||||||
key: "isConnectedState",
|
|
||||||
get: ({ get }) => {
|
|
||||||
const client = get(clientState);
|
|
||||||
|
|
||||||
return client !== null && client.isConnected;
|
|
||||||
},
|
|
||||||
dangerouslyAllowMutability: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
export const gotRpcAddrState = selector({
|
export const gotRpcAddrState = selector({
|
||||||
key: "getRpcAddrState",
|
key: "getRpcAddrState",
|
||||||
get: ({ get }) => {
|
get: ({ get }) => {
|
||||||
|
Reference in New Issue
Block a user