Skip to main content
search

How To Create Smart Contract Using Ethereum

By June 12, 2018December 11th, 2023Emerging Tech
Blog page image01

Overview of Ethereum Smart contracts:

A smart contract, it is a protocol which intends to be able to digitally facilitate or enforce the negotiation or the performance of a contract. Without any third parties involvement, smart contracts allow the performance of credible transactions. These transactions are irreversible and can be tracked. Smart contracts are account holding objects on the ethereum blockchain. They contain code functions and can interact with other contracts, make decisions, store data, and send ether to others.

Contracts are defined by their creators, but their execution, and by extension the services they offer, is provided by the ethereum network itself. They will exist and be executable as long as the whole network exists, and will only disappear if they were programmed to self-destruct.

Building the first smart contract:

Building a smart contract using the command line:

To begin with, create a contract and only then an own cryptocurrency token can be sent out. Once it’s done, through crowdfunding funds can be raised and thus if successful it will supply radically a transparent and democratic organization that will only obey its own citizens, will never swerve away from its constitution and cannot be censored or shut down.

Before beginning make sure to;

  • Install the Ethereum CLI
  • Learn more about contracts

Please confirm that the GUI is closed before entering the geth console. Run geth to begin the sync process (this may take a while on the first run).

First Citizen: The Greeter

The frontier is a major open region and some of the time you may feel forlorn, so our first request of business will be to make a little-programmed friend to welcome you at whatever point you feel desolate. We’ll call him the “Greeter”.

The Greeter is an intelligent digital entity that lives on the blockchain and can have discussions with any individual who collaborates with it, in view of its information. It’s a great listener.

Here is the code:

contract Mortal {
/* Define variable owner of the type address */
address owner;


/* This function is executed at initialization and sets the owner of the contract */
function Mortal() { owner = msg.sender; }


/* Function to recover the funds on the contract */
function kill() { if (msg.sender == owner) selfdestruct(owner); }
}


contract Greeter is Mortal {
/* Define variable greeting of the type string */
string greeting;


/* This runs when the contract is executed */
function Greeter(string _greeting) public {
greeting = _greeting;
}


/* Main function */
function greet() constant returns (string) {
return greeting;
}
}

It can be encountered that “mortal” and “greeter” two different contracts are used. Reason being, that solidity (the use of high-level contract language) has an inheritance, meaning that one contract can inherit characteristics of another. This is useful to simplify coding as there are common traits of contracts that don’t need to be rewritten again and again, and hence all the contracts can be written in smaller chunks. Henceforth, by just declaring that greeter is mortal you inherited all characteristics from the mortal contract and kept the greeter code simple.

The inherited characteristic “mortal” refers to the greeter contract which can be destroyed by the owner, in order to recover funds locked into the contract when not required and can also clean up the blockchain. There is no owner for immortal in contracts of ethereum, meaning that once deployed the author has no special privileges anymore. This is should be considered seriously before making any decision.

Compiling your contract using the Solc Compiler:

Before being able to deploy a contract, the following are required:

  1. The compiled code
  2. The application binary interface, which is a JavaScript object that defines how to interact with the contract

 

 

By the use of solidity compiler, both of these can be found. If you have not installed a compiler, either of the two can be done:

  1. Install a compiler on the computer machine by following the instructions for installing the Solidity Compiler
  2. Use Remix, a web-based Solidity IDE

Solc on the machine:

Once installed the compiler on the machine, the need to compile the contract is required to acquire the compiled code and Application Binary Interface.

solc -o target –bin –abi Greeter.sol

This will create two files, one file containing the compiled code and one file creating the Application Binary Interface in a directory called target.

$tree
.
├── Greeter.sol
└── target
├── Greeter.abi
├── Greeter.bin
├── Mortal.abi
└── Mortal.bin

It can be seen that there are files created for both contracts however greeter includes mortal therefore there is no need to deploy Mortal to deploy Greeter. You can use these two files to create and deploy the contract.

Once compiled, the code is ready. Therefore, open Ganache and where it would look something like this. These below steps are followed to link the ethereum wallet to the ecosystem, that is Ganache.

Smart-Contract-Using-Ethereum

 

Near the top of the screen, an option can be seen that says “RPC SERVER”, which will be further used, later.

Now, unzip your MyEtherWallet download and open the folder. Then, open the index.html file in your browser to see the following screen.

Smart-Contract-Using-Ethereum

 

In the top right corner, click on the drop-down stating that MyEtherWallet, select what Ethereum network to connect to. By default, it connects to the Ethereum (ETH) main network.

Smart-Contract-Using-Ethereum

 

Click “Add Custom Node”. Now you can input the RPC Server information that Ganache gave us earlier.

Smart-Contract-Using-Ethereum

 

MyEtherWallet is now linked to the self-hosted blockchain through Ganache. Let’s use MyEtherWallet to upload the counter smart contract to our blockchain. To do this, we’ll click “Contracts” in MyEtherWallet’s top navigation bar and select “Deploy Contract”.

Smart-Contract-Using-Ethereum

 

As you can see, MyEtherWallet asks us for the contract’s bytecode. To locate this, we’ll go back to our Solc complied work or Remix IDE and click the “Details” button.

You should now see a dialog with a lot of information about our Counter contract. To copy the bytecode, we’ll click the clipboard icon next to “BYTECODE”.Then, we’ll go back to MyEtherWallet and paste the bytecode into the dialog box.

Smart-Contract-Using-Ethereum

 

As noticed in the above picture, fortunately; Ganache gave us 5 address that could be used to associate with our private blockchain. Hence, to utilize one of them in order to upload this contract, we can return to Ganache and tap the key symbol for any of the addresses. The private key will be shown.

After we click “Unlock”, MyEtherWallet will ask whether we want to sign in this transaction and deploy the contract. If the transaction occurred successfully, then Ganache will increment its “Current Block” value and the transaction count of the account that we used to deploy the contract also increment.

Our contract is now uploaded to our blockchain. To interact with it by incrementing and decrementing the counter, we can go back to MyEtherWallet and select “Interact With Contract”.

Deploying it to Test network:

There are several testnets that can be used for example the Ropsten Test Network, Kovan Test Network or even the Rinkeby Test Network. These mentioned test networks can be used on a high scale to deploy the smart contracts without paying in ethereum and getting the feel of the real-time environment from end user’s perspective.

References:

https://medium.com/crypto-currently/build-your-first-smart-contract-fc36a8ff50ca

https://medium.com/swlh/deploy-smart-contracts-on-ropsten-testnet-through-ethereum-remix-233cd1494b4b

https://medium.com/practical-blockchain/deploying-smart-contract-to-rinkeby-testnet-using-truffle-and-mac-os-x-8fc16ff380e2

Raj Sanghvi

Raj Sanghvi is a technologist and founder of BitCot, a full-service award-winning software development company. With over 15 years of innovative coding experience creating complex technology solutions for businesses like IBM, Sony, Nissan, Micron, Dicks Sporting Goods, HDSupply, Bombardier and more, Sanghvi helps build for both major brands and entrepreneurs to launch their own technologies platforms. Visit Raj Sanghvi on LinkedIn and follow him on Twitter. View Full Bio