Lancer Visual Studio Code

npm install ethers 
npm i hardhat
npm i @nomiclabs/hardhat-waffle
npm i ethereum-waffle
npm i chai

Lancer la commande

npx hardhat

⇒ Create a basic sample project

Hardhat nous demander d’installer des dependances

npm install --save-dev "hardhat@^2.9.3" "@nomiclabs/hardhat-waffle@^2.0.0" "ethereum-waffle@^3.0.0" "chai@^4.2.0" "@nomiclabs/hardhat-ethers@^2.0.0" "ethers@^5.0.0"

Untitled

Nous avons de nouveaux dossiers qui ont été crées , uniquement Greeter.sol et sample-script.js vont nous interesser

On va remplacer le contenu de Greeter.sol avec une petit contract

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

import "hardhat/console.sol";

contract HardhatTuto {
    struct player {
        string pseudo;
        uint pdv;
        uint force;
        uint agilite;
    }

    mapping(uint256=>player) playerInfo;
    uint256 countPlayer = 1;

    function addPlayer(string memory _pseudo) public {
        playerInfo[countPlayer] = player(_pseudo, random(countPlayer)%20, random(countPlayer)%20, random(countPlayer)%20);
        console.log('Le pseudo %s a les stats suivantes ', playerInfo[countPlayer].pseudo);
        console.log(' vie: %s \\n force: %s \\n agilite %s', playerInfo[countPlayer].pdv, playerInfo[countPlayer].force,playerInfo[countPlayer].agilite);
        countPlayer++;
    }

    function random(uint256 _count) private view returns(uint256) {
        return uint(keccak256(abi.encodePacked(block.difficulty, block.timestamp, _count)));
    }

    function getPlayer(uint _id) public view returns (player memory) {
        return playerInfo[_id];
    }
}

La fonction console.log affiche dans le terminal le texte passé en parametre

Ce qui est important de noter c’est l’utilisation de “%s” , le second parametre de la fonction console.log represente la variable a afficher a la place de “%s”

Par exemple au dessus “Le pseudo %s ...”

Le %s va prendre la valeur de pseudoIndo[countPlayer].pseudo

Ensuite on va deployer notre contract et automatiser les test

RDV dans le fichier sample-script.js

// We require the Hardhat Runtime Environment explicitly here. This is optional
// but useful for running the script in a standalone fashion through `node <script>`.
//
// When running the script with `npx hardhat run <script>` you'll find the Hardhat
// Runtime Environment's members available in the global scope.
const hre = require("hardhat");

async function main() {
  // Hardhat always runs the compile task when running scripts with its command
  // line interface.
  //
  // If this script is run directly using `node` you may want to call compile
  // manually to make sure everything is compiled
  // await hre.run('compile');

  // We get the contract to deploy
  const Greeter = await hre.ethers.getContractFactory("HardhatTuto");
	const greeter = await Greeter.deploy();
// if needed to pass arguments to constructor function
  // const greeter = await Greeter.deploy("Hello, Hardhat!");

  await greeter.deployed();

  console.log("Greeter deployed to:", greeter.address);
}

// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });