Fixed InsufficientBalance VRF Mock
Some checks are pending
CI / Foundry project (push) Waiting to run

This commit is contained in:
hoelee 2024-08-16 04:06:04 +08:00
parent 12a67132f2
commit 4ece5acece
7 changed files with 111 additions and 24 deletions

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1 @@
f311b970b3640cfaa219b4d9f972721659306cb01e0d67e80103b81e397c36aa

View File

@ -0,0 +1,30 @@
<?xml version="1.0"?>
<doc>
<assembly>
<name>Meadow.DebugSol.Generated</name>
</assembly>
<members>
<member name="T:Meadow.DebugSol.Generated.DbgEntry">
<summary>From file Test1.t.sol<para/></summary>
</member>
<member name="M:Meadow.DebugSol.Generated.DbgEntry.Deploy(Meadow.JsonRpc.Client.IJsonRpcClient,Meadow.JsonRpc.Types.TransactionParams,System.Nullable{Meadow.Core.EthTypes.Address})">
<summary>
Deploys the contract. <para/>
</summary>
<param name="rpcClient">The RPC client to be used for this contract instance.</param>
<param name="defaultFromAccount">If null then the first account returned by eth_accounts will be used.</param>
<returns>An contract instance pointed at the deployed contract address.</returns>
</member>
<member name="M:Meadow.DebugSol.Generated.DbgEntry.New(Meadow.JsonRpc.Client.IJsonRpcClient,Meadow.JsonRpc.Types.TransactionParams,System.Nullable{Meadow.Core.EthTypes.Address})">
<summary>
Deploys the contract. <para/>
</summary>
<param name="rpcClient">The RPC client to be used for this contract instance.</param>
<param name="defaultFromAccount">If null then the first account returned by eth_accounts will be used.</param>
<returns>An contract instance pointed at the deployed contract address.</returns>
</member>
<member name="P:Meadow.DebugSol.Generated.DbgEntry.FallbackFunction">
<summary>The contract fallback function. <para/></summary>
</member>
</members>
</doc>

View File

@ -1,5 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity >= 0.4.21 < 0.9.0;
pragma solidity >=0.4.21 <0.9.0;
import {DeployRaffle} from "script/DeployRaffle.s.sol";
contract DbgEntry {
event EvmPrint(string);
@ -14,13 +16,14 @@ contract DbgEntry {
// or interact with an existing deployment by specifying a `fork` url in `dbg.project.json`
// eg:
// ICounter counter = ICounter(0x12345678.....)
// counter.increment();
// counter.increment();
//
// If you have correct symbols (`artifacts`) for the deployed contract, you can step-into calls.
uint256 abc = 123;
uint256 def = abc + 5;
DeployRaffle deployRaffle = new DeployRaffle();
emit EvmPrint("DbgEntry return");
}
}
}

View File

@ -44,7 +44,7 @@ contract CreateSubscription is Script {
forge script script/Interactions.s.sol:FundSubscription --rpc-url $SEPOLIA_RPC_URL --broadcast --myAccount hoelee
*/
contract FundSubscription is CodeConstants, Script {
uint96 public constant FUND_AMOUNT = 0.5 ether;
uint96 public constant FUND_AMOUNT = 3 ether;
function fundSubscriptionUsingConfig() public {
HelperConfig helperConfig = new HelperConfig();

View File

@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
import {Test, console2} from "forge-std/Test.sol";
import {Test, console2, console} from "forge-std/Test.sol";
import {DeployRaffle} from "script/DeployRaffle.s.sol";
import {Raffle} from "contracts/Raffle.sol";
import {HelperConfig, CodeConstants} from "script/HelperConfig.s.sol";
@ -32,11 +32,14 @@ contract RaffleTest is Test, CodeConstants {
address public PLAYER = makeAddr("player");
uint256 public constant STARTING_USER_BALANCE = 100 ether;
uint256 public constant STARTING_PLAYER_BALANCE = 10 ether;
uint256 public constant LINK_BALANCE = 100 ether;
function setUp() public {
function setUp() external {
DeployRaffle deployer = new DeployRaffle();
(raffle, helperConfig) = deployer.run();
vm.deal(PLAYER, STARTING_USER_BALANCE);
HelperConfig.NetworkConfig memory config = helperConfig.getConfig();
vrfCoordinatorV2_5 = config.vrfCoordinatorV2_5;
subscriptionId = config.subscriptionId;
@ -44,8 +47,26 @@ contract RaffleTest is Test, CodeConstants {
interval = config.automationUpdateInterval;
entranceFee = config.raffleEntranceFee;
callbackGasLimit = config.callbackGasLimit;
link = LinkToken(config.link);
}
vm.deal(PLAYER, STARTING_USER_BALANCE);
modifier raffleEntered() {
vm.prank(PLAYER);
raffle.enterRaffle{value: entranceFee}();
vm.warp(block.timestamp + interval + 1);
vm.roll(block.number + 1);
_;
}
modifier onlyOnDeployedContracts() {
if (block.chainid == 31337) {
return;
}
try vm.activeFork() returns (uint256) {
return;
} catch {
_;
}
}
function testRaffleInitializesInOpenState() public view {
@ -233,24 +254,10 @@ contract RaffleTest is Test, CodeConstants {
/*//////////////////////////////////////////////////////////////
FULFILLRANDOMWORDS
//////////////////////////////////////////////////////////////*/
modifier raffleEntered() {
vm.prank(PLAYER);
raffle.enterRaffle{value: entranceFee}();
vm.warp(block.timestamp + interval + 1);
vm.roll(block.number + 1);
_;
}
modifier skipFork() {
if (block.chainid != 31337) {
return;
}
_;
}
function testFulfillRandomWordsCanOnlyBeCalledAfterPerformUpkeep(
uint256 randomRequestId
) public raffleEntered skipFork {
) public raffleEntered onlyOnDeployedContracts {
// Arrange
// Act / Assert
vm.expectRevert(VRFCoordinatorV2_5Mock.InvalidRequest.selector);
@ -264,7 +271,11 @@ contract RaffleTest is Test, CodeConstants {
//..
}
function testFulfillRandomWordsPicksAWinnerResetsAndSendsMoney() public raffleEntered skipFork {
function testFulfillRandomWordsPicksAWinnerResetsAndSendsMoney()
public
raffleEntered
onlyOnDeployedContracts
{
address expectedWinner = address(1);
// Arrange
@ -288,6 +299,7 @@ contract RaffleTest is Test, CodeConstants {
console2.logBytes32(entries[1].topics[1]);
bytes32 requestId = entries[1].topics[1]; // get the requestId from the logs
/*
FundSubscription fundSubscription = new FundSubscription();
fundSubscription.fundSubscription(
helperConfig.getConfig().vrfCoordinatorV2_5,
@ -295,7 +307,7 @@ contract RaffleTest is Test, CodeConstants {
helperConfig.getConfig().link,
helperConfig.getConfig().myAccount
);
*/
VRFCoordinatorV2_5Mock(vrfCoordinatorV2_5).fulfillRandomWords(
uint256(requestId),
address(raffle)
@ -313,4 +325,45 @@ contract RaffleTest is Test, CodeConstants {
assert(winnerBalance == startingBalance + prize);
assert(endingTimeStamp > startingTimeStamp);
}
function testHoelee() public raffleEntered onlyOnDeployedContracts {
address expectedWinner = address(1);
// Arrange
uint256 additionalEntrances = 3;
uint256 startingIndex = 1; // We have starting index be 1 so we can start with address(1) and not address(0)
for (uint256 i = startingIndex; i < startingIndex + additionalEntrances; i++) {
address player = address(uint160(i));
hoax(player, 1 ether); // deal 1 eth to the player
raffle.enterRaffle{value: entranceFee}();
}
uint256 startingTimeStamp = raffle.getLastTimeStamp();
uint256 startingBalance = expectedWinner.balance;
// Act
vm.recordLogs();
raffle.performUpkeep(""); // emits requestId
Vm.Log[] memory entries = vm.getRecordedLogs();
console2.logBytes32(entries[1].topics[1]);
bytes32 requestId = entries[1].topics[1]; // get the requestId from the logs
VRFCoordinatorV2_5Mock(vrfCoordinatorV2_5).fulfillRandomWords(
uint256(requestId),
address(raffle)
);
// Assert
address recentWinner = raffle.getRecentWinner();
Raffle.RaffleState raffleState = raffle.getRaffleState();
uint256 winnerBalance = recentWinner.balance;
uint256 endingTimeStamp = raffle.getLastTimeStamp();
uint256 prize = entranceFee * (additionalEntrances + 1);
assert(recentWinner == expectedWinner);
assert(uint256(raffleState) == 0);
assert(winnerBalance == startingBalance + prize);
assert(endingTimeStamp > startingTimeStamp);
}
}