-
Notifications
You must be signed in to change notification settings - Fork 3
/
database.js
74 lines (67 loc) · 1.94 KB
/
database.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
import pg from "pg";
import * as dotenv from "dotenv";
dotenv.config();
const { Pool } = pg;
const pool = new Pool();
export async function writeInfoToDb(infoObj){
try {
let allKeys = "";
let allValues = "";
for (const key in infoObj) {
allKeys += key + ", ";
const nextValue =
typeof infoObj[key] == "string" ? `'${infoObj[key]}'` : infoObj[key];
allValues += nextValue + ", ";
}
allKeys = allKeys.slice(0, -2);
allValues = allValues.slice(0, -2);
const result = await pool.query(
`INSERT INTO bridge (${allKeys}) VALUES(${allValues}) RETURNING *;`
);
return result.rows[0];
} catch (e) {
console.log(e);
}
}
export async function getAllBridgeInfo(){
try {
const result = await pool.query(`SELECT * FROM bridge ORDER BY id DESC;`);
return result.rows;
} catch (e) {
console.log(e);
}
}
export async function getRecentBridgeInfo(){
try {
const result = await pool.query(`SELECT * FROM bridge ORDER BY id DESC LIMIT 20;`);
return result.rows;
} catch (e) {
console.log(e);
}
}
export async function bridgeInfoEthAddress(ethAddress){
try {
const result = await pool.query(`SELECT * FROM bridge WHERE sbchOriginAddress='${ethAddress}';`);
return result.rows;
} catch (e) {
console.log(e);
}
}
export async function checkAmountBridgedDb() {
try {
const result = await pool.query(`SELECT * FROM bridge WHERE txIdBCH IS NOT NULL;`);
return result.rows.length;
} catch (e) {
console.log(e);
}
}
export async function addBridgeInfoToNFT(nftNumber, infoObj) {
try {
const { timeBridged, signatureProof, txIdBCH, destinationAddress } = infoObj;
const result = await pool.query(
`UPDATE bridge SET timeBridged='${timeBridged}', signatureProof='${signatureProof}', txIdBCH='${txIdBCH}', destinationAddress='${destinationAddress}' WHERE nftNumber='${nftNumber}' RETURNING *;`
);
} catch (e) {
console.log(e);
}
}