Demystifying the Blockchain: Building a Simple Storage Smart Contract on Base Network

Oluwajuwonlo
3 min readMar 26, 2024

--

Demystifying the Blockchain: Building a Simple Storage Smart Contract on Base Network

The world of blockchain technology can seem complex, filled with jargon and intricate concepts. But at its core, blockchain offers a secure and transparent way to store and manage data. Smart contracts, self-executing programs on the blockchain, are a powerful tool leveraging this technology. In this post, we delve into creating a simple storage smart contract using Solidity, the leading programming language for blockchain development. We’ll specifically focus on deploying this contract on the Base Network, a high-performance Layer 2 scaling solution for Ethereum.

What is a Smart Contract?

Imagine a vending machine. You insert money, and the machine dispenses the product based on pre-defined rules. Similarly, a smart contract automates the execution of an agreement between two parties based on specific conditions written into the code.

Understanding the Code

Solidity

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract SimpleStorage {
uint256 public storedData;
event ValueChanged(uint256 newValue); constructor(uint256 _initialValue) {
storedData = _initialValue;
}
function set(uint256 _newValue) public {
storedData = _newValue;
emit ValueChanged(_newValue);
}
function get() public view returns (uint256) {
return storedData;
}
}

Use code with caution.

content_copy

  • SPDX-License-Identifier: This line specifies the license under which the code is distributed (MIT License in this case).
  • pragma solidity ^0.8.0: This line defines the version of Solidity compiler compatible with the code.
  • contract SimpleStorage: This declares a smart contract named “SimpleStorage.”

Variables and Events:

  • uint256 public storedData;: This declares a public variable named "storedData" of type uint256 (unsigned integer with 256 bits).
  • event ValueChanged(uint256 newValue);: This defines an event named "ValueChanged" that will be emitted whenever the stored data is updated.

Functions:

  • constructor(uint256 _initialValue): This is the constructor function, which is executed when the contract is deployed. It takes an initial value (_initialValue) and assigns it to the storedData variable.
  • function set(uint256 _newValue) public: This public function allows anyone to update the stored data by providing a new value (_newValue). It updates the storedData variable and emits the ValueChanged event with the new value.
  • function get() public view returns (uint256): This public view function retrieves the current value of storedData and returns it without modifying the contract state.

Deployment and Interaction on Base Network

To showcase a practical example, we’ll deploy this contract on the Base Sepolia testnet, a testing environment for Base Network. This allows us to experiment and learn without using real funds. For deployment, you can leverage tools like Remix https://remix.ethereum.org/, following guides specific to Base Network development. Once deployed, interact with the contract functions (set and get) using Base Sepolia test tokens (testnet version of BASE, the native token of Base Network). These test tokens can be obtained through faucets provided by the Base Network community.

Conclusion

This simple storage contract demonstrates the basic principles of smart contracts, deployed in this case on the Base Network. By understanding how data is stored and accessed, you gain a foundation for exploring more complex functionalities on the blockchain. As you delve deeper, you’ll discover the vast potential of smart contracts in revolutionizing various industries, from supply chain management to decentralized finance, with the added benefit of scalability offered by Layer 2 solutions like Base Network.

--

--