|
@@ -1,6 +1,6 @@
|
|
|
import 'dotenv/config'
|
|
import 'dotenv/config'
|
|
|
import grpc from "grpc"
|
|
import grpc from "grpc"
|
|
|
-import express, { Request } from "express"
|
|
|
|
|
|
|
+import express from "express"
|
|
|
import bodyParser from "body-parser"
|
|
import bodyParser from "body-parser"
|
|
|
import basicAuth from "express-basic-auth"
|
|
import basicAuth from "express-basic-auth"
|
|
|
import {
|
|
import {
|
|
@@ -12,6 +12,7 @@ import util from 'util'
|
|
|
import { exec } from 'child_process'
|
|
import { exec } from 'child_process'
|
|
|
import cors from 'cors'
|
|
import cors from 'cors'
|
|
|
import * as sql from 'sqlite3'
|
|
import * as sql from 'sqlite3'
|
|
|
|
|
+import fs from 'fs'
|
|
|
|
|
|
|
|
const app = express();
|
|
const app = express();
|
|
|
app.use(bodyParser.json());
|
|
app.use(bodyParser.json());
|
|
@@ -42,30 +43,67 @@ app.get("/", (req, res) => {
|
|
|
res.send("Chain4All Blockchain service");
|
|
res.send("Chain4All Blockchain service");
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
-app.get("/transactions", async (req, res, next) => {
|
|
|
|
|
- let db = getDbConnection();
|
|
|
|
|
|
|
+app.get("/transfers/:userId", async (req, res, next) => {
|
|
|
|
|
+ let db = await getDbConnection();
|
|
|
|
|
|
|
|
db.all(
|
|
db.all(
|
|
|
- "SELECT hash, user " +
|
|
|
|
|
|
|
+ "SELECT hash " +
|
|
|
"FROM transfers " +
|
|
"FROM transfers " +
|
|
|
|
|
+ "WHERE user=? " +
|
|
|
"ORDER BY id DESC " +
|
|
"ORDER BY id DESC " +
|
|
|
"LIMIT 10;",
|
|
"LIMIT 10;",
|
|
|
- [],
|
|
|
|
|
|
|
+ [req.params.userId],
|
|
|
(err, rows) => {
|
|
(err, rows) => {
|
|
|
- if(err){
|
|
|
|
|
- next(err);
|
|
|
|
|
|
|
+ if (err) {
|
|
|
|
|
+ next(err);
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- console.log(rows);
|
|
|
|
|
|
|
+
|
|
|
|
|
+ res.send(rows);
|
|
|
}
|
|
}
|
|
|
- );
|
|
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ db.close();
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
-app.post("/transaction", async (req, res, next) => {
|
|
|
|
|
- let db = getDbConnection();
|
|
|
|
|
|
|
+app.post("/transfer", async (req, res, next) => {
|
|
|
|
|
+ try {
|
|
|
|
|
+ if (!req.body) {
|
|
|
|
|
+ res.status(400);
|
|
|
|
|
+ throw Error(JSON.stringify({ error: { name: "Error, request has no body!" } }));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (!req.body.txHash) {
|
|
|
|
|
+ res.status(400);
|
|
|
|
|
+ throw Error(JSON.stringify({ error: { name: "Error, request body has no \"txHash\" property!" } }));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (!req.body.user) {
|
|
|
|
|
+ res.status(400);
|
|
|
|
|
+ throw Error(JSON.stringify({ error: { name: "Error, request body has no \"user\" property!" } }));
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
+ let db = await getDbConnection();
|
|
|
|
|
+
|
|
|
|
|
+ db.run(
|
|
|
|
|
+ "INSERT INTO transfers (hash, user)" +
|
|
|
|
|
+ "VALUES (?, ?);",
|
|
|
|
|
+ [req.body.txHash, req.body.user],
|
|
|
|
|
+ (err) => {
|
|
|
|
|
+ if (err) {
|
|
|
|
|
+ next(err);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ res.status(201);
|
|
|
|
|
+ res.send();
|
|
|
|
|
+ }
|
|
|
|
|
+ );
|
|
|
|
|
|
|
|
|
|
+ db.close();
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (err) {
|
|
|
|
|
+ next(err);
|
|
|
|
|
+ }
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
app.post("/price", (req, res) => { //add caching of same requests
|
|
app.post("/price", (req, res) => { //add caching of same requests
|
|
@@ -121,18 +159,31 @@ app.post("/buy", async (req, res, next) => {
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
-function getDbConnection() {
|
|
|
|
|
- let db = new sql.Database(DB_FILE_NAME);
|
|
|
|
|
-
|
|
|
|
|
- db.run(
|
|
|
|
|
- "CREATE TABLE [IF NOT EXISTS] transfers ( " +
|
|
|
|
|
- "id INTEGER PRIMARY KEY, " +
|
|
|
|
|
- "hash TEXT NOT NULL, " +
|
|
|
|
|
- "user TEXT NOT NULL, " +
|
|
|
|
|
- ") [WITHOUT ROWID]; "
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- return db;
|
|
|
|
|
|
|
+function getDbConnection(): Promise<sql.Database> {
|
|
|
|
|
+
|
|
|
|
|
+ return new Promise<sql.Database>((resolve, reject) => {
|
|
|
|
|
+ if(!fs.existsSync("./data")){
|
|
|
|
|
+ fs.mkdirSync("data");
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ let db = new sql.Database(DB_FILE_NAME);
|
|
|
|
|
+
|
|
|
|
|
+ db.run(
|
|
|
|
|
+ "CREATE TABLE IF NOT EXISTS transfers( " +
|
|
|
|
|
+ "id INTEGER PRIMARY KEY, " +
|
|
|
|
|
+ "hash TEXT NOT NULL, " +
|
|
|
|
|
+ "user TEXT NOT NULL " +
|
|
|
|
|
+ ");",
|
|
|
|
|
+ (err) => {
|
|
|
|
|
+ if (err) {
|
|
|
|
|
+ reject(err);
|
|
|
|
|
+ }
|
|
|
|
|
+ else{
|
|
|
|
|
+ resolve(db);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ );
|
|
|
|
|
+ });
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
async function getTransactionDetail(txHash: string, user: string) {
|
|
async function getTransactionDetail(txHash: string, user: string) {
|