-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwallets.js
More file actions
164 lines (134 loc) · 4.67 KB
/
Copy pathwallets.js
File metadata and controls
164 lines (134 loc) · 4.67 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
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
const { ApolloServer } = require('@apollo/server');
const { startStandaloneServer } = require('@apollo/server/standalone');
const { gql } = require('graphql-tag');
const fs = require('fs');
const jwt = require('jsonwebtoken');
// Secrets for JWT
const JWT_SECRET = 'adminsecretkey';
const ADMIN_SECRET = 'adminsecretkey';
// Define the GraphQL schema
const typeDefs = gql`
type Query {
getBalance(token: String!): Float!
getTransactions(token: String!): [Transaction!]!
getAllTransactions(adminToken: String!): [Transaction!]!
}
type Mutation {
updateTransactionStatus(adminToken: String!, transactionId: ID!, status: String!): Transaction!
topUpAccount(adminToken: String!, username: String!, amount: Float!): Transaction!
}
type Transaction {
id: ID!
userid: ID!
walletAddress: String!
privateKey: String!
amount: Float!
convertedAmount: Float!
status: String! # "success", "pending", "failed"
createdAt: String!
blockchain: String!
}
type User {
id: ID!
firstName: String!
lastName: String!
email: String!
}
type Admin {
id: ID!
username: String!
email: String!
}
`;
// Mock databases
const users = JSON.parse(fs.readFileSync('users.json', 'utf8')) || [];
const admins = JSON.parse(fs.readFileSync('admins.json', 'utf8')) || [];
const transactions = JSON.parse(fs.readFileSync('usertransactions.json', 'utf8')) || [];
const saveTransactionsToFile = () => {
fs.writeFileSync('usertransactions.json', JSON.stringify(transactions, null, 2));
};
// Resolver functions
const resolvers = {
Query: {
getBalance: (_, { token }) => {
const decoded = jwt.verify(token, JWT_SECRET);
const user = users.find((u) => u.id === decoded.id);
if (!user) {
throw new Error('Invalid token or user not found');
}
// Calculate balance from transactions
const userTransactions = transactions.filter((t) => t.userid === user.id && t.status === 'success');
const balance = userTransactions.reduce((sum, t) => sum + (t.type === 'deposit' ? t.amount : -t.amount), 0);
return balance;
},
getTransactions: (_, { token }) => {
const decoded = jwt.verify(token, JWT_SECRET);
const user = users.find((u) => u.id === decoded.id);
if (!user) {
throw new Error('Invalid token or user not found');
}
return transactions.filter((t) => t.userid === user.id);
},
getAllTransactions: (_, { adminToken }) => {
const decoded = jwt.verify(adminToken, ADMIN_SECRET);
const admin = admins.find((a) => a.id === decoded.id);
if (!admin) {
throw new Error('Invalid admin token or admin not found');
}
return transactions;
},
},
Mutation: {
updateTransactionStatus: (_, { adminToken, transactionId, status }) => {
const decoded = jwt.verify(adminToken, ADMIN_SECRET);
const admin = admins.find((a) => a.id === decoded.id);
if (!admin) {
throw new Error('Invalid admin token or admin not found');
}
const transaction = transactions.find((t) => t.id === transactionId);
if (!transaction) {
throw new Error('Transaction not found');
}
transaction.status = status;
saveTransactionsToFile();
return transaction;
},
topUpAccount: (_, { adminToken, username, amount }) => {
const decoded = jwt.verify(adminToken, ADMIN_SECRET);
const admin = admins.find((a) => a.id === decoded.id);
if (!admin) {
throw new Error('Invalid admin token or admin not found');
}
const user = users.find((u) => u.username === username);
if (!user) {
throw new Error('User not found');
}
const newTransaction = {
id: (transactions.length + 1).toString(),
userid: user.id,
walletAddress: "GeneratedWalletAddress", // Replace with logic for wallet address
privateKey: "GeneratedPrivateKey", // Replace with logic for private key
amount,
convertedAmount: amount * 1, // Replace with conversion logic if necessary
status: 'success',
createdAt: new Date().toISOString(),
blockchain: 'BSC', // Or Solana, based on logic
};
transactions.push(newTransaction);
saveTransactionsToFile();
return newTransaction;
},
},
};
// Create the Apollo Server
const server = new ApolloServer({
typeDefs,
resolvers,
introspection: true, // Enables introspection for Apollo Studio
});
(async () => {
const { url } = await startStandaloneServer(server, {
listen: { port: process.env.PORT || 4001 }, // Use the platform's assigned port or default to 4001
});
console.log(`\uD83D\uDE80 Server ready at ${url}`);
})();