npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

solrand

v1.0.0

Published

Simple randomness library for Solidity

Downloads

5

Readme

Solrand - Simple pseudo-random number generator library for Solidity

This library provides a simple and efficient way of pseudo-random number generation for Solidity smart contracts. It ensures that users commit to a given block in the future, making it impossible to know the outcome beforehand or manipulate it after. The package operates in two key phases:

  1. Request randomness (Commit): Users initiate a request by committing to a future block number, nonce (allowing multiple requests within the same transaction) and some custom data that can be chose by the protocol developer. This request is stored on-chain and remains unpredictable until the reveal phase. A future block number (e.g., current block + 5) is selected to ensure the result remains unpredictable.

  2. Fulfill randomness (Reveal): The actual randomness is determined by hashing the future block number hash and other committed parameters. This produces a bytes32 hash that can be cast to a number. In order to generate a number from range you can use the modulo operation. A safety check ensures the committed block number is not older than 256 blocks, as the EVM only retains block hashes for the last 256 blocks. If the block is too old, the transaction reverts with an "EXPIRED" status.

    If you plan to use this library to generate random traits on sellable items, make sure that users pay when requesting randomness. This way, they cannot cancel the request and try again if they don't like the result.

Installation

npm i solrand

How to use

1. SolrandConsumer base contract

You can inherit from the base SolrandConsumer contract and use _requestRandomness and _fullfillRandomness functions to request and fullfill randomness:

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

import { SolrandConsumer } from "solrand/contracts/Solrand.sol";

contract SolrandConsumerExample is SolrandConsumer {
  uint256 public number;

  /**
   * Request a random number from the Solrand library and wait at least 5 blocks for it to be
   * fullfillable.
   * @return requestId The ID of the request
   */
  function requestRandomNumber() public returns (bytes32 requestId) {
    return
      _requestRandomness(
        block.number + 5,
        abi.encodePacked("Counter", msg.sender)
      );
  }

  /**
   *
   * @param requestId The ID of the request to fullfill
   * @return The random number
   */
  function fullfillRandomNumber(bytes32 requestId) public returns (uint256) {
    bytes32 randomValue = _fullfillRandomness(requestId);
    number = uint256(randomValue);
    return number;
  }
}

SolrandConsumer uses ERC7201 storage pattern, so you can safely use it in upgradeable contracts.

2. SolrandLib

This is the underlying library used by the SolrandConsumer base contract. You can use the library directly to generate randomness in your contracts, just make sure to keep the parameters in your contract.

  uint requestBlockNumber = block.number;
  uint targetBlockNumber = block.number + 10;
  uint nonce = 1;
  bytes memory requesterIdentifier = abi.encodePacked(
    // arbitrary data like user address, NFT ID they hold, in game points amount, etc.
    "unique requester identifier",
    address(0x1337),
    timestamp
  );

  bytes32 requestId = SolrandLib.requestRandomness(
    targetBlockNumber,
    nonce,
    requesterIdentifier
  );

  // Wait until the target block +1 is reached (+1 because the blockhash of current block is not
  // available, only the last 256 block hashes are available)
  // In foundry you can use the following cheatcode to wait for the block:
  // vm.roll(targetBlockNumber + 1);

  // Consumer contract should hold this data like shown in the SolrandConsumer
  uint256 randomNumber = uint256(
    SolrandLib.fullfillRandomness(
      requestBlockNumber,
      targetBlockNumber,
      nonce,
      requesterIdentifier
    )
  );

Build

pnpm build

Test

pnpm test

Format

pnpm prettier:write

Deploy example contract

forge script script/SolrandConsumerDeployment.s.sol:SolrandConsumerDeploymentScript --rpc-url <your_rpc_url> --private-key <your_private_key> --broadcast