This repository has been archived by the owner on Jan 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest-deploy.html
104 lines (87 loc) · 3.87 KB
/
test-deploy.html
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
<html>
<head>
<title>Test Deploy - etheres.io</title>
</head>
<body>
<script type="text/javascript" src="./node_modules/web3/dist/web3.js"></script>
<script type="text/javascript" src="./node_modules/ethereumjs-tx/dist/ethereumjs-tx.js"></script>
<script type="text/javascript" src="./ethers.js"></script>
<script type="text/javascript">
var web3 = ethers.connect({endpoints: ['ws://localhost:5000/v1/morden']});
// Some key for test...
var privateKey = new Buffer(32);
privateKey.fill(0x42);
privateKey[5] = 0x05;
var address = '0x' + ethUtil.privateToAddress(privateKey).toString('hex');
console.log('Address', address);
function getGasPricePromise() {
return new Promise(function(resolve, reject) {
web3.eth.getGasPrice(function(error, gasPrice) {
console.log('a', arguments)
if (error) {
return reject(error);
}
resolve(gasPrice);
});
});
}
function getTransactionCountPromise(address) {
return new Promise(function(resolve, reject) {
web3.eth.getTransactionCount(address, 'pending', function(error, transactionCount) {
console.log('b', arguments)
if (error) {
return reject(error);
}
resolve(transactionCount);
});
});
}
function deploy(gasPrice, transactionCount) {
console.log(gasPrice, transactionCount);
console.log(gasPrice.toString(16));
var source = 'contract foo { }';
var contractName = 'foo';
var bytecode = '0x606060405260068060106000396000f3606060405200';
var runtimeBytecode = '0x60606040526008565b00';
var transactionInfo = {
nonce: transactionCount,
gasPrice: '0x' + gasPrice.toString(16),
gasLimit: 3000000,
to: '',
value: '0x00',
data: bytecode,
};
web3.eth.estimateGas(transactionInfo, function(error, estimatedGas) {
if (error) {
console.log(error);
return;
}
transactionInfo.gasLimit = estimatedGas;
var transaction = new EthTx(transactionInfo);
transaction.sign(privateKey);
var signedTransaction = transaction.serialize().toString('hex');
console.log(transaction, signedTransaction);
web3.ethers.deploySignedContract({
source: source,
compilerVersion: 'v0.3.1-2016-03-31-c67926c',
optimize: true,
contractName: contractName,
signedTransaction: '0x' + signedTransaction
}, function(error, result) {
console.log(error, result);
});
});
}
Promise.all([
getGasPricePromise(),
getTransactionCountPromise(address),
]).then(function(result) {
var gasPrice = result[0];
var transactionCount = result[1];
deploy(gasPrice, transactionCount);
}, function (error) {
console.log(error);
});
</script>
</body>
</html>