Skip to main content

Command Palette

Search for a command to run...

Day 9 #100DaysOfWeb3

Published
•2 min read•View as Markdown
Day 9 #100DaysOfWeb3

In the previous post 100daysofweb3 I very briefly summarised my career and the first steps I took to build the MetaMusicVerse, my first DApp ever - https://meta-music-verse.pedromartins31.repl.co/

My first smart-contract build with solidity is pretty simple. It contains a public method called wave in which the users can call and send messages across the Ethereum blockchain. If you are curious what the smart contract looks like see the code below:

/ SPDX-License-Identifier: UNLICENSED

pragma solidity ^0.8.0;

import "hardhat/console.sol";

contract WavePortal{
    uint256 totalWaves;
    uint256 private seed;


    event NewWave(address indexed from, uint256 timestamp, string message );
    struct Wave{
        address waver;
        string message;
        uint256 timestamp;
    }

    Wave[] waves;

    constructor()  payable {
        console.log("Yo yo, I'm a contract and I am smart");
        seed = (block.timestamp + block.difficulty) % 100;

    }

    function wave(string memory _message) public {
        totalWaves += 1;
        console.log("%s waved with message %s", msg.sender, _message);

        waves.push(Wave(msg.sender, _message, block.timestamp));
        seed = (block.difficulty + block.timestamp + seed) % 100;


        if (seed <= 50) {
                console.log("%s won!", msg.sender);
                uint256 prizeAmmount = 0.0001 ether;

                require(
                    prizeAmmount <= address(this).balance, 
                    "Trying to withdraw more money than the contract has."
                );

                (bool success, ) = (msg.sender).call{value: prizeAmmount}("");
                require(success, "Failed to withdraw money from the contract.");
        }

        emit NewWave(msg.sender, block.timestamp, _message);

    }

    function getAllWaves() public view returns (Wave[] memory) {
        return waves;
    }

    function getTotalWaves() public view returns (uint256) {
        // Optional: Add this line if you want to see the contract print the value!
        // We'll also print it over in run.js as well.
        console.log("We have %d total waves!", totalWaves);
        return totalWaves;
    }
}

To interact with this smart contract, I built a frontend page in which users can connect their wallets and send me their favourite song on Spotify. This song is then rendered in the page along with the wallet that made the transaction.

Screen Shot 2022-02-28 at 10.16.24 PM.png

Although this project is pretty simple, it gave me a good overview of how to work with smart contracts. In the next few days, I will continue studying blockchain and the development of smart contracts. Stay tuned!