Skip to content

Quickstart: a dice roll in 10 minutes ​

You will deploy a contract that rolls a six-sided die with Verity on PulseChain testnet v4.

You need: Foundry, a wallet with a few test PLS (from the faucet), and the network addresses.

1. The consumer contract ​

solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import {VerityConsumerBase} from "@verity/VerityConsumerBase.sol";

contract Dice is VerityConsumerBase {
    bytes32 constant KEY_HASH = 0xd60554a807f786634e67dadbf3187cd50973e1e18963b1c3d7b2ccdc29dd76cc;
    uint256 public immutable subId;
    mapping(uint256 => address) public rollerOf;
    mapping(address => uint8) public result; // 0 = none, 1..6, 255 = rolling

    constructor(address coordinator, uint256 _subId) VerityConsumerBase(coordinator) {
        subId = _subId;
    }

    function roll() external returns (uint256 requestId) {
        require(result[msg.sender] != 255, "already rolling");
        result[msg.sender] = 255; // lock BEFORE asking for randomness
        requestId = s_coordinator.requestRandomWords(KEY_HASH, subId, 3, 100_000, 1);
        rollerOf[requestId] = msg.sender;
    }

    function fulfillRandomWords(uint256 requestId, uint256[] memory words) internal override {
        result[rollerOf[requestId]] = uint8(words[0] % 6 + 1);
    }
}

Get the base contract from verity-contracts/src (VerityConsumerBase.sol and the files it imports). Compile for Shanghai; see PulseChain notes.

2. Create and fund a subscription ​

The subscription pays for your requests. Use the web app's My subscriptions tab, or the CLI:

sh
export VERITY_RPC_URL=https://rpc.v4.testnet.pulsechain.com
export VERITY_COORDINATOR=0xF3cBd70d61a8F5c8522d1377D9208844C3e1f0D7
export VERITY_KEYSTORE=~/.foundry/keystores/me
verity sub create --fund 5          # prints the subscription id

3. Deploy and authorise the consumer ​

sh
forge create Dice --rpc-url $VERITY_RPC_URL --account me --broadcast \
  --priority-gas-price 1gwei --with-gas-price 2gwei \
  --constructor-args $VERITY_COORDINATOR <subId>
verity sub add-consumer <subId> <dice address>

4. Roll ​

sh
cast send <dice> "roll()" --rpc-url $VERITY_RPC_URL --account me --priority-gas-price 1gwei
# about 45 s later:
cast call <dice> "result(address)(uint8)" <your address> --rpc-url $VERITY_RPC_URL

Look the request up in the app's Request lookup or Explorer tab to see its proof transaction and what it cost (about 0.0012 tPLS).

Next: Requesting randomness explains every parameter, and the security rules are mandatory reading before you hold real value.

Testnet: PulseChain testnet v4. Unaudited software; do not use on mainnet yet.