Aqua examples, Price oracle: miscellaneous fixes for web demo (#32)

This commit is contained in:
Pavel 2021-10-20 21:47:21 +03:00 committed by GitHub
parent dfefd0025e
commit 921938082b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 1613 additions and 414 deletions

File diff suppressed because it is too large Load Diff

View File

@ -3,7 +3,7 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"@fluencelabs/fluence": "0.10.1",
"@fluencelabs/fluence": "^0.13.0",
"@testing-library/jest-dom": "^5.14.1",
"@testing-library/react": "^11.2.7",
"@testing-library/user-event": "^12.8.3",
@ -44,7 +44,7 @@
]
},
"devDependencies": {
"@fluencelabs/aqua": "0.2.2-221",
"@fluencelabs/aqua": "^0.3.2-233",
"@fluencelabs/fluence-network-environment": "1.0.10",
"chokidar-cli": "^2.1.0",
"node-sass": "^6.0.1"

View File

@ -2,7 +2,7 @@ import React, { useEffect, useState } from "react";
import logo from "./logo.svg";
import "./App.scss";
import { createClient, FluenceClient } from "@fluencelabs/fluence";
import { Fluence } from "@fluencelabs/fluence";
import { krasnodar } from "@fluencelabs/fluence-network-environment";
import { get_price } from "./_aqua/get_crypto_prices";
@ -32,7 +32,7 @@ const TextInput = (props: {
};
function App() {
const [client, setClient] = useState<FluenceClient | null>(null);
const [isConnected, setIsConnected] = useState<boolean>(false);
const [coin, setCoin] = useState<string>("dogecoin");
const [currency, setCurrency] = useState<string>("usd");
const [node, setNode] = useState<string>(
@ -47,20 +47,18 @@ function App() {
const [result, setResult] = useState<Result | null>(null);
useEffect(() => {
createClient(relayNode.multiaddr)
.then(setClient)
Fluence.start({ connectTo: relayNode.multiaddr })
.then(() => setIsConnected(true))
.catch((err) => console.log("Client initialization failed", err));
}, [client]);
}, [isConnected]);
const isConnected = client !== null;
const doGetPrice = async () => {
if (client === null) {
if (!isConnected) {
return;
}
try {
const res = await get_price(
client!,
coin,
currency,
node,
@ -69,7 +67,7 @@ function App() {
);
console.log("Retrieved result: ", res);
setResult(res);
} catch (err) {
} catch (err: any) {
setResult({ error_msg: err.toString(), success: false, result: 0 });
}
};

View File

@ -0,0 +1,770 @@
/**
*
* 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.3.2-233
*
*/
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
import {
ResultCodes,
RequestFlow,
RequestFlowBuilder,
CallParams
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
function missingFields(obj: any, fields: string[]): string[] {
return fields.filter(f => !(f in obj))
}
// Services
export interface SrvDef {
add_alias: (alias: string, service_id: string, callParams: CallParams<'alias' | 'service_id'>) => void;
create: (blueprint_id: string, callParams: CallParams<'blueprint_id'>) => string;
get_interface: (service_id: string, callParams: CallParams<'service_id'>) => { function_signatures: { arguments: string[][]; name: string; output_types: string[]; }[]; record_types: { fields: string[][]; id: number; name: string; }[]; };
list: (callParams: CallParams<null>) => { blueprint_id: string; id: string; owner_id: string; }[];
remove: (service_id: string, callParams: CallParams<'service_id'>) => void;
resolve_alias: (alias: string, callParams: CallParams<'alias'>) => string;
}
export function registerSrv(service: SrvDef): void;
export function registerSrv(serviceId: string, service: SrvDef): void;
export function registerSrv(peer: FluencePeer, service: SrvDef): void;
export function registerSrv(peer: FluencePeer, serviceId: string, service: SrvDef): void;
export function registerSrv(...args: any) {
let peer: FluencePeer;
let serviceId: any;
let service: any;
if (FluencePeer.isInstance(args[0])) {
peer = args[0];
} else {
peer = Fluence.getPeer();
}
if (typeof args[0] === 'string') {
serviceId = args[0];
} else if (typeof args[1] === 'string') {
serviceId = args[1];
} else {
serviceId = "srv"
}
// Figuring out which overload is the service.
// If the first argument is not Fluence Peer and it is an object, then it can only be the service def
// If the first argument is peer, we are checking further. The second argument might either be
// an object, that it must be the service object
// or a string, which is the service id. In that case the service is the third argument
if (!(FluencePeer.isInstance(args[0])) && typeof args[0] === 'object') {
service = args[0];
} else if (typeof args[1] === 'object') {
service = args[1];
} else {
service = args[2];
}
const incorrectServiceDefinitions = missingFields(service, ['add_alias', 'create', 'get_interface', 'list', 'remove', 'resolve_alias']);
if (!!incorrectServiceDefinitions.length) {
throw new Error("Error registering service Srv: missing functions: " + incorrectServiceDefinitions.map((d) => "'" + d + "'").join(", "))
}
peer.internals.callServiceHandler.use((req, resp, next) => {
if (req.serviceId !== serviceId) {
next();
return;
}
if (req.fnName === 'add_alias') {
const callParams = {
...req.particleContext,
tetraplets: {
alias: req.tetraplets[0],service_id: req.tetraplets[1]
},
};
resp.retCode = ResultCodes.success;
service.add_alias(req.args[0], req.args[1], callParams); resp.result = {}
}
if (req.fnName === 'create') {
const callParams = {
...req.particleContext,
tetraplets: {
blueprint_id: req.tetraplets[0]
},
};
resp.retCode = ResultCodes.success;
resp.result = service.create(req.args[0], callParams)
}
if (req.fnName === 'get_interface') {
const callParams = {
...req.particleContext,
tetraplets: {
service_id: req.tetraplets[0]
},
};
resp.retCode = ResultCodes.success;
resp.result = service.get_interface(req.args[0], callParams)
}
if (req.fnName === 'list') {
const callParams = {
...req.particleContext,
tetraplets: {
},
};
resp.retCode = ResultCodes.success;
resp.result = service.list(callParams)
}
if (req.fnName === 'remove') {
const callParams = {
...req.particleContext,
tetraplets: {
service_id: req.tetraplets[0]
},
};
resp.retCode = ResultCodes.success;
service.remove(req.args[0], callParams); resp.result = {}
}
if (req.fnName === 'resolve_alias') {
const callParams = {
...req.particleContext,
tetraplets: {
alias: req.tetraplets[0]
},
};
resp.retCode = ResultCodes.success;
resp.result = service.resolve_alias(req.args[0], callParams)
}
next();
});
}
export interface PeerDef {
connect: (id: string, multiaddrs: string[] | null, callParams: CallParams<'id' | 'multiaddrs'>) => boolean;
get_contact: (peer: string, callParams: CallParams<'peer'>) => { addresses: string[]; peer_id: string; };
identify: (callParams: CallParams<null>) => { external_addresses: string[]; };
is_connected: (peer: string, callParams: CallParams<'peer'>) => boolean;
timestamp_ms: (callParams: CallParams<null>) => number;
timestamp_sec: (callParams: CallParams<null>) => number;
}
export function registerPeer(service: PeerDef): void;
export function registerPeer(serviceId: string, service: PeerDef): void;
export function registerPeer(peer: FluencePeer, service: PeerDef): void;
export function registerPeer(peer: FluencePeer, serviceId: string, service: PeerDef): void;
export function registerPeer(...args: any) {
let peer: FluencePeer;
let serviceId: any;
let service: any;
if (FluencePeer.isInstance(args[0])) {
peer = args[0];
} else {
peer = Fluence.getPeer();
}
if (typeof args[0] === 'string') {
serviceId = args[0];
} else if (typeof args[1] === 'string') {
serviceId = args[1];
} else {
serviceId = "peer"
}
// Figuring out which overload is the service.
// If the first argument is not Fluence Peer and it is an object, then it can only be the service def
// If the first argument is peer, we are checking further. The second argument might either be
// an object, that it must be the service object
// or a string, which is the service id. In that case the service is the third argument
if (!(FluencePeer.isInstance(args[0])) && typeof args[0] === 'object') {
service = args[0];
} else if (typeof args[1] === 'object') {
service = args[1];
} else {
service = args[2];
}
const incorrectServiceDefinitions = missingFields(service, ['connect', 'get_contact', 'identify', 'is_connected', 'timestamp_ms', 'timestamp_sec']);
if (!!incorrectServiceDefinitions.length) {
throw new Error("Error registering service Peer: missing functions: " + incorrectServiceDefinitions.map((d) => "'" + d + "'").join(", "))
}
peer.internals.callServiceHandler.use((req, resp, next) => {
if (req.serviceId !== serviceId) {
next();
return;
}
if (req.fnName === 'connect') {
const callParams = {
...req.particleContext,
tetraplets: {
id: req.tetraplets[0],multiaddrs: req.tetraplets[1]
},
};
resp.retCode = ResultCodes.success;
resp.result = service.connect(req.args[0], req.args[1].length === 0 ? null : req.args[1][0], callParams)
}
if (req.fnName === 'get_contact') {
const callParams = {
...req.particleContext,
tetraplets: {
peer: req.tetraplets[0]
},
};
resp.retCode = ResultCodes.success;
resp.result = service.get_contact(req.args[0], callParams)
}
if (req.fnName === 'identify') {
const callParams = {
...req.particleContext,
tetraplets: {
},
};
resp.retCode = ResultCodes.success;
resp.result = service.identify(callParams)
}
if (req.fnName === 'is_connected') {
const callParams = {
...req.particleContext,
tetraplets: {
peer: req.tetraplets[0]
},
};
resp.retCode = ResultCodes.success;
resp.result = service.is_connected(req.args[0], callParams)
}
if (req.fnName === 'timestamp_ms') {
const callParams = {
...req.particleContext,
tetraplets: {
},
};
resp.retCode = ResultCodes.success;
resp.result = service.timestamp_ms(callParams)
}
if (req.fnName === 'timestamp_sec') {
const callParams = {
...req.particleContext,
tetraplets: {
},
};
resp.retCode = ResultCodes.success;
resp.result = service.timestamp_sec(callParams)
}
next();
});
}
export interface OpDef {
array: (a: string, b: string | null, c: string | null, d: string | null, callParams: CallParams<'a' | 'b' | 'c' | 'd'>) => string[];
array_length: (array: string[], callParams: CallParams<'array'>) => number;
bytes_from_b58: (b: string, callParams: CallParams<'b'>) => number[];
bytes_to_b58: (bs: number[], callParams: CallParams<'bs'>) => string;
concat: (a: string[], b: string[] | null, c: string[] | null, d: string[] | null, callParams: CallParams<'a' | 'b' | 'c' | 'd'>) => string[];
identity: (s: string | null, callParams: CallParams<'s'>) => string | null;
noop: (callParams: CallParams<null>) => void;
sha256_string: (s: string, callParams: CallParams<'s'>) => string;
string_from_b58: (b: string, callParams: CallParams<'b'>) => string;
string_to_b58: (s: string, callParams: CallParams<'s'>) => string;
}
export function registerOp(service: OpDef): void;
export function registerOp(serviceId: string, service: OpDef): void;
export function registerOp(peer: FluencePeer, service: OpDef): void;
export function registerOp(peer: FluencePeer, serviceId: string, service: OpDef): void;
export function registerOp(...args: any) {
let peer: FluencePeer;
let serviceId: any;
let service: any;
if (FluencePeer.isInstance(args[0])) {
peer = args[0];
} else {
peer = Fluence.getPeer();
}
if (typeof args[0] === 'string') {
serviceId = args[0];
} else if (typeof args[1] === 'string') {
serviceId = args[1];
} else {
serviceId = "op"
}
// Figuring out which overload is the service.
// If the first argument is not Fluence Peer and it is an object, then it can only be the service def
// If the first argument is peer, we are checking further. The second argument might either be
// an object, that it must be the service object
// or a string, which is the service id. In that case the service is the third argument
if (!(FluencePeer.isInstance(args[0])) && typeof args[0] === 'object') {
service = args[0];
} else if (typeof args[1] === 'object') {
service = args[1];
} else {
service = args[2];
}
const incorrectServiceDefinitions = missingFields(service, ['array', 'array_length', 'bytes_from_b58', 'bytes_to_b58', 'concat', 'identity', 'noop', 'sha256_string', 'string_from_b58', 'string_to_b58']);
if (!!incorrectServiceDefinitions.length) {
throw new Error("Error registering service Op: missing functions: " + incorrectServiceDefinitions.map((d) => "'" + d + "'").join(", "))
}
peer.internals.callServiceHandler.use((req, resp, next) => {
if (req.serviceId !== serviceId) {
next();
return;
}
if (req.fnName === 'array') {
const callParams = {
...req.particleContext,
tetraplets: {
a: req.tetraplets[0],b: req.tetraplets[1],c: req.tetraplets[2],d: req.tetraplets[3]
},
};
resp.retCode = ResultCodes.success;
resp.result = service.array(req.args[0], req.args[1].length === 0 ? null : req.args[1][0], req.args[2].length === 0 ? null : req.args[2][0], req.args[3].length === 0 ? null : req.args[3][0], callParams)
}
if (req.fnName === 'array_length') {
const callParams = {
...req.particleContext,
tetraplets: {
array: req.tetraplets[0]
},
};
resp.retCode = ResultCodes.success;
resp.result = service.array_length(req.args[0], callParams)
}
if (req.fnName === 'bytes_from_b58') {
const callParams = {
...req.particleContext,
tetraplets: {
b: req.tetraplets[0]
},
};
resp.retCode = ResultCodes.success;
resp.result = service.bytes_from_b58(req.args[0], callParams)
}
if (req.fnName === 'bytes_to_b58') {
const callParams = {
...req.particleContext,
tetraplets: {
bs: req.tetraplets[0]
},
};
resp.retCode = ResultCodes.success;
resp.result = service.bytes_to_b58(req.args[0], callParams)
}
if (req.fnName === 'concat') {
const callParams = {
...req.particleContext,
tetraplets: {
a: req.tetraplets[0],b: req.tetraplets[1],c: req.tetraplets[2],d: req.tetraplets[3]
},
};
resp.retCode = ResultCodes.success;
resp.result = service.concat(req.args[0], req.args[1].length === 0 ? null : req.args[1][0], req.args[2].length === 0 ? null : req.args[2][0], req.args[3].length === 0 ? null : req.args[3][0], callParams)
}
if (req.fnName === 'identity') {
const callParams = {
...req.particleContext,
tetraplets: {
s: req.tetraplets[0]
},
};
resp.retCode = ResultCodes.success;
var respResult = service.identity(req.args[0].length === 0 ? null : req.args[0][0], callParams);
resp.result = respResult === null ? [] : [respResult]
}
if (req.fnName === 'noop') {
const callParams = {
...req.particleContext,
tetraplets: {
},
};
resp.retCode = ResultCodes.success;
service.noop(callParams); resp.result = {}
}
if (req.fnName === 'sha256_string') {
const callParams = {
...req.particleContext,
tetraplets: {
s: req.tetraplets[0]
},
};
resp.retCode = ResultCodes.success;
resp.result = service.sha256_string(req.args[0], callParams)
}
if (req.fnName === 'string_from_b58') {
const callParams = {
...req.particleContext,
tetraplets: {
b: req.tetraplets[0]
},
};
resp.retCode = ResultCodes.success;
resp.result = service.string_from_b58(req.args[0], callParams)
}
if (req.fnName === 'string_to_b58') {
const callParams = {
...req.particleContext,
tetraplets: {
s: req.tetraplets[0]
},
};
resp.retCode = ResultCodes.success;
resp.result = service.string_to_b58(req.args[0], callParams)
}
next();
});
}
export interface KademliaDef {
merge: (target: string, left: string[], right: string[], count: number | null, callParams: CallParams<'target' | 'left' | 'right' | 'count'>) => string[];
neighborhood: (key: string, already_hashed: boolean | null, count: number | null, callParams: CallParams<'key' | 'already_hashed' | 'count'>) => string[];
}
export function registerKademlia(service: KademliaDef): void;
export function registerKademlia(serviceId: string, service: KademliaDef): void;
export function registerKademlia(peer: FluencePeer, service: KademliaDef): void;
export function registerKademlia(peer: FluencePeer, serviceId: string, service: KademliaDef): void;
export function registerKademlia(...args: any) {
let peer: FluencePeer;
let serviceId: any;
let service: any;
if (FluencePeer.isInstance(args[0])) {
peer = args[0];
} else {
peer = Fluence.getPeer();
}
if (typeof args[0] === 'string') {
serviceId = args[0];
} else if (typeof args[1] === 'string') {
serviceId = args[1];
} else {
serviceId = "kad"
}
// Figuring out which overload is the service.
// If the first argument is not Fluence Peer and it is an object, then it can only be the service def
// If the first argument is peer, we are checking further. The second argument might either be
// an object, that it must be the service object
// or a string, which is the service id. In that case the service is the third argument
if (!(FluencePeer.isInstance(args[0])) && typeof args[0] === 'object') {
service = args[0];
} else if (typeof args[1] === 'object') {
service = args[1];
} else {
service = args[2];
}
const incorrectServiceDefinitions = missingFields(service, ['merge', 'neighborhood']);
if (!!incorrectServiceDefinitions.length) {
throw new Error("Error registering service Kademlia: missing functions: " + incorrectServiceDefinitions.map((d) => "'" + d + "'").join(", "))
}
peer.internals.callServiceHandler.use((req, resp, next) => {
if (req.serviceId !== serviceId) {
next();
return;
}
if (req.fnName === 'merge') {
const callParams = {
...req.particleContext,
tetraplets: {
target: req.tetraplets[0],left: req.tetraplets[1],right: req.tetraplets[2],count: req.tetraplets[3]
},
};
resp.retCode = ResultCodes.success;
resp.result = service.merge(req.args[0], req.args[1], req.args[2], req.args[3].length === 0 ? null : req.args[3][0], callParams)
}
if (req.fnName === 'neighborhood') {
const callParams = {
...req.particleContext,
tetraplets: {
key: req.tetraplets[0],already_hashed: req.tetraplets[1],count: req.tetraplets[2]
},
};
resp.retCode = ResultCodes.success;
resp.result = service.neighborhood(req.args[0], req.args[1].length === 0 ? null : req.args[1][0], req.args[2].length === 0 ? null : req.args[2][0], callParams)
}
next();
});
}
export interface ScriptDef {
add: (air_script: string, interval: string | null, callParams: CallParams<'air_script' | 'interval'>) => string;
list: (callParams: CallParams<null>) => { failures: number; id: string; interval: string; owner: string; src: string; };
remove: (script_id: string, callParams: CallParams<'script_id'>) => boolean;
}
export function registerScript(service: ScriptDef): void;
export function registerScript(serviceId: string, service: ScriptDef): void;
export function registerScript(peer: FluencePeer, service: ScriptDef): void;
export function registerScript(peer: FluencePeer, serviceId: string, service: ScriptDef): void;
export function registerScript(...args: any) {
let peer: FluencePeer;
let serviceId: any;
let service: any;
if (FluencePeer.isInstance(args[0])) {
peer = args[0];
} else {
peer = Fluence.getPeer();
}
if (typeof args[0] === 'string') {
serviceId = args[0];
} else if (typeof args[1] === 'string') {
serviceId = args[1];
} else {
serviceId = "script"
}
// Figuring out which overload is the service.
// If the first argument is not Fluence Peer and it is an object, then it can only be the service def
// If the first argument is peer, we are checking further. The second argument might either be
// an object, that it must be the service object
// or a string, which is the service id. In that case the service is the third argument
if (!(FluencePeer.isInstance(args[0])) && typeof args[0] === 'object') {
service = args[0];
} else if (typeof args[1] === 'object') {
service = args[1];
} else {
service = args[2];
}
const incorrectServiceDefinitions = missingFields(service, ['add', 'list', 'remove']);
if (!!incorrectServiceDefinitions.length) {
throw new Error("Error registering service Script: missing functions: " + incorrectServiceDefinitions.map((d) => "'" + d + "'").join(", "))
}
peer.internals.callServiceHandler.use((req, resp, next) => {
if (req.serviceId !== serviceId) {
next();
return;
}
if (req.fnName === 'add') {
const callParams = {
...req.particleContext,
tetraplets: {
air_script: req.tetraplets[0],interval: req.tetraplets[1]
},
};
resp.retCode = ResultCodes.success;
resp.result = service.add(req.args[0], req.args[1].length === 0 ? null : req.args[1][0], callParams)
}
if (req.fnName === 'list') {
const callParams = {
...req.particleContext,
tetraplets: {
},
};
resp.retCode = ResultCodes.success;
resp.result = service.list(callParams)
}
if (req.fnName === 'remove') {
const callParams = {
...req.particleContext,
tetraplets: {
script_id: req.tetraplets[0]
},
};
resp.retCode = ResultCodes.success;
resp.result = service.remove(req.args[0], callParams)
}
next();
});
}
export interface DistDef {
add_blueprint: (blueprint: { dependencies: string[]; name: string; }, callParams: CallParams<'blueprint'>) => string;
add_module: (wasm_b56_content: number[], conf: { name: string; }, callParams: CallParams<'wasm_b56_content' | 'conf'>) => string;
get_interface: (module_id: string, callParams: CallParams<'module_id'>) => { function_signatures: { arguments: string[][]; name: string; output_types: string[]; }[]; record_types: { fields: string[][]; id: number; name: string; }[]; };
list_blueprints: (callParams: CallParams<null>) => { dependencies: string[]; id: string; name: string; }[];
list_modules: (callParams: CallParams<null>) => { config: { name: string; }; hash: string; name: string; }[];
make_blueprint: (name: string, dependencies: string[], callParams: CallParams<'name' | 'dependencies'>) => { dependencies: string[]; name: string; };
make_module_config: (name: string, mem_pages_count: number | null, logger_enabled: boolean | null, preopened_files: string[] | null, envs: string[][] | null, mapped_dirs: string[][] | null, mounted_binaries: string[][] | null, logging_mask: number | null, callParams: CallParams<'name' | 'mem_pages_count' | 'logger_enabled' | 'preopened_files' | 'envs' | 'mapped_dirs' | 'mounted_binaries' | 'logging_mask'>) => { name: string; };
}
export function registerDist(service: DistDef): void;
export function registerDist(serviceId: string, service: DistDef): void;
export function registerDist(peer: FluencePeer, service: DistDef): void;
export function registerDist(peer: FluencePeer, serviceId: string, service: DistDef): void;
export function registerDist(...args: any) {
let peer: FluencePeer;
let serviceId: any;
let service: any;
if (FluencePeer.isInstance(args[0])) {
peer = args[0];
} else {
peer = Fluence.getPeer();
}
if (typeof args[0] === 'string') {
serviceId = args[0];
} else if (typeof args[1] === 'string') {
serviceId = args[1];
} else {
serviceId = "dist"
}
// Figuring out which overload is the service.
// If the first argument is not Fluence Peer and it is an object, then it can only be the service def
// If the first argument is peer, we are checking further. The second argument might either be
// an object, that it must be the service object
// or a string, which is the service id. In that case the service is the third argument
if (!(FluencePeer.isInstance(args[0])) && typeof args[0] === 'object') {
service = args[0];
} else if (typeof args[1] === 'object') {
service = args[1];
} else {
service = args[2];
}
const incorrectServiceDefinitions = missingFields(service, ['add_blueprint', 'add_module', 'get_interface', 'list_blueprints', 'list_modules', 'make_blueprint', 'make_module_config']);
if (!!incorrectServiceDefinitions.length) {
throw new Error("Error registering service Dist: missing functions: " + incorrectServiceDefinitions.map((d) => "'" + d + "'").join(", "))
}
peer.internals.callServiceHandler.use((req, resp, next) => {
if (req.serviceId !== serviceId) {
next();
return;
}
if (req.fnName === 'add_blueprint') {
const callParams = {
...req.particleContext,
tetraplets: {
blueprint: req.tetraplets[0]
},
};
resp.retCode = ResultCodes.success;
resp.result = service.add_blueprint(req.args[0], callParams)
}
if (req.fnName === 'add_module') {
const callParams = {
...req.particleContext,
tetraplets: {
wasm_b56_content: req.tetraplets[0],conf: req.tetraplets[1]
},
};
resp.retCode = ResultCodes.success;
resp.result = service.add_module(req.args[0], req.args[1], callParams)
}
if (req.fnName === 'get_interface') {
const callParams = {
...req.particleContext,
tetraplets: {
module_id: req.tetraplets[0]
},
};
resp.retCode = ResultCodes.success;
resp.result = service.get_interface(req.args[0], callParams)
}
if (req.fnName === 'list_blueprints') {
const callParams = {
...req.particleContext,
tetraplets: {
},
};
resp.retCode = ResultCodes.success;
resp.result = service.list_blueprints(callParams)
}
if (req.fnName === 'list_modules') {
const callParams = {
...req.particleContext,
tetraplets: {
},
};
resp.retCode = ResultCodes.success;
resp.result = service.list_modules(callParams)
}
if (req.fnName === 'make_blueprint') {
const callParams = {
...req.particleContext,
tetraplets: {
name: req.tetraplets[0],dependencies: req.tetraplets[1]
},
};
resp.retCode = ResultCodes.success;
resp.result = service.make_blueprint(req.args[0], req.args[1], callParams)
}
if (req.fnName === 'make_module_config') {
const callParams = {
...req.particleContext,
tetraplets: {
name: req.tetraplets[0],mem_pages_count: req.tetraplets[1],logger_enabled: req.tetraplets[2],preopened_files: req.tetraplets[3],envs: req.tetraplets[4],mapped_dirs: req.tetraplets[5],mounted_binaries: req.tetraplets[6],logging_mask: req.tetraplets[7]
},
};
resp.retCode = ResultCodes.success;
resp.result = service.make_module_config(req.args[0], req.args[1].length === 0 ? null : req.args[1][0], req.args[2].length === 0 ? null : req.args[2][0], req.args[3].length === 0 ? null : req.args[3][0], req.args[4].length === 0 ? null : req.args[4][0], req.args[5].length === 0 ? null : req.args[5][0], req.args[6].length === 0 ? null : req.args[6][0], req.args[7].length === 0 ? null : req.args[7][0], callParams)
}
next();
});
}
// Functions

View File

@ -1,266 +1,486 @@
/**
*
* This file is auto-generated. Do not edit manually: changes may be erased.
* Generated by Aqua compiler: https://github.com/fluencelabs/aqua/.
* 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.2.2-221
* Aqua version: 0.3.2-233
*
*/
import { FluenceClient, PeerIdB58 } from '@fluencelabs/fluence';
import { RequestFlowBuilder } from '@fluencelabs/fluence/dist/api.unstable';
import { RequestFlow } from '@fluencelabs/fluence/dist/internal/RequestFlow';
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
import {
ResultCodes,
RequestFlow,
RequestFlowBuilder,
CallParams
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
function missingFields(obj: any, fields: string[]): string[] {
return fields.filter(f => !(f in obj))
}
// Services
//PriceGetterService
//defaultId = undefined
export interface PriceGetterServiceDef {
price_getter: (coin: string, currency: string, timestamp_ms: number, callParams: CallParams<'coin' | 'currency' | 'timestamp_ms'>) => { error_msg: string; result: number; success: boolean; };
}
export function registerPriceGetterService(serviceId: string, service: PriceGetterServiceDef): void;
export function registerPriceGetterService(peer: FluencePeer, serviceId: string, service: PriceGetterServiceDef): void;
//price_getter: (coin: string, currency: string, timestamp_ms: number) => {error_msg:string;result:number;success:boolean}
//END PriceGetterService
export function registerPriceGetterService(...args: any) {
let peer: FluencePeer;
let serviceId: any;
let service: any;
if (FluencePeer.isInstance(args[0])) {
peer = args[0];
} else {
peer = Fluence.getPeer();
}
if (typeof args[0] === 'string') {
serviceId = args[0];
} else if (typeof args[1] === 'string') {
serviceId = args[1];
}
// Figuring out which overload is the service.
// If the first argument is not Fluence Peer and it is an object, then it can only be the service def
// If the first argument is peer, we are checking further. The second argument might either be
// an object, that it must be the service object
// or a string, which is the service id. In that case the service is the third argument
if (!(FluencePeer.isInstance(args[0])) && typeof args[0] === 'object') {
service = args[0];
} else if (typeof args[1] === 'object') {
service = args[1];
} else {
service = args[2];
}
const incorrectServiceDefinitions = missingFields(service, ['price_getter']);
if (!!incorrectServiceDefinitions.length) {
throw new Error("Error registering service PriceGetterService: missing functions: " + incorrectServiceDefinitions.map((d) => "'" + d + "'").join(", "))
}
peer.internals.callServiceHandler.use((req, resp, next) => {
if (req.serviceId !== serviceId) {
next();
return;
}
if (req.fnName === 'price_getter') {
const callParams = {
...req.particleContext,
tetraplets: {
coin: req.tetraplets[0],currency: req.tetraplets[1],timestamp_ms: req.tetraplets[2]
},
};
resp.retCode = ResultCodes.success;
resp.result = service.price_getter(req.args[0], req.args[1], req.args[2], callParams)
}
next();
});
}
export interface MeanServiceDef {
mean: (data: number[], callParams: CallParams<'data'>) => { error_msg: string; result: number; success: boolean; };
}
export function registerMeanService(serviceId: string, service: MeanServiceDef): void;
export function registerMeanService(peer: FluencePeer, serviceId: string, service: MeanServiceDef): void;
export function registerMeanService(...args: any) {
let peer: FluencePeer;
let serviceId: any;
let service: any;
if (FluencePeer.isInstance(args[0])) {
peer = args[0];
} else {
peer = Fluence.getPeer();
}
if (typeof args[0] === 'string') {
serviceId = args[0];
} else if (typeof args[1] === 'string') {
serviceId = args[1];
}
// Figuring out which overload is the service.
// If the first argument is not Fluence Peer and it is an object, then it can only be the service def
// If the first argument is peer, we are checking further. The second argument might either be
// an object, that it must be the service object
// or a string, which is the service id. In that case the service is the third argument
if (!(FluencePeer.isInstance(args[0])) && typeof args[0] === 'object') {
service = args[0];
} else if (typeof args[1] === 'object') {
service = args[1];
} else {
service = args[2];
}
const incorrectServiceDefinitions = missingFields(service, ['mean']);
if (!!incorrectServiceDefinitions.length) {
throw new Error("Error registering service MeanService: missing functions: " + incorrectServiceDefinitions.map((d) => "'" + d + "'").join(", "))
}
peer.internals.callServiceHandler.use((req, resp, next) => {
if (req.serviceId !== serviceId) {
next();
return;
}
if (req.fnName === 'mean') {
const callParams = {
...req.particleContext,
tetraplets: {
data: req.tetraplets[0]
},
};
resp.retCode = ResultCodes.success;
resp.result = service.mean(req.args[0], callParams)
}
next();
});
}
//MeanService
//defaultId = undefined
export interface F64OpDef {
identity: (x: number, callParams: CallParams<'x'>) => number;
}
export function registerF64Op(service: F64OpDef): void;
export function registerF64Op(serviceId: string, service: F64OpDef): void;
export function registerF64Op(peer: FluencePeer, service: F64OpDef): void;
export function registerF64Op(peer: FluencePeer, serviceId: string, service: F64OpDef): void;
//mean: (data: number[]) => {error_msg:string;result:number;success:boolean}
//END MeanService
export function registerF64Op(...args: any) {
let peer: FluencePeer;
let serviceId: any;
let service: any;
if (FluencePeer.isInstance(args[0])) {
peer = args[0];
} else {
peer = Fluence.getPeer();
}
if (typeof args[0] === 'string') {
serviceId = args[0];
} else if (typeof args[1] === 'string') {
serviceId = args[1];
} else {
serviceId = "op"
}
// Figuring out which overload is the service.
// If the first argument is not Fluence Peer and it is an object, then it can only be the service def
// If the first argument is peer, we are checking further. The second argument might either be
// an object, that it must be the service object
// or a string, which is the service id. In that case the service is the third argument
if (!(FluencePeer.isInstance(args[0])) && typeof args[0] === 'object') {
service = args[0];
} else if (typeof args[1] === 'object') {
service = args[1];
} else {
service = args[2];
}
const incorrectServiceDefinitions = missingFields(service, ['identity']);
if (!!incorrectServiceDefinitions.length) {
throw new Error("Error registering service F64Op: missing functions: " + incorrectServiceDefinitions.map((d) => "'" + d + "'").join(", "))
}
//F64Op
//defaultId = "op"
//identity: (x: number) => number
//END F64Op
peer.internals.callServiceHandler.use((req, resp, next) => {
if (req.serviceId !== serviceId) {
next();
return;
}
if (req.fnName === 'identity') {
const callParams = {
...req.particleContext,
tetraplets: {
x: req.tetraplets[0]
},
};
resp.retCode = ResultCodes.success;
resp.result = service.identity(req.args[0], callParams)
}
next();
});
}
// Functions
export type Get_priceResult = { error_msg: string; result: number; success: boolean; }
export function get_price(coin: string, currency: string, node: string, pg_sid: string, mean_sid: string, config?: {ttl?: number}): Promise<Get_priceResult>;
export function get_price(peer: FluencePeer, coin: string, currency: string, node: string, pg_sid: string, mean_sid: string, config?: {ttl?: number}): Promise<Get_priceResult>;
export function get_price(...args: any) {
let peer: FluencePeer;
let coin: any;
let currency: any;
let node: any;
let pg_sid: any;
let mean_sid: any;
let config: any;
if (FluencePeer.isInstance(args[0])) {
peer = args[0];
coin = args[1];
currency = args[2];
node = args[3];
pg_sid = args[4];
mean_sid = args[5];
config = args[6];
} else {
peer = Fluence.getPeer();
coin = args[0];
currency = args[1];
node = args[2];
pg_sid = args[3];
mean_sid = args[4];
config = args[5];
}
export async function get_price(client: FluenceClient, coin: string, currency: string, node: string, pg_sid: string, mean_sid: string, config?: {ttl?: number}): Promise<{error_msg:string;result:number;success:boolean}> {
let request: RequestFlow;
const promise = new Promise<{error_msg:string;result:number;success:boolean}>((resolve, reject) => {
const promise = new Promise<Get_priceResult>((resolve, reject) => {
const r = new RequestFlowBuilder()
.disableInjections()
.withRawScript(
`
(xor
(seq
(seq
(seq
(seq
(seq
(seq
(seq
(seq
(seq
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
(call %init_peer_id% ("getDataSrv" "coin") [] coin)
)
(call %init_peer_id% ("getDataSrv" "currency") [] currency)
)
(call %init_peer_id% ("getDataSrv" "node") [] node)
)
(call %init_peer_id% ("getDataSrv" "pg_sid") [] pg_sid)
)
(call %init_peer_id% ("getDataSrv" "mean_sid") [] mean_sid)
)
(call -relay- ("op" "noop") [])
)
(xor
(seq
(seq
(seq
(seq
(seq
(seq
(seq
(call node ("op" "string_to_b58") [node] k)
(call node ("peer" "timestamp_ms") [] ts_ms0)
)
(call node (pg_sid "price_getter") [coin currency ts_ms0] res0)
)
(call node ("op" "identity") [res0.$.result!] $prices)
)
(call node ("peer" "timestamp_ms") [] ts_ms1)
)
(call node (pg_sid "price_getter") [coin currency ts_ms1] res1)
)
(call node ("op" "identity") [res1.$.result!] $prices)
)
(call node (mean_sid "mean") [$prices] result)
)
(seq
(call -relay- ("op" "noop") [])
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
)
)
)
(call -relay- ("op" "noop") [])
)
(xor
(call %init_peer_id% ("callbackSrv" "response") [result])
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
)
)
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
)
.disableInjections()
.withRawScript(`
(xor
(seq
(seq
(seq
(seq
(seq
(seq
(seq
(seq
(seq
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
(call %init_peer_id% ("getDataSrv" "coin") [] coin)
)
(call %init_peer_id% ("getDataSrv" "currency") [] currency)
)
(call %init_peer_id% ("getDataSrv" "node") [] node)
)
(call %init_peer_id% ("getDataSrv" "pg_sid") [] pg_sid)
)
(call %init_peer_id% ("getDataSrv" "mean_sid") [] mean_sid)
)
(call -relay- ("op" "noop") [])
)
(xor
(seq
(seq
(seq
(seq
(seq
(seq
(seq
(call node ("op" "string_to_b58") [node] k)
(call node ("peer" "timestamp_ms") [] ts_ms0)
)
(call node (pg_sid "price_getter") [coin currency ts_ms0] res0)
)
(call node ("op" "identity") [res0.$.result!] $prices)
)
(call node ("peer" "timestamp_ms") [] ts_ms1)
)
(call node (pg_sid "price_getter") [coin currency ts_ms1] res1)
)
(call node ("op" "identity") [res1.$.result!] $prices)
)
(call node (mean_sid "mean") [$prices] result)
)
(seq
(call -relay- ("op" "noop") [])
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
)
)
)
(call -relay- ("op" "noop") [])
)
(xor
(call %init_peer_id% ("callbackSrv" "response") [result])
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
)
)
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
)
`,
)
.configHandler((h) => {
h.on('getDataSrv', '-relay-', () => {
return peer.getStatus().relayPeerId;
});
h.on('getDataSrv', 'coin', () => {return coin;});
h.on('getDataSrv', 'currency', () => {return currency;});
h.on('getDataSrv', 'node', () => {return node;});
h.on('getDataSrv', 'pg_sid', () => {return pg_sid;});
h.on('getDataSrv', 'mean_sid', () => {return mean_sid;});
h.onEvent('callbackSrv', 'response', (args) => {
const [res] = args;
resolve(res);
});
h.onEvent('errorHandlingSrv', 'error', (args) => {
const [err] = args;
reject(err);
});
})
.handleScriptError(reject)
.handleTimeout(() => {
reject('Request timed out for get_price');
})
`,
)
.configHandler((h) => {
h.on('getDataSrv', '-relay-', () => {
return client.relayPeerId!;
});
h.on('getDataSrv', 'coin', () => {return coin;});
h.on('getDataSrv', 'currency', () => {return currency;});
h.on('getDataSrv', 'node', () => {return node;});
h.on('getDataSrv', 'pg_sid', () => {return pg_sid;});
h.on('getDataSrv', 'mean_sid', () => {return mean_sid;});
h.onEvent('callbackSrv', 'response', (args) => {
const [res] = args;
resolve(res);
});
if (config && config.ttl) {
r.withTTL(config.ttl)
}
h.onEvent('errorHandlingSrv', 'error', (args) => {
// assuming error is the single argument
const [err] = args;
reject(err);
});
})
.handleScriptError(reject)
.handleTimeout(() => {
reject('Request timed out for get_price');
})
if(config && config.ttl) {
r.withTTL(config.ttl)
}
request = r.build();
request = r.build();
});
await client.initiateFlow(request!);
peer.internals.initiateFlow(request!);
return promise;
}
export type Get_price_parArgMean_topo = { node: string; service_id: string; }
export type Get_price_parResult = { error_msg: string; result: number; success: boolean; }
export function get_price_par(coin: string, currency: string, getter_topo: { node: string; service_id: string; }[], mean_topo: Get_price_parArgMean_topo, config?: {ttl?: number}): Promise<Get_price_parResult>;
export function get_price_par(peer: FluencePeer, coin: string, currency: string, getter_topo: { node: string; service_id: string; }[], mean_topo: Get_price_parArgMean_topo, config?: {ttl?: number}): Promise<Get_price_parResult>;
export function get_price_par(...args: any) {
let peer: FluencePeer;
let coin: any;
let currency: any;
let getter_topo: any;
let mean_topo: any;
let config: any;
if (FluencePeer.isInstance(args[0])) {
peer = args[0];
coin = args[1];
currency = args[2];
getter_topo = args[3];
mean_topo = args[4];
config = args[5];
} else {
peer = Fluence.getPeer();
coin = args[0];
currency = args[1];
getter_topo = args[2];
mean_topo = args[3];
config = args[4];
}
export async function get_price_par(client: FluenceClient, coin: string, currency: string, getter_topo: {node:string;service_id:string}[], mean_topo: {node:string;service_id:string}, config?: {ttl?: number}): Promise<{error_msg:string;result:number;success:boolean}> {
let request: RequestFlow;
const promise = new Promise<{error_msg:string;result:number;success:boolean}>((resolve, reject) => {
const promise = new Promise<Get_price_parResult>((resolve, reject) => {
const r = new RequestFlowBuilder()
.disableInjections()
.withRawScript(
`
(xor
(seq
(seq
(seq
(seq
(seq
(seq
(seq
(seq
(seq
(seq
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
(call %init_peer_id% ("getDataSrv" "coin") [] coin)
)
(call %init_peer_id% ("getDataSrv" "currency") [] currency)
)
(call %init_peer_id% ("getDataSrv" "getter_topo") [] getter_topo)
)
(call %init_peer_id% ("getDataSrv" "mean_topo") [] mean_topo)
)
(fold getter_topo topo
(par
(seq
(seq
(seq
(call -relay- ("op" "noop") [])
(xor
(seq
(seq
(seq
(call topo.$.node! ("op" "string_to_b58") [topo.$.node!] k)
(call topo.$.node! ("peer" "timestamp_ms") [] ts_ms)
)
(call topo.$.node! (topo.$.service_id! "price_getter") [coin currency ts_ms] res)
)
(call topo.$.node! ("op" "identity") [res.$.result!] $prices)
)
(seq
(call -relay- ("op" "noop") [])
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
)
)
)
(call -relay- ("op" "noop") [])
)
(call %init_peer_id% ("op" "noop") [])
)
(next topo)
)
)
)
(call %init_peer_id% ("op" "identity") [$prices.$.[2]!])
)
(call -relay- ("op" "noop") [])
)
(xor
(seq
(call -relay- ("op" "noop") [])
(call mean_topo.$.node! (mean_topo.$.service_id! "mean") [$prices] result)
)
(seq
(call -relay- ("op" "noop") [])
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
)
)
)
(call -relay- ("op" "noop") [])
)
(xor
(call %init_peer_id% ("callbackSrv" "response") [result])
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
)
)
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 4])
)
.disableInjections()
.withRawScript(`
(xor
(seq
(seq
(seq
(seq
(seq
(seq
(seq
(seq
(seq
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
(call %init_peer_id% ("getDataSrv" "coin") [] coin)
)
(call %init_peer_id% ("getDataSrv" "currency") [] currency)
)
(call %init_peer_id% ("getDataSrv" "getter_topo") [] getter_topo)
)
(call %init_peer_id% ("getDataSrv" "mean_topo") [] mean_topo)
)
(fold getter_topo topo
(par
(seq
(seq
(call -relay- ("op" "noop") [])
(xor
(seq
(seq
(seq
(call -relay- ("op" "noop") [])
(call topo.$.node! ("peer" "timestamp_ms") [] ts_ms)
)
(call topo.$.node! (topo.$.service_id! "price_getter") [coin currency ts_ms] res)
)
(call topo.$.node! ("op" "identity") [res.$.result!] $prices)
)
(seq
(seq
(call -relay- ("op" "noop") [])
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
)
(call -relay- ("op" "noop") [])
)
)
)
(call mean_topo.$.node! ("op" "noop") [])
)
(next topo)
)
)
)
(call -relay- ("op" "noop") [])
)
(xor
(seq
(call mean_topo.$.node! ("op" "identity") [$prices.$.[1]!])
(call mean_topo.$.node! (mean_topo.$.service_id! "mean") [$prices] result)
)
(seq
(call -relay- ("op" "noop") [])
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
)
)
)
(call -relay- ("op" "noop") [])
)
(xor
(call %init_peer_id% ("callbackSrv" "response") [result])
(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 peer.getStatus().relayPeerId;
});
h.on('getDataSrv', 'coin', () => {return coin;});
h.on('getDataSrv', 'currency', () => {return currency;});
h.on('getDataSrv', 'getter_topo', () => {return getter_topo;});
h.on('getDataSrv', 'mean_topo', () => {return mean_topo;});
h.onEvent('callbackSrv', 'response', (args) => {
const [res] = args;
resolve(res);
});
h.onEvent('errorHandlingSrv', 'error', (args) => {
const [err] = args;
reject(err);
});
})
.handleScriptError(reject)
.handleTimeout(() => {
reject('Request timed out for get_price_par');
})
`,
)
.configHandler((h) => {
h.on('getDataSrv', '-relay-', () => {
return client.relayPeerId!;
});
h.on('getDataSrv', 'coin', () => {return coin;});
h.on('getDataSrv', 'currency', () => {return currency;});
h.on('getDataSrv', 'getter_topo', () => {return getter_topo;});
h.on('getDataSrv', 'mean_topo', () => {return mean_topo;});
h.onEvent('callbackSrv', 'response', (args) => {
const [res] = args;
resolve(res);
});
if (config && config.ttl) {
r.withTTL(config.ttl)
}
h.onEvent('errorHandlingSrv', 'error', (args) => {
// assuming error is the single argument
const [err] = args;
reject(err);
});
})
.handleScriptError(reject)
.handleTimeout(() => {
reject('Request timed out for get_price_par');
})
if(config && config.ttl) {
r.withTTL(config.ttl)
}
request = r.build();
request = r.build();
});
await client.initiateFlow(request!);
peer.internals.initiateFlow(request!);
return promise;
}