Create Your First Ethereum Smart Contract With Remix Ide | by Thomas Guibert | Mar, 2022

Construct a Blockchain-powered chat out of your browser!

Nenad Novaković

Heard about Ethereum good contracts earlier than and want to construct your personal however you don’t actually know find out how to get began?

This tutorial is made for you. Collectively, we’re gonna undergo the creation and deployment of your very first good contract to the Ethereum Blockchain.

Every thing might be accomplished immediately in your browser. There might be nothing to arrange in your surroundings. Actually, we might be utilizing the online model of the Remix IDE, to write down and deploy our good contract.

Disclaimer: This tutorial gained’t focus in any respect on how the blockchain works, what may be achieved with a sensible contract, and find out how to make it fuel environment friendly. These are full subjects on their very own. The purpose right here is to cowl the steps from writing to deploying your good contract.

To begin with, here’s a very brief definition of a sensible contract, as described by the web site:

“A wise contract is a program that runs on the Ethereum blockchain. It’s a group of code (its capabilities) and knowledge (its state) that resides at a selected deal with on the Ethereum blockchain.”

So. The sport plan is straightforward. Write some code and one way or the other deploy it to the blockchain!

Here’s a extra detailed abstract of what we gonna do:

Getting Familiar With RemixWrite a Basic Smart ContractCompile Our Smart ContractAdd a Few Tests to Our Smart ContractDeploy Our Smart Contract to the Rinkeby Test NetworkInteract With Our Deployed Smart Contract From RemixConclusion

“Remix IDE is an open supply internet and desktop software. It fosters a quick growth cycle and has a wealthy set of plugins with intuitive GUIs. Remix is used for your entire journey of contract growth in addition to act as a playground for studying and instructing Ethereum.”

The File Explorers — Remix

This beautiful a lot seems like some other IDE proper? Within the sidebar, you may have 3 tabs, for now:

  • The file explorer, as seen within the image above. That is the place we’re going to write down our good contract and its take a look at file. It needs to be pre-filled with some recordsdata already. Be at liberty to undergo them!
  • The Solidity compiler. That is the place we’ll compile our good contract, as soon as it’s prepared.
  • The Deploy and Run Transactions, which is, you guessed it, the place we’ll ship our good contract to the Blockchain.

There may be not far more so as to add than this for now… Let’s work!

I assumed it will be enjoyable to write down a sensible contract that can allow folks to speak on the Blockchain!

The good contract might be actually easy. It can have two capabilities:

  • The primary operate will permit a consumer to submit a message that might be absolutely saved on the blockchain,
  • The second operate will permit us to retrieve the messages (this could permit us to show them in an online app)!

Let’s get began!

Create a brand new file within the contracts folder and title it BlockchainChat.sol.

Then, add the primary traces of our contract in it:

// SPDX-License-Identifier: MIT
pragma solidity 0.8.12;
contract BlockchainChat

So, what’s happening right here?

The primary line is the license declaration. A one-line remark in a predefined format that states how can others use (or not use) your supply code. It’s not obligatory to have this line however the Solidity Compiler will throw a warning if we don’t declare any license.

The road that follows is the directive that specifies the compiler model for use for the present solidity file. You bought it, we’ll use the model 0.8.12.

Lastly, our contract declaration, which begins with the phrase contract. Appears to be like just about like a Class declaration in lots of programming languages.

Add a Message Struct

So we mentioned we wish to retailer messages in our contract. Greater than the message, we have to additionally retailer the deal with of the consumer that has despatched it and likewise a timestamp of when the message was added.

That’s three completely different items of data that we one way or the other have to pack collectively. For that, we use what is known as astruct in Solidity:

// SPDX-License-Identifier: MIT
pragma solidity 0.8.12;
contract BlockchainChat
struct Message
deal with waver;
string message;
uint timestamp;
Message[] messages;

Lastly, we add a messages state variables, of sort Message[], which is an array of the struct created above.

A state variable is a variable whose worth is completely saved within the contract storage. — Ethereum StackExchange

Add the sendMessage operate

// SPDX-License-Identifier: MIT
pragma solidity 0.8.12;
contract BlockchainChat
struct Message
deal with waver;
string content material;
uint timestamp;
Message[] messages; operate sendMessage(string calldata _content) public
messages.push(Message(msg.sender, _content, block.timestamp));

What we do right here is fairly simple. We push the acquired message to our messages state variable. However right here are some things that you simply would possibly wish to know extra:

string calldata _content

That is the parameter that provides us the message despatched. It’s of sort string. Discover the calldatakey phrase? It defines the information space the place a variable is saved.

I don’t wish to confuse you with that on this article as a result of there may be a lot to say and perceive about it however when you really feel such as you wish to know extra, here’s a good thread on StackExchange.

msg.sender & block.timestamp

These two aren’t declared anyplace so the place are they coming from? They’re International Variables.

  • msg incorporates properties that permit entry to the blockchain. Right here, msg.sender will at all times be equal to the deal with the place the decision got here from (the consumer in our case).
  • block incorporates plenty of fascinating knowledge in regards to the present block being added to the blockchain. In our case, we get the timestamp!

Here’s a hyperlink to the solidity documentation that tells extra about what you will get from them:

Add the getMessage operate

// SPDX-License-Identifier: MIT
pragma solidity 0.8.12;
contract BlockchainChat
struct Message
deal with waver;
string content material;
uint timestamp;
Message[] messages; operate sendMessage(string calldata _content) public
messages.push(Message(msg.sender, _content, block.timestamp));
operate getMessages() view public returns (Message[] reminiscence)
return messages;

getMessages is less complicated than our sendMessageoperate. However listed here are fascinating information about it:


Discover the view key phrase? That is referred to as a Modifier. It states that this operate is read-only. Inside capabilities which are of sort view, state variables can’t be modified.

Since it’s read-only, it comes with the good benefit that calling this operate gained’t require doing a transaction and subsequently gained’t value any fuel to the consumer! Extra on that later once we attempt our contract.


As for our earlier operate, getMessagesis a public operate. It is a Function Visibility Specifier. It basically signifies that it may be referred to as from outdoors of the contract (versus a personal operate).


Lastly, the returns assertion adopted by the (Message[] reminiscence) states what knowledge this operate will return.

Our good contract is wanting good! Hold the BlockchainChat.sol file open and click on on the Solidity Compiler tab within the sidebar.

The Solidity Compiler — Remix

There are just a few choices that may have an effect on how the contract is compiled however chosen ones are already high quality. Let’s go forward and click on “Compile BlockchainChat.sol.”

If every part goes properly, you need to get the good contract artifacts added in contracts/artifacts within the file explorer.

In regards to the created artifacts

  1. artifacts/BlockchainChat.json: incorporates the hyperlink to the libraries, the bytecode, the deployed bytecode, the fuel estimation, the strategy identifiers, and the ABI. It’s used for linking a library deal with to the file.
  2. artifacts/BlockchainChat_metadata.json: incorporates the metadata from the output of Solidity compilation.

It’s primordial to check your contract extensively earlier than deploying it to the blockchain. The blockchain is by design immutable. So you possibly can’t simply deploy your contract and repair it later when you discover a bug, that’s merely not potential.

Remix presents the likelihood to write down and run checks. Let’s add a take a look at file so we are able to be certain that our good contract works as supposed earlier than deploying it.

Come again to the File Explorers and underneath the checks folder, create a brand new file named BlockchainChat_test.sol. Fill it with this code:

// SPDX-License-Identifier: MIT
pragma solidity 0.8.12;
import "remix_tests.sol";
import "../contracts/BlockchainChat.sol";
contract BlockchainChatTest
BlockchainChat blockchainChatToTest;
/// 'beforeAll' runs earlier than all different checks
operate beforeAll () public
blockchainChatToTest = new BlockchainChat();
operate checkSendMessage () public
// Ship a primary message
blockchainChatToTest.sendMessage("Hey World!");
// Make sure the messages variable incorporates 1 message
Assert.equal(blockchainChatToTest.getMessages().size, uint(1), "messages state variable ought to comprise 1 message");
// Make sure that our first message's content material is "Hey World!"
Assert.equal(blockchainChatToTest.getMessages()[0].content material, string("Hey World!"), "The primary Message in message needs to be "Hey World!"");
// Ship a second message
blockchainChatToTest.sendMessage("This chat is tremendous enjoyable.");
// Make sure the messages variable incorporates 2messages
Assert.equal(blockchainChatToTest.getMessages().size, uint(2), "messages state variable ought to comprise 2 messages");

Nothing actually complicated on this file. It’s basically ensuring that when a message is distributed, it’s saved in our good contract and that the messages may be retrieved.

Run our take a look at file

Within the sidebar, click on on the Solidity Unit Testing tab.

Is that this tab lacking? Click on the plug on the backside of the sidebar. This may open the Plugin Supervisor. Seek for Solidity Unit Testing and activate this plugin.

Click on on the run button. The checks for our good contract will run and hopefully move!

Efficiently passing checks — Remix

As beforehand mentioned, the blockchain is immutable. A deployed good contract can’t be modified any extra. Whereas we’ve coated our BlockchainChat good contract with just a few checks, it may not be smart to deploy it to Ethereum Mainnet but.

Deploying a sensible contract value plenty of fuel, which suggests plenty of actual cash. We’d wish to take a look at it in “actual situation” totally free first!

There are what we name Testnets (Check community). Rinkeby is one in all them. A testnet is an surroundings that’s just about like Mainnet, even though it’s right here to behave as a sandbox.

It’s a testing surroundings, that can be utilized to run your good contract in virtually actual situation, with out involving actual property (we’ll see in a bit that we’ll nonetheless use ETH, however pretend ETH!).

Now that you understand extra about why we wish to deploy to Rinkeby first, listed here are the steps to get began:

Observe that the steps to deploy to Ethereum Mainnet are precisely the identical as those that can comply with.

Set up Metamask

We are going to want Metamask to ship transactions, so guarantee that it’s put in.
You may get it on

Choose the Rinkeby community

Metamask permits us to alter the surroundings in a single click on. By default, it’s related to Mainnet, let’s change it to Rinkeby.

The place to get ETH to deploy our good contract

Rinkeby works as Mainnet. Which means that we nonetheless have to pay fuel to deploy our contract. And to pay for the fuel, we want ETH… “Rinkeby ETH”.

We are able to get some Rinkeby ETH to play with on Taps.

There are various Taps obtainable, right here is that you should use to get some Rinkeby ETH in your pockets.

Copy and paste the pockets deal with you wish to use to deploy your good contract and click on on Ship me ETH. Wait just a few seconds and your pockets ought to have acquired round 0.1 pretend ETH.

Let’s deploy our good contract (for actual this time)

Head again to Remix, and go to the Deploy and Run Transactions web page.

Right here is find out how to configure the settings:


Underneath the hood, Metamask exposes a Web3 object to the webpage. We wish to use it to deploy our contract. Due to this fact, choose Injected Web3 within the dropdown.

Metamask ought to pop up, asking for authorization to connect with Remix.

Choose the account you wish to join with. It needs to be the identical because the one you used with the Faucet beforehand.

As soon as accomplished, please be sure, as soon as once more, that you’re related to the Rinkeby Community.


The account you chose within the earlier step ought to seem right here. Be certain that it’s the similar pockets deal with.

Gasoline Restrict and Worth

Not a lot to alter right here, depart the default values.


Make it possible for BlockchainChat is chosen.

We’re all set!

Click on on deploy. This could open a Metamask contract deployment transaction request.

Go forward and click on on Affirm to deploy your contract.

After just a few seconds, the terminal shows a hit message!

Click on the View on Etherscan hyperlink, which ought to lead you to the transaction web page as follows:

What’s vital on this web page is:

  • From: that is the pockets used to deploy the contract
  • To: that is the deal with of our BlockchainChat good contract

Congratulations, you’ve deployed a sensible contract to Rinkeby!

Click on on the contract deal with to entry the good contract particulars. There, you will notice all of the transactions ever made along with your contract. For now, there needs to be solely the contract creation transaction.

Head again to Remix. There ought to now be a deployed contracts panel within the Deploy & Run Transactions tab:

Enjoyable! There are the 2 capabilities that we created in our BlockchainChat good contract: sendMessage and getMessages.

Ship the primary message!

Within the enter subsequent to the sendMessage button, sort any message you want to publish in your chat, after which click on the sendMessage button.

This could pop up Metamask, prompting you to validate the transaction. Settle for it! After just a few seconds, you need to have affirmation within the Remix terminal:

Check out the getMessages operate

Now, let’s click on on the getMessages operate and have a look at the terminal. (you would possibly have to broaden the response)

“0”: “tuple(deal with,string,uint256)[]:
0x096....D13fD,Hey there!,1647106198”

getMessages returns the listing of messages saved within the messages state variable of our good contract. That’s a hit!

Did you discover a distinction whereas calling getMessages?

Metamask didn’t ask you to simply accept a transaction to get the messages in our good contract.

Keep in mind, here’s what our getMessages operate look likes:

operate getMessages() view public returns (Message[] reminiscence) 
return messages;

That’s due to the view key phrase used within thegetMessages operate declaration. Actually, right here we’re simply studying the blockchain, we’re not modifying it.

Studying the blockchain is free! Think about that we’re creating an online app for our chat and we’re requiring our customers to do a transaction to obtain the newest printed messages… Our chat could be fairly costly to make use of! However because of view , it’s free to obtain the messages!

Congratulations for making it till the tip and for writing and deploying your first Ethereum good contract!

Now that you’ve got absolutely gone via the method, it’s time to experiment extra! Are you able to consider something that could possibly be improved? Right here is an thought:

  • At present, a consumer may ship an empty string as a message, which isn’t actually enjoyable. Right here is how the sendMessage operate could possibly be modified:
operate sendMessage(string calldata _content) public 
uint contentLength = bytes(_content).size;
require(contentLength > 0, "Please present a message!");
messages.push(Message(msg.sender, _content, block.timestamp));

This was a really complete first step in Solidity.

Sensible contract growth is a complete completely different paradigm. Blockchain’s immutability and fuel cost-efficiency require builders to be extraordinarily rigorous. These subjects weren’t coated on this article however positively have to be in your areas of exploration when you aspire to be a Solidity developer!

Thanks for studying.

More Posts