dotenv
package to securely manage environment variables.
Create a JavaScript project
Do you want to install this sample project's dependencies with npm (hardhat @nomicfoundation/hardhat-toolbox)?
hardhat.config.js
in the root directory. This file configures various settings for your Hardhat projects, such as the network you want to deploy your contracts on, the compilers you want to use, and the plugins you want to enable.
Delete the default code in the file and replace it with the following:
require("@nomicfoundation/hardhat-toolbox");
imports the Hardhat Toolbox plugin, which provides several useful tools and utilities for Hardhat projects.require("dotenv").config();
loads environment variables from a .env
file using the dotenv
package.module.exports = { ... }
exports a JavaScript object containing the configuration for the Hardhat project.solidity: "0.8.23",
sets the Solidity compiler version to 0.8.23.networks: { ... }
defines the network configurations for the Hardhat project. In this case, it defines a network called saigon
that connects to the Ronin Saigon blockchain network.saigon: { ... }
defines the configuration for the saigon
network.url: RONIN_SAIGON_CHAINSTACK,
sets the URL for the Saigon network using the RONIN_SAIGON_CHAINSTACK
environment variable.accounts: [PRIVATE_KEY],
sets the accounts for the saigon
network using the PRIVATE_KEY
environment variable. This will allow the Hardhat project to deploy contracts and interact with the Saigon network using the specified private key.contracts
with a sample contract in it. Rename this contract to Game.sol
and replace its code with the following:
Game
uint256
(unsigned integer)uint256
mapping(address => uint256)
address
gameResult
and withdraw
functions).owner
.deposits
mapping with the player’s deposit amount.user
(address of the player)bool
(True
if the player has enough deposit, False
otherwise)onlyOwner
player
— address of the playeruserWon
— boolean indicating whether the player won or notonlyOwner
.env
file for your endpoint and private keys:
scripts
directory inside the root of your project, you will find a file named deploy.js
. Replace its content with the following:
Game
smart contract.
Here’s a breakdown of what each part of the script does:
main
function is an asynchronous function where the main logic of the script is executed.
ethers
plugin to deploy a contract named Game
. The await
keyword is used because deployContract
is an asynchronous operation.
0x
prefix from the address:
0x
prefix from the Ethereum address using the substring
method. As the Ronin explorer uses this format: https://saigon-app.roninchain.com/address/ronin:49a1EA88e5F81850DE30Dc038c1d08028ecFc9b5
.
main
function, they are caught and printed to the console, and the script exits with an error code.
Game
contract, run the following command in the terminal: