DEV Community

aaronblondeau
aaronblondeau

Posted on

Hedera Smart Contracts - Guessing Game

I feel like I didn't do it right, but I got it to work! As a developer this sure happens to me a lot.

In order to explore how smart contracts work on Hedera I wanted to make a simple guessing game:

  • The smart contract contains a secret phrase. You can call a function on the smart contract to guess what it is.
  • Each guess costs 5 HBAR (you can send more if you want to show off)
  • If you guess correctly you will receive all the HBAR collected so far.

The repo is here https://github.com/aaronblondeau/guess-it.

I have very limited knowledge of solidity or even smart contracts so this was all new territory for me.

Here is what I came up with for the contract:

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.11;
pragma experimental ABIEncoderV2;

// Compile with:
// npm install -g solc
// solcjs --bin GuessIt.sol
contract GuessIt {

    string private secretPhrase;
    address public owner;
    uint pot = 0;

    event GuessAtttempt(address indexed from, string guess, bool success, uint pot);

    constructor (string memory _phrase) {
        owner = msg.sender;
        secretPhrase = _phrase;
    }

    function setPhrase(string memory _phrase) public {
        // Only contract owner can update phrase
        require(msg.sender == owner);
        secretPhrase = _phrase;
    }

    function guessPhrase(string memory _guess) public payable returns (bool) {
        // Make sure caller has sent at least 5 HBAR
        require (msg.value >= 500000000, "insufficient payable");

        // Add funds to the pot (why doesn't this show in contract balance on dragonglass?)
        pot += msg.value;

        if (keccak256(abi.encodePacked((_guess))) != keccak256(abi.encodePacked((secretPhrase)))) {
            emit GuessAtttempt(msg.sender, _guess, false, pot);
            return false;
        }

        // Guess was correct, transfer pot to caller
        emit GuessAtttempt(msg.sender, _guess, true, pot);
        payable(msg.sender).transfer(pot);
        pot = 0;
        return true;
    }

    function contractVersion() public pure returns (uint) {
        return 9;
    }
}
Enter fullscreen mode Exit fullscreen mode

Some of the key takeaways are:

  • Since secretPhrase is marked private I am assuming that there isn't a way to extract it from the network. This very well could be an incorrect assumption.
  • I needed the payable keyword on my function in order to allow callers to send funds. Also had to wrap the sender with a payable function call to be able to send the reward.
  • I kept track of the amount sent in the "pot" variable. However, it is still unclear to me where the funds were actually stored. The contract itself always showed a 0 balance even though payouts successfully occur.
  • It took me quite some time to figure out how to get the return value of the function after calling it. Using the getRecord function finally did the trick.

One of my biggest questions from this exercise is how do you rapidly iterate while developing a smart contract? I debugged mine by sending the contract to the testnet over and over (sorry testnet). Next I plan on figuring out how to unit test the contract as I think that will resolve the iteration issue and probably uncover some lovely bugs that I missed.

Top comments (4)

Collapse
 
idax profile image
Ironside Digital Assets

Chat GPT might be cheating but it can be a helpful tool as well :) hope this helps

  1. Modular Development: Building your contract in smaller, modular pieces allows for easier changes and testing of individual components. Are there specific modules or functionalities you're focusing on?

  2. Test-Driven Development (TDD): Writing tests before the actual code can ensure that each part of your contract meets its requirements before moving forward. Do you have specific test cases in mind, or would you like advice on formulating these?

  3. Automated Testing Tools: Tools like Truffle, Hardhat, or others can be used for automated testing and deployment. Are you familiar with these tools, or would you like a brief overview?

  4. Version Control: Using Git or similar systems for version control allows you to track changes and revert to previous versions if needed. How comfortable are you with version control systems?

  5. Continuous Integration/Continuous Deployment (CI/CD): This involves automated testing and deployment pipelines. Do you have a CI/CD pipeline set up?

  6. Use of Testnets: Deploying to Ethereum testnets (like Ropsten, Rinkeby, etc.) before the mainnet. Have you chosen a testnet for your development?

  7. Rapid Feedback Loops: Engaging with users or stakeholders frequently to get feedback. Do you have a process for gathering and integrating feedback?

  8. Security Audits and Analysis Tools: Regularly using tools like MythX, Slither, or others for security analysis. Are you looking for recommendations on security tools?

  9. Development Environment: Setting up a robust and efficient development environment, possibly using IDEs like Remix or Visual Studio Code. Do you need advice on setting up your development environment?

Collapse
 
idax profile image
Ironside Digital Assets

That's a really cool idea. Not sure if HBAR has Remix but I think a lot of Solidity Devs use it. I've only been able to get a contract or two through the compiler. It's not the easiest thing to learn. I've been at it for a bit now, and still not really sure how the stack comes together lol. Hope all is well and good luck

Collapse
 
idax profile image
Ironside Digital Assets

Just came across this as well today.

koii.network/founders

Collapse
 
idax profile image
Ironside Digital Assets

Not sure if this will help at all, but wanted to share it.

awesome-web3.com