-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
94 lines (84 loc) · 2.3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import * as dotenv from "dotenv"; // see https://github.com/motdotla/dotenv#how-do-i-use-dotenv-with-import
dotenv.config();
import express from "express";
import bodyParser from "body-parser";
import {
invoke,
queryFunctionInvocations,
queryEvents,
mapToScipError,
} from "./apis/api.js";
const app = express();
app.use(bodyParser.json());
app.post("/invoke", async (req, res, next) => {
try {
const data = req.body;
console.log("Execute called with body:", data);
const {
inputs,
outputs,
functionIdentifier,
smartContractPath,
signers,
requiredConfidence,
minimumNumberOfSignatures,
} = data;
const fields = smartContractPath.split("/");
const account = fields[0];
const contractName = fields[1];
const { success, transactionId, errorCode, errorMessage } = await invoke(
account,
contractName,
functionIdentifier,
inputs
);
if (success) {
res.status(200).json({ transactionHash: transactionId });
} else {
res
.status(400)
.json({ errorCode: errorCode, errorMessage: errorMessage });
}
} catch (err) {
const { errorCode, errorMessage } = mapToScipError(err);
res.status(400).json({ errorCode: errorCode, errorMessage: errorMessage });
}
});
app.post("/query", async (req, res, next) => {
try {
const data = req.body;
console.log("data", data);
const smartContractPath = data["smartContractPath"];
const functionIdentifier = data["functionIdentifier"];
const eventIdentifier = data["eventIdentifier"];
const filter = data["filter"];
const timeframe = data["timeframe"];
const parameters = data["parameters"];
let result;
if (functionIdentifier && functionIdentifier !== "") {
// function invocation query
result = await queryFunctionInvocations(
functionIdentifier,
filter,
timeframe,
parameters
);
} else {
//event query
result = await queryEvents(
smartContractPath,
eventIdentifier,
filter,
timeframe,
parameters
);
}
console.log("Query result:", JSON.stringify(result));
res.json(result);
} catch (err) {
next(err);
}
});
app.listen(process.env.HTTP_PORT, () => {
console.log(`Server is running`);
});