Skip to content

Commit

Permalink
cmt774
Browse files Browse the repository at this point in the history
  • Loading branch information
xthet committed Jun 2, 2023
1 parent 21deda1 commit 6042a92
Show file tree
Hide file tree
Showing 9 changed files with 351 additions and 45 deletions.
31 changes: 10 additions & 21 deletions contracts/Campaign.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pragma solidity ^0.8.11;
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "@chainlink/contracts/src/v0.8/interfaces/KeeperCompatibleInterface.sol";
import { Reward } from "./Reward.sol";
import { RewardFactory } from "./RewardFactory.sol";

// errors
// error Cmp_NIS(); /**not in state */
Expand All @@ -27,6 +28,7 @@ contract Campaign is KeeperCompatibleInterface, ReentrancyGuard{
// c_state variables
address immutable private i_crf;
address payable immutable public i_creator;
address immutable public i_rwdFactory;
string public s_title;
string public s_description;
string public s_category;
Expand All @@ -39,8 +41,8 @@ contract Campaign is KeeperCompatibleInterface, ReentrancyGuard{
uint256 private immutable i_initTimeStamp;
uint256 private constant i_maxDur = 5184000;
uint256 public deadline;
C_State public c_state = C_State.Fundraising; // default c_state
uint256 private rId;
C_State public c_state = C_State.Fundraising; // default c_state

struct CampaignObject {
address i_creator;
Expand Down Expand Up @@ -83,6 +85,7 @@ contract Campaign is KeeperCompatibleInterface, ReentrancyGuard{
constructor (
address _crowdfunder,
address _creator,
address _rwdFactory,
string memory _title,
string memory _description,
string memory _category,
Expand All @@ -91,6 +94,7 @@ contract Campaign is KeeperCompatibleInterface, ReentrancyGuard{
uint256 _duration,
string memory _imageURI
) {
i_rwdFactory = _rwdFactory;
i_crf = _crowdfunder;
i_creator = payable(_creator);
s_title = _title;
Expand All @@ -105,13 +109,6 @@ contract Campaign is KeeperCompatibleInterface, ReentrancyGuard{
currentBalance = 0;
}

// function timeBox(address _upkeepCreatorAddress, address _linkTokenAddress, address _campaignAddress) external isCreator {
// // UpkeepIDConsumer newUpkeepCreator = UpkeepIDConsumer(_upkeepCreatorAddress);
// // LinkTokenInterface token = LinkTokenInterface(_linkTokenAddress);
// // if(token.balanceOf(_upkeepCreatorAddress) == 0){revert("no funds");}
// // rId = newUpkeepCreator.registerAndPredictID(s_title, "0x", _campaignAddress, 500000, i_creator, "0x", "0x", 2000000000000000000);
// }

function donate(address _donator, bool _rewardable) public payable nonReentrant{
if(msg.sender != i_crf){revert();}
if(c_state != C_State.Fundraising){revert();}
Expand Down Expand Up @@ -179,19 +176,11 @@ contract Campaign is KeeperCompatibleInterface, ReentrancyGuard{
delete entDonations[_donator];
}

function makeReward(
uint256 _price, string memory _title,
// string memory _detsLink,
string memory _description, string memory _rpic,
string[] memory _perks,
uint256 _deadline, uint256 _quantity, bool _infinite,
string[] memory _shipsTo
) external isCreator {
if(rewards[_price] != address(0)){revert();}
rKeys.push(_price);
Reward newReward = new Reward(address(this), i_creator, _price, _title,
_description, _rpic, _perks, _deadline, _quantity, _infinite, _shipsTo);
rewards[_price] = address(newReward);
function makeReward(RewardFactory.rwdInput memory _rwd) external isCreator {
if(rewards[_rwd.price] != address(0)){revert();}
rKeys.push(_rwd.price);
address newReward = RewardFactory(i_rwdFactory).createReward(address(this), i_creator, _rwd);
rewards[_rwd.price] = newReward;
}

function endCampaign() external isCreator {
Expand Down
33 changes: 33 additions & 0 deletions contracts/CampaignFactory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.11;

import { Campaign } from "./Campaign.sol";

contract CampaignFactory {
struct cmpInput {
string _title;
string _description;
string _category;
string _tags;
uint256 _goalAmount;
uint256 _duration;
string _imageURI;
}

function createCampaign(address _crf, address payable _creator, address _rwdFactory, cmpInput memory _cmp) public returns (address) {
Campaign cmp = new Campaign(
_crf,
_creator,
_rwdFactory,
_cmp._title,
_cmp._description,
_cmp._category,
_cmp._tags,
_cmp._goalAmount,
_cmp._duration,
_cmp._imageURI
);

return address(cmp);
}
}
42 changes: 19 additions & 23 deletions contracts/CrowdFunder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pragma solidity ^0.8.11;

import { UpkeepIDConsumer } from "./UpkeepIDConsumer.sol";
import { LinkTokenInterface } from "@chainlink/contracts/src/v0.8/interfaces/LinkTokenInterface.sol";
import { CampaignFactory } from "./CampaignFactory.sol";
import "./Campaign.sol";

// errors
Expand All @@ -12,12 +13,6 @@ import "./Campaign.sol";
// error Crf_RefF();
// error Crf_PubF();

// contract CampaignFactory {
// function createCampaign() external returns(Campaign) {
// return new Campaign();
// }
// }

contract CrowdFunder {
// using SafeMath for uint256;

Expand Down Expand Up @@ -62,9 +57,16 @@ contract CrowdFunder {
address _creator
);

address immutable public i_cmpFactory;
address immutable public i_rewardFactory;
uint256 public campaignCounter;
mapping (address => address) private campaigns;

constructor (address _cmpFactory, address _rwdFactory){
i_rewardFactory = _rwdFactory;
i_cmpFactory = _cmpFactory;
}

function addUser(
address _address, string memory _username,
string memory _email,
Expand All @@ -74,24 +76,18 @@ contract CrowdFunder {
emit UserAdded(_address, _username, _email, _shipAddress, _pfp);
}

function addCampaign (
string memory _title,
string memory _description,
string memory _category,
string memory _tags,
uint256 _goalAmount,
uint256 _duration,
string calldata _imageURI
) external {
Campaign newCampaign = new Campaign(
address(this),
payable(msg.sender), _title,
_description, _category,
_tags, _goalAmount,
_duration, _imageURI
);
function addCampaign (CampaignFactory.cmpInput memory _cmp) external {
address newCampaign = CampaignFactory(i_cmpFactory).createCampaign(address(this), payable(msg.sender), i_rewardFactory, _cmp);
campaigns[address(newCampaign)] = address(newCampaign);
emit CampaignAdded(address(newCampaign), msg.sender, _title, _description, _category, _tags, _imageURI);
emit CampaignAdded(
newCampaign,
msg.sender,
_cmp._title,
_cmp._description,
_cmp._category,
_cmp._tags,
_cmp._imageURI
);
}

function donateToCampaign(address _campaignAddress, bool _rewardable) external payable {
Expand Down
1 change: 0 additions & 1 deletion contracts/Reward.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ contract Reward {
address _creator,
uint256 _price,
string memory _title,
// string memory _detsLink,
string memory _description,
string memory _rpic,
string[] memory _perks,
Expand Down
36 changes: 36 additions & 0 deletions contracts/RewardFactory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.11;

import { Reward } from "./Reward.sol";

contract RewardFactory {
struct rwdInput {
uint256 price;
string _title;
string _description;
string _rpic;
string[] _perks;
uint256 _deadline;
uint256 _quantity;
bool _infinite;
string[] _shipsTo;
}

function createReward(address _cmpAddress, address _creator, rwdInput memory _rwd) public returns (address) {
Reward rwd = new Reward(
_cmpAddress,
_creator,
_rwd.price,
_rwd._title,
_rwd._description,
_rwd._rpic,
_rwd._perks,
_rwd._deadline,
_rwd._quantity,
_rwd._infinite,
_rwd._shipsTo
);

return address(rwd);
}
}
34 changes: 34 additions & 0 deletions deploy/03-deploy-rewardFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { network, ethers } from "hardhat"
import { DeployFunction } from "hardhat-deploy/dist/types"
import { HardhatRuntimeEnvironment } from "hardhat/types"
import { developmentChains, networkConfig } from "../helper-hardhat-config"
import verify from "../utils/verify"
import hasKey from "../utils/hasKey"

const deployRewardFactory: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
const { deployments, getNamedAccounts } = hre
const { deploy, log } = deployments
const { deployer } = await getNamedAccounts()
const chainId = network.config.chainId
const waitBlockConfirmations = chainId?.toString() == "31337" ? 1 : 4

log("==========================")
if(hasKey(networkConfig, chainId!))
{ const args:any[] = []
const rewardFactory = await deploy("RewardFactory", {
from: deployer,
args,
log: true,
waitConfirmations: waitBlockConfirmations
})

if(!developmentChains.includes(network.name) && process.env.ETHERSCAN_API_KEY) {
log("Verifying...")
await verify(rewardFactory.address, args)
}
log("==========================")
}
}

export default deployRewardFactory
deployRewardFactory.tags = ["all", "rewardfactory"]
34 changes: 34 additions & 0 deletions deploy/04-deploy-campaignFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { network, ethers } from "hardhat"
import { DeployFunction } from "hardhat-deploy/dist/types"
import { HardhatRuntimeEnvironment } from "hardhat/types"
import { developmentChains, networkConfig } from "../helper-hardhat-config"
import verify from "../utils/verify"
import hasKey from "../utils/hasKey"

const deployCampaignFactory: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
const { deployments, getNamedAccounts } = hre
const { deploy, log } = deployments
const { deployer } = await getNamedAccounts()
const chainId = network.config.chainId
const waitBlockConfirmations = chainId?.toString() == "31337" ? 1 : 4

log("==========================")
if(hasKey(networkConfig, chainId!))
{ const args:any[] = []
const campaignFactory = await deploy("CampaignFactory", {
from: deployer,
args,
log: true,
waitConfirmations: waitBlockConfirmations
})

if(!developmentChains.includes(network.name) && process.env.ETHERSCAN_API_KEY) {
log("Verifying...")
await verify(campaignFactory.address, args)
}
log("==========================")
}
}

export default deployCampaignFactory
deployCampaignFactory.tags = ["all", "campaignfactory"]
Loading

0 comments on commit 6042a92

Please sign in to comment.