-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatmv4.js
341 lines (244 loc) · 8.38 KB
/
atmv4.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import express from 'express';
import fetch from 'node-fetch';
import crypto from 'crypto';
//express config
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: false}));
import { BitGo} from 'bitgo';
import { coins } from '@bitgo/sdk-core';
//bitgo variable
//enter your accesstoken
const accessToken = '';
const bitgo = new BitGo({ env: 'test', accessToken: accessToken});
const coin = 'ofc';
bitgo.register(coin, coins.Ofc.createInstance);
//enter walletId tradingWallet ID
const walletId = '';
const tradeWallet = '';
const stakeCoin = 'tsol';
//set express to ejs view
app.set('view engine', 'ejs');
//res.render to view ejs file
//to rework this as modules export in the near future.
let ethPrice = '';
async function retrievePrice() {
try{
const response = await fetch(`https://app.bitgo-test.com/api/prime/trading/v1/accounts/${tradeWallet}/products/GTETH-TUSD*/level1`, {
headers: {
'Content-Type': 'application/json',
'accept': 'application/json',
'Authorization': `Bearer ${accessToken}`
}
});
if(!response.ok){
throw new Error(`HTTP Error ${response.status}`);
}
const data = await response.json();
return ethPrice = data.askPrice;
} catch (error) {
console.error('Error with Price ETH:', error);
throw error;
}
}
async function updatePrice() {
ethPrice = await retrievePrice();
//console.log(ethPrice)
}
//get initial pricing for eth
updatePrice();
//get wallet information function
let walletInfo = [];
async function getWalletDetails() {
try{
const response = await fetch(`https://app.bitgo-test.com/api/v2/${stakeCoin}/wallet/${walletId}`, {
headers: {
'Content-Type': 'application/json',
'accept': 'application/json',
'Authorization': `Bearer ${accessToken}`
}
});
if(!response.ok){
throw new Error(`HTTP Error ${response.status}`);
}
const walletData = await response.json();
//console.log(walletData);
//zeroing the array
walletInfo.length = 0;
walletInfo.push({"confirmBalance" : walletData.confirmedBalanceString});
walletInfo.push({"spendableBalance" : walletData.spendableBalanceString});
walletInfo.push({"label": walletData.label});
} catch (error) {
console.error('Refer to Error msg:', error);
throw error;
}
}
let stakeInfo = [];
async function getStakeInfo() {
try{
const response = await fetch(`https://app.bitgo-test.com/api/staking/v1/${stakeCoin}/wallets/${walletId}`, {
headers: {
'Content-Type': 'application/json',
'accept': 'application/json',
'Authorization': `Bearer ${accessToken}`
}
});
if(!response.ok){
throw new Error(`HTTP Error ${response.status}`);
}
const stakeData = await response.json();
//clear the array
stakeInfo.length = 0;
stakeInfo.push({"name": stakeData.coin});
stakeInfo.push({"reward": stakeData.rewards});
stakeInfo.push({"apy": stakeData.apy});
} catch (error) {
console.error('Refer to Error msg:', error);
throw error;
}
}
//initial information to be loaded
getStakeInfo();
//Index page
app.get('/', (req, res) => {
//get Ent walletDetails and run initial info
getWalletDetails();
//update ETH price whenever navigate to / site
updatePrice();
console.log(ethPrice);
//get Coin Balance for trade wallet account
let coinBal = [];
let getBalance = fetch(`https://app.bitgo-test.com/api/prime/trading/v1/accounts/${tradeWallet}/balances`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'accept': 'application/json',
'Authorization': `Bearer ${accessToken}`
}
})
.then(response => response.json())
.then((getBalance) => {
//calculate the amount of items in the currency list for loop.
const data = getBalance;
const balances = data['data'];
const dataLength = balances.length;
//looking for specific currency GTETH
for (let i =0; i < dataLength; i++) {
if ( balances[i]['currency'] === 'GTETH') { //looking for GTETH
//shift it to row 0
coinBal.unshift({"name": balances[i]['currency'], "balance": balances[i]['balance']});
} else if ( balances[i]['currency'] === 'TUSD*') { //looking for TUSD
coinBal.push({"name": balances[i]['currency'], "balance": balances[i]['balance']});
}
}
res.render('pages/index', {
coinName: coinBal[0]['name'],
coinBalance: coinBal[0]['balance'],
ethUsd: ethPrice,
tUsd: coinBal[1]['balance']
});
})
});
//Trade Link
app.get('/trade', (req, res) => {
updatePrice();
res.render('pages/trade', {
ethUsd: ethPrice
});
});
//Wallet Link
app.get('/wallet', (req, res) => {
res.render('pages/wallet', {
});
});
app.post('/getadd', (req, res) => {
const coinName = req.body.getCoin;
console.log(coinName);
const walletId = req.body.getId;
console.log(walletId)
let walletInfo = [];
async function getWalletDetails() {
try{
const response = await fetch(`https://app.bitgo-test.com/api/v2/${coinName}/wallet/${walletId}/addresses?includeBalances=true`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'accept': 'application/json',
'Authorization': `Bearer ${accessToken}`
}
});
if(!response.ok){
throw new Error(`HTTP Error ${response.status}`);
}
const walletData = await response.json();
console.log(JSON.stringify(walletData, null, 4));
//zeroing the array
walletInfo.length = 0;
walletInfo.push({"confirmBalance" : walletData.confirmedBalanceString});
walletInfo.push({"spendableBalance" : walletData.spendableBalanceString});
walletInfo.push({"label": walletData.label});
} catch (error) {
console.error('Refer to Error msg:', error);
throw error;
}
}
getWalletDetails();
}
)
//Trade eth
app.post('/run', (req, res) => {
const eth = req.body.ethAmount;
console.log(eth)
const uuid = crypto.randomUUID();
const body = {
"clientOrderId": `${uuid}`,
"type": "market",
"product": "GTETH-TUSD*",
"side": "sell",
"quantity": `${eth}`,
"quantityCurrency": "GTETH"
}
//start of trade function
let tradeStatus = fetch('https://app.bitgo-test.com//api/prime/trading/v1/accounts/6513b5ed0816d8000797bb3c864cae6d/orders', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'accept': 'application/json',
'Authorization': `Bearer ${accessToken}`
},
body: JSON.stringify(body)
})
.then(response => response.json())
.then((tradeStatus) => {
console.log(tradeStatus)
res.render('pages/success')
})
})
//staking page
app.get('/stake', (req, res) => {
//call stake and wallet get info functions
getStakeInfo();
getWalletDetails();
let rewards = stakeInfo[1]['reward'];
let apyRate = stakeInfo[2]['apy'];
console.log('this is',walletInfo[1]);
res.render('pages/stake', {
apyRate: apyRate,
solRewards: rewards,
walletLabel: walletInfo[2]['label'],
confirmedBalance: walletInfo[0]['confirmBalance'],
spendableBalance: walletInfo[1]['spendableBalance']
});
});
app.post('/solstake', (req, res) => {
const sol = req.body.solAmount;
console.log(sol)
const stakeUuid = crypto.randomUUID();
const body = {
"amount": `${sol}`,
"clientId": `${stakeUuid}`,
"type": "STAKE"
};
});
app.listen(8080);
console.log('Server at 8080');