Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contracts helper #1

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions contracts/AbstractRandomnessReceiver.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

import {IRandomnessReceiver} from "./IRandomnessReceiver.sol";

abstract contract AbstractRandomnessReceiver {
address randomnessRequester;

error NotAuthorizedRandomnessProvider();

modifier onlyRandomnessProvider(){
if (msg.sender != randomnessRequester) revert NotAuthorizedRandomnessProvider();
_;
}

constructor(address _randomnessRequester) public {
randomnessRequester = _randomnessRequester;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ pragma solidity 0.8.24;

import "./TypesLib.sol";

interface IRandomnessRequester {
interface IRandomnessProvider {
/**
* @notice Requests the generation of a random value for a specified blockchain height.
* @dev Initiates a randomness request.
* The generated randomness will be associated with the returned `requestID`.
* @return requestID The unique identifier assigned to this randomness request.
* The generated randomness will be associated with the returned `requestId`.
* @return requestId The unique identifier assigned to this randomness request.
*/
function requestRandomness() external returns (uint256 requestID);
function requestRandomness() external returns (uint256 requestId);

/**
* @notice Retrieves a specific request by its ID.
Expand Down
4 changes: 2 additions & 2 deletions contracts/IRandomnessReceiver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ interface IRandomnessReceiver {
* @notice Receives a random value associated with a specific request.
* @dev This function is called to provide the randomness generated for a given request ID.
* It is intended to be called by a trusted source that provides randomness.
* @param requestID The unique identifier of the randomness request.
* @param requestId The unique identifier of the randomness request.
* @param randomness The generated random value, provided as a `bytes32` type.
*/
function receiveRandomness(uint256 requestID, bytes32 randomness) external;
function receiveRandomness(uint256 requestId, bytes32 randomness) external;
}
2 changes: 1 addition & 1 deletion contracts/TypesLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ library TypesLib {

// RandomnessRequest stores details needed to verify the signature
struct RandomnessRequest {
uint256 requestID;
uint256 requestId;
address callback;
}

Expand Down
42 changes: 42 additions & 0 deletions contracts/helper/MockRandomnessRequester.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import "../IRandomnessReceiver.sol";
import "../IRandomnessProvider.sol";

contract MockRandomnessRequester is IRandomnessProvider {
mapping(uint256 => address) requestIdToReceiver;
uint256 public lastRequestId;
uint256[] requests;
mapping(address => uint256[]) public receiverToRequestIds;

function requestRandomness() external returns (uint256) {
lastRequestId++;
requestIdToReceiver[lastRequestId] = msg.sender;
requests.push(lastRequestId);
return lastRequestId;
}

function getRequest(uint256 requestId) external view returns (TypesLib.RandomnessRequest memory){
return TypesLib.RandomnessRequest(requestId, requestIdToReceiver[requestId]);
}

function getAllRequests() external view returns (TypesLib.RandomnessRequest[] memory){
TypesLib.RandomnessRequest[] memory allRequests = new TypesLib.RandomnessRequest[](15);
for(uint i; i<15; i++){
address callBack = requestIdToReceiver[requests[i]];
allRequests[i] = TypesLib.RandomnessRequest(requests[i], callBack);
}
return allRequests;
}

function messageFrom(TypesLib.RandomnessRequest memory r) external pure returns (bytes memory){
return bytes("");
}

function answerRequest(uint256 requestId, bytes32 randomness) external {
address callback = requestIdToReceiver[requestId];
IRandomnessReceiver receiver = IRandomnessReceiver(callback);
receiver.receiveRandomness(requestId, randomness);
}
}