Communicating with the Blockchain

Interacting with Smart Contracts and Blockchain

The arrival of web 3.0 and blockchain technology has granted us a powerful benefit: the ability to eliminate intermediaries through decentralization. However, on this occasion, we will delve a little deeper into a method that provides us with the capability to execute code, program, and even imbue intelligence. Our focus will be on Solidity, the language that enables us to interact with the blockchain.

You are likely already familiar with the concept of "smart contracts," which allow us to define and establish rules that will be triggered in response to specific events. These contracts reside on the blockchain and provide us with trust, security, transparency, and autonomy. In this way, we can streamline and ensure the secure and efficient execution of agreements.

For all these reasons, I would like to encourage you to explore and, why not, create your own smart contract.


Understanding Solidity

First of all, Solidity is a programming language specifically designed for developing smart contracts that run on the Ethereum platform. With Solidity, we can efficiently and securely create smart contracts.

The Solidity language consists of two main parts:

  • Header: This section of the code contains important information, such as the license under which the smart contract is distributed and the compatible versions of the Solidity compiler.

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
  • Contract Body: This is where the logic and rules of the smart contract are defined. Variables, functions, and structures can be established to create the desired functionality of the contract.

contract MyContract{
  // all rules here!
...
}

So, let's get to work and start building!

We are building a simple contract that allows you to register your name as a writer in the Ethereum Cohort of Writers.

Let's get started by defining the necessary ingredients:

  • An empty list of Ethereum Writers: This list will be used to store our names in the blockchain, ensuring transparency and immutability.

string[] ethereumWriters;
  • a function to store you name, it should looks like this

function addNewWriter(string calldata writer) public {
        ethereumWriters.push(writer);
}
  • A function to store your name could look like this:

function getAllWriters() public view returns (string[] memory) {
     return ethereumWriters;
}

So, let's create a file named ethereum-cohort.sol and add the following content:

It was really simple, right?

Caution! From this point forward, continuing to read will be at your own risk.

Warning: the content that lies ahead is so mesmerizing and brimming with knowledge that it could make your mind explode into a thousand pieces. If you consider yourself brave and are willing to challenge the limits of your mind, then please, proceed.

...but remember, once you have acquired this knowledge, there is no turning back.

We will be using the Sepolia testnet in your MetaMask wallet. If it doesn't appear there, you can follow the tutorial to add it. To add funds, you can click here.

Now it's time to see our smart contract on the blockchain! Click here to proceed.

It should look like this.

and there is a lot of information available here:

  1. The code of the contract

  2. The name of our contract

  3. Buttons to review the content code, functions to read our writers, and function to write a new writer to the blockchain.

  4. Sepolia as testnet

So, now let's proceed to add your nickname to the blockchain. Follow these steps:

  • Go to the "Write Contract" option.

  • Click on the "Connect to Web3" button to connect your wallet.

Before connecting your wallet, ensure that you have your wallet application open and unlocked.

Once your wallet is connected, you will be able to write your nickname to the blockchain.


Now you can read the contract and search for your nickname.


Congratulations on making it this far! You've taken a huge step into the world of blockchain, but there's still so much more to explore.

Remember, while we may not be able to predict the future, we can certainly contribute to shaping it.

Loading...
highlight
Collect this post to permanently own it.
Road to blockchain logo
Subscribe to Road to blockchain and never miss a post.
#ethereum#blockchain#solidity