forked from ParkTaeKwang/trondapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
152 lines (139 loc) · 5.89 KB
/
index.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
var express = require('express');
var router = express.Router();
const TronWeb = require('tronweb');
var mysql = require('mysql');
const fullNode = 'https://api.shasta.trongrid.io';
const solidityNode = 'https://api.shasta.trongrid.io';
const eventServer = 'https://api.shasta.trongrid.io/';
var privateKey = '123';
var connection = mysql.createConnection({
host: 'localhost',
post: 3306,
user: 'tkpark',
password: '1234',
database: 'tkpark'
});
const tronWeb = new TronWeb(
fullNode,
solidityNode,
eventServer,
privateKey
);
tronWeb.setDefaultBlock('latest');
const nodes = tronWeb.isConnected();
const connected = !Object.entries(nodes).map(([ name, connected]) => {
if (!connected)
console.error(`Error: $ {name}is not connected`);
return connected;
}).includes(false);
if (!connected)
res.json( {'result':'connect fail'})
router.get('/createAccount/',function(req,res,next) {
const account = tronWeb.createAccount();
account.then(function (account) { const isValid = tronWeb.isAddress(account.address['hex']);
console.log('- Private Key:', account.privateKey);
console.log('- Base58:', account.address.base58);
console.log('- Valid: ', isValid, '\n');
account.isValid = isValid;
res.json( {'result':account.privateKey, '- Base58': account.address.base58});
});
});
router.get('/getBalance/:Address',function(req,res,next) {
const app = async () => {
try {
const Add = req.params.Address;
const gBalance = await tronWeb.trx.getBalance(Add);
const gBandwidth = await tronWeb.trx.getBandwidth(Add);
console.log("getBalance : ", gBalance);
console.log("getBandwidth : ", gBandwidth);
console.log(" ", Add);
res.json( { "getBalance ": gBalance, "getBandwidth": gBandwidth, "123": Add})
}catch (error) { console.log('Task Failure',error);
}
};
app();
});
router.get('/transactionview/:Address',function(req,res,next) {
try{
var Address = req.params.Address;
var select_sql = "select *, \"send\" from transactionscan where from_address=? union all select *, \"receive\" from transactionscan where to_address=? order by idx desc;";
connection.query(select_sql, [Address, Address], function (err, rows, fields) {
if (!err) {
var resultdown = JSON.stringify(rows);
var TRList = JSON.parse(resultdown);
var Result_data = [];
for(var i=0; i<TRList.length; i++){
var TR=TRList[i];
var info = {
send: TR.send,
idx: TR.idx,
from_address: TR.from_address,
to_address: TR.to_address,
amount: TR.amount,
txid: "https://shasta.tronscan.org/#/transaction/"+ TR.txid
};
Result_data.push(info);
}
var result_end={
count: Result_data.length, // count
Result_data: Result_data
};
console.log(result_end);
res.send(result_end);
} else {
console.log('query error : ' + err);
}
});
}catch (error) { console.log('Task Failure',error);
}
});
router.get('/sendToken',function(req,res,next) {
const app = async () => {
try {
const gPK = req.query.PK;
privateKey = tronWeb.setPrivateKey(gPK);
const gvalue = req.query.value;
const gsendAddress = await tronWeb.address.fromPrivateKey(gPK);
const gtoAddress = req.query.toAddress;
var txid = "";
var result = "";
var insert_sql = "insert into transactionscan (from_address, to_address, amount, txid) values (?, ?, ?, ?)";
var select_sql = "select * from transactionscan where txid=?";
/*
console.log("gPK : ", gPK);
console.log("gvalue : ", gvalue);
console.log("gsendAddres :", gsendAddress);
console.log("gtoAddress : ", gtoAddress);
*/
sendTransaction = await tronWeb.transactionBuilder.sendTrx(gtoAddress, gvalue, gsendAddress);
const signedTransaction = await tronWeb.trx.sign(sendTransaction);
const sendRaw = await tronWeb.trx.sendRawTransaction(signedTransaction);
result = JSON.stringify(sendRaw, null, 2);
var TRList = JSON.parse(result);
var txid = TRList.transaction.txID;
/*
console.log('- Transaction:\n' + TRList.transaction.txID);
res.json( { "gPK ": gPK, "gvalue ": gvalue, "gsendAddres ": gsendAddress, "gtoAddress": gtoAddress,'- Transaction:n': TRList.transaction.txID, "Transaction\n": JSON.stringify(sendRaw, null, 2)});
*/
var params = [gsendAddress, gtoAddress, gvalue, txid];
connection.query(insert_sql, params, function (err, rows, fields) {
if (!err) {
console.log('query error : ' + err);
//res.send(err);
}
});
connection.query(select_sql, txid, function (err, rows, fields) {
if (!err) {
console.log(rows);
var resultdown = 'rows : ' + JSON.stringify(rows);
res.send(resultdown);
} else {
console.log('query error : ' + err);
}
});
}catch (error) { console.log('Task Failure',error);
}
};
app();
});
module.exports = router;