.env
file to store the private key of your MetaMask account and node endpoints.package.json
file with default settings.
Create an empty hardhat.config.js
if you want to start from scratch.
/contracts
and /scripts
. This is where you will store the contracts and deployment scripts respectively.
hardhat-toolbox
plugin which, along with other useful functionalities, lets you use libraries and frameworks like ethers.js, Mocha, and Chai for developing and testing your smart contracts.
The sapphire-hardhat
plugin helps port your application onto the Oasis Sapphire by wrapping the provider used in the project. This will help your application interact with the Oasis Sapphire network by enabling functionalities like transaction encryption and signing.
In this project, you also require the dotenv
package for handling the environment variables. To install the package, run the following command:
.env
file.env
file.
.env
file:
.env
file:
/contracts
directory of your Hardhat project, create a new file named SecretNumberGame.sol
and add the following code to it:
SecretNumberGame
contract is a simple game where participants submit secret numbers. The game ends when the maximum number of entries is reached, and the participant with the highest secret number wins. The smart contract includes a modifier to check if the game is still accepting entries and an event to declare the winner when the game ends. The contract also includes functions to submit a secret number, get an entry at a specific index, and retrieve the submitted entries.
Once you add the contract, open a terminal and use the following command to compile the smart contract:
/artifacts
directory at the root of your project.
deploy.js
, in the /scripts
directory of the project and add the following code to it:
SecretNumberGame
contract to an Ethereum network.highestNumber
variable using the eth_getStorageAt
method.getStorageAt
— it fetches the storage data at a given slot number for a specified contract address.decodeTransactionInput
— it decodes the transaction input data using the contract’s ABI.main
function contains the core logic of the script. It first compiles and deploys the contract, then submits a secret number, decodes the transaction input data, and finally fetches the state data of the highestNumber
variable.
hardhat.config.js
file in the root directory of the project:
hardhat.config.js
file is a key configuration file used in this project. It is used to configure the project settings, such as the Solidity version, networks, and other customizations.
In this specific file, the Solidity version is set to 0.8.18
, indicating that the project uses the specified version of Solidity for compiling the contracts.
The networks
object in the file specifies the test network used for deployment, which in this case is the Sepolia Testnet. The url
key is used to specify the URL of the Sepolia Testnet node used for deployment.
Additionally, the accounts
key specifies the accounts used for deployment. This key uses an environment variable PRIVATE_KEY
and checks if it is defined in the .env
file. If it is defined, the private key is used for deployment.
Once the hardhat.config.js
file is modified, open a terminal in the root directory of your project and use the following command for deploying and interacting with your contract:
deploy.js
file that we created. The script will produce the following output:
hardhat.config.js
file:
@oasisprotocol/sapphire-hardhat
package onto the config file. This package handles the encryption of transactions and calls to the contract and thus ensuring the confidentiality and safety of the data involved.
Once the hardhat.config.js
file is modified, open a terminal in the root directory of your project and use the following command for deploying and interacting with your contract:
highestNumber
variable is to write explicit getter functions in the contract. To demonstrate this, we have included the getEntry
function within the contract. The function takes in an index value and returns the details of the Entry
struct instance stored against that index in the entries
list. To call the function, add the following code to your deploy.js
script and execute it:
SecretNumberGame
resembles a typical auction/bidding contract, and as we saw previously, the contract also faces similar challenges. Now, with the SecretNumberGame
, the user stakes are pretty much nonexistent, but if we re-imagine the scenario and add in an NFT bid or a token exchange, the challenges that we saw earlier will gain a greater impact.
Running this contract on Ethereum Sepolia Testnet and the Oasis Sapphire Testnet reveals differences in data privacy, security, and efficiency. Ethereum Sepolia is a public blockchain, meaning all contract data and transactions are public. This transparency could compromise the game’s secrecy.
In contrast, Oasis Sapphire emphasizes confidentiality and data privacy through advanced encryption techniques and confidential data storage. These features, when applied in a wider scope, provide unparalleled data protection and can even prevent common challenges like front/back running, which are especially relevant in decentralized finance (DeFi) and non-fungible token (NFT) markets.