Skip to content

Instantly share code, notes, and snippets.

@iisaint
Last active December 10, 2021 01:20
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save iisaint/0d46d71ea474d97ddc0e598ec998e76a to your computer and use it in GitHub Desktop.
Save iisaint/0d46d71ea474d97ddc0e598ec998e76a to your computer and use it in GitHub Desktop.
Using node.js to deploy a smart contract
const Web3 = require('web3');
const fs = require('fs');
const solc = require('solc');
/*
* connect to ethereum node
*/
const ethereumUri = 'http://localhost:8540';
const address = '0x004ec07d2329997267Ec62b4166639513386F32E'; // user
let 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);
let coinbase = web3.eth.coinbase;
console.log('coinbase:' + coinbase);
let balance = web3.eth.getBalance(coinbase);
console.log('balance:' + web3.fromWei(balance, 'ether') + " ETH");
let accounts = web3.eth.accounts;
console.log(accounts);
if (web3.personal.unlockAccount(address, 'user')) {
console.log(`${address} is unlocaked`);
}else{
console.log(`unlock failed, ${address}`);
}
}
/*
* Compile Contract and Fetch ABI
*/
let source = fs.readFileSync("./contracts/BasicToken.sol", 'utf8');
console.log('compiling contract...');
let compiledContract = solc.compile(source);
console.log('done');
for (let contractName in compiledContract.contracts) {
// code and ABI that are needed by web3
// console.log(contractName + ': ' + compiledContract.contracts[contractName].bytecode);
// console.log(contractName + '; ' + JSON.parse(compiledContract.contracts[contractName].interface));
var bytecode = compiledContract.contracts[contractName].bytecode;
var abi = JSON.parse(compiledContract.contracts[contractName].interface);
}
console.log(JSON.stringify(abi, undefined, 2));
/*
* deploy contract
*/
let gasEstimate = web3.eth.estimateGas({data: '0x' + bytecode});
console.log('gasEstimate = ' + gasEstimate);
let MyContract = web3.eth.contract(abi);
console.log('deploying contract...');
let myContractReturned = MyContract.new( {
from: address,
data: '0x'+ bytecode,
gas: gasEstimate + 50000
}, function (err, myContract) {
if (!err) {
// NOTE: The callback will fire twice!
// Once the contract has the transactionHash property set and once its deployed on an address.
// e.g. check tx hash on the first call (transaction send)
if (!myContract.address) {
console.log(`myContract.transactionHash = ${myContract.transactionHash}`); // The hash of the transaction, which deploys the contract
// check address on the second call (contract deployed)
} else {
console.log(`myContract.address = ${myContract.address}`); // the contract address
global.contractAddress = myContract.address;
}
// Note that the returned "myContractReturned" === "myContract",
// so the returned "myContractReturned" object will also get the address set.
} else {
console.log(err);
}
});
(function wait () {
setTimeout(wait, 1000);
})();
@AshwinPRJ
Copy link

I am getting the following error.. can you tell me how to solve it?
Error: Account not found
at Object.InvalidResponse (/home/ramesh/projects/DemoSmartContract/node_modules/web3/lib/web3/errors.js:38:16)
at RequestManager.send (/home/ramesh/projects/DemoSmartContract/node_modules/web3/lib/web3/requestmanager.js:61:22)
at Personal.send [as unlockAccount] (/home/ramesh/projects/DemoSmartContract/node_modules/web3/lib/web3/method.js:145:58)
at Object. (/home/ramesh/projects/DemoSmartContract/index.js:25:23)
at Module._compile (internal/modules/cjs/loader.js:702:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
at Module.load (internal/modules/cjs/loader.js:612:32)
at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
at Function.Module._load (internal/modules/cjs/loader.js:543:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)

@gauravnayyar
Copy link

Check your account is there or not.

Step to check on console.:
1: go to geth.ipc console.
2: type web3.coinbase or web3.eth.getAccounts()
3.Check the address

Copy link

ghost commented Dec 10, 2021

Check your account is there or not.

Step to check on console:

  1. go to geth.ipc console.
  2. type web3.coinbase or web3.eth.getAccounts()
  3. check the address
  4. finish

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment