-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
238 lines (197 loc) · 5.67 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import {
Ed25519Keypair,
JsonRpcProvider,
RawSigner,
Network,
} from "@mysten/sui.js";
import { fromB64 } from "@mysten/bcs";
import * as dotenv from "dotenv";
import express from "express";
import bodyParser from "body-parser";
dotenv.config();
// connect to local RPC server
const provider = new JsonRpcProvider(Network.LOCAL);
const privateKey = process.env.PRIVATE_KEY;
const secretKey = fromB64(privateKey);
const keypair = Ed25519Keypair.fromSecretKey(secretKey);
const app = express();
const subscriptions = [];
let lastTransactionScannedCount = await provider.getTotalTransactionNumber();
app.use(bodyParser.json());
app.post("/invoke", async (req, res, next) => {
try {
const data = req.body;
console.log("Invoke called with body:", data);
const smartContractPath = data.smartContractPath.split("/");
const packageObjectId = smartContractPath[0];
const module = smartContractPath[1];
const { inputs, outputs, functionIdentifier } = data;
const callArguments = inputs.map((e) => e.value);
const result = await callMove(
packageObjectId,
module,
functionIdentifier,
data.typeArguments,
callArguments
);
const transactionDigest =
result["EffectsCert"]["certificate"]["transactionDigest"];
res.json({ transactionHash: transactionDigest });
} catch (err) {
next(err);
}
});
app.post("/get-blocks-in-range", async (req, res, next) => {
try {
const data = req.body;
const smartContractPath = data["smartContractPath"];
const eventIdentifier = data["eventIdentifier"];
const start = data["start"];
const end = data["end"];
const filter = data["filter"];
const parameters = data["parameters"];
provider.getTransactions({});
res.json(result);
} catch (err) {
next(err);
}
});
app.post("/query", async (req, res, next) => {
try {
const data = req.body;
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 queryFunctionInvocation(functionIdentifier);
} else {
//event query
result = await queryEvent(eventIdentifier, filter, timeframe, parameters);
}
// TimeRange: { startTime: 0, endTime: 1669039604014000 },
res.json(result);
} catch (err) {
next(err);
}
});
app.post("/subscribe", async (req, res, next) => {
try {
const data = req.body;
subscriptions.push(devNftSub);
res.json({ result: "successful" });
} catch (err) {
next(err);
}
});
app.post("/unsubscribe", async (req, res, next) => {
try {
console.log(req.body);
await callMove();
res.json({ message: "from index api" });
} catch (err) {
next(err);
}
});
app.post("/txdata", async (req, res, next) => {
try {
const txId = req.body["txId"];
const txEvents = await provider.getTransactionWithEffects(txId);
res.json({ result: txEvents });
} catch (err) {
next(err);
}
});
app.listen(process.env.SERVER_PORT, () => {
console.log(`Server is running`);
});
const callMove = async (
packageObjectId,
module,
function_name,
typeArguments,
call_arguments
) => {
const signer = new RawSigner(keypair, provider);
const moveCallTxn = await signer.executeMoveCall({
packageObjectId: packageObjectId,
module: module,
function: function_name,
typeArguments: typeArguments,
arguments: call_arguments,
gasBudget: 10000,
});
console.log("moveCallTxn", moveCallTxn);
return moveCallTxn;
};
const scanForNewTransactions = async () => {
console.log("Start to scan for new transactions");
const newTxCount = await provider.getTotalTransactionNumber();
if (lastTransactionScannedCount < newTxCount) {
console.log(
`Found new transactions: ${newTxCount - lastTransactionScannedCount}`
);
lastTransactionScannedCount = newTxCount;
}
console.log("End scanning transactions");
};
// setInterval(scanForNewTransactions, 1000 * 10);
const queryEvent = async (eventIdentifier, filter, timeframe, parameters) => {
// const eventFilter = { MoveEvent: eventIdentifier };
const eventFilter = {
TimeRange: {
startTime: Number(timeframe["from"]),
endTime: Number(timeframe["to"]),
},
};
const result = await provider.getEvents(
eventFilter,
null,
1000,
"descending"
);
const filteredResult = result["data"].filter((e) => {
if (e["event"]["moveEvent"] === undefined) return false;
if (e["event"]["moveEvent"]["type"] === eventIdentifier) return true;
});
const transformedResult = transformQueryResult(filteredResult);
// TODO filter
return transformedResult;
};
const queryFunctionInvocation = async (
functionIdentifier,
filter,
timeframe,
parameters
) => {
const info = functionIdentifier.split("/");
// Todo: Transform result
const devnetNftFilter = {
MoveModule: { package: info[0], module: info[1] },
};
const result = await provider.getEvents(devnetNftFilter);
return result;
};
const transformQueryResult = (data) => {
let result = [];
for (let i = 0; i < data.length; i++) {
const fields = data[i]["event"]["moveEvent"]["fields"];
const keys = Object.keys(fields);
const parameters = [];
for (let j = 0; j < keys.length; j++) {
parameters.push({
name: keys[j],
value: fields[keys[j]],
type: "string",
});
}
result.push({
isoTimestamp: new Date(data[i]["timestamp"]).toISOString(),
parameters: parameters,
});
}
return result;
};