Deploying a smart contract in Blockchain — Ethereum with Remix

Vivek Singh
5 min readDec 14, 2021

--

Introduction

As we know smart contracts are just code/algorithms which a Blockchain uses for its day to day working. These smart contracts should be available publicly for it to be used by the users worldwide. This availability of code is dependent on deployment of the smart contract.

Since Ethereum is a protocol, it means that there could be multiple different independent networks where a smart contract could be deployed. That means you can have your own network where you can deploy your contract and make it private or public based on your use case.

Just like any monolithic or web2 based application like Facebook, or Instagram; web3 apps also go through the process of development, staging, testing, integration and then production to make sure everything’s working as expected. With smart contracts, one needs to be more careful, as once the code is deployed, it can’t be altered.

Mainnet is the primary public network for Ethereum where actual value transactions occur on the distributed ledger. There are testnets which are publicly available to test the code in a production like environment before deploying it to Mainnet. Example of public testnets : Rinkbery, Gorli, Kovan

With this we have a brief idea on why and where we need to deploy smart contracts.

Let’s look at some terms before beginning the actual practical deployment of your smart contracts :

Transactions and accounts

Transactions are an agreement of something between two parties like an exchange of Bitcoin is a transaction. Account is just a unique address which identifies a user in order to perform any transactions.

Gas

Gas refers to the fee that is needed to execute a transaction. This fee is paid to the Miners who validate your transactions. Ethereum uses Proof of Work (there is a change in algorithm from Ethereum 2) which means it relies on the miners to find and solve the mathematical problems to actually get to validate your transaction. A miner can decline to process your transaction if the gas fee is minimal. So for every transaction or smart contract to be run on a blockchain network, you need to pay gas fees.

Compiling Smart Contracts

You need to compile your solidity code in order for Ethereum Virtual Machine (EVM) to understand it. EVM understands bytecode so a solidity program is compiled to low level bytecode(also termed opcodes).

Every Ethereum node runs on the EVM to maintain consensus across the blockchain. The compilation also produces an Application Binary Interface(ABI) i.e. a json file, which you need in order for your application to understand the contract and call the functions defined in the contract. A JS client library like web3 will call and use this ABI file to interact with your smart contract.

Remix IDE
Remix is an application(IDE) which helps to write, debug, test and deploy Ethereum Smart Contracts

Installation https://www.npmjs.com/package/remix-ide

Hoping you have already installed Remix IDE.

Screenshot of what it looks like :

Let’s create a solidity program, compile, deploy, and test it manually.

You can check my Solidity blog, for better understanding of Solidity language.

Create a file named HelloWorldContract.sol:

pragma solidity >=0.4.22 <0.9.0;contract HelloWorldContract {
string greeting;
constructor() public {
greeting = 'Hello World';
}
function greet() public view returns (string memory) {
return greeting;
}
}

The first line declares the version of solidity you want to use for the compilation of this code. The program above creates a contract named HelloWorldContract and initializes the variable named greeting with string ‘Hello World’. We have a function created named greet() which will output the value of the variable greeting.

Compilation

Now, select the compiler based on the solidity version you have provided in the first line of your HelloWorldContract.sol code. Select your file and then compile.

After compilation this is what the folder structure would look like :

After compilation two main files are generated . You can get the bytecode and ABI by clicking on buttons like shown in the screenshot below :

Now click on the third menu button from the left panel in your IDE and make the correct selections for ‘Deploy and Run Transactions’

Environment is provided by default in Remix IDE. It’s basically a blockchain network where your code will be deployed. To connect to external network(not provided by Remix), you can select Web3 Provider and then provide your blockchain network IP :

In our case, we will use a network(Environment) provided by Remix which is JavaScript VM and deploy the smart contract :

After you click on deploy, a new address is created for your smart contract. This address can be used to connect your application to the smart contract.

Now to test it :

Click on greet.

The message ‘Hello World’ will be displayed as output after you click on greet :

Other ways to deploy your smart contract

You can use Truffle which is a world class development environment and gives you all the features a Remix IDE gives, actually a lot more than what a Remix IDE provides. It can be used as a command line tool. You can use Ganache to create a blockchain network and use this network throughout your development cycle.

References

https://www.youtube.com/watch?v=SeQluFcMPTc
https://ethereum.org/en/developers/docs/smart-contracts/deploying/
https://www.investopedia.com/terms/g/gas-ethereum.asp
https://www.tutorialspoint.com/ethereum/ethereum_deploying_contract.htm

Conclusion

Please let me know in case of any queries and feel free to react out at LinkedIn
Happy Learning. Cheers!

--

--

Vivek Singh
Vivek Singh

Written by Vivek Singh

Software Developer. I write about Full Stack, NLP and Blockchain. Buy me a coffee - buymeacoffee.com/viveksinless

No responses yet