0

hardhat.config.js ->

require("@nomicfoundation/hardhat-toolbox");
require("@nomicfoundation/hardhat-ethers");
require("dotenv").config();
console.log("1");
const SEPOLIA_URL = process.env.SEPOLIA_URL;
const PRIVATE_KEY = process.env.PRIVATE_KEY;

/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
  solidity: "0.8.19",
  networks: {
    sepolia: {
      url: SEPOLIA_URL,
      accounts: [PRIVATE_KEY]
    }
  }
};
console.log("2");

package.json ->

{
  "name": "rpsls-contracts",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@nomicfoundation/hardhat-ethers": "^3.0.0",
    "ethers": "^6.8.1",
    "hardhat": "^2.19.0",
    "ts-node": "^10.9.1"
  },
  "dependencies": {
    "@nomicfoundation/hardhat-toolbox": "^3.0.0",
    "dotenv": "^16.3.1"
  }
}

I am trying to deploy using the command npx hardhat run scripts/deploy.js --network sepolia but nothing is showing on my terminal. I did console.log so it is just showing 1212 deploy.js ->

const hre = require("hardhat");

 async function main(){
  const RPSLSF = await hre.ethers.deployContract("RPSLSFactory");
  // Deploy the contract.
  await RPSLSF.waitForDeployment();
  console.log("Contract deployed to:", RPSLSF.target);
}

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

Solidity code ->

// SPDX-License-Identifier: Unlicensed

pragma solidity ^0.8.12;

contract RPSLSFactory {
    RPSLS[] private gameSessions;
    mapping (address => RPSLS[]) private userGameSessions;

    event NewGameSession(address indexed gameSession);
    function createGameSession(
        bytes32 _move1Hash,
        address _player2
    ) external payable {
        RPSLS gameSession = (new RPSLS){value: msg.value}(
            _move1Hash,
            msg.sender,
            _player2
        );
        gameSessions.push(gameSession);
        userGameSessions[msg.sender].push(gameSession);
        userGameSessions[_player2].push(gameSession);

        emit NewGameSession(address(gameSession));
    }
    function getGameSessions()
        external
        view
        returns (RPSLS[] memory _gameSessions)
    {
        return userGameSessions[msg.sender];
    }
}

contract RPSLS {
    enum Move {
        Null, Rock, Paper, Scissors, Lizard, Spock
    }
    address public player1;
    address public player2;

    bytes32 move1Hash;

    Move public move1;
    Move public move2;

    uint256 public stake;

    uint256 public TIMEOUT_IN_MS = 5 minutes;
    uint256 public lastTimePlayed;

    modifier onlyOwner() {
        require(msg.sender == player1);
        _;
    }

    event Player2Played(address indexed _player2, Move indexed _move2);
    event GameSolved(address indexed winner);
    event GameTied();
    event GameTimedOut(address indexed fallbackWinner);

    constructor(bytes32 _move1Hash, address _player1, address _player2) payable {
        stake = msg.value;
        move1Hash = _move1Hash;
        player1 = _player1;
        player2 = _player2;
        lastTimePlayed = block.timestamp;
        // const contract = await ContractFactory.deploy('uintValue')
    }

    function play (Move _move2) external payable {
        require(msg.value == stake, "Insufficient funds for move. Make sure you stake the required amount of ETH for the transaction to succeed.");
        require(msg.sender == player2);
        require(move2 == Move.Null, "Move already played");

        move2 = _move2;
        lastTimePlayed = block.timestamp;

        emit Player2Played(player2, _move2);
    }

    function solve(Move _move1, string calldata _salt) onlyOwner external {
        require(player2 != address(0), "Player 2 should make his move in order to solve the round.");
        require(move2 != Move.Null, "Player 2 should move first.");
        require(keccak256(abi.encodePacked(_move1, _salt)) == move1Hash, "The exposed value is not the hashed one!");
        require(stake > 0, "Winner is already determined.");

        move1 = _move1;

        uint256 _stake = stake;

        if (win(move1, move2)) {
            stake = 0;
            (bool _success) = payable(player1).send(2 * _stake);
            if (!_success) {
                stake = _stake;
            }
            else {
                emit GameSolved(player1);
            }
        }
        else if (win(move1, move2)) {
            stake = 0;
            (bool _success) = payable(player2).send(2 * _stake);
            if (!_success) {
                stake = _stake;
            }
            else {
                emit GameSolved(player2);
            }
        }
        else {
            stake = 0;
            (bool _success1) = payable(player2).send(_stake);
            (bool _success2) = payable(player1).send(_stake);
            if (!(_success1 || _success2)) {
                stake = _stake;
            }
            else {
                emit GameTied();
            }
        }
    }

    function win(Move _move1, Move _move2) public pure returns (bool) {
        if (_move1 == _move2)
            return false; // They played the same so no winner.
        else if (_move1 == Move.Null)
            return false; // They did not play.
        else if (uint(_move1) % 2 == uint(_move2) % 2) 
            return (_move1 < _move2);
        else
            return (_move1 > _move2);
    }

    function claimTimeout() external {
        require(msg.sender == player1 || msg.sender == player2, "You're not a player of this game.");

        require(block.timestamp > lastTimePlayed + TIMEOUT_IN_MS, "Time has not run out yet.");

        uint256 _stake = stake;
        stake = 0;

        if (player2 == address(0)) {
            (bool _success) = payable(player1).send(_stake);
            if (!_success) {
                stake = _stake;
            }
            else {
                emit GameTimedOut(player1);
            }
        }
        else if (move2 != Move.Null) {
         (bool _success) = payable(player2).send(_stake * 2);
            if (!_success) {
                stake = _stake;
            }
            else {
                emit GameTimedOut(player2);
            }   
        }
    }
}
3
  • What is your deploy.js script?
    – Mila A
    Nov 3, 2023 at 10:47
  • Is there any error shown? Nov 3, 2023 at 11:17
  • No error is shown.
    – Indul
    Nov 3, 2023 at 15:11

1 Answer 1

0

Developer Advocate at Chainstack here.

You can follow this guide, which will explain to you how to deploy with Hardhat.

Initialize a New Project

Create a new Hardhat project. During initialization, opt to install hardhat-toolbox.

Install Dependencies

Ensure you've installed OpenZeppelin contracts:

npm install @openzeppelin/contracts

Smart contract

In the contracts directory, create a file named Token.sol and paste the following:

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

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract Token is ERC20 {
    constructor() ERC20("Token", "TKN") {
        _mint(msg.sender, 10000 * 10 ** decimals());
    }
}

Configure Hardhat

Add your private keys and RPC to the following.

require("@nomicfoundation/hardhat-toolbox");

const SEPOLIA_URL = process.env.SEPOLIA_URL;
const PRIVATE_KEY = process.env.PRIVATE_KEY;

/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
  defaultNetwork: "sepolia",
  networks: {
    sepolia: {
      url: SEPOLIA_URL,
      accounts: [PRIVATE_KEY],
    },
  },
  solidity: "0.8.20",
};

Deploy Script

Here's a sample deploy.js:

const hre = require("hardhat");

async function main() {
  console.log("Deploying contract...");

  // Deploy the contract
  const FICEToken = await hre.ethers.deployContract("Token");

  // Wait for the transaction to be mined
  await FICEToken.waitForDeployment();

  // Log the address of the deployed contract
  console.log("Contract deployed to:", FICEToken.target);
}

// Execute the script and handle errors
main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });

Remember that this will only work with the same smart contract I posted.

If you change the name of the contract, edit it in this line:

hre.ethers.deployContract("Token");

Compilation and Deployment

Compile your smart contract:

npx hardhat compile

Deploy it:

npx hardhat run scripts/deploy.js --network sepolia

It will return a result like this:

Deploying contract...
Contract deployed to: 0xa804D8845263719e2E9bff14930cE738950FB623

Feel free to reach out if you need more help!

3
  • I have attached deploy.js and solidity file also. I did what you said but it didn't work for this project. Thanks for the help.
    – Indul
    Nov 3, 2023 at 15:17
  • I was able to deploy your contract on Sepolia using your contract and your deploy script. sepolia.etherscan.io/address/… Maybe it's an issue with the RPC? Do you get any error? Nov 3, 2023 at 18:20
  • 1
    It's an error due to rpc i think. I deployed the same contract on some other pc using the same code and it was deployed.
    – Indul
    Nov 5, 2023 at 9:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.