EasyntropyEasyntropy
About
Documentation
Examples
Discord
About
Documentation
Examples
Discord
    • Documentation
    • Examples

Examples

Store Last Seed

Request and store RNG.

Example on Sepolia

import "../Easyntropy/EasyntropyConsumer.sol";

contract StoreLastSeedDefaultCallback is EasyntropyConsumer {
  //
  // support
  bytes32 public latestSeed;

  //
  // events & errors
  event RandomValueObtained(uint64 indexed requestId, bytes32 seed);
  error NotEnoughEth();

  constructor(address _entropy) EasyntropyConsumer(_entropy) {}

  function requestRandomValue() public payable {
    if (msg.value < easyntropyFee()) revert NotEnoughEth();

    easyntropyRequestWithCallback();
  }

  function easyntropyFulfill(uint64 requestId, bytes32 seed) external onlyEasyntropy {
    latestSeed = seed;

    emit RandomValueObtained(requestId, seed);
  }
}

Custom Response Callback Function

Request and store RNG using custom callback.

Example on Sepolia

import "../Easyntropy/EasyntropyConsumer.sol";

contract StoreLastSeedCustomCallback is EasyntropyConsumer {
  //
  // support
  bytes32 public latestSeed;

  //
  // events & errors
  event RandomValueObtained(uint64 indexed requestId, bytes32 seed);
  error NotEnoughEth();

  constructor(address _entropy) EasyntropyConsumer(_entropy) {}

  function requestRandomValueCustomCallback() public payable {
    if (msg.value < easyntropyFee()) revert NotEnoughEth();

    easyntropyRequestWithCallback(this.customFulfill.selector);
  }

  function customFulfill(uint64 requestId, bytes32 seed) external onlyEasyntropy {
    latestSeed = seed;

    emit RandomValueObtained(requestId, seed);
  }
}

Pass Player Metadata

In this example we use requestId to reference specific data we want to modify when RNG response arrive. To do this we need store requestId in pendingRequest map.

Request and store RNG using custom callback.

Example on Sepolia

import "../Easyntropy/EasyntropyConsumer.sol";

contract PassPlayerMetadata is EasyntropyConsumer {
  //
  // support
  struct Gladiator {
    uint8 strength;
  }

  struct RNGRequest {
    uint64 gladiatorId;
  }

  mapping(uint64 gladiatorId => Gladiator gladiator) public gladiators;
  mapping(uint64 requestId => RNGRequest rngRequest) public pendingRequests;
  bytes32 public latestSeed;

  //
  // events & errors
  error NotEnoughEth();

  constructor(address _entropy) EasyntropyConsumer(_entropy) {
    gladiators[0] = Gladiator({ strength: 1 });
    gladiators[1] = Gladiator({ strength: 1 });
  }

  function startTrainingGladiator(uint64 gladiatorId) public payable {
    if (msg.value < easyntropyFee()) revert NotEnoughEth();

    uint64 requestId = easyntropyRequestWithCallback(this.trainGladiator.selector);
    pendingRequests[requestId] = RNGRequest({ gladiatorId: gladiatorId });
  }

  function trainGladiator(uint64 requestId, bytes32 seed) external onlyEasyntropy {
    uint256 randomNumber = uint256(seed);
    uint64 gladiatorId = pendingRequests[requestId].gladiatorId;

    gladiators[gladiatorId].strength = uint8(randomNumber & 0xFF);

    delete pendingRequests[requestId];
  }
}

Prepaying contract balance

We can prepay contract balance not to have to pay fees when requesting RNG.

Example on Sepolia

import "../Easyntropy/EasyntropyConsumer.sol";

contract Prepaying is EasyntropyConsumer {
  //
  // support
  bytes32 public latestSeed;

  constructor(address _entropy) EasyntropyConsumer(_entropy) {}

  function requestRandomValueWithoutPaying() public {
    //
    // calling entropy.requestWithCallback directly without any fee.
    // this is only possible if easyntropyDeposit{ value: ... }() has been called earlier.
    entropy.requestWithCallback();
  }

  function easyntropyFulfill(uint64, bytes32 seed) external onlyEasyntropy {
    latestSeed = seed;
  }
}

Widthrawing prepaid funds from oracle

You can always widthraw prepaid funds. To do so implment a function wrapping easyntropyWithdraw(uint256 amount).

Make sure to implement:

  • proper permission rules
  • receive funciton allowing receiving funds

Example on Sepolia

import "../Easyntropy/EasyntropyConsumer.sol";

contract Widthrawing is EasyntropyConsumer {
  constructor(address _entropy) EasyntropyConsumer(_entropy) {}

  function withdrawFromOracle(uint256 amount) public {
    // add your own permission restrictions here...

    easyntropyWithdraw(amount);
  }

  receive() external payable {}
}
Prev
Documentation