Implementing in your contract
It is very easy to implement arrng in your contract. Just follow these steps.
Install the arrng package from NPM
$ npm install @arrng/contracts
Import the arrng consumer contract
Place the following at the top of your contract in the imports section:
import {ArrngConsumer} from @arrng/contracts/ArrngConsumer.sol;
Declare your contract as a consumer
Your contract needs to declare that it is an ArrngConsumer, for example:
contract YourContract isArrngConsumer {
<your amazing contract here>
}
Pass the controller address on the constructor
You need to pass in the arrng controller address on your constructor call, and pass that to the consumer contract you have imported. First, lookup the controller contract on your chain.
Pass that into your constructor, and on to the consumer as follows:
constructor(address arrngController_) ArrngConsumer(arrngController_) {
<anything else you need in your constructor>
}
Request RNG
You need a payable method that you can call to request the RNG, passing in native token for gas and the fee. See getting a cost estimate for how much to pass.
To make a request you then simply need to call requestRandomWords
on the controller, passing the native token:
arrngController.requestRandomWords {value: msg.value} (1);
Receive RNG
Finally, you need to do something with the RNG that will be passed back. To do this, override the internal function fulfillRandomness
as follows:
function fulfillRandomWords(
uint256 id, uint256[] memory numbers
) internal override {
< do something with numbers[0] >
}
Example Contract
I love examples!
Last updated