A Few Basic Components of a Smart Contract
Components to get your toes wet and not feel overwhelmed
Building anything on the Ethereum blockchain requires smart contracts. Smart contracts are digital contracts (yet not in a legal-binding sense) stored on the blockchain that can be executed via an address. In development terms, I like to think of a smart contract as modularized code that contains functions and data that when executed or changed, updates records in a database. The processes are similar, but the flows of the data and where the code is stored is where things are different.
Solidity isn’t that overwhelming
While the material on blockchain technology might have given off the impression that there are levels of complexity to its code, Solidity, the Ethereum language I’m using, is an object-oriented programming language like Python, PHP, Javascript and Ruby. This means that Solidity contains similar code structures to the most commonly used programming languages today. To prove this point, the remainder of the newsletter will show you some of the basic components of a Solidity smart contract. The example used to dissect these components is a smart contract for a hotel that is used to provide a unique greeting for each guest. For each component, I will give a description of its functionality and what it does within the context of the contract we are building.
Our Smart Contract Setup
Link to the code that you can use in your Remix IDE to recreate: HotelGreeting.sol (gist)
As you can see from this screenshot and the gist that has been shared, in only 47 lines of code we created this smart contract! While it is a very basic example of what can be done in a smart contract, it is a good way to demonstrate how to use commonly used smart contract components. In the following sections I will be going deeper on struct, enum, variables and functions.
Struct
Struct’s are data types that are used to group related data together. They share similar traits to objects or dictionaries in languages like JavaScript and Python. Defined by the reserved struct keyword, a name and then curly brackets with comma separated variables that are in the format of data type variableName. While you could technically define each variable as a State Variable, this grouping is preferred when there is a relationship between the variables. Structs can also be used inside mappings and arrays, as well as contain mappings and arrays. Mappings and arrays will be covered in a future newsletter.
Enum
Enums are a user-defined type that contain a finite number of possible values. While these values can be strings, the order in which they are found within the Enum is important because the blockchain interprets the values based on their index order value (0, 1, 2, 3) rather than the actual value that is located in that position.
Variables
Variables are used for storage of values. Depending on the scope in which the variable is declared or if it uses special names decides the rules and restrictions in the information that can be stored and accessed.
Global variables are declared with system reserved names that access information about the blockchain and transaction properties. Examples are block, which provides the information about the current block and msg, which contains call data.
Local variables are only declared within functions. These variables are constrained to the function in which they are declared and cannot be accessed within the larger scope of the whole smart contract.
State variables are declared outside of functions within a smart contract. They have the most flexibility of use and can be accessed and updated within or outside of functions.
Functions
Functions are one of the most utilized components of solidity contracts. Functions group (modularize) code together in a reusable format. They can be defined in or outside (commonly called “free functions”) of a smart contract. A common pattern for function parameters that will be used to pass a value to state variables is to use the same name, but prepend it with _, like function paper(string _author) public { }.
Functions can return multiple values by comma separating each returned value parameter within the returns statement.
An important distinction to understand about Solidity functions relative to other languages that utilize functions is that they can return an arbitrary number of values as output.
These components should give you the power to create most of the smart contracts that you dream up. There are additional components that Solidity offers (which I will cover in the future), but I find these four to be the most foundational elements of a smart contract that should handle most use cases and should be mastered first. In the next newsletter I will cover visibility, which applies rules to the components explained above and control the accessibility of the components and their data.