-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
63 lines (53 loc) · 1.69 KB
/
server.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
const express = require('express');
const { Actor, HttpAgent } = require('@dfinity/agent');
const { IDL } = require('@dfinity/candid');
const cors = require('cors');
const agent = new HttpAgent({ host: 'https://ic0.app' });
const app = express();
const port = 3000;
app.use(cors());
app.use(express.static(__dirname));
// Build IDL actor
const idlFactory = ({ IDL }) => {
const HttpHeader = IDL.Record({ 'value' : IDL.Text, 'name' : IDL.Text });
const HttpResponse = IDL.Record({
'status' : IDL.Nat,
'body' : IDL.Vec(IDL.Nat8),
'headers' : IDL.Vec(HttpHeader)
});
const HttpTransformArgs = IDL.Record({
'context' : IDL.Vec(IDL.Nat8),
'response' : HttpResponse
});
return IDL.Service({
'countScore' : IDL.Func([IDL.Text], [IDL.Float64], []),
'getScore' : IDL.Func([IDL.Text], [IDL.Float32], ['query']),
'xkcdTransform' : IDL.Func([HttpTransformArgs], [HttpResponse], ['query']),
});
};
const canisterId = "dx56p-yiaaa-aaaan-qejwa-cai";
const actor = Actor.createActor(idlFactory, { agent, canisterId });
// Endpoint for getting wallet
app.get('/get-score', async (req, res) => {
const walletId = req.query.walletId;
if (!walletId) {
return res.status(400).send('Wallet ID is required');
}
async function getScore(walletId) {
try {
const score = await actor.getScore(walletId);
return score;
} catch (error) {
throw error;
}
}
try {
const score = await getScore(walletId); // Use getScore function
res.json({ walletId, score });
} catch (error) {
res.status(500).send(`Error fetching score: ${error.message}`);
}
});
app.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`);
});