This repository has been archived by the owner on Nov 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
customNode.js
136 lines (135 loc) · 5.37 KB
/
customNode.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
'use strict';
var customNode = function(srvrUrl, port, httpBasicAuthentication) {
this.SERVERURL = port ? srvrUrl + ':' + port : srvrUrl;
if (httpBasicAuthentication) {
var authorization = 'Basic ' + btoa(httpBasicAuthentication.user + ":" + httpBasicAuthentication.password);
this.config.headers['Authorization'] = authorization;
}
}
customNode.prototype.config = {
headers: {
'Content-Type': 'application/json;'
}
};
customNode.prototype.getCurrentBlock = function(callback) {
this.post({
method: 'eth_blockNumber'
}, function(data) {
if (data.error) callback({ error: true, msg: data.error.message, data: '' });
else callback({ error: false, msg: '', data: new BigNumber(data.result).toString() });
});
}
customNode.prototype.getChainId = function(callback) {
this.post({
method: 'net_version'
}, function(data) {
if (data.error) callback({ error: true, msg: data.error.message, data: '' });
else callback({ error: false, msg: '', data: parseInt(data.result) });
});
}
customNode.prototype.getBalance = function(addr, callback) {
this.post({
method: 'eth_getBalance',
params: [addr, 'pending']
}, function(data) {
if (data.error) callback({ error: true, msg: data.error.message, data: '' });
else callback({ error: false, msg: '', data: { address: addr, balance: new BigNumber(data.result).toString() } });
});
}
customNode.prototype.getTransaction = function(txHash, callback) {
this.post({
method: 'eth_getTransactionByHash',
params: [txHash]
}, function(data) {
if (data.error) callback({ error: true, msg: data.error.message, data: '' });
else callback({ error: false, msg: '', data: data.result });
});
}
customNode.prototype.getTransactionData = function(addr, callback) {
var response = { error: false, msg: '', data: { address: addr, balance: '', gasprice: '', nonce: '' } };
var parentObj = this;
var reqObj = [
{ "id": parentObj.getRandomID(), "jsonrpc": "2.0", "method": "eth_getBalance", "params": [addr, 'pending'] },
{ "id": parentObj.getRandomID(), "jsonrpc": "2.0", "method": "eth_gasPrice", "params": [] },
{ "id": parentObj.getRandomID(), "jsonrpc": "2.0", "method": "eth_getTransactionCount", "params": [addr, 'pending'] }
];
this.rawPost(reqObj, function(data) {
for (var i in data) {
if (data[i].error) {
callback({ error: true, msg: data[i].error.message, data: '' });
return;
}
}
response.data.balance = new BigNumber(data[0].result).toString();
response.data.gasprice = data[1].result;
response.data.nonce = data[2].result;
callback(response);
});
}
customNode.prototype.sendRawTx = function(rawTx, callback) {
this.post({
method: 'eth_sendRawTransaction',
params: [rawTx]
}, function(data) {
if (data.error) callback({ error: true, msg: data.error.message, data: '' });
else callback({ error: false, msg: '', data: data.result });
});
}
customNode.prototype.getEstimatedGas = function(txobj, callback) {
txobj.value = ethFuncs.trimHexZero(txobj.value);
this.post({
method: 'eth_estimateGas',
params: [{ from: txobj.from, to: txobj.to, value: txobj.value, data: txobj.data }]
}, function(data) {
if (data.error) callback({ error: true, msg: data.error.message, data: '' });
else callback({ error: false, msg: '', data: data.result });
});
}
var ethCallArr = {
calls: [],
callbacks: [],
timer: null
};
customNode.prototype.getEthCall = function(txobj, callback) {
var parentObj = this;
if (!ethCallArr.calls.length) {
ethCallArr.timer = setTimeout(function() {
parentObj.rawPost(ethCallArr.calls, function(data) {
ethCallArr.calls = [];
var _callbacks = ethCallArr.callbacks.slice();
ethCallArr.callbacks = [];
for (var i in data) {
if (data[i].error) _callbacks[i]({ error: true, msg: data[i].error.message, data: '' });
else _callbacks[i]({ error: false, msg: '', data: data[i].result });
}
});
}, 500);
}
ethCallArr.calls.push({ "id": parentObj.getRandomID(), "jsonrpc": "2.0", "method": "eth_call", "params": [{ to: txobj.to, data: txobj.data }, 'latest'] });
ethCallArr.callbacks.push(callback);
}
customNode.prototype.getTraceCall = function(txobj, callback) {
this.post({
method: 'trace_call',
params: [txobj, ["stateDiff", "trace", "vmTrace"]]
}, function(data) {
if (data.error) callback({ error: true, msg: data.error.message, data: '' });
else callback({ error: false, msg: '', data: data.result });
});
}
customNode.prototype.rawPost = function(data, callback) {
ajaxReq.http.post(this.SERVERURL, JSON.stringify(data), this.config).then(function(data) {
callback(data.data);
}, function(data) {
callback({ error: true, msg: "connection error", data: "" });
});
}
customNode.prototype.getRandomID = function() {
return globalFuncs.getRandomBytes(16).toString('hex');
}
customNode.prototype.post = function(data, callback) {
data.id = this.getRandomID();
data.jsonrpc = "2.0";
this.rawPost(data, callback);
}
module.exports = customNode;