Deploy Contract
Deploy contract is same as make a transaction to node. I want to send the contract to node1
and cost the gas
from account of node1
.
- unlock account of
node2
on console
> personal.unlockAccount("683f257a240789cc8ffe6f0b4f17dc57405b6ce6")
Unlock account 683f257a240789cc8ffe6f0b4f17dc57405b6ce6
Passphrase:
true
- start mining on
node2
> miner.start()
INFO [08-15|15:16:12] Starting mining operation
null
> INFO [08-15|15:16:12] Commit new mining work number=1 txs=0 uncles=0 elapsed=118µs
INFO [08-15|15:16:12] Successfully sealed new block number=1 hash=0147be…bf5996
INFO [08-15|15:16:12] 🔨 mined potential block number=1 hash=0147be…bf5996
- modify
index.js
to unlock the account ofnode1
...
const accountAddress = '2a7a93ca16e95a6e4478bf67e33ce3a9c20bf66d';
const accountPassword = 'node1';
const web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider(ethereumUri));
if(!web3.isConnected()) {
throw new Error(`unable to connect to ethereum node at ${ethereumUri}`);
} else {
console.log(`connected to ehterum node at ${ethereumUri}`);
const coinbase = web3.eth.coinbase;
console.log(`coinbase: ${coinbase}`);
const balance = web3.eth.getBalance(coinbase);
console.log(`balance: ${web3.fromWei(balance, 'ether')} ETH`);
console.log(web3.eth.accounts);
if (web3.personal.unlockAccount(accountAddress, accountPassword)) {
console.log(`${accountAddress} is unlocaked`);
} else {
console.log(`unlock failed, ${accountAddress}`);
}
}
...
- append below code to deploy contract to
node1
// compile contract
const source = fs.readFileSync("./contracts/Sample.sol", 'utf8');
console.log('compiling contract...');
const compiledContract = solc.compile(source);
console.log('done');
for (let contractName in compiledContract.contracts) {
var bytecode = compiledContract.contracts[contractName].bytecode;
var abi = JSON.parse(compiledContract.contracts[contractName].interface);
}
console.log(JSON.stringify(abi, undefined, 2));
// deploy contract
const gasEstimate = web3.eth.estimateGas({ data: '0x' + bytecode });
console.log(`gasEstimate = ${gasEstimate}`);
const MyContract = web3.eth.contract(abi);
console.log('deploying contract...');
const myContractReturned = MyContract.new([], {
from: accountAddress,
data: `0x${bytecode}`,
gas: gasEstimate + 50000
}, function (err, myContract) {
if (!err) {
if (!myContract.address) {
console.log(`myContract.transactionHash = ${myContract.transactionHash}`);
} else {
console.log(`myContract.address = ${myContract.address}`);
global.contractAddress = myContract.address;
}
} else {
console.log(err);
}
});
(function wait () {
setTimeout(wait, 1000);
})();
$ node index.js
connected to ehterum node at http://localhost:8545
coinbase: 0x2a7a93ca16e95a6e4478bf67e33ce3a9c20bf66d
balance: 9.04625697166532776746648320380374280103671755200316906558262375061821325312e+56 ETH
[ '0x2a7a93ca16e95a6e4478bf67e33ce3a9c20bf66d' ]
2a7a93ca16e95a6e4478bf67e33ce3a9c20bf66d is unlocaked
compiling contract...
done
[
{
"constant": false,
"inputs": [
{
"name": "a",
"type": "uint256"
},
{
"name": "b",
"type": "uint256"
}
],
"name": "add",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "getCount",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"type": "function"
}
]
gasEstimate = 120724
deploying contract...
myContract.transactionHash = 0x51718da05b2106ad2fa8652efca39c529dfefa87f2866ddbff426906bba4e432
myContract.address = 0xce5b2a924367a7c7f638b1c269518d6064bd0e93