Skip to content

Commit

Permalink
feat: Invariant helpers and mockVRF (#26)
Browse files Browse the repository at this point in the history
* feat: Invariant helpers and mockVRF

* fix: Lockfile

* fix: Update prettier command

---------

Co-authored-by: Hiroshi <[email protected]>
  • Loading branch information
0xBunta and 0xhiroshi authored Nov 10, 2023
1 parent caa7bdb commit f013af5
Show file tree
Hide file tree
Showing 4 changed files with 1,269 additions and 430 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
"scripts": {
"compile": "hardhat compile",
"compile:force": "hardhat compile --force",
"format:check": "prettier --check '**/*.{js,jsx,ts,tsx,sol,json,yaml,md}'",
"format:write": "prettier --write '**/*.{js,jsx,ts,tsx,json,yaml,md}'",
"format:check": "npx prettier --check '**/*.{js,jsx,ts,tsx,sol,json,yaml,md}'",
"format:write": "npx prettier --write '**/*.{js,jsx,ts,tsx,sol,json,yaml,md}'",
"lint": "eslint '**/*.{js,jsx,ts,tsx}'",
"prepare": "husky install",
"test": "hardhat test",
Expand Down Expand Up @@ -67,6 +67,7 @@
"typescript": "^4.5.2"
},
"dependencies": {
"@chainlink/contracts": "^0.6.1",
"@looksrare/contracts-libs": "^3.1.0"
}
}
40 changes: 40 additions & 0 deletions test/foundry/InvariantTestHelpers.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;

import {CommonBase} from "forge-std/Base.sol";
import {StdCheats} from "forge-std/StdCheats.sol";
import {StdUtils} from "forge-std/StdUtils.sol";

contract InvariantTestHelpers is CommonBase, StdCheats, StdUtils {
address[100] internal actors;
address internal currentActor;
bool internal callsMustBeValid;
mapping(bytes => uint256) internal calls;

constructor() {
_populateActors();
_setCallsMustBeValid();
}

modifier useActor(uint256 actorIndexSeed) {
currentActor = actors[bound(actorIndexSeed, 0, 99)];
vm.startPrank(currentActor);
_;
vm.stopPrank();
}

modifier countCall(bytes memory key) {
calls[key]++;
_;
}

function _populateActors() internal {
for (uint256 i; i < 100; i++) {
actors[i] = address(uint160(uint256(keccak256(abi.encodePacked(i)))));
}
}

function _setCallsMustBeValid() internal {
callsMustBeValid = vm.envBool("FOUNDRY_INVARIANT_FAIL_ON_REVERT");
}
}
45 changes: 45 additions & 0 deletions test/mock/MockVRFCoordinatorV2.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;

import {VRFConsumerBaseV2} from "@chainlink/contracts/src/v0.8/VRFConsumerBaseV2.sol";

contract MockVRFCoordinatorV2 {
VRFConsumerBaseV2 public vrfConsumerBaseV2;
uint256 public requestCount;
uint256[] public requestIds;

function setVRFConsumerBaseV2(address _vrfConsumerBaseV2) external {
vrfConsumerBaseV2 = VRFConsumerBaseV2(_vrfConsumerBaseV2);
}

function requestRandomWords(
bytes32 keyHash,
uint64 subId,
uint16 requestConfirmations,
uint32 callbackGasLimit,
uint32 numWords
) external returns (uint256 requestId) {
requestId = uint256(
keccak256(abi.encodePacked(keyHash, subId, requestConfirmations, callbackGasLimit, numWords, requestCount))
);
requestIds.push(requestId);
requestCount++;
}

function fulfillRandomWords(uint256 randomWord) external returns (uint256 requestId) {
uint256 requestsInQueue = requestIds.length;
if (requestsInQueue == 0) {
return 0;
}

requestId = requestIds[requestsInQueue - 1];
if (requestId == 0) return requestId;

uint256[] memory randomWords = new uint256[](1);
randomWords[0] = randomWord;

vrfConsumerBaseV2.rawFulfillRandomWords(requestId, randomWords);

requestIds.pop();
}
}
Loading

0 comments on commit f013af5

Please sign in to comment.