Skip to content

Commit

Permalink
fix(contract): update get quest function to return participation and …
Browse files Browse the repository at this point in the history
…winner count
  • Loading branch information
oyyblin committed Nov 18, 2024
1 parent 3c473ba commit 5e517a0
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 168 deletions.
6 changes: 2 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ cache/
out/

# Ignores development broadcast logs
!/broadcast
/broadcast/*/31337/
/broadcast/**/dry-run/
broadcast

# Docs
docs/
Expand All @@ -30,4 +28,4 @@ pgo-data.profdata
**/proof-with-io.json

# Dotenv file
.env
.env
2 changes: 1 addition & 1 deletion contracts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@

## 📄 Example Deployment

- Gravity Alpha Testnet Sepolia: [0xc0e9eb9299bd2349ca0bb711b4ba4a1d43b1524f](https://explorer-sepolia.gravity.xyz/address/0xc0e9eb9299bd2349ca0bb711b4ba4a1d43b1524f)
- Gravity Alpha Testnet Sepolia: [0x4349F1d73237c8Eb43FC04A16CFf70261D0F1343](https://explorer-sepolia.gravity.xyz/address/0x4349F1d73237c8Eb43FC04A16CFf70261D0F1343)
70 changes: 0 additions & 70 deletions contracts/broadcast/Raffle.s.sol/13505/run-1730752044.json

This file was deleted.

70 changes: 0 additions & 70 deletions contracts/broadcast/Raffle.s.sol/13505/run-latest.json

This file was deleted.

3 changes: 3 additions & 0 deletions contracts/deployments/13505.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"RAFFLE": "0x4349F1d73237c8Eb43FC04A16CFf70261D0F1343"
}
5 changes: 5 additions & 0 deletions contracts/script/deploy/Raffle.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {BaseScript} from "../utils/Base.s.sol";
import {Raffle} from "../../src/Raffle.sol";

contract RaffleScript is BaseScript {
string internal constant KEY = "RAFFLE";

function run() external chain broadcaster {
bytes32 CREATE2_SALT = vm.envBytes32("CREATE2_SALT");
address owner = vm.envAddress("OWNER");
Expand All @@ -23,5 +25,8 @@ contract RaffleScript is BaseScript {

Raffle raffle = new Raffle{salt: CREATE2_SALT}(owner, signer, verifier, vkey, drandOracle);
console.log("Raffle deployed at:", address(raffle));

// Write address
writeAddress(KEY, address(raffle));
}
}
83 changes: 83 additions & 0 deletions contracts/script/utils/Base.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,87 @@ abstract contract BaseScript is Script {

_;
}

/// @notice Returns the directory of the deployments.
function directory() internal view returns (string memory) {
return string.concat(vm.projectRoot(), "/deployments/");
}

/// @notice Returns the file name for the current chain.
function file() internal view returns (string memory) {
return string.concat(vm.toString(block.chainid), ".json");
}

/// @notice Returns the path to the deployments file for the current chain.
function path() internal view returns (string memory) {
return string.concat(directory(), file());
}

/// @notice Returns the deployments file contents for the current chain.
function deployments() internal view returns (string memory) {
return vm.readFile(path());
}

/// @notice Ensures that the deployments file exists for the current chain.
function ensureExists() internal {
if (!vm.exists(directory())) {
vm.createDir(directory(), true);
}

if (!vm.exists(path())) {
vm.writeFile(path(), "{}");
}
}

/// @notice Tries to read an address from the env.
function envAddress(string memory key) internal view returns (address) {
return vm.envOr(key, address(0));
}

/// @notice Tries to read a bytes32 from the env.
function envBytes32(string memory key) internal view returns (bytes32) {
return vm.envOr(key, bytes32(0));
}

/// @notice Tries to read an address from the env first, then from the deployments file for the current chain.
function readAddress(string memory key) internal view returns (address) {
if (envAddress(key) != address(0)) {
return envAddress(key);
}
return deployments().readAddress(string.concat(".", key));
}

/// @notice Tries to read a bytes32 from the env first, then from the deployments file for the current chain.
function readBytes32(string memory key) internal view returns (bytes32) {
if (envBytes32(key) != bytes32(0)) {
return envBytes32(key);
}
return deployments().readBytes32(string.concat(".", key));
}

/// @notice Writes an address to the deployments file for the current chain.
function writeAddress(string memory key, address value) internal {
ensureExists();

if (vm.keyExists(deployments(), string.concat(".", key))) {
vm.writeJson(vm.toString(value), path(), string.concat(".", key));
} else {
string memory root = "root";
vm.serializeJson(root, deployments());
vm.writeJson(vm.serializeAddress(root, key, value), path());
}
}

/// @notice Writes a bytes32 to the deployments file for the current chain.
function writeBytes32(string memory key, bytes32 value) internal {
ensureExists();

if (vm.keyExists(deployments(), string.concat(".", key))) {
vm.writeJson(vm.toString(value), path(), string.concat(".", key));
} else {
string memory root = "root";
vm.serializeJson(root, deployments());
vm.writeJson(vm.serializeBytes32(root, key, value), path());
}
}
}
8 changes: 7 additions & 1 deletion contracts/src/IRaffle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,13 @@ interface IRaffle {
function getQuest(uint256 _questID)
external
view
returns (bool _active, IDrandOracle.Random memory random, bytes32 _merkleRoot);
returns (
bool _active,
IDrandOracle.Random memory random,
uint256 _participantCount,
uint256 _winnerCount,
bytes32 _merkleRoot
);

function hasParticipated(uint256 _verifyID) external view returns (bool);

Expand Down
13 changes: 9 additions & 4 deletions contracts/src/Raffle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -236,19 +236,24 @@ contract Raffle is IRaffle, Ownable2Step, Pausable, EIP712 {
ISP1Verifier(verifier).verifyProof(vkey, _publicValues, _proofBytes);

quest.merkleRoot = merkleRoot;
quest.winnerCount = winnerCount;

emit IRaffle.Reveal(_questID, participantCount, winnerCount, randomness, merkleRoot);
}

function getQuest(uint256 _questID)
external
view
returns (bool _active, IDrandOracle.Random memory random, bytes32 _merkleRoot)
returns (
bool _active,
IDrandOracle.Random memory random,
uint256 _participantCount,
uint256 _winnerCount,
bytes32 _merkleRoot
)
{
RaffleQuest storage quest = quests[_questID];
_active = quest.active;
random = quest.random;
_merkleRoot = quest.merkleRoot;
return (quest.active, quest.random, quest.participantCount, quest.winnerCount, quest.merkleRoot);
}

function hasParticipated(uint256 _verifyID) public view returns (bool) {
Expand Down
10 changes: 9 additions & 1 deletion contracts/test/Raffle.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,17 @@ contract RaffleTest is Test {
raffle.reveal(questID, fixture.publicValues, fixture.proof);

// verify quest state
(bool active, IDrandOracle.Random memory random, bytes32 merkleRoot) = raffle.getQuest(questID);
(
bool active,
IDrandOracle.Random memory random,
uint256 participantCount,
uint256 winnerCount,
bytes32 merkleRoot
) = raffle.getQuest(questID);
assertEq(random.round, 1);
assertEq(random.randomness, fixture.randomness);
assertEq(participantCount, fixture.numParticipants);
assertEq(winnerCount, fixture.numWinners);
assertEq(active, false);
assertEq(merkleRoot, fixture.merkleRoot);
}
Expand Down
Loading

0 comments on commit 5e517a0

Please sign in to comment.