Nerif Docs
  • Quickstart
  • Tutorials
    • Lottery Tutorial
      • Step 1. Configure your account
      • Step 2. Add a smart contract
      • Step 3. Create a new automation workflow
      • Step 4. Build your automation workflow
      • Step 5. Test workflow execution
    • Bridge Tutorial
      • Step 1. Configure your account
      • Step 2. Add smart contracts
      • Step 3. Create a new automation workflow
      • Step 4. Build your automation workflow
      • Step 5. Test workflow execution
    • Price Feed Tutorial
      • Step 1. Configure your account
      • Step 2. Add smart contract
      • Step 3. Create a new automation workflow
      • Step 4. Build your automation workflow
  • 1. What is Nerif Network?
    • Our Mission
    • Our Vision
    • Terms
  • 2. Architecture
    • Core components
    • Validators
      • Networking
        • Bootnode
        • Joining the network
        • Leaving the network
        • Slashing
      • Consensus
        • Epoch & Rounds
        • Automation workflow
          • Triggers
          • Actions
          • Condition
    • Contracts
      • System Contracts
      • Operational Contracts
    • Security
      • DKG
      • Threshold ECDSA
      • Security checks
  • 3. How it works
    • Introduction
    • Nerif App
    • Login
    • Account Configuration
      • 1. Deploy a Gateway Contract
        • 1.1 Create a new gateway
        • 1.2. Use existing gateway contract
        • 1.3.What if I want to update my gateway contract?
      • 2. Top up your balance
        • 2.1. Topping up your balance on Polygon Mumbai
        • 2.2. Topping up your balance on Ethereum Goerli
    • Fees
    • User balance
      • 1. Topping up the balance
        • 1.1 Top up your balance via Nerif App
        • 1.2 Top up your balance directly via Registry smart contract
      • 2. Withdraw from balance
        • 2.1 Withdraw via Nerif App
        • 2.2 Withdraw directly via Registry smart contract
    • API
      • Off-chain APIs
      • On-chain APIs
    • SDK
  • 5. Nerif DAO
    • Governance
    • Treasury
    • Proposal process
  • 6. Support
Powered by GitBook
On this page
  1. Tutorials

Lottery Tutorial

In this guide, we will explore the process of leveraging Nerif Network to automate the mechanism for picking a winner in a lottery.

By following the step-by-step instructions provided, developers will gain the knowledge and practical skills to seamlessly automate smart contract functions execution using Nerif Network and unlock the power of automation for these operations.

To ensure a seamless flow of lottery winner-picking mechanism, Nerif Network will be calling 2 functions from the below Lottery smart contract:

  1. playersExist checks if there are at least two participants taking part in a lottery (doesn’t change state);

  2. pickWinner selects the winners among the participants (changes state so the transactions should be sent to execute it).

contract Lottery {
  uint public jackpot;
  address[] public players;
  
  // enter the given amount to the lottery pool
  function enter() public payable {
    require(msg.value > 0, "You must enter with a positive value");
    players.push(msg.sender);
    jackpot += msg.value;
  }
  
  // playersExist checks if there are some player in the list
  function playersExist() public view returns (bool exist) {
    exist = players.length >= 2;
  }
  
  // random choses a random winner
  function random() private view returns (uint) {
    return uint(keccak256(abi.encodePacked(block.timestamp, block.difficulty, players.length))) % players.length;
  }
  
  // pickWinner picks a random winner and transfer funds to them
  function pickWinner() public {
    require(players.length >= 2, "Not enough players entered the lottery");
    uint randomIndex = random();
    address payable winner = payable(players[randomIndex]);
    winner.transfer(jackpot);
    delete players;
    jackpot = 0;
  }
}

Let’s go!

PreviousTutorialsNextStep 1. Configure your account

Last updated 1 year ago