forked from bokkypoobah/OpenANXTokenOld
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.js
326 lines (292 loc) · 15.3 KB
/
functions.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
// Jun 12 2017
var ethPriceUSD = 380.39;
// -----------------------------------------------------------------------------
// Accounts
// -----------------------------------------------------------------------------
var accounts = [];
var accountNames = {};
addAccount(eth.accounts[0], "Account #0 - Miner");
addAccount(eth.accounts[1], "Account #1 - Token Owner");
addAccount(eth.accounts[2], "Account #2 - KYCed");
addAccount(eth.accounts[3], "Account #3 - KYCed");
addAccount(eth.accounts[4], "Account #4");
addAccount(eth.accounts[5], "Account #5");
addAccount(eth.accounts[6], "Account #6");
addAccount(eth.accounts[7], "Account #7");
addAccount(eth.accounts[8], "Account #8");
addAccount(eth.accounts[9], "Account #9 - Crowdfund Wallet");
addAccount(eth.accounts[10], "Account #10 - Foundation");
addAccount(eth.accounts[11], "Account #11 - Advisors");
addAccount(eth.accounts[12], "Account #12 - Directors");
addAccount(eth.accounts[13], "Account #13 - Early Backers");
addAccount(eth.accounts[14], "Account #14 - Developers");
addAccount(eth.accounts[15], "Account #15 - Precommitments");
var minerAccount = eth.accounts[0];
var tokenOwnerAccount = eth.accounts[1];
var account2 = eth.accounts[2];
var account3 = eth.accounts[3];
var account4 = eth.accounts[4];
var account5 = eth.accounts[5];
var account6 = eth.accounts[6];
var account7 = eth.accounts[7];
var account8 = eth.accounts[8];
var crowdfundWallet = eth.accounts[9];
var foundationAccount = eth.accounts[10];
var advisorsAccount = eth.accounts[11];
var directorsAccount = eth.accounts[12];
var earlyBackersAccount = eth.accounts[13];
var developersAccount = eth.accounts[14];
var precommitmentsAccount = eth.accounts[15];
var baseBlock = eth.blockNumber;
function unlockAccounts(password) {
for (var i = 0; i < 15; i++) {
personal.unlockAccount(eth.accounts[i], password, 100000);
}
}
function addAccount(account, accountName) {
accounts.push(account);
accountNames[account] = accountName;
}
// -----------------------------------------------------------------------------
// Token Contract
// -----------------------------------------------------------------------------
var tokenContractAddress = null;
var tokenContractAbi = null;
var lockedTokenContractAbi = null;
function addTokenContractAddressAndAbi(address, tokenAbi, lockedTokenAbi) {
tokenContractAddress = address;
tokenContractAbi = tokenAbi;
lockedTokenContractAbi = lockedTokenAbi;
}
// -----------------------------------------------------------------------------
// Account ETH and token balances
// -----------------------------------------------------------------------------
function printBalances() {
console.log("DEBUG: tokenContractAddress: " + tokenContractAddress);
console.log("DEBUG: tokenContractAbi: " + tokenContractAbi);
var token = tokenContractAddress == null || tokenContractAbi == null ? null : web3.eth.contract(tokenContractAbi).at(tokenContractAddress);
var decimals = token == null ? 18 : token.decimals();
var lockedTokenContract = token == null || lockedTokenContractAbi == null ? null : web3.eth.contract(lockedTokenContractAbi).at(token.lockedTokens());
var i = 0;
var totalTokenBalanceUnlocked = new BigNumber(0);
var totalTokenBalance1Y = new BigNumber(0);
var totalTokenBalance2Y = new BigNumber(0);
var totalTokenBalance = new BigNumber(0);
console.log("RESULT: # Account EtherBalanceChange Unlocked Token Locked 1Y Locked 2Y Total Name");
console.log("RESULT: -- ------------------------------------------ --------------------------- ------------------------------ ------------------------------ ------------------------------ ------------------------------ ---------------------------");
accounts.forEach(function(e) {
i++;
var etherBalanceBaseBlock = eth.getBalance(e, baseBlock);
var etherBalance = web3.fromWei(eth.getBalance(e).minus(etherBalanceBaseBlock), "ether");
var tokenBalanceUnlocked = token == null ? new BigNumber(0) : token.balanceOf(e).shift(-decimals);
var tokenBalance1Y = lockedTokenContract == null ? new BigNumber(0) : lockedTokenContract.balanceOfLocked1Y(e).shift(-decimals);
var tokenBalance2Y = lockedTokenContract == null ? new BigNumber(0) : lockedTokenContract.balanceOfLocked2Y(e).shift(-decimals);
var tokenBalance = tokenBalanceUnlocked.add(tokenBalance1Y).add(tokenBalance2Y);
totalTokenBalanceUnlocked = totalTokenBalanceUnlocked.add(tokenBalanceUnlocked);
totalTokenBalance1Y = totalTokenBalance1Y.add(tokenBalance1Y);
totalTokenBalance2Y = totalTokenBalance2Y.add(tokenBalance2Y);
totalTokenBalance = totalTokenBalance.add(tokenBalance);
console.log("RESULT: " + pad2(i) + " " + e + " " + pad(etherBalance) + " " + padToken(tokenBalanceUnlocked, decimals) + " " + padToken(tokenBalance1Y, decimals) + " " +
padToken(tokenBalance2Y, decimals) + " " + padToken(tokenBalance, decimals) + " " + accountNames[e]);
});
console.log("RESULT: -- ------------------------------------------ --------------------------- ------------------------------ ------------------------------ ------------------------------ ------------------------------ ---------------------------");
console.log("RESULT: " + padToken(totalTokenBalanceUnlocked, decimals) + " " +
padToken(totalTokenBalance1Y, decimals) + " " + padToken(totalTokenBalance2Y, decimals) + " " + padToken(totalTokenBalance, decimals) + " Total Token Balances *");
console.log("RESULT: -- ------------------------------------------ --------------------------- ------------------------------ ------------------------------ ------------------------------ ------------------------------ ---------------------------");
console.log("RESULT: * Note that the sum of all the locked tokens is represented in the unlocked balance at the token contract address, and this will be double counted in the grand total balance above");
console.log("RESULT: ");
}
function pad2(s) {
var o = s.toFixed(0);
while (o.length < 2) {
o = " " + o;
}
return o;
}
function pad(s) {
var o = s.toFixed(18);
while (o.length < 27) {
o = " " + o;
}
return o;
}
function padToken(s, decimals) {
var o = s.toFixed(decimals);
var l = parseInt(decimals)+12;
while (o.length < l) {
o = " " + o;
}
return o;
}
// -----------------------------------------------------------------------------
// Transaction status
// -----------------------------------------------------------------------------
function printTxData(name, txId) {
var tx = eth.getTransaction(txId);
var txReceipt = eth.getTransactionReceipt(txId);
var gasPrice = tx.gasPrice;
var gasCostETH = tx.gasPrice.mul(txReceipt.gasUsed).div(1e18);
var gasCostUSD = gasCostETH.mul(ethPriceUSD);
console.log("RESULT: " + name + " gas=" + tx.gas + " gasUsed=" + txReceipt.gasUsed + " costETH=" + gasCostETH +
" costUSD=" + gasCostUSD + " @ ETH/USD=" + ethPriceUSD + " gasPrice=" + gasPrice + " block=" +
txReceipt.blockNumber + " txId=" + txId);
}
function assertEtherBalance(account, expectedBalance) {
var etherBalance = web3.fromWei(eth.getBalance(account), "ether");
if (etherBalance == expectedBalance) {
console.log("RESULT: OK " + account + " has expected balance " + expectedBalance);
} else {
console.log("RESULT: FAILURE " + account + " has balance " + etherBalance + " <> expected " + expectedBalance);
}
}
function gasEqualsGasUsed(tx) {
var gas = eth.getTransaction(tx).gas;
var gasUsed = eth.getTransactionReceipt(tx).gasUsed;
return (gas == gasUsed);
}
function failIfGasEqualsGasUsed(tx, msg) {
var gas = eth.getTransaction(tx).gas;
var gasUsed = eth.getTransactionReceipt(tx).gasUsed;
if (gas == gasUsed) {
console.log("RESULT: FAIL " + msg);
return 0;
} else {
console.log("RESULT: PASS " + msg);
return 1;
}
}
function passIfGasEqualsGasUsed(tx, msg) {
var gas = eth.getTransaction(tx).gas;
var gasUsed = eth.getTransactionReceipt(tx).gasUsed;
if (gas == gasUsed) {
console.log("RESULT: PASS " + msg);
return 1;
} else {
console.log("RESULT: FAIL " + msg);
return 0;
}
}
function failIfGasEqualsGasUsedOrContractAddressNull(contractAddress, tx, msg) {
if (contractAddress == null) {
console.log("RESULT: FAIL " + msg);
return 0;
} else {
var gas = eth.getTransaction(tx).gas;
var gasUsed = eth.getTransactionReceipt(tx).gasUsed;
if (gas == gasUsed) {
console.log("RESULT: FAIL " + msg);
return 0;
} else {
console.log("RESULT: PASS " + msg);
return 1;
}
}
}
// -----------------------------------------------------------------------------
// Token Contract details
// -----------------------------------------------------------------------------
function printTokenContractStaticDetails() {
if (tokenContractAddress != null && tokenContractAbi != null) {
var contract = eth.contract(tokenContractAbi).at(tokenContractAddress);
var decimals = contract.decimals();
console.log("RESULT: token.symbol=" + contract.symbol());
console.log("RESULT: token.name=" + contract.name());
console.log("RESULT: token.decimals=" + decimals);
console.log("RESULT: token.DECIMALSFACTOR=" + contract.DECIMALSFACTOR());
var startDate = contract.START_DATE();
console.log("RESULT: token.START_DATE=" + startDate + " " + new Date(startDate * 1000).toUTCString() +
" / " + new Date(startDate * 1000).toGMTString());
var endDate = contract.END_DATE();
console.log("RESULT: token.END_DATE=" + endDate + " " + new Date(endDate * 1000).toUTCString() +
" / " + new Date(endDate * 1000).toGMTString());
console.log("RESULT: token.TOKENS_SOFT_CAP=" + contract.TOKENS_SOFT_CAP().shift(-decimals));
console.log("RESULT: token.TOKENS_HARD_CAP=" + contract.TOKENS_HARD_CAP().shift(-decimals));
console.log("RESULT: token.TOKENS_TOTAL=" + contract.TOKENS_TOTAL().shift(-decimals));
}
}
var dynamicDetailsFromBlock = 0;
function printTokenContractDynamicDetails() {
if (tokenContractAddress != null && tokenContractAbi != null && lockedTokenContractAbi != null) {
var contract = eth.contract(tokenContractAbi).at(tokenContractAddress);
var lockedTokenContract = eth.contract(lockedTokenContractAbi).at(contract.lockedTokens());
var decimals = contract.decimals();
console.log("RESULT: token.finalised=" + contract.finalised());
console.log("RESULT: token.tokensPerKEther=" + contract.tokensPerKEther());
console.log("RESULT: token.totalSupply=" + contract.totalSupply().shift(-decimals));
var locked1YDate = contract.LOCKED_1Y_DATE();
console.log("RESULT: token.LOCKED_1Y_DATE=" + locked1YDate + " " + new Date(locked1YDate * 1000).toUTCString() +
" / " + new Date(locked1YDate * 1000).toGMTString());
var locked2YDate = contract.LOCKED_2Y_DATE();
console.log("RESULT: token.LOCKED_2Y_DATE=" + locked2YDate + " " + new Date(locked2YDate * 1000).toUTCString() +
" / " + new Date(locked2YDate * 1000).toGMTString());
console.log("RESULT: lockedToken.TOKENS_LOCKED_1Y_TOTAL=" + lockedTokenContract.TOKENS_LOCKED_1Y_TOTAL().shift(-decimals));
console.log("RESULT: lockedToken.TOKENS_LOCKED_2Y_TOTAL=" + lockedTokenContract.TOKENS_LOCKED_2Y_TOTAL().shift(-decimals));
console.log("RESULT: lockedToken.totalSupplyLocked1Y=" + lockedTokenContract.totalSupplyLocked1Y().shift(-decimals));
console.log("RESULT: lockedToken.totalSupplyLocked2Y=" + lockedTokenContract.totalSupplyLocked2Y().shift(-decimals));
console.log("RESULT: lockedToken.totalSupplyLocked=" + lockedTokenContract.totalSupplyLocked().shift(-decimals));
console.log("RESULT: token.owner=" + contract.owner());
console.log("RESULT: token.newOwner=" + contract.newOwner());
var latestBlock = eth.blockNumber;
var i;
var ownershipTransferredEvent = contract.OwnershipTransferred({}, { fromBlock: dynamicDetailsFromBlock, toBlock: latestBlock });
i = 0;
ownershipTransferredEvent.watch(function (error, result) {
console.log("RESULT: OwnershipTransferred Event " + i++ + ": from=" + result.args._from + " to=" + result.args._to + " " +
result.blockNumber);
});
ownershipTransferredEvent.stopWatching();
var tokensPerKEtherUpdatedEvent = contract.TokensPerKEtherUpdated({}, { fromBlock: dynamicDetailsFromBlock, toBlock: latestBlock });
i = 0;
tokensPerKEtherUpdatedEvent.watch(function (error, result) {
console.log("RESULT: TokensPerKEtherUpdated Event " + i++ + ": tokensPerKEther=" + result.args.tokensPerKEther + " block=" + result.blockNumber);
});
tokensPerKEtherUpdatedEvent.stopWatching();
var walletUpdatedEvent = contract.WalletUpdated({}, { fromBlock: dynamicDetailsFromBlock, toBlock: latestBlock });
i = 0;
walletUpdatedEvent.watch(function (error, result) {
console.log("RESULT: WalletUpdated Event " + i++ + ": from=" + result.args.newWallet + " block=" + result.blockNumber);
});
walletUpdatedEvent.stopWatching();
var precommitmentAddedEvent = contract.PrecommitmentAdded({}, { fromBlock: dynamicDetailsFromBlock, toBlock: latestBlock });
i = 0;
precommitmentAddedEvent.watch(function (error, result) {
console.log("RESULT: PrecommitmentAdded Event " + i++ + ": participant=" + result.args.participant +
" balance=" + result.args.balance.shift(-decimals) +
" block=" + result.blockNumber);
});
precommitmentAddedEvent.stopWatching();
var tokensBoughtEvent = contract.TokensBought({}, { fromBlock: dynamicDetailsFromBlock, toBlock: latestBlock });
i = 0;
tokensBoughtEvent.watch(function (error, result) {
console.log("RESULT: TokensBought Event " + i++ + ": buyer=" + result.args.buyer +
" ethers=" + web3.fromWei(result.args.ethers, "ether") +
" newEtherBalance=" + web3.fromWei(result.args.newEtherBalance, "ether") +
" tokens=" + result.args.tokens.shift(-decimals) +
" newTotalSupply=" + result.args.newTotalSupply.shift(-decimals) +
" tokensPerKEther=" + result.args.tokensPerKEther +
" block=" + result.blockNumber);
});
tokensBoughtEvent.stopWatching();
var kycVerifiedEvent = contract.KycVerified({}, { fromBlock: dynamicDetailsFromBlock, toBlock: latestBlock });
i = 0;
kycVerifiedEvent.watch(function (error, result) {
console.log("RESULT: KycVerified Event " + i++ + ": participant=" + result.args.participant + " block=" + result.blockNumber);
});
kycVerifiedEvent.stopWatching();
var approvalEvent = contract.Approval({}, { fromBlock: dynamicDetailsFromBlock, toBlock: latestBlock });
i = 0;
approvalEvent.watch(function (error, result) {
console.log("RESULT: Approval Event " + i++ + ": owner=" + result.args._owner + " spender=" + result.args._spender + " " +
result.args._value.shift(-decimals) + " block=" + result.blockNumber);
});
approvalEvent.stopWatching();
var transferEvent = contract.Transfer({}, { fromBlock: dynamicDetailsFromBlock, toBlock: latestBlock });
i = 0;
transferEvent.watch(function (error, result) {
console.log("RESULT: Transfer Event " + i++ + ": from=" + result.args._from + " to=" + result.args._to +
" value=" + result.args._value.shift(-decimals) + " block=" + result.blockNumber);
});
transferEvent.stopWatching();
dynamicDetailsFromBlock = latestBlock + 1;
}
}