As we saw in Compile.js spits out Bytecode and ABI when compiled.
In this article we will write Node.js which takes all the input (Bytecode, ABI, Constructor Arguments, Gas amount) and outputs contract address. Later contract address and ABI can be used by other applications or Smart Contracts to build dApps. Above diagram describes what I just wrote. We will write Node.js for deploying to local environment, Ganache. It makes deployment process just so easy to test. Later in other articles I will show how to deploy to test networks. Create a folder deploy and file deploy.js in this folder. We will use libraries which allows to connect with local or test networks. Web3.js is a JavaScript framework to build distributed apps (dApps). Follow below code. In bold are actual commands.
const Web3 = require(‘web3’); //Web3 is a constructor
Using above constructor, we create an instance of web3.
ganache-cli is a provider for connection to local . Consider it as communication layer between web3 and a particular network. Each network (Test or Live) has its own provider. This provider sends and receive messages between network and web3.
const ganache = require(‘ganache-cli’);
Below require statement refers to the compile.js from and captures Interface and Bytecode from compile.js. Remember module.exports as a last statement within compile.js.
const = require(‘../compile’);
While running deploy script in case you land into error “possible memory leak detected. 11 error listeners added. Use emitter.setMaxListeners() to increase limit.” Use below command
require(‘events’). EventEmitter.prototype._maxListeners = 100;
Write a function deploySC which is deploying smart contract. Remember every function in web3 is of asynchronous nature. That’s why function is wrapped with async. Read Promises, async and await in Java script in case you are not familiar. For asynchronous programming getting grasp of Promise and async/await is quite an important.
const deploySC = async () => )
.send();
console.log(‘deployment done’);
//This print contract address on successful deployment
console.log(‘SC Address ‘+deployContract.options.address);
}
deploySC(); //Call function to deploy contract
From the command prompt run node deploy.js from same folder where deploy.js is residing. When contract gets deployed, contract address is generated. In world there are 2 kinds of addresses.
Externally owned Account (EOA) and Contract address. In above script accounts [0] is EOA or public address through which one can send and receive ethers. Every account address resides in a and linked to its unique private key. Contract address can also receive Ethers, but main distinction from EOA is Contract address can also receive and send messages/data. Watch out for my next article which will be an explanation of Encryption, Wallets, MetaMask, Public and Private keys concepts.
Useful links for Promise, Async/Await
I will appreciate your claps in case you like this article.
Published at Sun, 06 Jan 2019 19:50:33 +0000