# RNGSender

<figure><img src="https://725212399-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FzKptRbJd7Q2IePzThNWw%2Fuploads%2FKMUcmlpfRkm16hzUqeUc%2FRNG-2024-01-19-075633.png?alt=media&#x26;token=f706e10c-bc74-4f8d-92e6-46a118044c4c" alt=""><figcaption><p>The RNG process between Ethereum and Arbitrum</p></figcaption></figure>

## Overview

The `RNGSender` contract is designed to be deployed on the `Arbitrum` network. It serves as a random number sender generated from the `Randomizer.ai`. The contract requests random numbers by providing a slug and paying a fee to `Randomizer.ai`. Once the random number is generated from `Randomizer.ai`, it can be sent to Obelisk on another chain using the `LayerZero` protocol via the `RNGSender`. All these processes are fundamentally automated, and in case of any issues preventing automatic execution, the `owner` has the ability to manually intervene and resolve them.

## Random Number Generation

Random numbers are generated by [Randomizer](https://randomizer.ai/docs), a decentralized and on-chain RNG protocol. It requires ETH to be settled for random numbers to be generated and it's been deployed  Arbitrum mainnet only so far. That's the reasoning behind RNGSender being deployed to Arbitrum.

If `RNGSender.request()` was called with proper arguments, after a period of confirmations, `RNGSender.randomizerCallback()` is triggered by Randomizer protocol with the number generated included as a parameter. The number has a range from 0 to `type(uint256).max`.

Actually `RNGSender.request()` is called automatically when a LayerZero message arrives from [Obelisk](https://lz-asia.gitbook.io/obelisk/development/v0-contracts/obelisk) contract on another chain. Only when that fails, `owner` can call it manually with proper arguments.

## Randomizer Fee

To use Randomizer's RNG, a certain amount of ETH fee needs to be paid and that amount is transferred from RNGSender contract. The value is calculated via `RNGSender.estimateRandomizerFee().`

Since the value is paid from RNGSender contract, ETH amount needs to have been stored in the contract, it was transferred from [Obelisk](https://lz-asia.gitbook.io/obelisk/development/v0-contracts/obelisk) or it can be paid when calling `RNGSender.request()`.&#x20;

For more info, refer to <https://randomizer.ai/docs>.

## LayerZero Messaging

After a random number is generated, it needs to be transferred back to [Obelisk](https://lz-asia.gitbook.io/obelisk/development/v0-contracts/obelisk) contract automatically or by calling `RNGSender.sendValue()`.

Automatic transfer meaning, inside `RNGSender.randomizerCallback()`, it calls `RNGSender.sendValue()` as an external call automatically but it could fail due to various reasons (ex. insufficient gas, relaying issues, etc). In that case, `owner` can call `sendValue()` with proper arguments in a right timing.

## LayerZero Fee

Just like how Randomizer works, to use LayerZero messaging, a certain amount of ETH fee needs to be paid and that amount is transferred from RNGSender contract. The value is calculated via `RNGSender.estimateLayerZeroFee()`.

Since the value is paid from RNGSender contract, ETH amount needs to have been stored in the contract, it was transferred from [Obelisk](https://lz-asia.gitbook.io/obelisk/development/v0-contracts/obelisk) or it can be paid when calling `RNGSender.sendValue()`.&#x20;

For more info,refer to <https://layerzero.gitbook.io/docs/evm-guides/code-examples/estimating-message-fees>
