-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.js
More file actions
276 lines (246 loc) · 8.8 KB
/
main.js
File metadata and controls
276 lines (246 loc) · 8.8 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
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
"use strict";
var Promise = require("bluebird");
var fs = require('fs');
var _path = require("path");
// Step 0: Configure Network and Path
var Nebulas = require("nebulas"),
Account = Nebulas.Account,
Neb = Nebulas.Neb,
Utils = Nebulas.Utils,
Unit = Nebulas.Unit,
neb = new Neb();
var rpcURL = "https://mainnet.nebulas.io";
var chainId = -1;
var nonce = -1;
var cgDappAddress = 'n1sr4JA4e9QPB4opLk2Kjmp8NkP6GGoAmnt';
var dappAddress = 'n1kVKK53C85Cu6PBkgE8Qvch9ym5GxnDSWr';
neb.setRequest(new Nebulas.HttpRequest(rpcURL));
var acc;
var key;
var passphrase = "password";
var path = "./accounts/";
new Promise(function(resolve, reject) {
// Step 1: Prepare account
try {
var accounts = require(path + "accounts.json");
key = JSON.stringify(require(path + accounts[0] + ".json"));
passphrase = accounts[1];
resolve();
} catch (e){
if (typeof process.env.KEY !== 'undefined' && typeof process.env.PASSWORD !== 'undefined'){
resolve();
return
}
try {
mkdirsSync(path);
generateWallets(1, passphrase, resolve);
} catch (e){
console.log('Can not configure automatically, please configure by steps.');
reject();
}
}
}).then(() => {
// Step 1: Prepare account
if(typeof process.env.KEY !== 'undefined' && typeof process.env.PASSWORD !== 'undefined'){
key = process.env.KEY;
passphrase = process.env.PASSWORD;
} else {
var accounts = require(path + "accounts.json");
key = JSON.stringify(require(path + accounts[0] + ".json"));
passphrase = accounts[1]
}
acc = new Account();
acc = acc.fromKey(key, passphrase, true);
// Step 2: Send NAS and NTT to generated wallets
console.log('Send 0.01 NAS and some NTT to the generated wallet: ' + acc.getAddressString());
// Step 3: Start Mining
setInterval(CGTPriceDaemon, 60000);
});
function CGTPriceDaemon(){
if(chainId > 0){
getNewCGTPrice(function(newPrice){
getOldCGTPrice(function(oldPrice){
if(Math.abs(newPrice - oldPrice) > 0.0001){
updateTokenPrice(dappAddress, {NAS: newPrice});
} else {
console.log("Price not changes: " + newPrice + ' NAS');
}
})
});
} else {
neb.api.getNebState().then((nebstate) => {
chainId = nebstate.chain_id;
});
}
}
function updateTokenPrice(address, price){
//updateTokenPrice
var fun = 'updateTokenPrice';
var args = [];
args.push(cgDappAddress);
args.push(price);
callContract(address, fun, args, 0, acc);
}
function callContract(contractAddress, fun, args, value, acc){
let address = acc.getAddressString();
neb.api.getAccountState(address).then((accstate) => {
if(Unit.fromBasic(accstate.balance, "nas").toNumber() > (value + 0.000001)){
let _value = Unit.toBasic(value);
if(nonce < 0){
nonce = parseInt(accstate.nonce);
}
nonce ++;
let _nonce = nonce;
let _to = contractAddress;
let _gasPrice = 1000000;
let _gasLimit = 2000000;
let _contract = {
"function": fun,
"args": JSON.stringify(args)
};
//generate transaction information
var Transaction = Nebulas.Transaction;
var tx = new Transaction({
chainID: chainId,
from: acc,
to: _to,
value: _value,
nonce: _nonce,
gasPrice: _gasPrice,
gasLimit: _gasLimit,
contract: _contract
});
tx.signTransaction();
console.log(address + ' call ' + contractAddress + ' @ ' + _contract.function + ": " +JSON.stringify(_contract.args) + ' with value: ' + value);
//send a transfer request to the NAS node
neb.api.sendRawTransaction({
data: tx.toProtoString()
}).then((result) => {
let txhash = result.txhash;
let trigger = setInterval(() => {
try{
neb.api.getTransactionReceipt({hash: txhash}).then((receipt) => {
if (receipt.status != 2) //not in pending
{
//console.log(JSON.stringify(receipt));
clearInterval(trigger);
}
});
} catch(err){
console.log(err);
clearInterval(trigger);
}
}, 5000);
});
} else {
console.log("Escape " + address + " balance less than "+ (value + 0.000001) +" NAS.");
}
});
}
function innerCall(fun, args, value, callback, address) {
let params = {};
var acc = new Account();
acc = acc.fromKey(key, passphrase, true);
params.from = acc;
if(Account.isValidAddress(address)){
params.to = address;
} else{
params.to = dappAddress;
}
params.gasPrice = Utils.toBigNumber(1000000);
params.gasLimit = Utils.toBigNumber(20000000);
params.value = value;
// prepare contract
params.contract = {
"function": fun,
"args": JSON.stringify(args)
};
console.log(params.from.getAddressString() + ' call ' + params.to + ' @ ' + fun + ": " + JSON.stringify(args) + ' with value: ' + value + ' time: ' + (new Date().toLocaleString()));
callback(params);
}
function getNewCGTPrice(callback){
var fun = 'sellPrice';
var args = [];
args.push(0.1);
innerCall(fun, args, 0, function(params){
neb.api.call(params.from.getAddressString(), params.to, params.value, 0, params.gasPrice, params.gasLimit, params.contract).then(function (resp) {
var result = resp.result;
if(result === 'null' || result === '""'){
return;
}
try{
var order = JSON.parse(result);
callback(order.price);
}catch (err){
//result is the error message
console.log("error:" + err.message)
}
}).catch(function (err) {
console.log("error:" + err.message)
});
}, cgDappAddress);
}
function getOldCGTPrice(callback){
var fun = 'getTokenByIndex';
var args = [];
args.push(0);
innerCall(fun, args, 0, function(params){
neb.api.call(params.from.getAddressString(), params.to, params.value, 0, params.gasPrice, params.gasLimit, params.contract).then(function (resp) {
var result = resp.result;
if(result === 'null' || result === '""'){
return;
}
try{
var token = JSON.parse(result);
callback(token.price.NAS);
}catch (err){
//result is the error message
console.log("error:" + err.message)
}
}).catch(function (err) {
console.log("error:" + err.message)
});
}, dappAddress);
}
function generateWallets(amount, passphrase, callback){
var accounts = [];
try {
accounts = require(path + "accounts.json")
} catch (err){
console.log("No exist accounts detected");
}
var length = accounts.length / 2;
if(amount > length){
for(var i = 0; i < amount - length; i++){
var acc = Account.NewAccount();
var address = acc.getAddressString();
var privateKeyString = acc.getPrivateKeyString();
//console.log('Account: ' + address + ' ; Private: ' + privateKeyString);
var key = acc.toKey(passphrase);
fs.writeFileSync(path + address + ".json", JSON.stringify(key), function(err) {
if(err) {
return console.log(err);
}
});
accounts.push(address);
accounts.push(passphrase);
console.log("The key " + address + ".json" + " was saved!");
}
fs.writeFileSync(path + "accounts.json", JSON.stringify(accounts), function(err) {
if(err) {
return console.log(err);
}
});
}
callback();
}
function mkdirsSync(dirname) {
if (fs.existsSync(dirname)) {
return true;
} else {
if (mkdirsSync(_path.dirname(dirname))) {
fs.mkdirSync(dirname);
return true;
}
}
}