Creating a Basic Smart Contract

Maharshi Barot
7 min readJan 22, 2021

How I made a simple Ethereum smart contract without any prior experience

Photo by Nick Chong on Unsplash

What is a Smart Contract?

As far as the relationship between cryptocurrency and blockchain goes, new applications are being discovered at an alarming rate. Arguably, the most prominent of these applications is the smart contract. From start-up funding platforms to day-to-day expenses, there are no limits on where smart contracts can and cannot be applied. At their core, these contracts provide another medium of payment transfer.

In more technical terms, a smart contract can either be a transactional protocol or a computer program that is carried out after its requirements or terms are met. For example, when requirement x is met, the funds (y) will be exchanged. In the case of a start-up funding platform, the funds (y) will be exchanged as soon as the funding goal (x) is reached. However, if the goal (x) is not reached, the funds (y) will be returned to their original owners.

The objective of smart contracts is to remove any transaction mediator who moderates the transaction while also removing any arbitrary fees. In many ways, a simple vending machine works in a very similar way to a smart contract.

Regarding smart contract enforcement, the United States has no regulations or laws at a federal level. Enforceability rules are instead determined at a state level. Although the key concepts of smart contract enforceability are more or less the same across all states, there is still a strong drive to universalize smart contract binding laws. Despite this, states normally look at smart contracts the same way they evaluate traditional contracts. States look at whether three requirements: offer, acceptance, and consideration, are met. If the requirements are met, the state can judge a smart contract on the same basis as a conventional contract.

Photo by Cytonn Photography on Unsplash

Creating a Basic Smart Contract

Now that you have a basic understanding of smart contracts and how they work, let's go deeper. Initially, I didn’t know how I would go about making my first smart contract. However, after a few hours of trial-and-error and Google searches, I have created a simple and easy to comprehend guide. Keep in mind that this is a very rudimentary Ethereum smart contract.

Before we get started, you must download the Meta Mask extension from the chrome web store. Meta Mask gives us a platform to simulate our smart contract by allowing us to play with test-Ether.

After completing the basic setup, such as creating a password and a secret phrase, you should be met with a screen that looks similar to what is shown below. From this point, you must click the tab at the top right (circled in yellow). This tab initially reads “Main Ethereum Network”: you must change it to “Rinkeby Test Network”. The Rinkeby Test Network is a test environment in which we can use test-Ether to simulate a transaction. The Rinkeby Test network will assign you a test Ethereum address. This address can be found right under the account name (circled in blue). This address will be an essential part of the next step of our process.

Next, you must navigate to the Rinkeby Ether Faucet. This faucet provides users with test-Ether with which they can play around with. To receive test-Ether, you must navigate to your Twitter account and tweet out your assigned Ether address mentioned earlier. This is done to ensure any one individual cannot withdraw too much test-Ether. You must then copy the tweet’s URL and paste it within the Rinkeby faucet, as shown below.

Soon after pasting the link into the faucet, it will deposit test-Ether into your Meta Mask account. We will use this test-Ether to simulate the conditions of our smart contract. Remix Ethereum will be the platform we will be using to write the smart contract. Once you have navigated to the Remix Ethereum platform, you must click the “+” option at the top right (circled in yellow below). Delete the prewritten text and replace it with whatever you’d like your coin name to be. As you can see below, I went with “BarotCoin”.

After your file is created, you should be met with a screen similar to what you see above.

At this point, you will paste this code into the provided space. This code is used to define what functionality our token possess.

(Note: You could go about manually figuring out the code with the intention of learning, however for this guide, I wanted to keep it simple)

The default coin name within this code is “EIP20”. You must manually replace this with your coin’s name. In my case, I had to replace it with “BarotCoin” as shown above. You will then have to create an entirely new file named “EIP Interface.” In this file, you must paste this code. This code defines the protocol the coin will follow. As soon as the contract is activated, these protocols will be executed.

Before we activate our contract, there are a few steps we must take. We must start by compiling our contract. A compiler is used to convert instructions into a machine-code. This machine-code makes it easier for your computer to comprehend and execute the code.

To run the compiler, you must navigate over to the tab underlined in the graphic above. From there, select the compile option.

As soon as you have completed this step, we must begin providing values for a few variables. These variables, which can be seen in the graphic above, include the initial amount (how many tokens will be in existence), the token name, the decimal units (how many decimals the token has), and the token symbol. The function will give the creator of the contract all of the coins. This means that when this contract is launched, we will receive all of the tokens in existence. In my case, I would receive all of the “BarotCoin” in existence.

To account for these variables, you must navigate over to the run option. To assign these variables values, the environment must be changed to Injected Web3. This is done to connect Meta Mask and Remix Ether. If Meta Mask and Remix Ether are not connected, your contract will not run through the main Ether network. Instead, the contract will be run as a simulation on the website itself; therefore, the test-ether we harvested earlier would be of no use.

Additionally, under the contract tab, you must change the default EIP20Interface option to your token name. As you can see above, it says “BarotCoin” under the contract option.

Next, you must click on the deploy text bar (highlighted in the picture above). For my smart contract, I went with an initial amount of 10,000 and 0 decimal points. However, if you’d like to play around with these values, you may have different values. I next entered my coin’s name and its symbol. After this, select deploy!

(10000, “BarotCoin”, 0, “MBC”)→ The format you must use when specifying the variables.

As soon as the deploy button is selected, a Meta Mask notification will appear. From here, you must click confirm. Some test-ether will be spent to deploy the contract on the Ether network.

Meta Mask will redirect you to a page that looks similar to what you see above. At this point, you have run a basic Ether contract via test-ether!

Photo by Thought Catalog on Unsplash

As blockchain technology continues to pick up speed, the sky is the limit regarding where smart contracts can be applied. From healthcare to real estate, smart contracts have potential in a wide array of fields.

“The potential for [smart contracts] to alter aspects of society is of significant magnitude. This is something that would provide a technical basis for all sorts of social changes, and I find that exciting.” — Ethereum CTO, Gavin Wood.

Stay in Touch

Thanks for reading! I hope you enjoyed the article!

Connect with me on Linkedin
Follow me on Medium

Feel free to reach out by e-mail with any questions or inquiries: maharshih.barot@gmail.com

👏 if you enjoyed the article!

Many fascinating articles to come, so stay tuned!

--

--