ichinose9372 / ft_transcendense_42

3 stars 1 forks source link

Major module: Store the score of a tournament in the Blockchain. #85

Closed yuidvg closed 3 weeks ago

yuidvg commented 1 month ago

This Major module focuses on implementing a feature within the Pong website to store tournament scores securely on a blockchain. It is essential to clarify that for development and testing purposes, we will utilize a testing blockchain environment. The chosen blockchain for this implementation is Ethereum , and Solidity will be the programming language used for smart contract development.

  • Blockchain Integration: The primary goal of this module is to seamlessly integrate blockchain technology, specifically Ethereum , into the Pong website. This integration ensures the secure and immutable storage of tournament scores, providing players with a transparent and tamper-proof record of their gaming achievements.
  • Solidity Smart Contracts: To interact with the blockchain, we will develop Solidity smart contracts. These contracts will be responsible for recording, managing, and retrieving tournament scores.
  • Testing Blockchain: As mentioned earlier, a testing blockchain will be employed for development and testing purposes. This ensures that all blockchain- related functionalities are thoroughly validated without any risks associated with a live blockchain.
  • Interoperability: This module may have dependencies on other modules, par- ticularly the Backend Framework module. Integrating blockchain functionality might necessitate adjustments in the backend to accommodate interactions with the blockchain.

By implementing this module, we aim to enhance the Pong website by introducing a blockchain-based score storage system. Users will benefit from the added layer of security and transparency, ensuring the integrity of their gaming scores. The module emphasizes the use of a testing blockchain environment to minimize risks associated with blockchain development.

yuidvg commented 1 month ago

To interact with the TournamentScore contract from the Truffle console, you need to follow these steps:

  1. Get the Deployed Contract Instance: First, get the deployed instance of the TournamentScore contract.

    truffle(development)> const tournamentScore = await TournamentScore.deployed();
  2. Call the addScore Function: You can call the addScore function to add a new score. Note that this is a transaction, so it will modify the blockchain state and require gas.

    truffle(development)> await tournamentScore.addScore("match1", "player1", 100);
  3. Call the getScore Function: You can call the getScore function to retrieve a score. This is a view function, so it does not require gas.

    truffle(development)> const score = await tournamentScore.getScore(0);
    truffle(development)> console.log(score);

Here is a complete example of interacting with the contract:

truffle(development)> const tournamentScore = await TournamentScore.deployed();
truffle(development)> await tournamentScore.addScore("match1", "player1", 100);
truffle(development)> const score = await tournamentScore.getScore(0);
truffle(development)> console.log(score);

This should output the details of the score you added:

[
  'match1',
  'player1',
  '100'
]

Make sure you have the correct network and contract instance. If you encounter any issues, ensure that the contract is properly deployed and that you are connected to the correct network.

yuidvg commented 1 month ago

Using ganache-cli (now known as Ganache CLI) is a great way to start a local Ethereum blockchain for development and testing. Here’s how you can use Ganache CLI in place of the Ganache GUI:

Step-by-Step Guide Using Ganache CLI

  1. Install Ganache CLI: If you haven't already installed Ganache CLI, you can do so using npm:

    npm install -g ganache-cli
  2. Start Ganache CLI: Open a terminal and start Ganache CLI:

    ganache-cli

    By default, Ganache CLI runs on http://127.0.0.1:8545. You can change the port using the -p option if needed:

    ganache-cli -p 7545
  3. Configure Truffle to Connect to Ganache CLI: Edit your truffle-config.js file to connect to the Ganache CLI network.

    module.exports = {
     networks: {
       development: {
         host: "127.0.0.1",    // Localhost (default: none)
         port: 8545,           // Ganache CLI default port
         network_id: "*",      // Any network (default: none)
       },
     },
     // Other configurations...
    };

    If you started Ganache CLI on a different port (e.g., 7545), make sure to update the port in the configuration.

  4. Deploy the Smart Contract: In the terminal, run:

    truffle migrate --network development
  5. Interact with Your Smart Contract: You can use Truffle's console to interact with your contract as follows:

    truffle console --network development

    In the console, you can interact with the deployed contract:

    // Get the deployed instance
    let instance = await DataStorage.deployed();
    
    // Set a new string
    await instance.setString("Hello, Ethereum!");
    
    // Get the stored string
    let storedStr = await instance.getString();
    console.log(storedStr);  // Outputs: Hello, Ethereum!
    
    // Set a new uint
    await instance.setUint(42);
    
    // Get the stored uint
    let storedNum = await instance.getUint();
    console.log(storedNum.toString());  // Outputs: 42

Example Smart Contract (for reference)

Here's the example Solidity smart contract again for reference:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract DataStorage {
    string public storedString;
    uint256 public storedUint;

    // Function to store a new string
    function setString(string memory newString) public {
        storedString = newString;
    }

    // Function to store a new uint
    function setUint(uint256 newUint) public {
        storedUint = newUint;
    }

    // Function to get the stored string
    function getString() public view returns (string memory) {
        return storedString;
    }

    // Function to get the stored uint
    function getUint() public view returns (uint256) {
        return storedUint;
    }
}

Summary

  1. Install and Start Ganache CLI:

    • Install with npm install -g ganache-cli.
    • Start with ganache-cli.
  2. Configure Truffle:

    • Set up the truffle-config.js to connect to the Ganache CLI network.
  3. Deploy and Interact:

    • Deploy your contracts using truffle migrate --network development.
    • Interact with your contracts using truffle console --network development.

By following these steps, you can effectively develop and test your Ethereum smart contracts using Ganache CLI.