Writing and Deploying a Smart Contract to Linea Blockchain - Part 1
Building a Blockchain-Based Prediction Game on Linea
Introduction
In this tutorial, I'll guide you through writing and deploying a Solidity Smart Contract on the Linea blockchain. This smart contract will form the backbone of a simple prediction game where users can wager Ether, make predictions, and either win or lose based on the outcome.
Prerequisites:
Before diving into the tutorial, ensure you have the following:
Basic knowledge of Solidity and Smart Contracts.
Familiarity with Ethereum development tools like Remix, MetaMask, and a code editor (e.g., VS Code).
MetaMask wallet installed and connected to the Linea Sepolia Testnet.
Some test Ether in your MetaMask wallet for deployment and testing.
Please note: You need some Ether on Linea Mainnet to get Test Ether on the Linea Faucets.
Step 1: Setting Up Your Development Environment
Install MetaMask:
If you haven't already, install the MetaMask browser extension.
Connect MetaMask to the Linea Sepolia testnet. You can add the Linea Sepolia network by visiting ChainList . You can optionally add Linear Network information using the following details:
Network Name: Linea Sepolia
New RPC URL:
https://rpc.sepolia.linea.build
Chain ID:
59141
Currency Symbol: ETH
Access Remix IDE:
Open Remix IDE in your browser.
Remix is an online IDE that allows you to write, compile, and deploy Solidity Smart Contracts directly from your browser.
Step 2: Writing the Smart Contract
Let's start by writing the Solidity smart contract for the prediction game.
Create a New File:
- In Remix, create a new file named
PredictionGame.sol
.
- In Remix, create a new file named
Write the Contract:
- Copy and paste the following Solidity code into your new file:
solidityCopy code// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract PredictionGame {
address public owner;
uint256 public houseBalance;
event GamePlayed(address indexed player, uint256 amount, uint256 prediction, uint256 houseNumber);
event GameWon(address indexed player, uint256 amountWon);
event GameLost(address indexed player, uint256 amountLost);
constructor() {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner, "Only the owner can call this function");
_;
}
modifier sufficientFunds(uint256 amount) {
require(msg.value >= amount, "Insufficient funds to play");
_;
}
function depositToHouse() public payable onlyOwner {
houseBalance += msg.value;
}
function withdrawHouseFunds(uint256 amount) public onlyOwner {
require(amount <= houseBalance, "Not enough funds in the house");
houseBalance -= amount;
payable(owner).transfer(amount);
}
function generateRandomNumber() private view returns (uint256) {
return uint256(keccak256(abi.encodePacked(block.timestamp, block.prevrandao, msg.sender))) % 10;
}
function play(uint256 userPrediction) public payable sufficientFunds(0.0001 ether) {
require(houseBalance >= msg.value * 10 / 100, "House doesn't have enough balance to cover potential winnings");
require(msg.value >= 0.0001 ether, "Must bet at least 0.0001 Ether");
require(userPrediction >= 0 && userPrediction <= 9, "Prediction must be between 0 and 9");
uint256 houseNumber = generateRandomNumber();
emit GamePlayed(msg.sender, msg.value, userPrediction, houseNumber);
if (userPrediction == houseNumber) {
uint256 winnings = msg.value + msg.value * 10 / 100;
houseBalance -= winnings - msg.value;
payable(msg.sender).transfer(winnings);
emit GameWon(msg.sender, winnings);
} else {
uint256 loss = msg.value * 15 / 100;
houseBalance += loss;
payable(owner).transfer(loss);
payable(msg.sender).transfer(msg.value - loss);
emit GameLost(msg.sender, loss);
}
}
receive() external payable {
houseBalance += msg.value;
}
}
Step 3: Understanding the Smart Contract
This contract allows users to wager an amount and predict a number between 0 and 9, where they are playing "against the house." If the user guesses correctly, they win their bet amount plus 10% from the house. If they lose, the house takes 15% of their bet. The "house" stakes an amount in the Smart Contract that users can wager against.
Key Functions:
depositToHouse
: Allows the owner of the Smart Contract to deposit Ether into the house balance.withdrawHouseFunds
: Allows the owner to withdraw Ether from the house balance.play
: The primary function where users place their bet and make a prediction.generateRandomNumber
: Generates a pseudo-random number between 0 and 9.receive
: Fallback function to receive Ether.
Step 4: Compiling the Smart Contract
Compile the Contract:
In Remix, navigate to the "Solidity Compiler" tab on the left sidebar.
Ensure the correct Solidity version (
^0.8.18
) is selected.Click "Compile PredictionGame.sol".
You should see a green checkmark next to the compilation button if there are no errors.
Step 5: Deploying the Smart Contract to Linea Sepolia
Deploy the Contract:
Go to the "Deploy & Run Transactions" tab in Remix.
Ensure the "Environment" is set to "Injected Web3" (MetaMask should be connected).
Select the
PredictionGame
contract from the dropdown.Click "Deploy" and confirm the transaction in MetaMask.
Verify Deployment:
Once the transaction is confirmed, your contract will be deployed on the Linea Sepolia network.
Copy the contract address for use in the next part of the tutorial.
Step 6: Interacting with the Smart Contract
Interacting via Remix:
In the "Deploy & Run Transactions" tab, you'll see the deployed contract under "Deployed Contracts".
You can interact with the contract by calling functions like
play
,depositToHouse
,withdrawHouseFunds
, andhouseBalance
.
Testing the Contract:
Test the
play
function by making predictions and placing bets.Use
houseBalance
to check the balance of the contract andwithdrawHouseFunds
to withdraw funds as the owner.
Conclusion:
In this first part of the tutorial, you learned how to write a Solidity smart contract for a prediction game and deploy it to the Linea Sepolia Testnet. You also tested the Smart Contract to ensure it works as expected. You can find my Smart Contract version here.
In the next part of the tutorial, we'll build a frontend application using Next.js to interact with this Smart Contract. The application will allow users to connect their wallets, place bets, and see the outcomes.