0xCord - Hello World

0xCord is the easiest way to access web3 infrastructure via native payments, composability, and configuration, all in one unified place. In this tutorial, we'll show you how to use 0xCord's Chainlink VRF V2 API for generating random numbers. Whether you prefer on-chain or off-chain consumption, we've got you covered. Let's get started!

  1. To get started, head over to 0xCord.com and sign up to create your account. Once you've submitted your info, we'll send you a magic link via email. Just click on it and voila - you'll be taken straight to your dashboard!

  2. Once you're logged in, you'll see your dashboard which displays all of your dapps, including NFTs, DeFi products, and DAOs - basically anything that requires Web3 infrastructure. To get started, you'll want to create a new dapp. Just hit the "Create new" button and give it a name.

  3. Now that you've created your dapp, let's take a look at its page by clicking "view details". As you'll see, there isn't any Web3 infrastructure set up yet. That's where the Infra Hub comes in - this is where you can provision the resources you need. Our first piece of infra is Chainlink VRF V2. Simply select your dapp from the dropdown, and we'll automatically provision Chainlink VRF V2 for you. Best of all, it supports both on-chain and off-chain consumption for all blockchains that Chainlink supports. As of 2/28, we have several testnets available for you to use for free, including Polygon Mumbai, Sepolia, Fuji, BSC testnet, Klaytn testnet, Fantom testnet, and Goerli.

Onchain API

Usually, using Chainlink VRF V2 onchain requires several steps, such as acquiring gas tokens, $LINK tokens, creating subscriptions, and managing subscription balances. But with 0xCord, we simplify the process by provisioning and managing the subscriptions for you - no crypto required! Here's how to use Chainlink VRF V2 onchain with 0xCord:

  1. Click "Fetch Config Vars" to view the fully-funded subscriptions that are ready to use.

  2. Copy the subscription ID for the chain you'll deploy your contract to. For example, on Polygon Mumbai this might be 2894.

    1. If you’re following the Chainlink VRF tutorial , use our subscription ID in this step:

    2. Configure your deployment. On the Deploy tab in Remix, select the Injected Provider environment, select the VRFv2Consumer contract from the contract list, and specify your subscriptionId so the constructor can set it.

  3. Once your contract is deployed, copy its address. The next step is to link the consumer to the subscription, which you can do in one of two ways:

    1. Using the 0xCord app, click the gears icon on the Chainlink instance to open the ChainLink Subscription Consumers modal. Enter the contract address, select the chain from the dropdown, and click save. This will create a transaction linking the consumer to the chain.

    2. Alternatively, you can programmatically link the consumer to the subscription and use our API’s Add Consumer endpoint. First go to the dapp page on 0xCord, and copy the dappId from the URL params. Then, authenticate using the API key, and pass in the network, consumer, and dappId as query params.

      1. Example request:

        curl --location --request POST 'https://0xcord.com/api/vrfv2/addConsumer?dappId=1&network=polygon_mumbai&consumer=0x53470b76D3f57E6C7be40BB333014481ec1c5530' \
        --header 'Authorization: xxxx' 
        

And that's it! With 0xCord, you can now easily use Chainlink VRF in your contract without worrying about managing subscriptions.

Offchain API

With 0xCord's offchain API, you can easily consume VRF V2 through a simple RESTful JSON API call, bridging the gap between web2 and decentralized web3 infrastructure. Here's how to use it:

  1. Copy the API key from the "Fetch Config Vars" section, which is used to access the 0xCord RESTful JSON API. Docs can be found here.

  2. In Postman, select the production environment and paste the API key into the "apiKey" variable.

  3. Hit the "Request Random Number" API call, selecting the desired network (polygon_mumbai, sepolia, fuji, bsc_testnet, klaytn_testnet, fantom_testnet, goerli) and specifying the number of random numbers with the "numWords" parameter (1 to 500).

    1. Example request:

      curl --location --request POST 'https://0xcord.com/api/vrfv2/requestRandomNumber/?network=polygon_mumbai&numWords=1' \
      --header 'Authorization: xxx'
      
    2. Example response:

      {
          "data": {
              "success": true,
              "requestId": "82772297461836742329238512432706064630856291450561249462036537828944094389038",
              "transactionHash": "0x1f636827ba3572472e117f8f008b3fc6726c1284c0ceca586e3a0e676f0406eb",
              "url": "https://mumbai.polygonscan.com/tx/0x1f636827ba3572472e117f8f008b3fc6726c1284c0ceca586e3a0e676f0406eb"
          }
      }
      
    3. This will return a Chainlink request ID as well as a link to etherscan.

  4. Retrieve the random numbers using the "Get Random Numbers" endpoint, specifying the same network and request ID.

    1. Example request:

      curl --location 'https://0xcord.com/api/vrfv2/getRandomNumber/?network=polygon_mumbai&requestId=82772297461836742329238512432706064630856291450561249462036537828944094389038' \
      --header 'Authorization: xxx'
      
    2. Example response:

      {
          "data": {
              "fulfilled": true,
              "randomNumber": [
      "111296169371105942025256963044261804772682237016873883880854565109176540425880", "48853847565293424268317777741688717343601339637655607855994234898572199201663",
      "22503081880076003702041350408548819131186130333868717438739033661493489536203",
      "78662195269794346569705780123769753316601136453919903121333806175221242048442",
      "26015862336455109621425908291815156389231643676531347218138706099532711105913"
              ]
          }
      }
      
    3. That's it! The returned random numbers are provably verifiable on-chain and ready to use in your application.

In this tutorial, we've shown you how to use 0xCord's Chainlink VRF V2 API to generate random numbers for your Web3 application. With 0xCord, you can easily provision subscriptions and start consuming VRF V2 without having to worry about managing subscriptions and monitoring balances.

Whether you prefer onchain or offchain consumption, 0xCord has you covered. By using our offchain API, you can make simple RESTful JSON API calls to start consuming VRF V2.

We hope you found this tutorial helpful and that it gets you one step closer to building your Web3 application. If you have any questions or feedback, feel free to reach out to us at contact@0xcord.com. Happy coding!

Subscribe to 0xCord
Receive the latest updates directly to your inbox.
Mint this entry as an NFT to add it to your collection.
Verification
This entry has been permanently stored onchain and signed by its creator.