Skip to content

Commit

Permalink
feat: add foundry test templates
Browse files Browse the repository at this point in the history
  • Loading branch information
dutterbutter committed May 8, 2024
1 parent 17ca270 commit 49e0d43
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 84 deletions.
34 changes: 0 additions & 34 deletions templates/quickstart/foundry/testing/.github/workflows/test.yml

This file was deleted.

2 changes: 2 additions & 0 deletions templates/quickstart/foundry/testing/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Compiler files
cache/
out/
zkout/

# Ignores development broadcast logs
!/broadcast
Expand All @@ -12,3 +13,4 @@ docs/

# Dotenv file
.env
broadcast/
4 changes: 4 additions & 0 deletions templates/quickstart/foundry/testing/foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ src = "src"
out = "out"
libs = ["lib"]

[rpc_endpoints]
zkSyncSepoliaTestnet = "https://sepolia.era.zksync.dev"
inMemoryNode = "http://127.0.0.1:8011"

# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options
12 changes: 0 additions & 12 deletions templates/quickstart/foundry/testing/script/Counter.s.sol

This file was deleted.

31 changes: 31 additions & 0 deletions templates/quickstart/foundry/testing/script/DeployFactory.s.sol
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();
}
}
14 changes: 0 additions & 14 deletions templates/quickstart/foundry/testing/src/Counter.sol

This file was deleted.

23 changes: 23 additions & 0 deletions templates/quickstart/foundry/testing/src/CrowdfundFactory.sol
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 templates/quickstart/foundry/testing/src/CrowdfundingCampaign.sol
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;
}
}
24 changes: 0 additions & 24 deletions templates/quickstart/foundry/testing/test/Counter.t.sol

This file was deleted.

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}();
}
}

0 comments on commit 49e0d43

Please sign in to comment.