-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
17ca270
commit 49e0d43
Showing
10 changed files
with
159 additions
and
84 deletions.
There are no files selected for viewing
34 changes: 0 additions & 34 deletions
34
templates/quickstart/foundry/testing/.github/workflows/test.yml
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
31 changes: 31 additions & 0 deletions
31
templates/quickstart/foundry/testing/script/DeployFactory.s.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.13; | ||
|
||
import "forge-std/Script.sol"; | ||
import "forge-std/console.sol"; | ||
import "../src/CrowdfundFactory.sol"; | ||
import "../src/CrowdfundingCampaign.sol"; | ||
|
||
contract DeployFactoryAndCreateCampaign is Script { | ||
function run() external { | ||
uint256 deployerPrivateKey = vm.envUint("WALLET_PRIVATE_KEY"); | ||
vm.startBroadcast(deployerPrivateKey); | ||
|
||
// Deploy the CrowdfundingFactory contract | ||
CrowdfundingFactory factory = new CrowdfundingFactory(); | ||
|
||
// Log the factory's address | ||
console.log("CrowdfundingFactory deployed at: %s", address(factory)); | ||
|
||
// Define the funding goal for the new campaign | ||
uint256 fundingGoalInWei = 0.01 ether; | ||
|
||
// Use the factory to create a new CrowdfundingCampaign | ||
factory.createCampaign(fundingGoalInWei); | ||
|
||
// Not sure how to get the address of the new campaign | ||
// TODO: Log the address of the new campaign | ||
|
||
vm.stopBroadcast(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
23 changes: 23 additions & 0 deletions
23
templates/quickstart/foundry/testing/src/CrowdfundFactory.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.0; | ||
|
||
// Crowdfunding campaign contract | ||
import "./CrowdfundingCampaign.sol"; | ||
|
||
// Factory contract to create and manage crowdfunding campaigns | ||
contract CrowdfundingFactory { | ||
CrowdfundingCampaign[] public campaigns; | ||
|
||
event CampaignCreated(address campaignAddress, uint256 fundingGoal); | ||
|
||
function createCampaign(uint256 fundingGoal) public { | ||
CrowdfundingCampaign newCampaign = new CrowdfundingCampaign(fundingGoal); | ||
campaigns.push(newCampaign); | ||
|
||
emit CampaignCreated(address(newCampaign), fundingGoal); | ||
} | ||
|
||
function getCampaigns() public view returns (CrowdfundingCampaign[] memory) { | ||
return campaigns; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
templates/quickstart/foundry/testing/src/CrowdfundingCampaign.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.0; | ||
|
||
contract CrowdfundingCampaign { | ||
address public owner; | ||
uint256 public fundingGoal; | ||
uint256 public totalFundsRaised; | ||
mapping(address => uint256) public contributions; | ||
|
||
event ContributionReceived(address contributor, uint256 amount); | ||
event GoalReached(uint256 totalFundsRaised); | ||
|
||
constructor(uint256 _fundingGoal) { | ||
owner = msg.sender; | ||
fundingGoal = _fundingGoal; | ||
} | ||
|
||
function contribute() public payable { | ||
require(msg.value > 0, "Contribution must be greater than 0"); | ||
contributions[msg.sender] += msg.value; | ||
totalFundsRaised += msg.value; | ||
|
||
emit ContributionReceived(msg.sender, msg.value); | ||
|
||
if (totalFundsRaised >= fundingGoal) { | ||
emit GoalReached(totalFundsRaised); | ||
} | ||
} | ||
|
||
function withdrawFunds() public { | ||
require(msg.sender == owner, "Only the owner can withdraw funds"); | ||
require(totalFundsRaised >= fundingGoal, "Funding goal not reached"); | ||
|
||
uint256 amount = address(this).balance; | ||
totalFundsRaised = 0; | ||
|
||
(bool success, ) = payable(owner).call{value: amount}(""); | ||
require(success, "Transfer failed."); | ||
} | ||
|
||
function getTotalFundsRaised() public view returns (uint256) { | ||
return totalFundsRaised; | ||
} | ||
|
||
function getFundingGoal() public view returns (uint256) { | ||
return fundingGoal; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
51 changes: 51 additions & 0 deletions
51
templates/quickstart/foundry/testing/test/CrowdfundingCampaign.t.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.0; | ||
|
||
import "forge-std/Test.sol"; | ||
import "forge-std/console.sol"; | ||
import "../src/CrowdfundingCampaign.sol"; | ||
|
||
contract CrowdfundingCampaignTest is Test { | ||
CrowdfundingCampaign campaign; | ||
event GoalReached(uint256 totalFundsRaised); | ||
address owner; | ||
address addr1; | ||
address addr2; | ||
|
||
function setUp() public { | ||
owner = address(this); | ||
|
||
addr1 = vm.addr(1); | ||
addr2 = vm.addr(2); | ||
|
||
campaign = new CrowdfundingCampaign(1 ether); | ||
console.log("CrowdfundingCampaign deployed at: %s", address(campaign)); | ||
} | ||
|
||
function test_RejectZeroContributions() public { | ||
vm.expectRevert("Contribution must be greater than 0"); | ||
campaign.contribute{value: 0}(); | ||
} | ||
|
||
function test_AggregateContributions() public { | ||
uint256 initialTotal = campaign.getTotalFundsRaised(); | ||
|
||
vm.prank(addr1); | ||
vm.deal(addr1, 2 ether); | ||
campaign.contribute{value: 0.5 ether}(); | ||
|
||
vm.prank(addr2); | ||
vm.deal(addr2, 2 ether); | ||
campaign.contribute{value: 0.3 ether}(); | ||
|
||
assertEq(campaign.getTotalFundsRaised(), initialTotal + 0.8 ether); | ||
} | ||
|
||
function test_EmitGoalReachedWhenFundingGoalMet() public { | ||
vm.prank(addr1); | ||
vm.deal(addr1, 2 ether); | ||
vm.expectEmit(true, true, false, true); | ||
emit GoalReached(1 ether); | ||
campaign.contribute{value: 1 ether}(); | ||
} | ||
} |