Writing Your First Smart Contract On Ethereum Blockchain

Gbenga Olaseni
EthereumNigeria
Published in
7 min readDec 18, 2023
Photo by Art Rachen on Unsplash

If you’ve ever sent ether from one wallet address to another then you have a basic understanding of Ethereum as a cryptocurrency but it is important to know that Ethereum is much more. Ethereum can be viewed as a decentralized world computer and ether is the fee you pay for running programs which are smart contracts on the EVM. The Ethereum virtual machine is a virtual computer running on every Ethereum node where each program can run and the changes that occur are stored on the Ethereum block chain.

There are two types of accounts in the Ethereum blockchain

  1. Externally Owned Account (EOA): An EOA is controlled by a private key and is typically owned and managed by an individual. It’s used for interacting with the Ethereum network, performing transactions, deploying contracts, and holding Ether (ETH) or other tokens.
  2. Contract Account: These accounts are created when a smart contract is deployed onto the Ethereum blockchain. Each contract has its own unique address and code, and it can hold Ether and other assets. These accounts don’t have private keys and their behavior is governed by the contract’s code.

Note: In the context of Ethereum, “account” and “wallet” are often used interchangeably, but they represent slightly different concepts. An account refers to the actual Ethereum address on the blockchain, whether it’s an EOA or a contract account while a wallet on the other hand refers to the software or service used to manage Ethereum accounts, providing tools for key management, transaction handling, and more.

Photo by Annika Wischnewsky on Unsplash

What is a smart contract?

Smart contracts are self-executing contracts with the terms of the agreement directly written into code. They run on a blockchain and automatically enforce and execute actions when predefined conditions are met. These contracts enable trusted transactions and agreements without the need for intermediaries. Smart contracts do not contain the legal language or terms of a contract between two parties. They are scripts that contain if/then statements, functions, module imports, and other programming that automate the actions specified in a contract.
The most prominent high-level language used for writing smart contracts in Ethereum is solidity. Solidity was created by Dr. Gavin Wood specifically for writing smart contracts and it has a similar syntax to JS and C++. We would be writing a smart contract that controls as faucet. A faucet is an application that provides a small amount of cryptocurrency for free. We would be getting some sepolia ether from a faucet to continue and if you can’t do this check this link .

Link to code and let examine what each line of code means.

// SPDX-License-Identifier: CC-BY-SA-4.0

Comments serve as explanatory notes for human comprehension and aren’t part of the executable EVM bytecode. Typically, they’re placed before or alongside the code they elucidate, denoted by double forward slashes (//). Any content following these slashes until the line’s end is disregarded, similar to a blank line, by the system.

// Version of Solidity compiler this program was written for

pragma solidity 0.6.4;

This line specifies the version of the Solidity compiler that the code was written for. In this case, it’s written for version 0.6.4 of the Solidity programming language.

contract Faucet {

This line initializes a contract object, resembling the creation of a class in other object-oriented programming languages. Within the curly braces ({}), the contract definition encapsulates all lines, establishing a scope similar to how curly braces define scopes in numerous programming languages.

receive () external payable {}

The receive function gets triggered when the contract-received transaction doesn’t specify any declared functions or doesn’t carry data, indicating a simple transfer of Ether. Contracts can possess a single receive function (unnamed) designed explicitly for receiving Ether. It’s externally accessible and capable of accepting Ether, denoted by its definition as an external and payable function. Essentially, its purpose is solely to receive Ether, evident from its empty definition within curly braces ({}). When we execute a transaction that sends Ether to the contract address resembling a wallet, this function manages and handles that incoming Ether.

function withdraw(uint withdraw_amount) public {

The function, titled withdraw, requires a single argument of unsigned integer type (uint) named withdraw_amount. It’s specified as a public function, enabling invocation by other contracts. The subsequent part, enclosed within curly braces, contains the function’s definition. The initial segment of the withdraw function establishes a restriction on the withdrawal amounts:

require(withdraw_amount <= 100000000000000000);

It utilizes the inherent Solidity function require to verify a condition: that the withdraw_amount doesn’t exceed 100,000,000,000,000,000 wei, which equals 0.1 ether, the fundamental unit of ether. Should the withdraw function be invoked with an amount surpassing this limit, the require function halts contract execution, causing it to fail and terminate with an exception. It’s important to note in Solidity that statements necessitate termination with a semicolon.

msg.sender.transfer(withdraw_amount);

Several intriguing elements are at play here. The msg object, accessible to all contracts, represents the triggering transaction for this contract’s execution. The sender attribute within msg denotes the transaction’s sender address. Meanwhile, the transfer function, an intrinsic part of the system, moves Ether from the current contract to the sender’s address. In essence, it signifies a transfer to the sender of the triggering msg.

In reverse order, this function accepts an amount as its sole argument. We input the withdraw_amount, the value defined earlier as the parameter in the withdraw function a few lines prior.

The subsequent line, a closing curly brace, marks the conclusion of the withdraw function’s definition.

Directly beneath our default function lies the ultimate closing curly brace, effectively concluding the definition of the Faucet contract.

Let’s compile our code and deploy it using Remix

Head over to https://remix.ethereum.org to get started. Remix is an open-source web application and integrated development environment (IDE) specifically designed for writing, testing, and deploying smart contracts on the Ethereum blockchain.

  1. Create a new file by clicking on the file icon and give it any name possible Faucet.sol

2. Copy and paste the code from the link above

3. On the tab, click on the solidity compiler which is the icon shaped S and click on compile

4. To deploy the smart contract on the Ethereum blockchain, we’ll utilize the Sepolia test network. Make sure your Metamask network is configured to the Sepolia test network, and ensure you possess some Sepolia test ether. If you don’t have any or can’t configure your Metamask to Sepolia test network, you can refer to this link for guidance.

5. Click on the deploy and run transaction tab which is below the solidity compiler tab.

Make sure the environment is configured to the “injected provider” and below it, you should see “Sepolia” listed. Connect your Metamask wallet to initiate contract deployment. Choose the specific contract you wish to deploy. Click on the “deploy” button. If everything proceeds smoothly, you’ll find an interface below allowing you to interact with your smart contract.

We’ve successfully deployed a smart contract on the Ethereum blockchain, and it now possesses an Ethereum address. To explore the transaction history associated with this address, simply copy the address and visit https://sepolia.etherscan.io/. There, you’ll find the transaction details and history linked to that specific Ethereum address.

We can also fund the faucet by sending some sepolia test ether to the contract address as we have a function to handle incoming ether sent to the address.

You can withdraw funds from the faucet using Remix’s interface by inputting “100000000000000000” wei, which is equivalent to 0.1 ETH.

Throughout this journey, we’ve explored various account types, delved into the process of writing and deploying a smart contract on the Ethereum blockchain, and even learned how to interact with it. Keep learning!

--

--

Gbenga Olaseni
EthereumNigeria

I'm Gbenga Olaseni, a frontend developer and web 3 researcher.