-
Notifications
You must be signed in to change notification settings - Fork 3
/
compile-sign-deploy.js
74 lines (62 loc) · 2.47 KB
/
compile-sign-deploy.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
const Web3 = require('../ethereum/node_modules/web3/index.js')
const web3 = new Web3()
const config = require('../ethereum/config')
var Tx = require('../ethereum/node_modules/ethereumjs-tx/index.js')
const keth = require('../ethereum/node_modules/keythereum/index.js')
web3.setProvider(new web3.providers.HttpProvider(config['provider-remote'].dev))
const contractFile = "contract.sol"
const fs = require('fs')
const exec = require('child_process').execSync
console.log("compiling",contractFile)
exec(`solc --bin --abi --optimize --overwrite -o bin contract.sol`)
/*pay attention to file names here!*/
var abi = fs.readFileSync("bin/BucJSToken.abi")
var compiled = "0x" + fs.readFileSync("bin/BucJSToken.bin")
console.log("we should now have in 'bin' the abi and the compiled contract")
var contract = web3.eth.contract(JSON.parse(abi))
/*
helper function to check when a transaction is mined
*/
var getReceipt = (hash) => {
console.log("waiting for transaction",hash,"to be mined")
block = web3.eth.getTransactionReceipt(hash)
if(block == null) {
setTimeout(()=>getReceipt(hash),1000)
} else {
console.log("transaction mined in block",block.blockNumber,"which has hash",block.blockHash)
}
}
/*
read key and address from a file
*/
var personal = require('./key')
var deployer = personal.address
console.log("deploying from", deployer, "using", web3.currentProvider)
const tokenCfg = require('./config')
//let's see how much gas it will take to deploy the contract
var cData = contract.new.getData(parseInt(tokenCfg.supply),tokenCfg.name,parseInt(tokenCfg.decimals),tokenCfg.symbol,{data:compiled})
var gasLimit = web3.eth.estimateGas({data: cData})
console.log("it's about",gasLimit,"gas")
nonceHex = web3.toHex(web3.eth.getTransactionCount(deployer))
gasPriceHex = web3.toHex(web3.eth.gasPrice+1)
gasLimitHex = web3.toHex(gasLimit+1)
console.log("nonce is",nonceHex,"for",deployer,"gasLimit",gasLimitHex,"gasPrice",gasPriceHex)
var rawTx = {
nonce: nonceHex,
gasPrice: gasPriceHex,
gasLimit: gasLimitHex,
from: deployer,
data: cData
}
console.log("raw transaction",JSON.stringify(rawTx))
var tx = new Tx(rawTx);
var privateKey = new Buffer(personal.key, 'hex')
tx.sign(privateKey);
var serializedTx = tx.serialize()
var txhex = serializedTx.toString("hex")
if(txhex.substring(0,3) != '0x')
txhex = '0x'+txhex
console.log("sending hex TX",txhex)
var hash = web3.eth.sendRawTransaction(txhex)
console.log("transaction sent with hash",hash)
getReceipt(hash)