Scuti Chapter 2 - Ethereum Smart Contracts With Solidity

unsplash-image-8iIUDnRq87o.jpg

In chapter 1, we introduced the concept of smart contracts. We discussed smart contracts being pieces of code that live on the blockchain that can serve as an automated agreement that tracks, verifies, and executes transactions between various parties. Different blockchains have different languages and features that help with developing smart contracts. Cardano uses Plutus as its smart contract development language, Algorand uses Teal and Ethereum has a variety of development languages with Solidity being the most popular of them all.  

Solidity is an object-oriented programming language; it has been influenced by C++ and is statically typed meaning variable types have to be explicitly declared. Solidity variable types include value types, reference types, and maps. Value types are types that are always passed by value and these include booleans, integers, fixed-point numbers, addresses, contract types, fixed-size byte arrays, rational and integer literals, and enums. Reference types are passed by reference, examples of reference types are arrays and structs. To get more insight into the different types of solidity, you can reference the documentation. Additionally, solidity supports inheritance, libraries, and imports as well as complex user-defined types. 

Let’s go over a quick solidity example contract. In this example, we are writing a smart contract to handle a fundraiser where donors can deposit into the smart contracts and only the organizer can withdraw the funds from the contract. To get a more detailed introduction to solidity, you can reference the documentation. To run this example, you can use Remix, an online IDE that helps you write Solidity contracts within your browser. Remix allows you to edit, compile, deploy & interact with smart contracts all within our browser.

Example solidity smart contract:

Screen Shot 2021-10-05 at 8.50.10 PM.png

The first line of the code snippet specifies that the code is licensed under the GPL version 3.0. SPDX license identifiers are used to indicate licensing information of our code. Please note that the contract will still compile and deploy with this line omitted. The next line specifies that the source code is written for Solidity version 0.7.0 or a newer version of the language. The compiler versions ensure that the contract is not compatible with other compiler versions where our code may behave differently. Next, we define a contract and name it “FundRaiser”, we can assign our contracts any name we prefer. Defining a contract in solidity is similar to defining classes in most object-oriented languages, contracts have methods and state variables that belong to the contract. Depending on the access modifiers on a contracts methods and variables these may or may not be accessible outside the contract. 

Define State Variables 

The line ‘address public organizer’ declares a state variable called organizer of type ‘address’. Ethereum addresses are 20 bytes long therefore the address type holds a 20-byte value. Our state variable also has a public access modifier, this means that our variable is accessible from other contracts. The private modifier on the other hand ensures the variable is only visible within the contract they are defined in. We then define a map ‘mapping (address => uint) public donors’ which stores address and unsigned integer key value pairs and name it ‘donors’.  

Constructor 

Next, we define our constructor. The constructor in solidity is a special function that is optional, it is used to initialize state variables of a contract. A contract can only have one constructor and the code within the constructor is executed only once when the contract is deployed. Within our constructor we assign our ‘organizer’ state variable the address of the account our smart contract is being deployed from. We will cover smart contract deployment using both JavaScript and truffle in a later series. 

Smart Contract Functions 

Functions in solidity are capable of getting or setting information in response to incoming transactions. Functions can either be public or private, just like with state variables, public functions can be called internally from within the contract or externally via messages, whereas private functions are only visible for the contract they are defined in. Functions can also accept parameters and values. 

All the functions within our FundRaiser contract are public functions meaning they can be accessed internally or externally. The ‘getAmountRaised’ function is defined as a ‘view’ function which indicates that it is read-only, this ensures that state variables can’t be modified after calling the function. The function body returns the following statement ‘address(this).balance’ which returns how much is currently stored in the contract.  

Our next function ‘donateToCause’ is a payable function, this means the function can receive ether which will be stored within the contract. DonateToCause also modifies our contract state by adding the value for the amount paid to the contract into our map. Recall our map is a key value pair store. The key for our map is of type address, we use ‘msg.sender’ to get the address of the person interacting with the contract and the value accessible from ‘msg.value’ is the amount the person pays when calling the function. Msg is a special variable provided to us by solidity, it is used to provide information about the blockchain. 

The final function in our smart contract has no extra modifiers, however it contains the following line - ‘require(msg.sender == organizer)’. Require can be used to check conditions and throw exceptions if the condition is not met. In our contract, the condition is to ensure the person attempting to call our function is the organizer. ‘payable(organizer).transfer(getAmountRaised())’ the transfer function is how to send ether in solidity, it accepts 1 argument and that is the amount that needs to be sent. The specific address we’re sending the money to is provided as an argument to the payable cast function. 

That’s it! You’re done with your smart contract. The next steps are to compile and deploy the contract. Deploying smart contracts cost money, every time we send a transaction to the Ethereum network, there’s a gas cost attached to it. To briefly explain Continue on to part 3 of these tutorial series to understand more about gas & gas prices and to learn more about compiling and deploying contracts. 

Previous
Previous

Quantum Payment Optimization Chapter 3 -Approaches to Optimization

Next
Next

Quantum Payment Optimization Chapter 2 -Faceoff Xanadu. Qiskit. DWave.