In Part 1 we’ve showed you how to bootstrap a simple web app that uses our random beacon’s alpha API contract. In this post we’ll try to give you a more detailed guide how to communicate with the contract and an overview of its methods defined by our alpha API.

KeepRandomBeacon.sol

This contract is the interface to our “threshold relay”, a secure mechanism based on a threshold signature scheme, and it allows us to randomly select providers for a new keep. The threshold relay is a fairly complex subject and deserves a separate post, but in the meantime you can check out section 6.2.2 of our whitepaper if you want to learn more.

Our demo case only requires one method and its corresponding event. Here is the example of the requestRelayEntry method that we will call:

We don’t have to worry about blockReward and seed params for now as these are catered for Keep network advanced mechanics. The callback contract params are also optional, so our process to get a random number falls down just to these few steps:

Call requestRelayEntry method on the Random Beacon contract. Receive back requestId and keep it in your application state. Listen for the RelayEntryGenerated event on the Random Beacon contract. Once you catch the event you can read the random generated value in requestResponse from the event args. The args also include requestId and we assume you compare it with the one you’ve received in the earlier step to make sure you get response for your request and not someone else’s.

Please see the example from the random avatar app for steps 1-2:

For steps 3–4 please refer to the snippet below; it demonstrates how you can watch the RelayEntryGenerated event, check the requestId , and get your random number:

Callback contract

In case you want to use a callback contract to receive the result you, can provide the contract address and the method signature represented as a string like in the example below:

Here’s an example of a callback contract:

Deploying

All of the code above can be easily deployed via “Truffle Box”. Please make sure you have Truffle installed and then run:

truffle unbox keep-network/random-beacon-box

Once you’ve run truffle unbox, you’ll see the screen with the initial set of truffle commands that should help you to bootstrap your application. Please refer to this great Truffle tutorial for more information.

We hope our post helps shed more light into how our Random Beacon works, what kind of contract methods will be available in our public API on mainnet and how you can use it in your web apps. If you have any questions, leave them below and we’ll be happy to answer them; happy building! :)