mirror of
https://github.com/fluencelabs/examples
synced 2024-12-04 19:20:17 +00:00
Gateway (#421)
This commit is contained in:
parent
c660c57b6f
commit
0f1f72ff41
56
aqua-examples/decentralized-blockchain-gateway/gateway/aqua-compiled/rpc.d.ts
vendored
Normal file
56
aqua-examples/decentralized-blockchain-gateway/gateway/aqua-compiled/rpc.d.ts
vendored
Normal file
@ -0,0 +1,56 @@
|
||||
/* eslint-disable */
|
||||
// @ts-nocheck
|
||||
/**
|
||||
*
|
||||
* 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.9.3
|
||||
*
|
||||
*/
|
||||
import { FluencePeer } from '@fluencelabs/fluence';
|
||||
|
||||
import {
|
||||
callFunction$$,
|
||||
registerService$$,
|
||||
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v4.js';
|
||||
|
||||
|
||||
// Services
|
||||
export interface LoggerDef {
|
||||
log: (s: string[], callParams: CallParams$$<'s'>) => void | Promise<void>;
|
||||
}
|
||||
export function registerLogger(service: LoggerDef): void;
|
||||
export function registerLogger(serviceId: string, service: LoggerDef): void;
|
||||
export function registerLogger(peer: FluencePeer, service: LoggerDef): void;
|
||||
export function registerLogger(peer: FluencePeer, serviceId: string, service: LoggerDef): void;
|
||||
|
||||
export interface EthCallerDef {
|
||||
eth_call: (uri: string, method: string, json_args: string[], callParams: CallParams$$<'uri' | 'method' | 'json_args'>) => { error: string; success: boolean; value: string; } | Promise<{ error: string; success: boolean; value: string; }>;
|
||||
}
|
||||
export function registerEthCaller(serviceId: string, service: EthCallerDef): void;
|
||||
export function registerEthCaller(peer: FluencePeer, serviceId: string, service: EthCallerDef): void;
|
||||
|
||||
|
||||
// Functions
|
||||
|
||||
export type CallResult = { error: string; success: boolean; value: string; }
|
||||
export function call(
|
||||
uri: string,
|
||||
method: string,
|
||||
json_args: string[],
|
||||
serviceId: string,
|
||||
config?: {ttl?: number}
|
||||
): Promise<CallResult>;
|
||||
|
||||
export function call(
|
||||
peer: FluencePeer,
|
||||
uri: string,
|
||||
method: string,
|
||||
json_args: string[],
|
||||
serviceId: string,
|
||||
config?: {ttl?: number}
|
||||
): Promise<CallResult>;
|
||||
|
||||
|
||||
/* eslint-enable */
|
@ -0,0 +1,216 @@
|
||||
/**
|
||||
*
|
||||
* 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.9.3
|
||||
*
|
||||
*/
|
||||
import { FluencePeer } from '@fluencelabs/fluence';
|
||||
|
||||
import {
|
||||
callFunction$$,
|
||||
registerService$$,
|
||||
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v4.js';
|
||||
|
||||
|
||||
// Services
|
||||
|
||||
|
||||
|
||||
export function registerLogger(...args) {
|
||||
registerService$$(
|
||||
args,
|
||||
{
|
||||
"defaultServiceId" : "logger",
|
||||
"functions" : {
|
||||
"tag" : "labeledProduct",
|
||||
"fields" : {
|
||||
"log" : {
|
||||
"tag" : "arrow",
|
||||
"domain" : {
|
||||
"tag" : "labeledProduct",
|
||||
"fields" : {
|
||||
"s" : {
|
||||
"tag" : "array",
|
||||
"type" : {
|
||||
"tag" : "scalar",
|
||||
"name" : "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"codomain" : {
|
||||
"tag" : "nil"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
export function registerEthCaller(...args) {
|
||||
registerService$$(
|
||||
args,
|
||||
{
|
||||
"functions" : {
|
||||
"tag" : "labeledProduct",
|
||||
"fields" : {
|
||||
"eth_call" : {
|
||||
"tag" : "arrow",
|
||||
"domain" : {
|
||||
"tag" : "labeledProduct",
|
||||
"fields" : {
|
||||
"uri" : {
|
||||
"tag" : "scalar",
|
||||
"name" : "string"
|
||||
},
|
||||
"method" : {
|
||||
"tag" : "scalar",
|
||||
"name" : "string"
|
||||
},
|
||||
"json_args" : {
|
||||
"tag" : "array",
|
||||
"type" : {
|
||||
"tag" : "scalar",
|
||||
"name" : "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"codomain" : {
|
||||
"tag" : "unlabeledProduct",
|
||||
"items" : [
|
||||
{
|
||||
"tag" : "struct",
|
||||
"name" : "JsonString",
|
||||
"fields" : {
|
||||
"error" : {
|
||||
"tag" : "scalar",
|
||||
"name" : "string"
|
||||
},
|
||||
"success" : {
|
||||
"tag" : "scalar",
|
||||
"name" : "bool"
|
||||
},
|
||||
"value" : {
|
||||
"tag" : "scalar",
|
||||
"name" : "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// Functions
|
||||
|
||||
export function call(...args) {
|
||||
|
||||
let script = `
|
||||
(xor
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||
(call %init_peer_id% ("getDataSrv" "uri") [] uri)
|
||||
)
|
||||
(call %init_peer_id% ("getDataSrv" "method") [] method)
|
||||
)
|
||||
(call %init_peer_id% ("getDataSrv" "json_args") [] json_args)
|
||||
)
|
||||
(call %init_peer_id% ("getDataSrv" "serviceId") [] serviceId)
|
||||
)
|
||||
(xor
|
||||
(call -relay- (serviceId "eth_call") [uri method json_args] res)
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||
)
|
||||
)
|
||||
(xor
|
||||
(call %init_peer_id% ("callbackSrv" "response") [res])
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||
)
|
||||
)
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
|
||||
)
|
||||
`
|
||||
return callFunction$$(
|
||||
args,
|
||||
{
|
||||
"functionName" : "call",
|
||||
"arrow" : {
|
||||
"tag" : "arrow",
|
||||
"domain" : {
|
||||
"tag" : "labeledProduct",
|
||||
"fields" : {
|
||||
"uri" : {
|
||||
"tag" : "scalar",
|
||||
"name" : "string"
|
||||
},
|
||||
"method" : {
|
||||
"tag" : "scalar",
|
||||
"name" : "string"
|
||||
},
|
||||
"json_args" : {
|
||||
"tag" : "array",
|
||||
"type" : {
|
||||
"tag" : "scalar",
|
||||
"name" : "string"
|
||||
}
|
||||
},
|
||||
"serviceId" : {
|
||||
"tag" : "scalar",
|
||||
"name" : "string"
|
||||
}
|
||||
}
|
||||
},
|
||||
"codomain" : {
|
||||
"tag" : "unlabeledProduct",
|
||||
"items" : [
|
||||
{
|
||||
"tag" : "struct",
|
||||
"name" : "JsonString",
|
||||
"fields" : {
|
||||
"error" : {
|
||||
"tag" : "scalar",
|
||||
"name" : "string"
|
||||
},
|
||||
"success" : {
|
||||
"tag" : "scalar",
|
||||
"name" : "bool"
|
||||
},
|
||||
"value" : {
|
||||
"tag" : "scalar",
|
||||
"name" : "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"names" : {
|
||||
"relay" : "-relay-",
|
||||
"getDataSrv" : "getDataSrv",
|
||||
"callbackSrv" : "callbackSrv",
|
||||
"responseSrv" : "callbackSrv",
|
||||
"responseFnName" : "response",
|
||||
"errorHandlingSrv" : "errorHandlingSrv",
|
||||
"errorFnName" : "error"
|
||||
}
|
||||
},
|
||||
script
|
||||
)
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
data JsonString:
|
||||
value: string
|
||||
success: bool
|
||||
error: string
|
||||
|
||||
service Logger("logger"):
|
||||
log(s: []string)
|
||||
|
||||
service EthCaller:
|
||||
eth_call(uri: string, method: string, json_args: []string) -> JsonString
|
||||
|
||||
func call(uri: string, method: string, json_args: []string, serviceId: string) -> JsonString:
|
||||
EthCaller serviceId
|
||||
on HOST_PEER_ID:
|
||||
res <- EthCaller.eth_call(uri, method, json_args)
|
||||
<- res
|
9756
aqua-examples/decentralized-blockchain-gateway/gateway/package-lock.json
generated
Normal file
9756
aqua-examples/decentralized-blockchain-gateway/gateway/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,30 @@
|
||||
{
|
||||
"name": "@fluencelabs/aqua-eth-gateway",
|
||||
"version": "0.0.9",
|
||||
"description": "",
|
||||
"main": "src/index.js",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"run:example": "node src/index.js 3000 \"/dns4/kras-02.fluence.dev/tcp/19001/wss/p2p/12D3KooWHLxVhUQyAuZe6AHMB29P7wkvTNMn7eDMcsqimJYLKREf\" \"https://goerli.infura.io/v3/c48f3b538f154204ad53d04aa8990544\" \"d071085b-639c-4716-8bf3-d86b13210173\"",
|
||||
"run": "node src/index.js",
|
||||
"req": "node web3run.js"
|
||||
},
|
||||
"bin": {
|
||||
"aqua-eth-gateway": "src/index.js"
|
||||
},
|
||||
"files": [
|
||||
"src/*",
|
||||
"aqua/*",
|
||||
"aqua-compiled/"
|
||||
],
|
||||
"author": "Fluence Labs",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"json-rpc-2.0": "1.4.2",
|
||||
"web3": "1.8.2",
|
||||
"@fluencelabs/fluence": "0.27.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@fluencelabs/aqua-api": "0.9.3"
|
||||
}
|
||||
}
|
@ -0,0 +1,176 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
"use strict";
|
||||
|
||||
handleEPIPE(process.stderr)
|
||||
handleEPIPE(process.stdout)
|
||||
function handleEPIPE(stream) {
|
||||
stream.on('error', onerror)
|
||||
function onerror(err) {
|
||||
if (err.code === 'EPIPE') {
|
||||
stream._write = noopWrite
|
||||
stream._writev = noopWritev
|
||||
stream._read = noopRead
|
||||
return stream.removeListener('error', onerror)
|
||||
}
|
||||
if (EE.listenerCount(stream, 'error') === 1) {
|
||||
stream.removeListener('error', onerror)
|
||||
stream.emit('error', err)
|
||||
}
|
||||
}
|
||||
}
|
||||
function noopWrite(chunk, enc, cb) {
|
||||
cb()
|
||||
}
|
||||
function noopRead() {
|
||||
this.push('')
|
||||
}
|
||||
function noopWritev(chunks, cb) {
|
||||
cb()
|
||||
}
|
||||
|
||||
import express from "express";
|
||||
import bodyParser from "body-parser";
|
||||
import { JSONRPCServer } from "json-rpc-2.0";
|
||||
import { FluencePeer } from "@fluencelabs/fluence";
|
||||
import {call, registerLogger} from "../aqua-compiled/rpc.js";
|
||||
|
||||
var args = process.argv.slice(2);
|
||||
|
||||
const port = args[0]
|
||||
const relay = args[1]
|
||||
const ethRpcURI = args[2]
|
||||
const serviceId = args[3]
|
||||
|
||||
let errors = []
|
||||
if (!port) {
|
||||
errors.push("Specify port")
|
||||
}
|
||||
if (!relay) {
|
||||
errors.push("Specify Fluence peer address")
|
||||
}
|
||||
if (!ethRpcURI) {
|
||||
errors.push("Specify uri to ethereum RPC")
|
||||
}
|
||||
if (!serviceId) {
|
||||
errors.push("Specify id to ethereum Aqua service")
|
||||
}
|
||||
|
||||
if (errors.length > 0) {
|
||||
console.log("Example: aqua-eth-gateway <port> <fluence-addr> <eth-rpc-uri> <service-id>")
|
||||
errors.forEach((err) => console.log(err))
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
console.log("Running server...")
|
||||
|
||||
const route = "/"
|
||||
|
||||
const methods = ['eth_accounts',
|
||||
'eth_blockNumber',
|
||||
'eth_call',
|
||||
'eth_chainId',
|
||||
'eth_estimateGas',
|
||||
'eth_getBalance',
|
||||
'eth_getBlockByHash',
|
||||
'eth_getBlockByNumber',
|
||||
'eth_getBlockTransactionCountByHash',
|
||||
'eth_getBlockTransactionCountByNumber',
|
||||
'eth_getCode',
|
||||
'eth_getLogs',
|
||||
'eth_getStorageAt',
|
||||
'eth_getTransactionByBlockHashAndIndex',
|
||||
'eth_getTransactionByBlockNumberAndIndex',
|
||||
'eth_getTransactionByHash',
|
||||
'eth_getTransactionCount',
|
||||
'eth_getTransactionReceipt',
|
||||
'eth_sendTransaction',
|
||||
'net_version',
|
||||
'web3_sha3',
|
||||
'eth_sendRawTransaction',
|
||||
'eth_subscribe',
|
||||
'eth_maxPriorityFeePerGas',
|
||||
'eth_getUncleCountByBlockHash',
|
||||
'eth_getUncleCountByBlockNumber',
|
||||
'net_listening',
|
||||
'net_peerCount',
|
||||
'eth_protocolVersion',
|
||||
'eth_syncing',
|
||||
'eth_coinbase',
|
||||
'eth_mining',
|
||||
'eth_hashrate',
|
||||
'eth_gasPrice',
|
||||
'eth_getStorageAt',
|
||||
'eth_sign',
|
||||
'eth_getCompilers',
|
||||
'eth_newBlockFilter',
|
||||
'eth_newPendingTransactionFilter',
|
||||
'eth_uninstallFilter',
|
||||
'eth_getFilterChanges',
|
||||
'eth_getWork',
|
||||
'eth_submitWork',
|
||||
'eth_submitHashrate',
|
||||
'db_putString',
|
||||
'db_getString',
|
||||
'db_putHex',
|
||||
'db_getHex',
|
||||
'shh_post',
|
||||
'shh_version',
|
||||
'shh_newIdentity',
|
||||
'shh_hasIdentity',
|
||||
'shh_newGroup',
|
||||
'shh_addToGroup',
|
||||
'shh_newFilter',
|
||||
'shh_uninstallFilter',
|
||||
'shh_getFilterChanges',
|
||||
'shh_getMessages']
|
||||
|
||||
|
||||
const server = new JSONRPCServer();
|
||||
|
||||
// initialize fluence client
|
||||
const fluence = new FluencePeer();
|
||||
await fluence.start({connectTo: relay})
|
||||
|
||||
// handler for logger
|
||||
registerLogger(fluence, {
|
||||
log: s => {
|
||||
console.log("log: " + s)
|
||||
}
|
||||
})
|
||||
|
||||
async function methodHandler(req, op) {
|
||||
console.log(`Receiving request '${op}'`)
|
||||
const result = await call(fluence, ethRpcURI, op, req.map((s) => JSON.stringify(s)), serviceId)
|
||||
|
||||
return JSON.parse(result.value)
|
||||
|
||||
}
|
||||
|
||||
function addMethod(op) {
|
||||
server.addMethod(op, async (req) => methodHandler(req, op));
|
||||
}
|
||||
|
||||
// register all eth methods
|
||||
methods.forEach( (m) =>{
|
||||
addMethod(m);
|
||||
})
|
||||
|
||||
const app = express();
|
||||
app.use(bodyParser.json());
|
||||
|
||||
// register JSON-RPC handler
|
||||
app.post(route, (req, res) => {
|
||||
const jsonRPCRequest = req.body;
|
||||
server.receive(jsonRPCRequest).then((jsonRPCResponse) => {
|
||||
if (jsonRPCResponse) {
|
||||
res.json(jsonRPCResponse);
|
||||
} else {
|
||||
res.sendStatus(204);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
app.listen(port);
|
||||
|
||||
console.log("Server was started on port " + port)
|
@ -0,0 +1,14 @@
|
||||
import Web3 from 'web3';
|
||||
|
||||
const web3 = new Web3("http://localhost:3000");
|
||||
|
||||
async function main() {
|
||||
const bn = await web3.eth.getBlockNumber()
|
||||
console.log(bn)
|
||||
|
||||
const resp = await web3.eth.getTransaction("0x8bad403edde37642e4dab6c91eeca77b979fce1c979c14ca8755f5c3573eaeb4")
|
||||
console.log(resp)
|
||||
}
|
||||
|
||||
main();
|
||||
|
Loading…
Reference in New Issue
Block a user