Skip to content

Commit df680e1

Browse files
authored
Merge pull request #2 from its-everdred/feature/format_contracts
Format contracts aligned with fmt
2 parents 068dc2c + 7defbcd commit df680e1

File tree

11 files changed

+73
-130
lines changed

11 files changed

+73
-130
lines changed

.github/workflows/test.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ jobs:
2727
run: |
2828
forge --version
2929
30-
# - name: Run Forge fmt
31-
# run: |
32-
# forge fmt --check
33-
# id: fmt
30+
- name: Run Forge fmt
31+
run: |
32+
forge fmt --check
33+
id: fmt
3434

3535
- name: Run Forge build
3636
run: |

script/1_DeployQuest.s.sol

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,15 @@ import {Script, console} from "forge-std/Script.sol";
55
import {Helper} from "./Helper.sol";
66
import {QuestFactory} from "../src/QuestFactory.sol";
77

8-
9-
108
contract DeployQuestScript is Script, Helper {
11-
129
function run() external returns (address questFactoryAddress, address questDonationAddress) {
1310
uint256 deployerPrivateKey = getDeployerPrivateKey();
1411
vm.startBroadcast(deployerPrivateKey);
1512

1613
// Deploy QuestFactory with constructor parameters
1714
// Sets msg.sender as admin for factory and all future quests created
1815
QuestFactory questFactory = new QuestFactory();
19-
questFactoryAddress = address(questFactory);
16+
questFactoryAddress = address(questFactory);
2017

2118
// Create a new Quest with a target amount (for example, 1 ether)
2219
// Msg.sender is the creator of the quest, but only admin can withdraw funds
@@ -29,6 +26,4 @@ contract DeployQuestScript is Script, Helper {
2926

3027
vm.stopBroadcast();
3128
}
32-
3329
}
34-

script/2_SetupQuest.s.sol

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ import {Script, console} from "forge-std/Script.sol";
55
import {Helper} from "./Helper.sol";
66
import {QuestDonation} from "../src/QuestDonation.sol";
77

8-
98
contract SetupQuestScript is Script, Helper {
10-
119
function run(address questDonationAddress) external {
1210
uint256 deployerPrivateKey = getDeployerPrivateKey();
1311
vm.startBroadcast(deployerPrivateKey);
@@ -22,11 +20,9 @@ contract SetupQuestScript is Script, Helper {
2220

2321
// Set price oracles
2422
questDonation.setPriceOracle(address(0), ETH_USD_FEED); // For Native ETH
25-
questDonation.setPriceOracle(USDC, USDC_USD_FEED); // USDC Oracle
23+
questDonation.setPriceOracle(USDC, USDC_USD_FEED); // USDC Oracle
2624
console.log("Price oracles set for ETH and USDC");
2725

2826
vm.stopBroadcast();
2927
}
30-
3128
}
32-

script/3_InteractionQuest.s.sol

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ import {Helper} from "./Helper.sol";
66
import {QuestDonation} from "../src/QuestDonation.sol";
77
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
88

9-
109
contract InteractionQuestScript is Script, Helper {
11-
1210
function run(address questDonationAddress) external {
1311
uint256 deployerPrivateKey = getDeployerPrivateKey();
1412
vm.startBroadcast(deployerPrivateKey);
@@ -19,41 +17,38 @@ contract InteractionQuestScript is Script, Helper {
1917
// CHECK ETH BALANCE
2018
address deployerAddress = vm.addr(deployerPrivateKey);
2119
console.log("Address:", deployerAddress);
22-
console.log("ETH Balance: ", deployerAddress.balance/ 10**18);
20+
console.log("ETH Balance: ", deployerAddress.balance / 10 ** 18);
2321
// CHECK USDC BALANCE
24-
console.log("USDC balance:", IERC20(USDC).balanceOf(address(deployerAddress))/ 10**6);
25-
22+
console.log("USDC balance:", IERC20(USDC).balanceOf(address(deployerAddress)) / 10 ** 6);
2623

2724
// Make donations
2825
// ETH donation
29-
questDonation.donateETH{value: 0.1 ether}(); // Donate ETH
26+
questDonation.donateETH{value: 0.1 ether}(); // Donate ETH
3027
console.log("Donated 0.1 ETH");
3128

3229
// USDC donation
33-
uint256 usdcAmount = 100 * 10**6;
30+
uint256 usdcAmount = 100 * 10 ** 6;
3431
IERC20(USDC).approve(address(questDonation), usdcAmount); // DONATE USDC
3532
questDonation.donateERC20(USDC, usdcAmount);
3633
console.log("Donated 100 USDC");
3734

38-
3935
// Log balances after donations
4036
console.log("===QUEST CONTRACT: Balances after donations ===");
4137
console.log("ETH balance:", address(questDonation).balance);
4238
console.log("USDC balance:", IERC20(USDC).balanceOf(address(questDonation)));
4339

44-
4540
// Withdraw funds
46-
uint256 ethBalance = address(questDonation).balance;
47-
uint256 usdcBalance = IERC20(USDC).balanceOf(address(questDonation));
41+
uint256 ethBalance = address(questDonation).balance;
42+
uint256 usdcBalance = IERC20(USDC).balanceOf(address(questDonation));
4843

4944
if (ethBalance > 0) {
50-
questDonation.withdraw(address(0), ethBalance/2); // WITHDRAW ETH
51-
console.log("Withdrawn ETH balance:", ethBalance/2);
45+
questDonation.withdraw(address(0), ethBalance / 2); // WITHDRAW ETH
46+
console.log("Withdrawn ETH balance:", ethBalance / 2);
5247
}
53-
48+
5449
if (usdcBalance > 0) {
55-
questDonation.withdraw(USDC, usdcBalance/2); // EITHDRAW USDC
56-
console.log("Withdrawn USDC balance:", usdcBalance/2);
50+
questDonation.withdraw(USDC, usdcBalance / 2); // EITHDRAW USDC
51+
console.log("Withdrawn USDC balance:", usdcBalance / 2);
5752
}
5853

5954
// Log balances after withdrawals
@@ -63,6 +58,4 @@ contract InteractionQuestScript is Script, Helper {
6358

6459
vm.stopBroadcast();
6560
}
66-
6761
}
68-

script/Deploy.s.sol

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ import {InteractionQuestScript} from "./3_InteractionQuest.s.sol";
88

99
contract DeployScript is Helper {
1010
function run() external {
11-
1211
//DEPLOYS FACTORY AND FIRST EXAMPLE QUEST
1312
DeployQuestScript deployQuest = new DeployQuestScript();
14-
( , address firstQuest) = deployQuest.run();
13+
(, address firstQuest) = deployQuest.run();
1514

1615
// ALLOW TOKENS AND ADD ORACLE
1716
SetupQuestScript setupQuest = new SetupQuestScript();
@@ -20,6 +19,5 @@ contract DeployScript is Helper {
2019
// INTERACTING WITH FIRST QUEST
2120
InteractionQuestScript interactingQuest = new InteractionQuestScript();
2221
interactingQuest.run(firstQuest);
23-
2422
}
25-
}
23+
}

script/Helper.sol

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// SPDX-License-Identifier: GPL-3.0-or-later
22
pragma solidity ^0.8.24;
3-
import {Script} from "forge-std/Script.sol";
43

4+
import {Script} from "forge-std/Script.sol";
55

6-
contract Helper is Script{
6+
contract Helper is Script {
77
address constant USDC = 0x94a9D9AC8a22534E3FaCa9F4e7F2E2cf85d5E4C8;
8-
8+
99
// Chainlink Price Feed addresses
1010
address constant ETH_USD_FEED = 0x694AA1769357215DE4FAC081bf1f309aDC325306;
1111
address constant USDC_USD_FEED = 0xA2F78ab2355fe2f984D808B5CeE7FD0A93D5270E;
12-
error InvalidPrivateKey(string);
1312

13+
error InvalidPrivateKey(string);
1414

1515
function getDeployerPrivateKey() internal view returns (uint256 deployerPrivateKey) {
1616
deployerPrivateKey = vm.envUint("DEPLOYER_PRIVATE_KEY");
@@ -20,6 +20,4 @@ contract Helper is Script{
2020
);
2121
}
2222
}
23-
24-
25-
}
23+
}

src/QuestDonation.sol

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,24 @@ contract QuestDonation is Ownable {
2626
event FundsWithdrawn(address indexed token, uint256 amount);
2727
event PriceOracleSet(address indexed token, address indexed oracle);
2828

29-
constructor(
30-
address _admin,
31-
uint256 _targetAmount,
32-
address _creator
33-
) Ownable(_admin) {
29+
constructor(address _admin, uint256 _targetAmount, address _creator) Ownable(_admin) {
3430
require(_admin != address(0), "Invalid admin address");
3531
require(_creator != address(0), "Invalid creator address");
3632
require(_targetAmount > 0, "Invalid target amount");
37-
33+
3834
admin = _admin;
3935
targetAmount = _targetAmount;
4036
creator = _creator;
4137
}
4238

43-
4439
modifier withinDonationLimit(address token, uint256 amount) {
4540
require(address(priceOracles[token]) != address(0), "No price oracle set for token");
4641
AggregatorV3Interface priceFeed = priceOracles[token];
47-
(, int256 price, , , ) = priceFeed.latestRoundData();
42+
(, int256 price,,,) = priceFeed.latestRoundData();
4843
uint8 priceDecimals = priceFeed.decimals();
49-
44+
5045
require(price > 0, "Invalid price");
51-
46+
5247
uint256 usdValue;
5348
if (token == address(0)) {
5449
// Native token (ETH) has 18 decimals
@@ -59,23 +54,25 @@ contract QuestDonation is Ownable {
5954
// Adjust the calculation based on token decimals
6055
usdValue = (amount * uint256(price)) / (10 ** tokenDecimals) / (10 ** priceDecimals);
6156
}
62-
57+
6358
uint256 lastYear = lastDonationTimestamp[msg.sender];
6459
if (block.timestamp - lastYear > YEAR) {
6560
yearlyDonations[msg.sender] = 0;
6661
lastDonationTimestamp[msg.sender] = block.timestamp;
6762
}
68-
require(yearlyDonations[msg.sender] + usdValue <= MAX_DONATION, "Donation exceeds $5000/year. Contact info@etherguild.xyz");
69-
63+
require(
64+
yearlyDonations[msg.sender] + usdValue <= MAX_DONATION,
65+
"Donation exceeds $5000/year. Contact info@etherguild.xyz"
66+
);
67+
7068
yearlyDonations[msg.sender] += usdValue;
7169
_;
7270
}
7371

7472
function donateETH() external payable withinDonationLimit(address(0), msg.value) {
75-
7673
require(msg.value > 0, "Donation amount must be greater than 0");
7774
AggregatorV3Interface priceFeed = priceOracles[address(0)];
78-
(, int256 price, , , ) = priceFeed.latestRoundData();
75+
(, int256 price,,,) = priceFeed.latestRoundData();
7976
uint8 priceDecimals = priceFeed.decimals();
8077
emit DonationReceived(msg.sender, address(0), msg.value, msg.value * uint256(price) / (10 ** priceDecimals));
8178
}
@@ -84,7 +81,7 @@ contract QuestDonation is Ownable {
8481
require(allowedTokens[token], "Token not allowed");
8582
IERC20(token).safeTransferFrom(msg.sender, address(this), amount);
8683
AggregatorV3Interface priceFeed = priceOracles[token];
87-
(, int256 price, , , ) = priceFeed.latestRoundData();
84+
(, int256 price,,,) = priceFeed.latestRoundData();
8885
uint8 priceDecimals = priceFeed.decimals();
8986
emit DonationReceived(msg.sender, token, amount, amount * uint256(price) / (10 ** priceDecimals));
9087
}
@@ -100,12 +97,11 @@ contract QuestDonation is Ownable {
10097
function setPriceOracle(address token, address oracle) external onlyOwner {
10198
require(allowedTokens[token], "Token not allowed");
10299
require(oracle != address(0), "Invalid oracle address");
103-
100+
104101
priceOracles[token] = AggregatorV3Interface(oracle);
105102
emit PriceOracleSet(token, oracle);
106103
}
107104

108-
109105
function withdraw(address token, uint256 amount) external {
110106
require(msg.sender == admin, "Only admin can withdraw");
111107
if (token == address(0)) {

src/QuestFactory.sol

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity ^0.8.19;
33

4-
import "./QuestDonation.sol";
4+
import "./QuestDonation.sol";
55

66
contract QuestFactory {
77
address public admin;
88

99
constructor() {
10-
admin = msg.sender; // Set contract deployer as admin
10+
admin = msg.sender; // Set contract deployer as admin
1111
}
1212

1313
// Structure to store quest details
@@ -22,22 +22,12 @@ contract QuestFactory {
2222
Quest[] public quests;
2323

2424
// Event emitted when new quest is created
25-
event QuestCreated(
26-
address indexed questContract,
27-
address indexed creator,
28-
uint256 timestamp
29-
);
25+
event QuestCreated(address indexed questContract, address indexed creator, uint256 timestamp);
3026

3127
// Function to create new quest
32-
function createQuest(
33-
uint256 _targetAmount
34-
) external returns (address) {
28+
function createQuest(uint256 _targetAmount) external returns (address) {
3529
// Deploy new QuestDonation contract
36-
QuestDonation newQuest = new QuestDonation(
37-
admin,
38-
_targetAmount,
39-
msg.sender
40-
);
30+
QuestDonation newQuest = new QuestDonation(admin, _targetAmount, msg.sender);
4131

4232
// Store quest details
4333
quests.push(
@@ -50,11 +40,7 @@ contract QuestFactory {
5040
);
5141

5242
// Emit event
53-
emit QuestCreated(
54-
address(newQuest),
55-
msg.sender,
56-
block.timestamp
57-
);
43+
emit QuestCreated(address(newQuest), msg.sender, block.timestamp);
5844

5945
return address(newQuest);
6046
}
@@ -65,19 +51,13 @@ contract QuestFactory {
6551
}
6652

6753
// Get quest by index
68-
function getQuestByIndex(uint256 _index) external view returns (
69-
address questContract,
70-
uint256 targetAmount,
71-
address creator,
72-
uint256 timestamp
73-
) {
54+
function getQuestByIndex(uint256 _index)
55+
external
56+
view
57+
returns (address questContract, uint256 targetAmount, address creator, uint256 timestamp)
58+
{
7459
require(_index < quests.length, "Quest index out of bounds");
7560
Quest memory quest = quests[_index];
76-
return (
77-
quest.questContract,
78-
quest.targetAmount,
79-
quest.creator,
80-
quest.timestamp
81-
);
61+
return (quest.questContract, quest.targetAmount, quest.creator, quest.timestamp);
8262
}
8363
}

0 commit comments

Comments
 (0)