Skip to content

Commit

Permalink
Updated to Witnet 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
asselstine committed Mar 25, 2024
1 parent 24dc7b5 commit 64693c5
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 18 deletions.
1 change: 1 addition & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ env:

jobs:
forge:
environment: RPC
strategy:
fail-fast: true
permissions:
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# PoolTogether RNG Witnet 0.7 integration

This is an RNG interface for Witnet 0.7
This is an RNG interface for Witnet 2.0

For Witnet 0.7 see the `witnet-0.7` branch.

## Development

Expand Down
2 changes: 1 addition & 1 deletion lib/witnet-solidity-bridge
6 changes: 4 additions & 2 deletions src/Requestor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ contract Requestor {
/// @dev You can send Ether along with this call
/// @param value The amount of Ether to send to the Witnet Randomness Oracle
/// @param _witnetRandomness The Witnet Randomness Oracle contract
function randomize(uint value, IWitnetRandomness _witnetRandomness) external payable onlyCreator {
_witnetRandomness.randomize{ value: value }();
/// @return The actual value used by the Randomness Oracle
function randomize(uint value, IWitnetRandomness _witnetRandomness) external payable onlyCreator returns (uint256) {
uint cost = _witnetRandomness.randomize{ value: value }();
return cost;
}

/// @notice Withdraws the balance of the contract to the specified address
Expand Down
15 changes: 8 additions & 7 deletions src/RngWitnet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ contract RngWitnet is IRng {
/// @param sender The address that requested the random number
event RandomNumberRequested(
uint32 indexed requestId,
address indexed sender
address indexed sender,
uint256 cost
);

/// @notice The Witnet Randomness contract
Expand Down Expand Up @@ -74,16 +75,16 @@ contract RngWitnet is IRng {
/// @param rngPaymentAmount The amount of Ether to send to the Witnet Randomness Oracle. This amount should be sent in this call, remaining from a previous call, or a combination thereof. The Requestor holds the current balance.
/// @return requestId The id of the request
/// @return lockBlock The block number at which the request was made
function requestRandomNumber(uint256 rngPaymentAmount) public payable returns (uint32 requestId, uint256 lockBlock) {
function requestRandomNumber(uint256 rngPaymentAmount) public payable returns (uint32 requestId, uint256 lockBlock, uint256 cost) {
Requestor requestor = getRequestor(msg.sender);
unchecked {
requestId = ++lastRequestId;
lockBlock = block.number;
}
requests[requestId] = lockBlock;
requestor.randomize{value: msg.value}(rngPaymentAmount, witnetRandomness);
cost = requestor.randomize{value: msg.value}(rngPaymentAmount, witnetRandomness);

emit RandomNumberRequested(requestId, msg.sender);
emit RandomNumberRequested(requestId, msg.sender, cost);
}

/// @notice Withdraws the balance of the Requestor contract of the caller
Expand All @@ -105,7 +106,7 @@ contract RngWitnet is IRng {
/// @param requestId The ID of the request used to get the results of the RNG service
/// @return randomNum The random number
function randomNumber(uint32 requestId) external view returns (uint256 randomNum) {
return uint256(witnetRandomness.getRandomnessAfter(requests[requestId]));
return uint256(witnetRandomness.fetchRandomnessAfter(requests[requestId]));
}

/// @notice Starts a draw using the random number from the Witnet Randomness Oracle
Expand All @@ -114,7 +115,7 @@ contract RngWitnet is IRng {
/// @param _rewardRecipient The address of the reward recipient
/// @return The id of the draw
function startDraw(uint256 rngPaymentAmount, DrawManager _drawManager, address _rewardRecipient) external payable returns (uint24) {
(uint32 requestId,) = requestRandomNumber(rngPaymentAmount);
(uint32 requestId,,) = requestRandomNumber(rngPaymentAmount);
return _drawManager.startDraw(_rewardRecipient, requestId);
}
}
}
3 changes: 2 additions & 1 deletion test/RngWitnet.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ contract RngWitnetTest is Test {

function testRequestRandomNumber() external {
vm.mockCall(address(witnetRandomness), 1e18, abi.encodeWithSelector(IWitnetRandomness.randomize.selector), abi.encode(0.5e18));
(uint32 requestId, uint256 lockBlock) = rngWitnet.requestRandomNumber{value: 1e18}(1e18);
(uint32 requestId, uint256 lockBlock, uint256 cost) = rngWitnet.requestRandomNumber{value: 1e18}(1e18);
assertEq(requestId, 1);
assertEq(lockBlock, block.number);
assertEq(cost, 0.5e18);
assertEq(address(rngWitnet.getRequestor(address(this))).balance, 1e18, "witnet balance should be 1e18");
}

Expand Down
14 changes: 8 additions & 6 deletions test/fork/RngWitnetFork.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@ contract RngWitnetForkTest is Test {
uint256 fork;

function setUp() public {
fork = vm.createFork("sepolia");
fork = vm.createFork("optimism-sepolia");
vm.selectFork(fork);
witnetRandomness = IWitnetRandomness(0xa579563E3ee6884e3b4cdE1BBb2fF6305686A83f);
witnetRandomness = IWitnetRandomness(0xc0ffee84FD3B533C3fA408c993F59828395319A1);
rngWitnet = new RngWitnet(witnetRandomness);
vm.deal(address(this), 1000e18);
}

function testRequestRandomNumberFromFork() external {
(uint32 requestId, uint256 lockBlock) = rngWitnet.requestRandomNumber{value: 1e18}(1e18);
assertEq(requestId, 1);
assertEq(lockBlock, block.number);
uint fee = 0.00001e18;
(uint32 requestId, uint256 lockBlock, uint256 cost) = rngWitnet.requestRandomNumber{value: fee}(fee);
assertEq(requestId, 1, "request id");
assertEq(lockBlock, block.number, "block number");
assertGt(cost, 0, "cost");
}

receive() external payable {}
}
}

0 comments on commit 64693c5

Please sign in to comment.