Skip to content

Commit 69f0f6f

Browse files
committed
feat(ChainlinkRNG.sol): add Kleros Liquid as owner
1 parent ae389c8 commit 69f0f6f

File tree

1 file changed

+54
-24
lines changed

1 file changed

+54
-24
lines changed

contracts/standard/rng/ChainlinkRNG.sol

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,14 @@ pragma solidity ^0.6.6;
1010
import "@chainlink/contracts/src/v0.6/VRFConsumerBase.sol";
1111
import "./RNG.sol";
1212

13+
14+
interface IKlerosLiquid {
15+
function passPhase() external {}
16+
}
17+
18+
1319
/**
14-
* @title Random Number Generator using Chainlink Verifiable Randomness Mechanism
20+
* @title Random Number Generator using Chainlink Verifiable Randomness Mechanism on Polygon
1521
* @author Merlin Egalite - <egalite.merlin@gmail.com>
1622
*
1723
* @dev This contract implements the RNG standard and inherits from VRFConsumerBase to use Chainlink Verifiable Randomness Mechanism.
@@ -24,49 +30,70 @@ contract ChainlinkRNG is RNG, VRFConsumerBase {
2430

2531
/* Storage */
2632

27-
bytes32 internal keyHash; // The key hash to use for Chainlink's VRFCoordinator.
28-
uint256 internal fee; // The fee to pay to Chainlink.
29-
address public owner; // The owner of the contract.
33+
IKlerosLiquid public kleros; // The address of Kleros Liquid.
34+
bytes32 internal keyHash; // The key hash for the VRF Coordinator.
35+
uint256 internal fee; // The amount of LINK to send with a request.
36+
mapping(bytes32 => uint256) public randomNumber; // randomNumber[requestId] is the random number for the requestId, 0 otherwise.
3037

31-
mapping (bytes32 => uint256) public randomNumber; // randomNumber[requestId] is the random number for the requestId, 0 otherwise.
38+
/* Modifier */
3239

33-
modifier onlyOwner() {
34-
require(msg.sender == owner, "Not the owner");
40+
modifier onlyByKleros() {
41+
require(msg.sender == kleros, "ChainlinkRNG: not called by Kleros");
42+
_;
3543
}
3644

3745
/* Constructor */
3846

3947
/**
4048
* @dev Constructs the ChainlinkRNG contract.
41-
* @param _vrfCoordinator Address of VRFCoordinator contract.
42-
* @param _link Address of LINK token contract.
43-
* @param _keyHash The _keyHash for the VRF Coordinator.
49+
* @param _vrfCoordinator The address of VRFCoordinator contract.
50+
* @param _link The address of LINK token contract.
51+
* @param _kleros The address of Kleros Liquid's contract.
52+
* @param _keyHash The key hash for the VRF Coordinator.
4453
* @param _fee The amount of LINK to send with a request.
4554
*
4655
* @dev https://docs.chain.link/docs/link-token-contracts
4756
*/
48-
constructor(address _vrfCoordinator, address _link, bytes32 _keyHash, uint256 _fee)
49-
VRFConsumerBase(_vrfCoordinator, _link) public
57+
constructor(
58+
address _vrfCoordinator,
59+
address _link,
60+
IKlerosLiquid _kleros,
61+
bytes32 _keyHash,
62+
uint256 _fee
63+
)
64+
VRFConsumerBase(_vrfCoordinator, _link)
65+
public
5066
{
5167
keyHash = _keyHash;
68+
kleros = _kleros;
5269
fee = _fee;
53-
owner = msg.sender;
5470
}
5571

72+
/* External */
73+
5674
/**
5775
* @dev Withdraws all LINK tokens locked in this contract.
5876
*/
59-
function withdrawLink() external onlyOwner {
60-
require(LINK.transfer(msg.sender, LINK.balanceOf(address(this))), "Unable to transfer");
77+
function withdrawLink() external onlyByKleros {
78+
require(LINK.transfer(msg.sender, LINK.balanceOf(address(this))), "ChainlinkRNG: unable to transfer LINK tokens");
6179
}
6280

6381
/**
64-
* @dev Changes the fee used when requesting a new random number.
82+
* @dev Changes the `fee` storage variable.
83+
* @param _newFee The new value for the `fee` storage variable.
6584
*/
66-
function changeFee(uint256 _newFee) external onlyOwner {
85+
function changeFee(uint256 _newFee) external onlyByKleros {
6786
fee = _newFee;
6887
}
6988

89+
/**
90+
* @dev Changes the `kleros` storage variable.
91+
* @param _newKleros The new value for the `kleros` storage variable.
92+
*/
93+
function changeKleros(IKlerosLiquid _newKleros) external onlyByKleros {
94+
kleros = _newKleros;
95+
}
96+
7097
/**
7198
* @dev Requests a random number.
7299
* @dev The _seed parameter is vestigial, and is kept only for API
@@ -77,27 +104,30 @@ contract ChainlinkRNG is RNG, VRFConsumerBase {
77104
* @param _seed seed mixed into the input of the VRF.
78105
* @return requestId unique ID for this request.
79106
*/
80-
function requestRN(uint _seed) public returns (uint256 requestId) {
81-
require(LINK.balanceOf(address(this)) >= fee, "Not enough LINK to pay the fee");
107+
function requestRN(uint _seed) external onlyByKleros returns (bytes32 requestId) {
108+
require(LINK.balanceOf(address(this)) >= fee, "ChainlinkRNG: not enough LINK to pay the fee");
82109
return requestRandomness(keyHash, fee, _seed);
83110
}
84111

85112
/**
86-
* @dev Returns the random number associatd to a request Id.
87-
* @param _requestId The Id initially returned by requestRN.
113+
* @dev Gets the random number associated to a `_requestId`.
114+
* @param _requestId The request Id initially returned by requestRN.
88115
* @return RN Random Number. If the number is not ready or has not been required it returns 0.
89116
*/
90-
function getRN(bytes32 _requestId) public returns (uint256 RN) {
117+
function getRN(bytes32 _requestId) external returns (bytes32 RN) {
91118
return randomNumber[_requestId];
92119
}
93120

121+
/* Internal */
122+
94123
/**
95-
* @dev Stores the random number given by the VRF Coordinator.
124+
* @dev Stores the random number given by the VRF Coordinator and calls passPhase function on Kleros Liquid.
96125
* @dev This is the callback function used by the VRF Coordinator.
97-
* @param _requestId The Id initially returned by requestRN.
126+
* @param _requestId The request Id initially returned by requestRN.
98127
* @param _randomness the VRF output.
99128
*/
100129
function fulfillRandomness(bytes32 _requestId, uint256 _randomness) internal override {
101130
randomNumber[_requestId] = _randomness;
131+
IKlerosLiquid(kleros).passPhase();
102132
}
103133
}

0 commit comments

Comments
 (0)