Aún no tienes acceso a esta clase

Crea una cuenta y continúa viendo este curso

Terminando nuestro contrato

18/21
Recursos

Aportes 9

Preguntas 5

Ordenar por:

¿Quieres ver más aportes, preguntas y respuestas de la comunidad? Crea una cuenta o inicia sesión.

Dificil el ultimo desafio, tuve que apoyarme con material de otros e internet xD

// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;

contract CrowdFunding {

    struct Contribution {
        address contributor;
        uint amount;
    }

    enum Status { Active, Inactive }
    struct Project {
        string id;
        string name;
        string description;
        uint fundraisingGoal;
        address payable wallet;
        address owner;
        uint funds;
        Status status;
    }

    Project project;
    Project[] public projectCluster;
    
    mapping(string => Contribution[]) public contributions;
    uint private initialFunds = 0;
    
    event NewFundNotification(address sender, uint fundAmount);
    
    event NewStatusChange(Status newStatus);

    modifier ownerNotSendFunds() {
        require(project.owner != msg.sender, "Owners shouldnt send funds to its own projects!");
        _;
    }

    modifier onlyOwnerModifyStates() {
         require(project.owner == msg.sender, "You must be the project owner!");
         _;
    }

    constructor(string memory _id, string memory _name, string memory _description, uint _fundraisingGoal) {
        project = Project(
            _id,
            _name,
            _description,
            _fundraisingGoal,
            payable(msg.sender),
            msg.sender,
            initialFunds,
            Status.Active
        );
        projectCluster.push(project);
    }

    function createProject(string memory _id, string memory _name, string memory _description, uint _fundraisingGoal) public {
        project = Project(
            _id,
            _name,
            _description,
            _fundraisingGoal,
            payable(msg.sender),
            msg.sender,
            initialFunds,
            Status.Active
        );
        projectCluster.push(project);
    }

    function fundProject(uint index) public payable ownerNotSendFunds {
        Project memory project = projectCluster[index];
        require(project.status == Status.Active, "This project state is Closed!");
        require(msg.value > 0, "Funds can not be Zero!");

        project.wallet.transfer(msg.value);
        project.funds += msg.value;
        projectCluster[index] = project;
        contributions[project.id].push(Contribution(msg.sender, msg.value));
        emit NewFundNotification(msg.sender, msg.value);
    }

    function changeProjectStatus(Status newStatus) public onlyOwnerModifyStates {
        require(project.status != newStatus, "Project has that state already, choose another!");
        project.status = newStatus;
        emit NewStatusChange(newStatus);
    }

}

Proyecto finalizado 😃 CrowdFunding repositorio

Que proyecto tan genial! Me gusto mucho la forma como se fue llevando 😃

Por fin, mi primer Smart Contract.

Proyecto Terminado, dejo el repo con mis versiones.
Repositorio

Proyecto terminado

A los fines educativos esta bien, pero no veo como alguien pueda saber el indice interno del array donde esta el proyecto que quiere fondear.
Creo que habria sido mejor usar un mapping con el identificador que se pasa para crear el proyecto… amen q debe verificarse tambien que esa key no exista previamente.

si alguno le llega a pasar en la funcion de createProject tienen que declararla como “public”

Mi desarrollo del contrato! Un gusto aprender con ustedes 😃

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;

contract crowfundingContract {

    enum Status { Opened, Closed }
    struct ProjectData {
        string name;
        string description;
        string status;
        uint fundingGoal;
        uint funds;
        Status statusID;
    }
    struct ContributorData {
        address contributor;
        uint contributedEther;
    }
    
    ProjectData[] public projects;
    
    mapping (uint => ContributorData[]) public contributors;
    mapping (uint => address) public projectOwner;

    event fundedBy (
        string project,
        address participant,
        uint vulue
    );
    event changedBy (
        string project,
        address author,
        string newStatus
    );

    function createProject(string memory _projectName, string memory _description, uint _fundingGoal) public {
        projects.push(ProjectData(_projectName,_description,"Opened",_fundingGoal,0,Status.Opened));
        uint id = projects.length -1;
        projectOwner[id] = msg.sender;
    }

    function fundProject(uint projectIndex) public payable fundRestriction (projectIndex) {
        ProjectData memory selectedProject = projects[projectIndex];
        require(selectedProject.statusID == Status.Opened, "Project is closed! you cannot fund");
        require(msg.value > 0, "You cannot fund 0 wei!");
        address payable author = payable(projectOwner[projectIndex]);
        author.transfer(msg.value);
        selectedProject.funds += msg.value;
        contributors[projectIndex].push(ContributorData(msg.sender,msg.value));

        emit fundedBy(selectedProject.name, msg.sender, msg.value);
    }

    function changeProjectStatus(Status newStatus, uint projectIndex) public changeStatusRestriction(projectIndex) {
        ProjectData memory selectedProject = projects[projectIndex];
        require(newStatus != selectedProject.statusID, "Status already selected!");
        if(newStatus == Status.Opened){
            selectedProject.status = "Opened";
        }else{
            selectedProject.status = "Closed";
        }
        selectedProject.statusID = newStatus;

        emit changedBy(selectedProject.name, msg.sender, selectedProject.status);
    }

    modifier changeStatusRestriction(uint projectIndex){
        require(
            msg.sender == projectOwner[projectIndex],
            "Only the owner can change the status"
        );
        _;
    }

    modifier fundRestriction(uint projectIndex){
        require(
            msg.sender != projectOwner[projectIndex],
            "You as the author cannot fund this project"
        );
        _;
    }
}