Skip to content

Commit

Permalink
fix githubworkflow
Browse files Browse the repository at this point in the history
  • Loading branch information
WhatMe1on committed Dec 15, 2024
1 parent 04bd502 commit e353be4
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 209 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
"search.exclude": {
"/lib/**": true,
},
"commentTranslate.source": "Copilot",
}
54 changes: 25 additions & 29 deletions script/HelperConfig.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ pragma solidity ^0.8.18;

import {Script} from "forge-std/Script.sol";
import {Raffle} from "../src/Raffle.sol";
import {VRFCoordinatorV2_5Mock} from "chainlink-brownie-contracts/contracts/src/v0.8/vrf/mocks/VRFCoordinatorV2_5Mock.sol";
import {VRFCoordinatorV2_5Mock} from
"chainlink-brownie-contracts/contracts/src/v0.8/vrf/mocks/VRFCoordinatorV2_5Mock.sol";
import {LinkToken} from "../test/mock/LinkToken.sol";

abstract contract Constants {
Expand All @@ -12,7 +13,7 @@ abstract contract Constants {
int256 public MOCK_WEI_PER_UINT_LINK = 4e18;
}

contract HelperConfig is Script, Constants{
contract HelperConfig is Script, Constants {
struct NetworkConfig {
uint256 entranceFee;
uint256 interval;
Expand All @@ -22,6 +23,7 @@ contract HelperConfig is Script, Constants{
bytes32 keyHash;
address link;
}

NetworkConfig public activeNetworkConfig;

constructor() {
Expand All @@ -33,46 +35,40 @@ contract HelperConfig is Script, Constants{
}

function getSepoliaEthConfig() public pure returns (NetworkConfig memory) {
return
NetworkConfig({
entranceFee: 0.01 ether,
interval: 30,
vrfCoordinator: 0x9DdfaCa8183c41ad55329BdeeD9F6A8d53168B1B,
keyHash: 0x787d74caea10b2b357790d5b5247c2f63d1d91572a9846f780606e4d953677ae,
subscriptionId: 0, // Update with subId;
callbackGasLimit: 500000,
link: 0x779877A7B0D9E8603169DdbD7836e478b4624789
});
return NetworkConfig({
entranceFee: 0.01 ether,
interval: 30,
vrfCoordinator: 0x9DdfaCa8183c41ad55329BdeeD9F6A8d53168B1B,
keyHash: 0x787d74caea10b2b357790d5b5247c2f63d1d91572a9846f780606e4d953677ae,
subscriptionId: 0, // Update with subId;
callbackGasLimit: 500000,
link: 0x779877A7B0D9E8603169DdbD7836e478b4624789
});
}

function getOrCreateAnvilConfig() public returns (NetworkConfig memory) {
if (activeNetworkConfig.vrfCoordinator != address(0)) {
return activeNetworkConfig;
}


vm.startBroadcast();
VRFCoordinatorV2_5Mock coordinator = new VRFCoordinatorV2_5Mock(
MOCK_BASE_FEE,
MOCK_GAS_PRICE_LINK,
MOCK_WEI_PER_UINT_LINK
);
VRFCoordinatorV2_5Mock coordinator =
new VRFCoordinatorV2_5Mock(MOCK_BASE_FEE, MOCK_GAS_PRICE_LINK, MOCK_WEI_PER_UINT_LINK);
LinkToken link = new LinkToken();
vm.stopBroadcast();

return
NetworkConfig({
entranceFee: 0.01 ether,
interval: 30,
vrfCoordinator: address(coordinator),
keyHash: 0x787d74caea10b2b357790d5b5247c2f63d1d91572a9846f780606e4d953677ae,
subscriptionId: 0, // Update with subId;
callbackGasLimit: 500000,
link: address(link)
});
return NetworkConfig({
entranceFee: 0.01 ether,
interval: 30,
vrfCoordinator: address(coordinator),
keyHash: 0x787d74caea10b2b357790d5b5247c2f63d1d91572a9846f780606e4d953677ae,
subscriptionId: 0, // Update with subId;
callbackGasLimit: 500000,
link: address(link)
});
}

function getNetworkConfig() public view returns(NetworkConfig memory) {
function getNetworkConfig() public view returns (NetworkConfig memory) {
return activeNetworkConfig;
}
}
61 changes: 12 additions & 49 deletions script/Interaction.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,22 @@ pragma solidity ^0.8.18;
import {Script, console} from "forge-std/Script.sol";
import {Raffle} from "../src/Raffle.sol";
import {HelperConfig} from "../script/HelperConfig.s.sol";
import {VRFCoordinatorV2_5Mock} from "chainlink-brownie-contracts/contracts/src/v0.8/vrf/mocks/VRFCoordinatorV2_5Mock.sol";
import {VRFCoordinatorV2_5Mock} from
"chainlink-brownie-contracts/contracts/src/v0.8/vrf/mocks/VRFCoordinatorV2_5Mock.sol";
import {LinkToken} from "../test/mock/LinkToken.sol";
import {DevOpsTools} from "lib/foundry-devops/src/DevOpsTools.sol";

contract CreateSubscription is Script {
function createSubscriptionUsingConfig() public returns (uint256) {
HelperConfig helperConfig = new HelperConfig();
(, , address vrfCoordinator, , , , ) = helperConfig
.activeNetworkConfig();
(,, address vrfCoordinator,,,,) = helperConfig.activeNetworkConfig();
return createSubscription(vrfCoordinator);
}

function createSubscription(
address vrfCoordinator
) public returns (uint256 subscriptionId) {
function createSubscription(address vrfCoordinator) public returns (uint256 subscriptionId) {
console.log("Creating subscription on Chain:", block.chainid);
vm.startBroadcast();
subscriptionId = VRFCoordinatorV2_5Mock(vrfCoordinator)
.createSubscription();
subscriptionId = VRFCoordinatorV2_5Mock(vrfCoordinator).createSubscription();
console.log("Your subscriptionId: ", subscriptionId);
vm.stopBroadcast();
}
Expand All @@ -37,40 +34,21 @@ contract FundSubscription is Script {

function fundSubscriptionUsingConfig() public {
HelperConfig helperConfig = new HelperConfig();
(
,
,
address vrfCoordinator,
uint256 subscriptionId,
,
,
address link
) = helperConfig.activeNetworkConfig();
(,, address vrfCoordinator, uint256 subscriptionId,,, address link) = helperConfig.activeNetworkConfig();
fundSubscription(vrfCoordinator, subscriptionId, link);
}

function fundSubscription(
address vrfCoordinator,
uint256 subscriptionId,
address link
) public {
function fundSubscription(address vrfCoordinator, uint256 subscriptionId, address link) public {
console.log("Funding subscriptionId: ", subscriptionId);
console.log("using vrfCoordinator: ", vrfCoordinator);
console.log("On chainId: ", block.chainid);
if (block.chainid == 31337) {
vm.startBroadcast();
VRFCoordinatorV2_5Mock(vrfCoordinator).fundSubscription(
subscriptionId,
FUND_AMOUNT
);
VRFCoordinatorV2_5Mock(vrfCoordinator).fundSubscription(subscriptionId, FUND_AMOUNT);
vm.stopBroadcast();
} else {
vm.startBroadcast();
LinkToken(link).transferAndCall(
vrfCoordinator,
FUND_AMOUNT,
abi.encode(subscriptionId)
);
LinkToken(link).transferAndCall(vrfCoordinator, FUND_AMOUNT, abi.encode(subscriptionId));
vm.stopBroadcast();
}
}
Expand All @@ -82,32 +60,17 @@ contract FundSubscription is Script {

contract AddConsumer is Script {
function run() external {
address raffle = DevOpsTools.get_most_recent_deployment(
"Raffle",
block.chainid
);
address raffle = DevOpsTools.get_most_recent_deployment("Raffle", block.chainid);
addConsumerUsingConfig(raffle);
}

function addConsumerUsingConfig(address raffle) public {
HelperConfig helperConfig = new HelperConfig();
(
,
,
address vrfCoordinator,
uint256 subscriptionId,
,
,

) = helperConfig.activeNetworkConfig();
(,, address vrfCoordinator, uint256 subscriptionId,,,) = helperConfig.activeNetworkConfig();
addConsumers(raffle, vrfCoordinator, subscriptionId);
}

function addConsumers(
address raffle,
address vrfCoordinator,
uint256 subscriptionId
) public {
function addConsumers(address raffle, address vrfCoordinator, uint256 subscriptionId) public {
console.log("Adding consumer contract: ", raffle);
console.log("vrfCoordinator: ", vrfCoordinator);
console.log("subscriptionId: ", subscriptionId);
Expand Down
39 changes: 15 additions & 24 deletions src/Raffle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {CCEncoder} from "./tools/CCEncoder.sol";

contract Raffle is VRFConsumerBaseV2Plus, Script {
using CCEncoder for bool[];

uint16 private constant REQUEST_CONFIRMATIONS = 3;
uint32 private constant NUM_WORDS = 1;
uint256 private constant ROLL_IN_PROGRESS = 42;
Expand All @@ -31,11 +32,7 @@ contract Raffle is VRFConsumerBaseV2Plus, Script {
error Raffle__NotEnoughEtherSent();
error Raffle__WinnerWithdrawFailed();
error Raffle__RaffleStateNotOpen();
error Raffle__UpKeepNotFeed(
bytes upKeepFlags,
uint256 balance,
uint256 length
);
error Raffle__UpKeepNotFeed(bytes upKeepFlags, uint256 balance, uint256 length);

enum RaffleState {
OPEN,
Expand Down Expand Up @@ -78,9 +75,11 @@ contract Raffle is VRFConsumerBaseV2Plus, Script {
* 3.Contract has eth
* 4.Subscription is funded with Link
*/
function checkUpkeep(
bytes memory /* checkData */
) public view returns (bool upkeepNeeded, bytes memory /* performData */) {
function checkUpkeep(bytes memory /* checkData */ )
public
view
returns (bool upkeepNeeded, bytes memory /* performData */ )
{
bool[] memory flags = new bool[](4);
upkeepNeeded = true;

Expand All @@ -99,14 +98,10 @@ contract Raffle is VRFConsumerBaseV2Plus, Script {
return (upkeepNeeded, upKeepFlags);
}

function performUpkeep(bytes calldata /* performData */) external {
function performUpkeep(bytes calldata /* performData */ ) external {
(bool upkeepNeeded, bytes memory upKeepFlags) = checkUpkeep("");
if (!upkeepNeeded) {
revert Raffle__UpKeepNotFeed(
upKeepFlags,
address(this).balance,
s_players.length
);
revert Raffle__UpKeepNotFeed(upKeepFlags, address(this).balance, s_players.length);
}

s_raffleState = RaffleState.CALCULATING;
Expand All @@ -118,33 +113,29 @@ contract Raffle is VRFConsumerBaseV2Plus, Script {
callbackGasLimit: i_callbackGasLimit,
numWords: NUM_WORDS,
// Set nativePayment to true to pay for VRF requests with Sepolia ETH instead of LINK
extraArgs: VRFV2PlusClient._argsToBytes(
VRFV2PlusClient.ExtraArgsV1({nativePayment: false})
)
extraArgs: VRFV2PlusClient._argsToBytes(VRFV2PlusClient.ExtraArgsV1({nativePayment: false}))
})
);

emit RequestedRaffleWinner(requestId);
}

function fulfillRandomWords(
uint256 /* requestId */,
uint256[] calldata randomWords
) internal virtual override {
function fulfillRandomWords(uint256, /* requestId */ uint256[] calldata randomWords) internal virtual override {
uint256 indexOfWinner = randomWords[0] % s_players.length;
s_recentWinner = s_players[indexOfWinner];
s_raffleState = RaffleState.OPEN;
s_players = new address payable[](0);
s_lastTimestamp = block.timestamp;
emit WinnerPicked(s_recentWinner);
(bool success, ) = s_recentWinner.call{value: address(this).balance}("");
(bool success,) = s_recentWinner.call{value: address(this).balance}("");
if (!success) {
revert Raffle__WinnerWithdrawFailed();
}
}

/**Getter Function */

/**
* Getter Function
*/
function getEntranceFee() external view returns (uint256) {
return i_entranceFee;
}
Expand Down
8 changes: 2 additions & 6 deletions src/tools/CCEncoder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,14 @@ library CCEncoder {
output = flag ? bytes("1") : bytes("0");
}

function castFlags(
bool[] memory flags
) public pure returns (bytes memory output) {
function castFlags(bool[] memory flags) public pure returns (bytes memory output) {
output = "";
for (uint256 i = 0; i < flags.length; i++) {
output = bytes.concat(output, castFlag(flags[i]));
}
}

function castFlags(
bool[4] memory flags
) public pure returns (bytes memory output) {
function castFlags(bool[4] memory flags) public pure returns (bytes memory output) {
output = "";
for (uint256 i = 0; i < flags.length; i++) {
output = bytes.concat(output, castFlag(flags[i]));
Expand Down
2 changes: 1 addition & 1 deletion test/mock/LinkToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ contract LinkToken is ERC20 {
}
return length > 0;
}
}
}
Loading

0 comments on commit e353be4

Please sign in to comment.