Repository navigation
Expand file tree
/
Copy pathTestDefrostProcedure.js
More file actions
229 lines (178 loc) · 7.69 KB
/
Copy pathTestDefrostProcedure.js
File metadata and controls
229 lines (178 loc) · 7.69 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
const path = require('path');
const fs = require('fs');
// LOAD PARAMETERS --------------------------------
const ETHNODE_FILEPATH = path.resolve(__dirname) + '/PARAMS/ethereum_node.txt'
const PWD_FILEPATH = path.resolve(__dirname) + '/PARAMS/owner_pwd.txt'
const ACCOUNTSAMOUNTS_FILEPATH = path.resolve(__dirname) + '/OUTPUTS/generated_input_accounts_amounts.txt'
const CONTRACTADDRESS_FILEPATH = path.resolve(__dirname) + '/OUTPUTS/smart-contract-address.txt'
const DEFROSTED_LOG_ROOT = path.resolve(__dirname) + '/DEFROSTED/'
// set parameters -------------------------------------------------
var urlEthereumNode = require('fs').readFileSync(ETHNODE_FILEPATH, 'utf-8')
var ownerPassword = require('fs').readFileSync(PWD_FILEPATH, 'utf-8')
var contractAddress = require('fs').readFileSync(CONTRACTADDRESS_FILEPATH, 'utf-8')
console.log('urlEthereumNode = ' + urlEthereumNode)
console.log('ownerPwd = ' + ownerPassword)
console.log('filePathAccountsAmounts = ' + ACCOUNTSAMOUNTS_FILEPATH)
console.log('contractAddress = ' + contractAddress)
const DRTCoin = require('./build/DRTCoin.json');
const Web3 = require('web3');
let web3 = new Web3(new Web3.providers.HttpProvider(urlEthereumNode))
// PROGRAM VARIABLES (local)
var cntTimer = parseInt(0)
var defrostTimerId = -1
var drtContract;
// FROM FILES VARIABLES
var objAssignParams;
var vAccounts; // accounts/amounts from txt file
// init ethereum DRT smart contract ----------------------------------------------------------
drtCcontract =web3.eth.contract(DRTCoin.abi).at(contractAddress);
// unlock ethereum base account
web3.personal.unlockAccount(web3.eth.accounts[0], ownerPassword)
console.log('unlockAccount OK')
web3.eth.defaultAccount = web3.eth.accounts[0];
// read account/amounts file to assign -------------------------------------------------
vAccounts = require('fs').readFileSync(ACCOUNTSAMOUNTS_FILEPATH).toString().split('\n')
console.log('NUM ACCOUNTS = ' + vAccounts.length)
var vaddr = []
var vamounts = []
var viced = []
for(i=0;i<vAccounts.length;i++){
var vv = vAccounts[i].split(",");
if(vv.length == 3){
vaddr.push(vv[0]);
vamounts.push(parseInt(vv[1]));
viced.push(parseInt(vv[2])===0);
}
else if(vAccounts[i].length>0){
console.log('Fatal error: item size mismatch !!!!!!!!!!!!!!!!!!!!!!! ' + vAccounts[i])
}
}
console.log('_____________________________________________________________________________')
// launch assign timer ----------------------------------------------------------------
var defrostIntervalSec = parseInt(120)
console.log('intervalSec = ' + defrostIntervalSec)
defrostTimerId = setInterval(timerDefrostFunction, defrostIntervalSec * 1000);
var cntTimer = parseInt(0)
function timerDefrostFunction() {
console.log('timerDefrostFunction scheduler call ........ cntTimer = ' + cntTimer)
/*if( ENDofDEFROST ){
console.log('timer stopped //////////////////////////////////');
clearInterval(defrostTimerId);
}*/
tryDefrostAdvisors();
cntTimer++;
console.log('.......................................................................')
}
function estimateGas(dataparam){
var estimatedGas = web3.eth.estimateGas({data: dataparam})
gasLimit = web3.eth.getBlock("latest").gasLimit
gasOk=0
if(estimatedGas < gasLimit){
gasOk=estimatedGas;
}else{
gasOk=gasLimit;
}
return gasOk;
}
function tryDefrostAdvisors() {
var bCanDefrost = drtCcontract.canDefrost();
console.log("=======> bCanDefrost = " + bCanDefrost);
if(bCanDefrost === true){
// ADVISORS -------------------------------------
dataparam = drtCcontract.defrostToken.getData()
var gasOk = estimateGas(dataparam);
drtCcontract.defrostToken( { gas: gasOk }, function(error, result){
if (!error) {
console.log("defrostToken OK:" + result); // OK
waitForBlock(result, false);
} else {
console.log("Error: calling defrostToken => " + error);
}
});
// OWNER -------------------------------------
dataparamOwner = drtCcontract.defrostOwner.getData()
var gasOkOwner = estimateGas(dataparamOwner );
drtCcontract.defrostOwner( { gas: gasOkOwner }, function(error, result){
if (!error) {
console.log("defrostOwnerOK:" + result); // OK
waitForBlock(result, true);
} else {
console.log("Error: calling defrostToken => " + error);
}
});
}
}
// http://stackoverflow.com/questions/951021/what-is-the-javascript-version-of-sleep
function mySleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// We need to wait until any miner has included the transaction
// in a block to get the address of the contract
async function waitForBlock(txhash, isOwner) {
while (true) {
let receipt = web3.eth.getTransactionReceipt(txhash);
if (receipt && receipt.blockNumber) {
//console.log("Your defrostToken call has been mined in block " + receipt.blockNumber);
//console.log("Note that it might take 30 - 90 seconds for the block to propagate before it's visible in etherscan.io");
if(isOwner)
{
checkDefrostedOwner();
}
else
{
checkDefrostedAdvisors();
}
break;
}
//console.log("Waiting a mined block including your defrostToken call ... currently in block " + web3.eth.blockNumber);
await mySleep(10000);
}
}
function checkDefrostedOwner() {
var blockTimestamp = drtCcontract.getBlockTimestamp();
var defrostedLogFileOwner = DEFROSTED_LOG_ROOT + 'defrosted_' + blockTimestamp +'_OWNER' + '.txt'
fs.appendFileSync(defrostedLogFileOwner, blockTimestamp + '\n');
drtCcontract.getOwnerInfos(function(error, result){
if (!error)
{
owneraddr = result[0];
balance = parseInt(result[1]);
frosted = parseInt(result[2]);
defrosted = parseInt(result[3]);
console.log('OWNER ====> ' + owneraddr + " => bal: " + balance + " - frosted: " + frosted + " - defrosted: " + defrosted);
var strlog = owneraddr + ";balance=" + balance + ";frosted=" + frosted + ";defrosted=" + defrosted;
fs.appendFileSync(defrostedLogFileOwner , strlog + '\n');
}
else
{
console.log("Error: calling getIcedInfos => " + error);
}
})
}
function checkDefrostedAdvisors() {
var blockTimestamp = drtCcontract.getBlockTimestamp();
var defrostedLogFile = DEFROSTED_LOG_ROOT + 'defrosted_' + blockTimestamp + '.txt'
fs.appendFileSync(defrostedLogFile, blockTimestamp + '\n');
for(i=0;i<vAccounts.length;i++){
var isIced = viced[i];
if(isIced){
var addr = vaddr[i];
drtCcontract.getIcedInfos(addr, function(error, result){
if (!error)
{
icedaddr = result[0];
balance = parseInt(result[1]);
frosted = parseInt(result[2]);
defrosted = parseInt(result[3]);
console.log('ADVISOR => ' + icedaddr + " => bal: " + balance + " - frosted: " + frosted + " - defrosted: " + defrosted);
var strlog = icedaddr + ";balance=" + balance + ";frosted=" + frosted + ";defrosted=" + defrosted;
fs.appendFileSync(defrostedLogFile, strlog + '\n');
}
else
{
console.log("Error: calling getIcedInfos => " + error);
}
})
}
}
}