Skip to content

Commit

Permalink
chore: format codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
khayss committed Aug 24, 2024
1 parent 0e73a21 commit dab9633
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 101 deletions.
83 changes: 32 additions & 51 deletions src/Crowdfund.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,27 @@ contract Crowdfund {
string title;
string description;
address benefactor;
uint goal;
uint deadline;
uint amountRaised;
uint256 goal;
uint256 deadline;
uint256 amountRaised;
}

// State variables
mapping(uint => Campaign) campaigns;
mapping(address => uint[]) userCampaigns;
uint numCampaigns;
uint totalFunding;
mapping(uint256 => Campaign) campaigns;
mapping(address => uint256[]) userCampaigns;
uint256 numCampaigns;
uint256 totalFunding;

// Events
event CampaignCreated(address indexed creator, uint indexed campaignId);
event CampaignEnded(
address indexed creator,
uint indexed campaignId,
uint indexed amountRaised
);
event DonationReceived(
uint indexed campaignId,
address indexed donator,
uint indexed amount
);
event CampaignCreated(address indexed creator, uint256 indexed campaignId);
event CampaignEnded(address indexed creator, uint256 indexed campaignId, uint256 indexed amountRaised);
event DonationReceived(uint256 indexed campaignId, address indexed donator, uint256 indexed amount);

// Errors
error Crowdfund_InvalidCampaign(uint id);
error Crowdfund_InvalidCampaign(uint256 id);
error Crowdfund_CannotDonateZero();
error Crowdfund_CampaignInactive();
error Crowdfund_CampaignNotEnded(uint deadline);
error Crowdfund_CampaignNotEnded(uint256 deadline);
error Crowdfund_NoFundsRaised();
error Crowdfund_PayoutFailed();

Expand All @@ -56,12 +48,10 @@ contract Crowdfund {
/// @param _goal The funding goal of the campaign.
/// @param _deadline The deadline for the campaign.
/// @return campaignId The ID of the created campaign.
function createCampaign(
string calldata _title,
string calldata _description,
uint _goal,
uint _deadline
) public returns (uint campaignId) {
function createCampaign(string calldata _title, string calldata _description, uint256 _goal, uint256 _deadline)
public
returns (uint256 campaignId)
{
Campaign memory newCampaign = Campaign({
isInitialized: true,
isPaidOut: false,
Expand All @@ -74,7 +64,7 @@ contract Crowdfund {
});

campaignId = numCampaigns;
uint[] storage _userCampaigns = userCampaigns[msg.sender];
uint256[] storage _userCampaigns = userCampaigns[msg.sender];

numCampaigns += 1;

Expand All @@ -86,13 +76,15 @@ contract Crowdfund {

/// @notice Allows a user to donate to a specific campaign.
/// @param campaignId The ID of the campaign to donate to.
function donateToCampaign(uint campaignId) public payable {
function donateToCampaign(uint256 campaignId) public payable {
if (msg.value == 0) revert Crowdfund_CannotDonateZero();
Campaign storage campaign = campaigns[campaignId];
if (!campaign.isInitialized)
if (!campaign.isInitialized) {
revert Crowdfund_InvalidCampaign(campaignId);
if (block.timestamp > campaign.deadline)
}
if (block.timestamp > campaign.deadline) {
revert Crowdfund_CampaignInactive();
}

totalFunding += msg.value;
campaign.amountRaised += msg.value;
Expand All @@ -102,10 +94,11 @@ contract Crowdfund {

/// @notice Ends a specific campaign and pays out the funds to the benefactor.
/// @param campaignId The ID of the campaign to end.
function endCampaign(uint campaignId) external {
function endCampaign(uint256 campaignId) external {
Campaign storage campaign = campaigns[campaignId];
if (block.timestamp < campaign.deadline)
if (block.timestamp < campaign.deadline) {
revert Crowdfund_CampaignNotEnded(campaign.deadline);
}

if (campaign.isPaidOut) revert Crowdfund_CampaignInactive();

Expand All @@ -114,55 +107,43 @@ contract Crowdfund {
campaign.isPaidOut = true;
totalFunding -= campaign.amountRaised;

(bool success, ) = payable(campaign.benefactor).call{
value: campaign.amountRaised
}("");
(bool success,) = payable(campaign.benefactor).call{value: campaign.amountRaised}("");

if (!success) revert Crowdfund_PayoutFailed();

emit CampaignEnded(
campaign.benefactor,
campaignId,
campaign.amountRaised
);
emit CampaignEnded(campaign.benefactor, campaignId, campaign.amountRaised);
}

/// @notice Gets the total number of campaigns.
/// @return The total number of campaigns.
function getTotalCampaigns() external view returns (uint) {
function getTotalCampaigns() external view returns (uint256) {
return numCampaigns;
}

/// @notice Gets the total funding amount.
/// @return The total funding amount.
function getTotalFunding() external view returns (uint) {
function getTotalFunding() external view returns (uint256) {
return totalFunding;
}

/// @notice Gets the campaigns created by a specific user.
/// @param user The address of the user.
/// @return The array of campaign IDs created by the user.
function getUserCampaigns(
address user
) external view returns (uint[] memory) {
function getUserCampaigns(address user) external view returns (uint256[] memory) {
return userCampaigns[user];
}

/// @notice Gets the campaign details by ID.
/// @param campaignId The ID of the campaign.
/// @return The campaign details.
function getCampaignById(
uint campaignId
) external view returns (Campaign memory) {
function getCampaignById(uint256 campaignId) external view returns (Campaign memory) {
return campaigns[campaignId];
}

/// @notice Gets the amount raised for a specific campaign.
/// @param campaignId The ID of the campaign.
/// @return The amount raised for the campaign.
function getCampaignAmountRaised(
uint campaignId
) external view returns (uint) {
function getCampaignAmountRaised(uint256 campaignId) external view returns (uint256) {
return campaigns[campaignId].amountRaised;
}
}
78 changes: 28 additions & 50 deletions test/Crowdfund.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,14 @@ contract CrowdfundTest is Test {
crowdfund = new Crowdfund();
}

function newCampaign() public returns (uint campaignId) {
function newCampaign() public returns (uint256 campaignId) {
string memory title = "Test Campaign";
string memory description = "This is a test campaign.";
uint goal = 1 ether;
uint deadline = 1 minutes;
uint256 goal = 1 ether;
uint256 deadline = 1 minutes;

vm.prank(fakeUser);
campaignId = crowdfund.createCampaign(
title,
description,
goal,
deadline
);
campaignId = crowdfund.createCampaign(title, description, goal, deadline);
}

function test_IsInitializedCorrectly() public view {
Expand All @@ -35,8 +30,8 @@ contract CrowdfundTest is Test {
}

function test_CanCreateCampaign() public {
uint campaignId = newCampaign();
uint[] memory userCampaigns = crowdfund.getUserCampaigns(fakeUser);
uint256 campaignId = newCampaign();
uint256[] memory userCampaigns = crowdfund.getUserCampaigns(fakeUser);

assertEq(campaignId, 0);
assertEq(crowdfund.getTotalCampaigns(), 1);
Expand All @@ -45,9 +40,9 @@ contract CrowdfundTest is Test {
}

function test_CanDonateToCampaign() public {
uint campaignId = newCampaign();
uint amount = 10 ether;
uint amountToDonate = 1 ether;
uint256 campaignId = newCampaign();
uint256 amount = 10 ether;
uint256 amountToDonate = 1 ether;

address fakeDonor1 = vm.addr(2);
address fakeDonor2 = vm.addr(3);
Expand All @@ -62,45 +57,37 @@ contract CrowdfundTest is Test {
crowdfund.donateToCampaign{value: amountToDonate}(campaignId);

assertEq(crowdfund.getTotalFunding(), amountToDonate * 2);
assertEq(
crowdfund.getCampaignAmountRaised(campaignId),
amountToDonate * 2
);
assertEq(crowdfund.getCampaignAmountRaised(campaignId), amountToDonate * 2);
}

function test_CannotDonateZero() public {
uint amount = 10 ether;
uint256 amount = 10 ether;
address fakeDonor = vm.addr(2);
vm.deal(fakeDonor, amount);
uint campaignId = newCampaign();
uint256 campaignId = newCampaign();

vm.expectRevert(Crowdfund.Crowdfund_CannotDonateZero.selector);
vm.prank(fakeDonor);
crowdfund.donateToCampaign{value: 0}(campaignId);
}

function test_CannotDonateToInvalidCampaign() public {
uint campaignId = 1;
uint256 campaignId = 1;
address fakeDonor = vm.addr(2);
uint amount = 10 ether;
uint amountToDonate = 1 ether;
uint256 amount = 10 ether;
uint256 amountToDonate = 1 ether;

vm.deal(fakeDonor, amount);

vm.expectRevert(
abi.encodeWithSelector(
Crowdfund.Crowdfund_InvalidCampaign.selector,
campaignId
)
);
vm.expectRevert(abi.encodeWithSelector(Crowdfund.Crowdfund_InvalidCampaign.selector, campaignId));
vm.prank(fakeDonor);
crowdfund.donateToCampaign{value: amountToDonate}(campaignId);
}

function test_BenefactorCanWithdrawWhenCampaignEnd() public {
uint campaignId = newCampaign();
uint amount = 10 ether;
uint amountToDonate = 1 ether;
uint256 campaignId = newCampaign();
uint256 amount = 10 ether;
uint256 amountToDonate = 1 ether;

address fakeDonor1 = vm.addr(2);
address fakeDonor2 = vm.addr(3);
Expand All @@ -117,7 +104,7 @@ contract CrowdfundTest is Test {
vm.warp(block.timestamp + 2 minutes);
vm.roll(block.number + 4);

uint userBalanceBefore = fakeUser.balance;
uint256 userBalanceBefore = fakeUser.balance;

vm.prank(fakeUser);
crowdfund.endCampaign(campaignId);
Expand All @@ -127,9 +114,9 @@ contract CrowdfundTest is Test {
}

function test_CannotEndCampaignBeforeDeadline() public {
uint campaignId = newCampaign();
uint amount = 10 ether;
uint amountToDonate = 1 ether;
uint256 campaignId = newCampaign();
uint256 amount = 10 ether;
uint256 amountToDonate = 1 ether;

address fakeDonor1 = vm.addr(2);
address fakeDonor2 = vm.addr(3);
Expand All @@ -143,20 +130,15 @@ contract CrowdfundTest is Test {
vm.prank(fakeDonor2);
crowdfund.donateToCampaign{value: amountToDonate}(campaignId);

vm.expectRevert(
abi.encodeWithSelector(
Crowdfund.Crowdfund_CampaignNotEnded.selector,
block.timestamp + 60
)
);
vm.expectRevert(abi.encodeWithSelector(Crowdfund.Crowdfund_CampaignNotEnded.selector, block.timestamp + 60));
vm.prank(fakeUser);
crowdfund.endCampaign(campaignId);
}

function test_CannotDonateAfterCampaignEnd() public {
uint campaignId = newCampaign();
uint amount = 10 ether;
uint amountToDonate = 1 ether;
uint256 campaignId = newCampaign();
uint256 amount = 10 ether;
uint256 amountToDonate = 1 ether;

address fakeDonor1 = vm.addr(2);
address fakeDonor2 = vm.addr(3);
Expand All @@ -170,11 +152,7 @@ contract CrowdfundTest is Test {
vm.warp(block.timestamp + 2 minutes);
vm.roll(block.number + 4);

vm.expectRevert(
abi.encodeWithSelector(
Crowdfund.Crowdfund_CampaignInactive.selector
)
);
vm.expectRevert(abi.encodeWithSelector(Crowdfund.Crowdfund_CampaignInactive.selector));
vm.prank(fakeDonor2);
crowdfund.donateToCampaign{value: amountToDonate}(campaignId);
}
Expand Down

0 comments on commit dab9633

Please sign in to comment.