-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstorage.js
More file actions
137 lines (127 loc) · 3.37 KB
/
storage.js
File metadata and controls
137 lines (127 loc) · 3.37 KB
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
const sqlite3 = require("sqlite3").verbose();
class Storage {
createDatabase() {
var newdb = new sqlite3.Database("./storage.db", (err) => {
if (err) {
console.log("Getting error " + err);
exit(1);
}
this.createTables(newdb);
});
return newdb;
}
createTables(newdb) {
newdb.exec(
`
CREATE TABLE IF NOT EXISTS BridgeNFT (
id INTEGER PRIMARY KEY AUTOINCREMENT,
contractAddress TEXT NOT NULL,
originOwner TEXT NOT NULL,
tokenId INTEGER NOT NULL,
xrplAddress TEXT NOT NULL,
signed INTEGER DEFAULT 0,
offerCreated INTEGER DEFAULT 0
);`,
() => {}
);
}
getInstance() {
return new sqlite3.Database(
"./storage.db",
sqlite3.OPEN_READWRITE,
(err) => {
if (err && err.code == "SQLITE_CANTOPEN") {
console.log(err);
return;
} else if (err) {
console.log("Getting error " + err);
exit(1);
}
}
);
}
insert(db, contractAddress, originOwner, tokenId, xrplAddress) {
db.run(
`INSERT INTO BridgeNFT(contractAddress,originOwner,tokenId,xrplAddress,signed) select ?,?,?,?,? WHERE (SELECT COUNT(*) FROM BridgeNFT WHERE contractAddress = ? and tokenId = ?) = 0`,
[
contractAddress,
originOwner,
tokenId,
xrplAddress,
0,
contractAddress,
tokenId,
],
function () {
console.log("New BridgeNFT added with id " + this.lastID);
}
);
}
insertRecovery(
db,
contractAddress,
originOwner,
tokenId,
signed,
signedOffer
) {
try {
db.run(
`INSERT INTO BridgeNFT(contractAddress,originOwner,tokenId,xrplAddress,signed,offerCreated) select ?,?,?,?,?,? WHERE (SELECT COUNT(*) FROM BridgeNFT WHERE contractAddress = ? and tokenId = ?) = 0`,
[
contractAddress,
originOwner,
tokenId,
"",
signed,
signedOffer,
contractAddress,
tokenId,
]
);
} catch (err) {
console.log(err);
}
}
updateSigned(db, contractAddress, originOwner, tokenId) {
try {
db.run(
`UPDATE BridgeNFT SET signed = 1 WHERE contractAddress = ? and originOwner = ? and tokenId = ?`,
[contractAddress, originOwner, tokenId]
);
} catch (err) {
console.log(err);
}
}
updateOfferCreated(db, contractAddress, originOwner, tokenId) {
try {
db.run(
`UPDATE BridgeNFT SET offerCreated = 1 WHERE contractAddress = ? and originOwner = ? and tokenId = ?`,
[contractAddress, originOwner, tokenId]
);
} catch (err) {
console.log(err);
}
}
async recordexists(db, contractAddress, originOwner, tokenId) {
return new Promise(function (resolve, reject) {
let sql = `SELECT * FROM BridgeNFT WHERE contractAddress = ? and tokenId = ? and originOwner = ? `;
let returnVal = undefined;
try {
db.all(sql, [contractAddress, tokenId, originOwner], (err, rows) => {
if (err) {
console.log("Record Exists Error: " + err);
}
if (rows.length > 0) {
resolve(true);
} else {
resolve(false);
}
});
} catch (err) {
console.log(err);
}
});
}
}
module.exports = Storage;