Hello!

© 2024 Kishan Kumar. All rights reserved.

Smart Contracts: A Simple Guide to Understand, Use, and Deploy Them

Smart contracts are digital contracts or programs, a set of instructions stored on a blockchain that is automatically executed when predetermined terms and conditions are met.

June 03, 2023

Hero

Smart contracts are digital contracts or programs, a set of instructions stored on a blockchain that is automatically executed when predetermined terms and conditions are met.

1 Million Dollar Bet

E.g, On March 17th, 2023, former CTO of Coinbase, Balaji Srinivasan, placed a bet worth $1 million on the future Bitcoin (BTC) price during a bearish market. He accepted a tweet from James Medlock, who said he would bet anyone $1 million that the US does not enter hyperinflation. Srinivasan said he would take that bet and asked Medlock to buy one Bitcoin. He promised to send $1 million if he loses after 90 days.What if he didn't respect the promise? Can this happen? Yes, What is the solution, then?
Smart contracts can help in this scenario by creating a trustless and transparent way to settle the bet between Srinivasan and Medlock.Instead of relying on their word or a third party, they could use a smart contract that would automatically execute the payment based on the price of Bitcoin at a specific date. The smart contract also ensures the funds are locked and secured until the bet is resolved.The smart contract could also use an Oracle service to get accurate and reliable price data from an external source. This way, both parties could be confident that the bet is fair and verifiable.

Let's look at some of the benefits of smart contracts:

  1. Speed, efficiency, and accuracy: Once a condition is met (i.e., price of BTC < 1 million), the contract is executed immediately. There are no paperwork or manual errors involved, thus transferring the money from Srinivasan to Medlock without any hassle.
  2. Trust and transparency: The records of transactions are available to every participant of the network, the platform where the smart contracts are executed. As described, Smart Contracts are programs that need to run for them to be meaningful, and to run, they'll need resources that the World Computer, Ethereum, can provide.
  3. Security: No one can change the terms and conditions of the Smart Contract once they have been deployed to the Blockchain; it is not possible for anyone, let alone Srinivasan, to sneak up and change the bet amount from 1 million to 1 hundred dollars. The participants of the network will detect any malicious behavior.

The above example is one of the many applications where smart contracts can play an important role, let's look at some more examples to understand it better:

  1. Insurance: Smart contracts can automate the claims process and verify the validity of claims based on the data and evidence.
  2. Voting: Smart contracts can ensure secure and transparent voting by recording and verifying votes on a blockchain.
  3. Identity management: Smart contracts can store and manage personal data and credentials on a blockchain, allowing users to control their identity and access rights.
  4. Crowdfunding Campaign: Smart Contract can collect funds from backers and release them to the project creator when a specific goal is met. The program refunds the backers if the goal is not met by a deadline.
block

block

Let's go ahead and build a Hello World of Smart Contracts

Hello World.

Smart contracts are programs; one should choose a programming language to create one. We'll be using Solidity language to create our HelloWorld.sol contract.

1// SPDX-License-Identifier: MIT
2pragma solidity ^0.8.0;
3
4contract HelloWorld {
5  string public message;
6
7constructor() {
8  message = "Hello, World!";
9  }
10
11function getMessage() public view returns (string memory) {
12   return message;
13 }
14
15function setMessage(string memory newMessage) public {
16   message = newMessage;
17  }
18}

Let's understand each line of the code one by one:

  • The contract is named HelloWorld. Note: contract is a keyword, just like class is a keyword in Java.
  • It has a state variable message of type string that stores the greeting message.
  • The constructor function is executed once during contract deployment. It sets the initial value of the message to "Hello, World!".
  • The getMessage function is a public view function that allows other contracts or users to retrieve the value of a message.
  • The setMessage function is a public function that allows other contracts or users to update the value of the message by providing a new string.

Deploy Smart Contracts

To deploy and interact with this smart contract, you must use a development environment, such as Remix IDE, or a framework like Truffle. These tools provide an interface to deploy the contract to a test network or the Ethereum mainnet and interact with its functions and state variables. This might look scary initially, but don't worry; I'll walk you through the process.

Note: Images are provided before each step; please take them as a reference.
Remix Homepage

Remix Homepage

Step 1: Access Remix IDE
  • Open your web browser and visit the Remix IDE website: https://remix.ethereum.org/
  • Ensure that you have a compatible web browser like Chrome or Firefox.
Remix Create New File

Remix Create New File

Step 2: Create a new file
  • On the Remix IDE homepage, click on the file icon, or click on contracts and right-click to get the option shown in the above figure.
  • Name the file with a .sol extension, for example, HelloWorld.sol
Remix Copy and Paste Hello World Content

Remix Copy and Paste Hello World Content

Step 3: Write the smart contract code
  • Copy and paste the HelloWorld content into the newly created file.
Remix Compile

Remix Compile

Step 4: Compile the smart contract
  • In the Solidity Compiler section of Remix IDE, select the appropriate version of Solidity for your contract (e.g., 0.8.0).
  • Click the Compile HelloWorld.sol button to compile your smart contract code. Make sure there are no compilation errors.
Remix Select Environment

Remix Select Environment

Step 5: Deploy the smart contract
  • In the Deploy & Run Transactions section, select the "Injected Web3" environment from the Environment dropdown if you have Metamask (Don't forget to switch to a test network). This will allow you to connect to your Ethereum wallet. If you don't have Metamask installed for some reason, simply select one of the Remix VM, e.g., Remix VM Shanghai. They will give you a disposable account with some test Ethers.
  • Note: I recommend using Remix VM for this and not Metamask.
  • Click on the "Deploy" button next to the HelloWorld contract.
  • You'll see some logs in the Remix Terminal window containing these details with a green checkbox.
Remix Logs

Remix Logs

Remix Interact

Remix Interact

Step 6: Interact with the deployed contract
  • After the contract is deployed, Remix IDE will display information about the deployment, including the contract's address.
  • Remix Function

    Remix Function

  • You can now interact with the contract using the available functions. In the Deployed Contracts section, expand the contract and access its functions.
  • You can call the getMessage function to retrieve the greeting message or use the setMessage function to update it.

Congratulations! You have successfully deployed and interacted with your first smart contract using Remix IDE.

Thank you for taking the time to read my article.

References:

    .   .   .

    The 0xkishan Newsletter

    Subscribe to the newsletter to learn more about the decentralized web, AI and technology.

    © 2024 Kishan Kumar. All rights reserved.