LogoLogo
Cryptum.ioGitHub
  • 🌐Cryptum
    • What is Cryptum
    • DeFi Market
    • Ecosystem
      • Bitcoin
      • Ethereum
      • Polygon
      • Avalanche C-Chain
      • BNB Chain
      • Cardano
      • Celo
      • Hathor
      • Solana
      • Stellar
      • XRP Ledger
      • Stratus
      • Hyperledger Besu
  • 👝Products
    • Dashboard Analytics
    • Tokenization
    • Crypto Checkout
      • Overview
      • Getting started
      • Checkout Dashboard
        • Creating your Store
        • Customer Checkout screens
        • Analytics
        • Managing Wallets
        • Creating Collections and NFTs
        • Linking Products to NFTs
        • Monitoring Orders
      • Checkout API
        • Authentication
        • Store
        • Orders
          • Handling Orders
    • Wallets
    • DeFi-as-a-Service
    • On & Off Ramp (soon)
    • E-commerce Plugins
      • Wordpress NFT Plugin
        • How to install NFT Plugin
        • Connect your Credentials to NFT Plugin
      • Wordpress Checkout Plugin
        • How to install Checkout Plugin
        • Connect your Credentials to Checkout Plugin
      • Plugins Guides
        • Creating your Store
        • Manage your Wallets
        • Creating Collections and NFTs
        • Linking Products to NFTs
        • Monitoring Orders
        • Monitoring Store (Analytics)
  • 💻Community Edition
    • Overview
    • Architecture
    • Start for free
    • Getting started
    • Dashboard guide
      • 🔑Creating a Project (API Key)
      • 🛠️Start building!
      • 📊Monitoring your Project
      • 📈Monitoring Requests
    • SDK guides
      • Get test currencies
      • Chainlink
        • 📈Price Feeds
        • ⚙️Automation
        • 🎲VRF
        • 🛤️CCIP
        • 🛤️CCIP
        • Project Examples
          • Lottery
          • Send Message CCIP
      • Wallets
      • Balances
      • Prices
      • Tokens
        • EVM Tokens (ERC-20)
        • Solana Tokens (SPL)
        • Cardano Tokens
        • Stellar Tokens
        • XRP Tokens
        • Hathor Tokens
      • NFTs
        • EVM NFTs (Ethereum, Polygon and others)
        • NFTs on Solana
        • NFTs on Hathor Network
      • Queries
        • Wallet information
        • Get transaction by hash
        • Get block information
        • Get transaction receipt by hash
        • Get fees information
        • Get NFT data
        • Get NFT balance
        • Get UTXOs (Unspent transaction outputs)
      • Uniswap
      • Staking
      • Smart Contracts
        • Deploy custom Smart Contracts
        • Loot Box
    • Features and credits
      • Avalanche C Chain
      • Bitcoin
      • BNB Chain
      • Cardano
      • Celo
      • Ethereum
      • Hathor
      • Polygon
      • Solana
      • Stellar
      • XRP Ledger
      • Stratus
    • API guides
      • API Cryptum
      • API Connector
  • 📃GLOSSARY
    • Blockchain terms
    • Cryptum terms
Powered by GitBook
On this page
  • About The Project
  • Requirements
  • Faucets
  • Getting Started
  1. Community Edition
  2. SDK guides
  3. Chainlink
  4. Project Examples

Lottery

PreviousProject ExamplesNextSend Message CCIP

Last updated 9 months ago

About The Project

This tutorial is a step-by-step guide to creating an automation for generating random words using Chainlink's VRF and Chainlink Automation features.

We will demonstrate how to build this project, showcasing the use of our SDK and Chainlink's resources. Follow each step carefully to generate your own automation for random words.

In this project, we will be using the following functionalities:

  • Price Feeds

  • VRF (Verifiable Random Function)

  • Automation

In this example, we'll cover the main part of creating VRF and Automation using our SDK.


DEMO

We've developed a sample project utilizing these features, and you can check out the complete implementation at:

The project involves a lottery where the user places a bet by providing 10 numbers. If the newly generated word contains 6 or more of these specified numbers, the user receives a reward.

Explore this playground to simulate bets and observe Chainlink VRF's functionality generating random numbers every 100 mined blocks on the POLYGON - MUMBAI network.

Access the link:

Provide your email and start playing.

If the game is paused, you can click to initiate.


Requirements

  • Basic knowledge of JavaScript.

  • NPM

  • Node version: ^14.17.0

  • Create account Dashboard Cryptum

  • Faucets (testnet) LINK and MAUTIC

Faucets

Many developers and test users of Web3 applications are faced with a barrier: the lack of balance in their wallets to test. This is because for you to carry out any transaction on the blockchain, you need to pay fees for the network to mine (validate) your transaction.

To solve this, all blockchain protocols in Test environment (Testnet) provide websites for you to inform your wallet and receive balance to be able to use it.

  • MATIC - https://mumbaifaucet.com/

  • LINK - https://faucets.chain.link/mumbai

Getting Started

Open your project

cd my-amazing-project/

Install using npm manager or yarn

npm install -S cryptum-sdk

yarn add cryptum-sdk

step 1

With a valid Cryptum API Key, you can then instantiate the SDK as follows:

const CryptumSdk = require('cryptum-sdk')

const sdk = new CryptumSdk({
  environment: 'testnet',  // 'testnet', 'mainnet'
  apiKey: "YOUR-API-KEY-HERE",
})

step 2

Then you call the generateWallet() function passing the protocol you want:

const wallet = await sdk.wallet.generateWallet({ protocol: 'POLYGON' })

step 3

Add faucets to your new wallet.

MATIC and LINK as described in: Faucets

step 4

Create Subscription Chainlink VRF.

const { hash } = await sdk.chainlink.createVRF({
    protocol,
    wallet,
    updateIntervalUpkeep: num_blocks_interval
    // Block interval, the new random word will be generated at each defined interval
})

const { contractAddress } = await sdk.transaction.getTransactionReceiptByHash({
    protocol,
    hash
})

return contractAddress

step 5

Add Funds with LINK in your Subscription.

const { hash } = await sdk.chainlink.topUpVRF({
    protocol,
    address, // contract address generated in step 4
    wallet,
    amount: 10
})

step 6

Automation setup; this stage is where we create the process responsible for generating a new random word every 'N' blocks.

const { hash } = await sdk.chainlink.createAutomation({
    protocol,
    wallet
})

const { contractAddress } = await sdk.transaction.getTransactionReceiptByHash({
    protocol,
    hash
})

return contractAddress

step 7

Add Funds with LINK in your Automation.

Perform a transfer of LINKs from your wallet to the contract address generated in step 6.

const { hash } = await sdk.token.transfer({
    protocol,
    amount,
    wallet,
    token: LINK_TOKEN_POLYGON_MUMBAI, // 0x326C977E6efc84E512bB9C30f76E30c160eD06FB
    destination: contractAddress // contract address generated in step 6
})

step 8

Register Upkeep.

At this stage, we will register our automation, initiating the process so that new random words are generated after every N number of blocks mined on the blockchain.

const { hash } = await sdk.chainlink.registerUpkeep({
        protocol,
        wallet,
        address: upkeepContract, // contract address generated in step 6
        name: 'Cryptum Random Words',
        encryptedEmail: '0x',
        upkeepContract: subscriptionContract, // contract address generated in step 4
        gasLimit: 500000,
        triggerType: 0,
        checkData: '0x',
        triggerConfig: '0x',
        offchainConfig: '0x',
        amount: '10' // lINK
})

step 9

Getting the generated random words.

Get the last request ID generated in your subscription:

const latestRequestID = await sdk.chainlink.latestRequestVRF({
    protocol,
    address // contract address generated in step 4
})

const randomWords = await sdk.chainlink.getRandomWordsVRF({
        protocol,
        address, // contract address generated in step 4
        requestId // latestRequestID
})

/*
Ex. Response:
{
    fulfilled: true,
    randomWords: [ "123456.....891011"]
}

*/

step 10

Getting information about your subscription and automation:

const subscription = await sdk.chainlink.getSubscriptionVRF({
    protocol,
    address // contract address generated in step 4
})

List all upkeeps

const upkeeps = await sdk.chainlink.listUpkeeps({
    protocol,
    address // contract address generated in step 6
})

Get info Upkeep ID:

const upkeep = await sdk.chainlink.getUpkeep({
    protocol,
    upkeepID
})

You can also track your subscription and automation through the chainlink website.

Implementation example in:

Implementation example in:

Implementation example in:

Implementation example in:

Implementation example in:

Implementation example in:

Implementation example in:

Implementation example in:

💻
Lottery Playground
cryptum-lottery/service/Cryptum.js
cryptum-lottery/controller/Wallet.js
cryptum-lottery/controller/Lottery.js
cryptum-lottery/controller/Lottery.js
cryptum-lottery/controller/Lottery.js
cryptum-lottery/controller/Lottery.js
cryptum-lottery/cron/index.js
cryptum-lottery/controller/Lottery.js
VRF
Automation
Cryptum - Lottery