aboutsummaryrefslogtreecommitdiff
path: root/api/routes/getSLatest/index.js
blob: 3f400fa8820d3f9192e6fc5ab30c1436f022e211 (plain)
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
const { spawn } = require('child_process');

/*
 * Gets the latest SBLOCK from superchain
 * LOL
 * sorry I was laughing at the term.. superchain
 */
function getSLatest(req, res) {
    const command = spawn("ak-schain",["-l"]);
    response_string = "";
    command.stdout.on("data", data => {
        response_string = response_string + data;
    });

    command.stderr.on("data", data => {
        console.log(`stderr: ${data}`);
    });

    command.on('error', (error) => {
        console.log(`error: ${error.message}`);
    });

    command.on("close", code => {
        console.log(`child process exited with code ${code}`);
        if (code === 0){
            res.writeHead(200, {'Content-Type': 'application/json'});
            res.end(JSON.stringify(JSON.parse(response_string)));
        } else {
            res.writeHead(404, {'Content-Type': 'application/json'});
            res.end({"error":"unreachable"});
        }
    });
}

module.exports = getSLatest;