moringaman
3/22/2018 - 7:21 PM

Basic kickstarter contract

pragma solidity ^0.4.17;

contract campaignFactory {
    address[] public deployedContracts;
    
    function createCampaign(uint minimum) public {
        address newCampaign = new Campaign(minimum, msg.sender);
        deployedContracts.push(newCampaign);
    }
    
    function getDeployedContacts () public view returns (address[]) {
        return deployedContracts;
    }
}

contract Campaign {
    // decalre a new struct type called Request
    struct Request {
        string description;
        uint value;
        address recipient;
        bool complete;
        uint approvalCount;
        mapping(address => bool) approvals;
        
    }
    
     modifier restricted () {
        require(msg.sender == manager);
        _;
    }
    
    address public manager;
    // create a mapping to hold all approvers/contributers
    mapping (address => bool) public approvers;
    uint public approversCount;
    uint public minimumContribution;
    // create an array of type struct based on our Request struct called requests 
    Request[] public requests;
    // initiate contract manager abn minimumcContribution variables
    function Campaign (uint amount, address creator) public {
       manager = creator;
       minimumContribution = amount;
    }
    
    function contribute () public payable {
        require(msg.value > minimumContribution);
        // Add new approver into our mapping
        approvers[msg.sender] = true;
        approversCount++;
    }
    // create a new request for funds passing info into a newRequest object called Request
    function createRequest (string description, uint value, address recipient)
    public restricted {
        Request memory newRequest = Request({
            description: description,
            value: value,
            recipient: recipient,
            complete: false,
            approvalCount: 0
        });
   // push this struct instance into a requests array
        requests.push(newRequest);
    }
    
    function approveRequest(uint index) public {
        Request storage request = requests[index];
        //require(msg.sender != manager);
        require(approvers[msg.sender]);
        require(!request.approvals[msg.sender]);
        
        request.approvals[msg.sender] = true;
        request.approvalCount++;
    }
    
    function finalizeRequest(uint index) public restricted {
        // create a pointer to requests array to quiery
        Request storage request = requests[index];
        require(!request.complete);
        require(request.approvalCount > approversCount / 2);
        request.recipient.transfer(request.value);
        request.complete = true;
    }
}