Skip to content

Commit

Permalink
add custom encoder for bool[]
Browse files Browse the repository at this point in the history
  • Loading branch information
WhatMe1on committed Dec 7, 2024
1 parent bca4aa5 commit f89ad61
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 14 deletions.
3 changes: 3 additions & 0 deletions src/Raffle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ contract Raffle is VRFConsumerBaseV2Plus,Script{
// console.log(hasPlayer);
upkeepNeeded = timeHasPassed && stateIsOpen && hasBalance && hasPlayer;
//TODO if output false ,use event to replace the upKeepFlags, And compare the gas spent
// bytes memory outputFlagg = abi.encodePacked(uint8(0x02), timeHasPassed);

// console.log(hasPlayer);
bytes memory upKeepFlags = abi.encode(timeHasPassed,stateIsOpen,hasBalance,hasPlayer);
return (upkeepNeeded, upKeepFlags);
}
Expand Down
17 changes: 17 additions & 0 deletions src/tools/CCEncoder.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SPDX-License-Identifier: SEE LICENSE IN LICENSE
pragma solidity ^0.8.18;

library CCEncoder {
function castFlag(bool flag) public returns (bytes memory output) {
output = flag ? bytes("1") : bytes("0");
}

function castFlags(
bool[] memory flags
) public returns (bytes memory output) {
output = "";
for (uint256 i = 0; i < flags.length; i++) {
output = bytes.concat(output, castFlag(flags[i]));
}
}
}
45 changes: 31 additions & 14 deletions test/unit/RaffleTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {Raffle} from "../../src/Raffle.sol";
import {HelperConfig} from "../../script/HelperConfig.s.sol";
import {DeployRaffle} from "../../script/DeployRaffle.s.sol";
import {Test, console} from "forge-std/Test.sol";
import {CCEncoder} from "../../src/tools/CCEncoder.sol";

contract RaffleTest is Test {
event EnterRaffle(address indexed player);
Expand Down Expand Up @@ -97,10 +98,7 @@ contract RaffleTest is Test {
assertEq(upKeepFlags, flags);
}

function testCheckUpkeepReturnsFalseIfStateNotOpen()
public
M_prankPlayer
{
function testCheckUpkeepReturnsFalseIfStateNotOpen() public M_prankPlayer {
raffle.enterRaffle{value: entranceFee}();
vm.warp(block.timestamp + interval + 1);
vm.roll(block.number + 1);
Expand All @@ -112,10 +110,7 @@ contract RaffleTest is Test {
assertEq(upKeepFlags, flags);
}

function testCheckUpkeepReturnsFalseIfBalanceZero()
public
M_prankPlayer
{
function testCheckUpkeepReturnsFalseIfBalanceZero() public M_prankPlayer {
raffle.enterRaffle{value: entranceFee}();
vm.warp(block.timestamp + interval + 1);
vm.roll(block.number + 1);
Expand All @@ -127,17 +122,14 @@ contract RaffleTest is Test {
assertEq(upKeepFlags, flags);
}

function testCheckUpkeepReturnsFalseIfNoPlayer()
public
M_prankPlayer
{
function testCheckUpkeepReturnsFalseIfNoPlayer() public M_prankPlayer {
vm.warp(block.timestamp + interval + 1);
vm.roll(block.number + 1);
vm.deal(address(raffle), 1 ether);
bytes memory upKeepFlags = abi.encode(true, true, true, false);

(, bytes memory flags) = raffle.checkUpkeep("");

assertEq(upKeepFlags, flags);
}

Expand All @@ -155,7 +147,7 @@ contract RaffleTest is Test {
function testPerformUpkeepCanOnlyRunIfCheckUpkeepIsTrue()
public
M_prankPlayer
{
{
raffle.enterRaffle{value: entranceFee}();
vm.warp(block.timestamp + interval + 1);
vm.roll(block.number + 1);
Expand Down Expand Up @@ -194,3 +186,28 @@ contract RaffleTest is Test {
_;
}
}

contract ToolTest is Test {
using CCEncoder for bool;
using CCEncoder for bool[];

function testCastSingleTrueFlag() public {
bool trueFlag = true;
assertEq(trueFlag.castFlag(), bytes("1"));
}

function testCastSingleFalseFlag() public {
bool falseFlag = false;
assertEq(falseFlag.castFlag(), bytes("0"));
}

function testCastMultiFlag() public {
bool[] memory flags = new bool[](4);
flags[0] = true;
flags[1] = false;
flags[2] = false;
flags[3] = false;

assertEq(flags.castFlags(), bytes.concat(bytes("1"), bytes("0"), bytes("0"), bytes("0")));
}
}

0 comments on commit f89ad61

Please sign in to comment.