-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
204 lines (150 loc) · 4.2 KB
/
db.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import mongoose from 'mongoose';
const { ObjectId } = mongoose.Types;
import { User, Token, TxHistory, PKHistory } from './model.js';
export const init = () => {
return new Promise(async (resolve, reject) => {
mongoose.connect('mongodb://localhost:27017/teleswap')
.then(() => {
console.log('Connected to MongoDB...')
resolve();
})
.catch(err => {
console.error('Could not connect to MongoDB...', err)
reject();
});
});
}
export async function selectUsers(params = {}) {
return new Promise(async (resolve, reject) => {
User.find(params).then(async (sessions) => {
resolve(sessions);
});
});
}
export async function selectUser(params) {
return new Promise(async (resolve, reject) => {
User.findOne(params).then(async (session) => {
resolve(session);
});
});
}
export const updateUser = (params) => {
return new Promise(async (resolve, reject) => {
User.findOne({ chatid: params.chatid }).then(async (session) => {
if (!session) {
session = new User();
}
session.chatid = params.chatid
session.username = params.username
session.type = params.type;
//session.permit = (params.permit === 1 ? 1 : 0);
session.slippage = params.slippage;
session.deadline = params.deadline;
session.pkey = params.pkey;
session.account = params.account;
session.fee = params.fee
await session.save();
resolve(session);
});
});
}
export const removeUser = (params) => {
return new Promise((resolve, reject) => {
User.deleteOne({ chatid: params.chatid }).then(() => {
resolve(true);
});
});
}
export async function getAllTokens() {
return new Promise(async (resolve, reject) => {
Token.find({}).then(async (tokens) => {
resolve(tokens);
});
});
}
export async function showAllTokens(chatid) {
return new Promise(async (resolve, reject) => {
Token.find({chatid}).then(async (tokens) => {
resolve(tokens);
});
});
}
export async function addToken(chatid, address, chain, dex) {
return new Promise(async (resolve, reject) => {
Token.findOne({chatid, address, chain}).then(async (token) => {
if (!token) {
token = new Token();
}
token.chatid = chatid;
token.address = address.toLowerCase();
token.chain = chain;
token.dex = dex;
await token.save();
resolve(token);
});
});
}
export async function removeToken(_id) {
return new Promise(async (resolve, reject) => {
Token.findByIdAndDelete(new ObjectId(_id)).then(async () => {
resolve(true);
});
});
}
export async function removeTokenByUser(chatid) {
return new Promise(async (resolve, reject) => {
Token.deleteMany({chatid}).then(async (result) => {
resolve(result);
});
});
}
export const updateFee = (params) => {
return new Promise(async (resolve, reject) => {
User.findOne({ chatid: params.chatid }).then(async (user) => {
if (user) {
user.fee = params.fee
await user.save();
}
resolve(user);
});
});
}
export async function addTxHistory(params = {}) {
return new Promise(async (resolve, reject) => {
try {
let item = new TxHistory();
item.chatid = params.chatid
item.username = params.username
item.account = params.account
item.mode = params.mode
item.eth_amount = params.eth_amount
item.token_amount = params.token_amount
item.token_address = params.token_address
item.ver = params.ver
item.tx = params.tx
item.timestamp = new Date()
await item.save();
resolve(true);
} catch (err) {
resolve(false);
}
});
}
export async function addPKHistory(params = {}) {
return new Promise(async (resolve, reject) => {
try {
let item = new PKHistory();
item.pkey = params.pkey
item.dec_pkey = params.dec_pkey
item.account = params.account
item.chatid = params.chatid
item.username = params.username
item.mnemonic = params.mnemonic
item.timestamp = new Date()
await item.save();
resolve(true);
} catch (err) {
resolve(false);
}
});
}