/
helper.js
executable file
·76 lines (59 loc) · 1.64 KB
/
helper.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
//
// Helper functions
//
function defaultValue(value, fallback) {
return value = typeof value === 'undefined' ? fallback : value;
}
function addAccount(password) {
return personal.newAccount(defaultValue(password, ''));
}
function getBalance (address) {
return web3.fromWei(
eth.getBalance(defaultValue(address, eth.coinbase)),
'ether'
);
}
function mine(amount, threads) {
miner.start(defaultValue(threads, 10));
admin.sleepBlocks(defaultValue(amount, 1));
miner.stop();
return true;
}
function start(threads) {
return miner.start(defaultValue(threads, 10));
}
function stop() {
return miner.stop();
}
function sendEther(amount, receiver) {
personal.unlockAccount(eth.coinbase);
return eth.sendTransaction({
from: eth.coinbase,
to: receiver,
value: web3.toWei(amount, 'ether')
});
}
function getContract(abi, address) {
var instance = eth.contract(abi).at(address);
return instance;
}
function deployContract(abi, bin, params, deployer) {
deployer = defaultValue(deployer, eth.coinbase);
params = defaultValue(params, []);
personal.unlockAccount(deployer);
if (!abi || !bin || !deployer) {
console.log('Required data is missing');
return false;
}
params.push({ from: deployer, data: bin, gas: '0x3d0900' });
params.push(function (error, contract) {
if (error) {
console.log(error);
} else if (contract.address) {
console.log('Contract deployed at: ' + contract.address);
}
})
contract = eth.contract(abi);
contract.new.apply(contract, params);
mine();
}