-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modify L2Endpoint contract for VRF/RR (#910)
* Created requestRandomWords function for VRF on L2 Endpoint * Created L2VRFConsumerMock smc, added full fil function on L2Enpoint * test case request fulfill for L2 VRFConsumerMock * checked submmiter for fulfillRandomword function * Resolved some comments * fixed test L2 Endpoint
- Loading branch information
1 parent
23117a8
commit ee5c8e4
Showing
5 changed files
with
273 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.16; | ||
|
||
interface IL2Endpoint { | ||
function requestRandomWords( | ||
bytes32 keyHash, | ||
uint64 accId, | ||
uint32 callbackGasLimit, | ||
uint32 numWords | ||
) external returns (uint256); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.16; | ||
|
||
import "../VRFConsumerBase.sol"; | ||
import "../interfaces/IL2Endpoint.sol"; | ||
|
||
contract L2VRFConsumerMock is VRFConsumerBase { | ||
uint256 public sRandomWord; | ||
address private sOwner; | ||
|
||
IL2Endpoint L2ENDPOINT; | ||
|
||
error OnlyOwner(address notOwner); | ||
|
||
modifier onlyOwner() { | ||
if (msg.sender != sOwner) { | ||
revert OnlyOwner(msg.sender); | ||
} | ||
_; | ||
} | ||
|
||
constructor(address l2Endpoint) VRFConsumerBase(l2Endpoint) { | ||
sOwner = msg.sender; | ||
L2ENDPOINT = IL2Endpoint(l2Endpoint); | ||
} | ||
|
||
// Receive remaining payment from requestRandomWordsPayment | ||
receive() external payable {} | ||
|
||
function requestRandomWords( | ||
bytes32 keyHash, | ||
uint64 accId, | ||
uint32 callbackGasLimit, | ||
uint32 numWords | ||
) public onlyOwner returns (uint256 requestId) { | ||
requestId = L2ENDPOINT.requestRandomWords(keyHash, accId, callbackGasLimit, numWords); | ||
} | ||
|
||
function fulfillRandomWords( | ||
uint256 /* requestId */, | ||
uint256[] memory randomWords | ||
) internal override { | ||
sRandomWord = (randomWords[0] % 50) + 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
const { expect } = require('chai') | ||
const { ethers } = require('hardhat') | ||
const { time, loadFixture } = require('@nomicfoundation/hardhat-network-helpers') | ||
|
||
const { createSigners } = require('../utils.cjs') | ||
const { vrfConfig } = require('./VRFCoordinator.config.cjs') | ||
|
||
async function deploy() { | ||
const { | ||
account0: deployerSigner, | ||
account1: consumerSigner, | ||
account2, | ||
account3, | ||
account4, | ||
account5 | ||
} = await createSigners() | ||
|
||
// L2 endpoint | ||
let l2EndpointContract = await ethers.getContractFactory('L2Endpoint', { signer: deployerSigner }) | ||
l2EndpointContract = await l2EndpointContract.deploy() | ||
await l2EndpointContract.deployed() | ||
|
||
const endpoint = { | ||
contract: l2EndpointContract, | ||
signer: deployerSigner | ||
} | ||
|
||
// L2 consumer | ||
let l2VRFConsumerMock = await ethers.getContractFactory('L2VRFConsumerMock', { | ||
signer: deployerSigner | ||
}) | ||
l2VRFConsumerMock = await l2VRFConsumerMock.deploy(l2EndpointContract.address) | ||
await l2VRFConsumerMock.deployed() | ||
|
||
const consumer = { | ||
contract: l2VRFConsumerMock, | ||
signer: deployerSigner | ||
} | ||
|
||
return { | ||
endpoint, | ||
consumer, | ||
account2, | ||
account3, | ||
account4, | ||
account5 | ||
} | ||
} | ||
|
||
describe('Consumer', function () { | ||
it('Request and fullfil', async function () { | ||
const { consumer, endpoint } = await loadFixture(deploy) | ||
const { maxGasLimit: callbackGasLimit, keyHash } = vrfConfig() | ||
const accMock = 1 | ||
const numWords = 1 | ||
const txRequestRandomWords = await ( | ||
await consumer.contract.requestRandomWords(keyHash, accMock, callbackGasLimit, numWords) | ||
).wait() | ||
const event = endpoint.contract.interface.parseLog(txRequestRandomWords.events[0]) | ||
expect(event.name).to.be.equal('RandomWordsRequested') | ||
const { requestId } = event.args | ||
|
||
const randomWords = [1] | ||
await expect( | ||
endpoint.contract.fulfillRandomWords(requestId, randomWords) | ||
).revertedWithCustomError(endpoint.contract, 'InvalidSubmitter') | ||
|
||
await (await endpoint.contract.addSubmitter(endpoint.signer.address)).wait() | ||
await (await endpoint.contract.fulfillRandomWords(requestId, randomWords)).wait() | ||
const result = await consumer.contract.sRandomWord() | ||
expect(result).to.be.equal((randomWords % 50) + 1) | ||
}) | ||
}) |