How To Create An Ethereum Smart Contract
The most interesting part of Ethereum are smart contracts. You can think of smart contracts like autonomous agents deployed onto the Ethereum blockchain.
Smart contracts are made up of some data, as well as some code that manipulates that data. Just like a user account, smart contract accounts also have their own balance as well as a public address, which looks the same as the user account address.
To interact with smart contracts, you send transactions to it with some extra data, to specify which function you want to invoke, as well as any input parameters for the function. The transaction will invoke the function, and return any possible output once it has been mined. We’re going to be using Remix to write smart contracts, which is an online IDE, available for free at remix.ethereum.org. It comes with a compiler, editor and debugger for solidity. The great part is that you don’t need to download or install or set up anything, you just go to this webpage and start writing smart contracts, which is awesome.
The UI is divided into four main sections. The left part is our file explorer where we can open Solidity files from our hard drive. The middle section is our editor where we can view and write our source code. At the bottom we have a terminal which prints out important events and transactions, and the rightmost section contains some handy tools for compiling running and debugging our code.
The first example we’ll look at is a smart contract called Hodor. Let’s load up the source code. You’ll notice that Solidity source files end with the .sol extension. The syntax of Solidity resembles java script, so it should be fairly easy to pick up. The first we see at the top uses the pragma directive. This tells the compiler that the contract is written for a Solidity compiler version of at least, or anything newer that doesn’t break functionality.
We then have a multi-line comment block, briefly describing what this class does. The next line is where we declare the name of our smart contract using the contract keyword. We’ve simply called out class Hodor because, as you’ll see, all it does is return a simple greeting. We also declared two state variables called Creator and Greeting. Creator has an address data type, which is used for storing addresses of accounts. Greeting is a string data type, which just stores some text greeting. We initialized both of these variables in our constructor.
The constructor is declared using the function keyword, followed by the name of the class. The constructor is a special function that is invoked only once, when a contract is first deployed to the Ethereum blockchain. You can only declare a single constructor for a contract. We also inject the initial string greeting as a parameter, into the constructor, and set the greeting variable to that value. In the second line of the constructor, we initialize the creator variable to a value called message.sender, but if you look closely, you might wonder where this value came from, since it’s not being injected into the constructor.
This is because message is a global variable that provides certain information about the message, such as the address of the account sending it. We can get the address of the account creating the contract, using message.sender in the constructor. You may notice that we don’t actually use this creator variable anywhere in our contract, because this is just a simple example. We could potentially use this information to implement access control to certain functions. We’ll see an example of this later.