A煤n no tienes acceso a esta clase

Crea una cuenta y contin煤a viendo este curso

Struct types

15/21
Recursos

Aportes 23

Preguntas 6

Ordenar por:

驴Quieres ver m谩s aportes, preguntas y respuestas de la comunidad? Crea una cuenta o inicia sesi贸n.

Struct types

Solidity permite al usuario crear su propio tipo de datos en forma de estructura. La estructura contiene un grupo de elementos con un tipo de datos diferente. Generalmente, se usa para representar un registro. Para definir una estructura se utiliza la palabra clave struct, que crea un nuevo tipo de datos.

struct <structure_name> {  
   <data type> variable_1;  
   <data type> variable_2; 
}

M谩s informaci贸n aqu铆

Anteriormente la variable state estaba declarada como string e inicializada con un valor, as铆: string public state = "Opened", en est谩 clase se cambi贸 el tipo de dato a uint, pero sin inicializar el valor de la variable quedando ahora as铆: uint public state. Al no darle un valor inicial la variable adquiere un valor por defecto, para el tipo de datos uint este valor por defecto es 0. M谩s informaci贸n en la Documentaci贸n de Solidity

Soluci贸n reto 5

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

contract Crowfunding{
    
    struct Project {
        int id;
        string name;
        bool isActive;
        address payable owner;
        uint founds;
        uint goal;
    }
    
    Project public project;
    
    constructor(string memory _name, uint _goal) {
        project = Project(1, _name, true, payable(msg.sender), 0, _goal);
    }
    
    event FundProject(address donor, uint quantity);
    
    event ChangedState(bool isActive);
    
    modifier onlyOwner() {
        require(project.owner == msg.sender, "You are not the project owner");
        _;
    }
    
    modifier notOwner() {
        require(project.owner != msg.sender, "You cannot transfer to your own project");
        _;
    }
    
    modifier onlyWhenActive() {
        require(project.isActive, "This project is not active.");
        _;
    }
    
    function fundProject() public payable notOwner onlyWhenActive {
        require(msg.value > 0, "You cannot fund 0 eth");
        project.owner.transfer(msg.value);
        project.founds += msg.value;
        emit FundProject(msg.sender, msg.value);
    }
    
    function changeProjectState() public onlyOwner {
        project.isActive = !project.isActive;
        emit ChangedState(project.isActive);
    }
}

Los struct me recuerdan a las 鈥渃lass鈥 o 鈥渋nterface鈥 en otros lenguajes.

Struct Types

struct Hero {
  string name;
  uint age;
  uint power;
  string team;
}
Hero cap = Hero('Cap',103, 80, 'Avengers');
cap.name  //Cap
cap.age   //103
cap.power  //80
cap.team  //Avengers

Aqui les dejo la forma de comparar strings en solidity:
keccak256(bytes(鈥渙pen鈥)) == keccak256(bytes(鈥渙pen鈥))

Batman: Poder 鈥淪oy millonario鈥 XD

Recuerden que al convertir la variable 鈥渟tate鈥 de string a uint, deben hacerlo igual en:

Event ProjectStateChanged()
function changeProjectState(uint newState)

Recuerden que Solidity es altamente tipado sino se hacen los cambios el compilador les dara error.

Record茅 mi 茅poca programando en C++ en la universidad. Dicho lenguaje tambi茅n utiliza la palabra clave Struct para definir interfaces de datos.
Solidity claramente est谩 inspirado en C++ y tiene cosas de Javascript tambi茅n.

Los Structs son como diccionatrios en python 馃槂

Funcion de los struct

Mi codigo falla cuando quiero setear el estado de nuevo a 0, la consola tira:

transact to CrowFunding.changeProjectState errored: VM error: out of gas.

out of gas
	The transaction ran out of gas. Please increase the Gas Limit.

Que puede ser ? Este es el SC:

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

contract CrowFunding {
    string public id;
    string public name;
    string public description;
    address payable public author;
    uint public state;
    uint public funds;
    uint public fundraisingGoal;

    event ProjectStateChanged(
        string newState
    );

    event FundsAdded (
        uint256 amount,
        address sender
    );

    error NotZeroValues(uint256 amount);
    error UnknownState(uint state);

    constructor(string memory _id, string memory _name, string memory _description, uint _fundraisingGoal) {
        id = _id;
        name = _name;
        description = _description;
        fundraisingGoal = _fundraisingGoal;
        author = payable(msg.sender);
    }

    modifier notAuthor {
        require(msg.sender != author, "Only people who are not the author can fund the project");
        // la function que tenga el modifier se inserta debajo de este simbolo
        _;
    }

    modifier onlyAuthor {
        require(msg.sender == author, "Only author can chanage the project state");
        // la function que tenga el modifier se inserta debajo de este simbolo
        _;
    }

    function fundProject() public payable notAuthor {
        require(state != 1, "The project is closed for new funds");
        require(msg.value > 0, "Fund must be greater than 0");
        author.transfer(msg.value);
        funds += msg.value;
        emit FundsAdded(funds, msg.sender);
    }

    function changeProjectState(uint newState) public onlyAuthor {
        state = newState;
        emit ProjectStateChanged("Project state changed");
    }
}

RETO #5 con Struct

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

contract projectFundPlatzi{
    struct Datos {  
        string  name;
        bool  isFundable;
        uint  deposits;
        uint  depositsGoal;
        uint  depositTest;
        address  autor;
        string  state;
        string  tokenLog;
        string  tokenFeedback;
    }
    Datos public dato;
    constructor(
        string memory _name, string  memory _state, uint _depositsGoal
        ){
            dato.name = _name;
            dato.isFundable = true;
            dato.deposits = 0;
            dato.depositsGoal=_depositsGoal;
            dato.autor = msg.sender; 
            dato.state = _state;
            dato.tokenLog = "Log: project status changed";
            dato.tokenFeedback = "Feedback: Gracias";
    }
    error stateNotDefined (string newState);

    event newFundFeedback(
        address autor,
        uint256 deposit,
        string Feedback
    );
    event newFundLog(
        uint256 depositsGoal,
        uint256 deposits,
        string Log
    );
    modifier notOnlyOwnerPay{
        require (dato.autor != msg.sender, "El propietario no puede abonar fondos al proyecto" );
        //la funcion es insertada donde aparece este simbolo _
        _;
    }
    modifier ownerRequired {
        require (msg.sender == dato.autor, "Esta accion es restringida al propietario.");
        _;
    }
    function viewDeposits() public view returns (uint){
        return dato.deposits;
    }

    function viewRemaining() public view returns(uint256){
        return dato.depositsGoal - dato.deposits;
    }

    function fundProject(address payable benefactor) public payable notOnlyOwnerPay {     
        require (msg.value > 0, "No se acepta abonos sin valor.");
        dato.depositTest = dato.deposits + msg.value;
       
        if (dato.isFundable == true && dato.depositTest <= dato.depositsGoal) {
            benefactor.transfer(msg.value);
            dato.deposits +=  msg.value;
            emit newFundFeedback (dato.autor, msg.value,dato.tokenFeedback);
        }
    
    }

    function changeProjectState(string memory _newState) public ownerRequired{
        require (keccak256(abi.encodePacked(_newState))  != keccak256(abi.encodePacked(dato.state)), "El nombre de estado es igual al actual y no es actualizable");
        if (keccak256(abi.encodePacked(_newState)) == keccak256(abi.encodePacked("Open"))){
            dato.isFundable = true;
            dato.state = "Open";
        }else if (keccak256(abi.encodePacked(_newState)) == keccak256(abi.encodePacked("Closed"))){
            dato.isFundable = false;
            dato.state = "Closed";
        } else {
            revert stateNotDefined (_newState);
        }          
       dato.isFundable = !dato.isFundable;
       emit newFundLog (dato.depositsGoal, dato.deposits, dato.tokenLog);
    }

Comparto mi soluci贸n:

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

contract PlatziPrueba {
    string public id;
    string public name;
    string public description;
    address payable public author;
    uint public stateCode = 1;
    string public stateDescription = "Opened";
    uint public funds;
    uint public fundralsInGoal;

    constructor (string memory _id, string memory _name, string memory _description, uint _fundralsInGoal) {
        id = _id;
        name = _name;
        description = _description;
        fundralsInGoal = _fundralsInGoal;
        author = payable(msg.sender);
    }

    modifier ownerNotAported() {
        require(msg.sender != author,
        "El propio owner no pude aportar a su mismo proyecto..!");

        _;
    }

    event eFoundProject(
        string id,
        uint value
    );

    function foundProject() public payable ownerNotAported{
        require(stateCode == 1, "No se puede aportar en estado cerrado");
        require(msg.value > 0, "Por favor ingresar un monto mayor a cero");
        author.transfer(msg.value);
        funds += msg.value;
        emit eFoundProject(id, msg.value);
    }

    modifier onlyOwnerChangeState() {
        require(msg.sender == author,
        "Solo el owner puede cambiar el estado...!");

        _;
    }

    event eChangeState(
        string id,
        string state
    );

    error errorChangeState(uint stateCode);

    function changeProjectState(uint newstate) public onlyOwnerChangeState {
        require(newstate != stateCode, "no puedes cambiar de estado a su mismo estado");
        if(newstate == 1) {
            stateCode = newstate;
            stateDescription = "Opened";
        } else if(newstate == 2) {
            stateCode = newstate;
            stateDescription = "Closed";
        } else {
            revert errorChangeState(newstate);
        }
        
        emit eChangeState(id, stateDescription);
    }
}

Asemejo mucho los struct types a las clases en Python

Me hac铆a muy mal no ver el c贸digo con los diccionarios o struct. Ahora no tardo tanto en leer y modificar el c贸digo

驴Como se comparan strings? si al igual que yo no quer铆an cambiar el tipo de dato de la variable state pueden comprar strings como una cadena de bloques y el c贸digo esta ac谩:
require(keccak256(abi.encodePacked(status)) != keccak256(abi.encodePacked(鈥渃losed鈥)), "Lo sentimos el proyecto esta cerrado ");

Este es mi avance del proyecto con el reto #5:

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

contract Proyecto_Platzi{

    struct projectInfo{
        string name;
        string description;
        string owner;
        address payable ownerWallet;
        uint state;
        uint goal;
        uint currentFund;
    }

    projectInfo public CrowFundingProject;

    constructor(string memory _name, string memory _owner, string memory _description, uint _goal){
        CrowFundingProject.name = _name;
        CrowFundingProject.description = _description;
        CrowFundingProject.owner = _owner;
        CrowFundingProject.ownerWallet = payable(msg.sender);
        CrowFundingProject.state = 0;
        CrowFundingProject.goal = _goal;
        CrowFundingProject.currentFund = 0;
    }

    modifier onlyOwner() {
        require(
            msg.sender == CrowFundingProject.ownerWallet,
            "Only owner can change the project state"
        );
        //la funci贸n es insertada en donde aparece este simbolo
        _;
    }


    modifier differentToTheOwner() {
        require(
            msg.sender != CrowFundingProject.ownerWallet,
            "The owner can't fund the project"
        );
        //la funci贸n es insertada en donde aparece este simbolo
        _;
    }

    event ChangeState(
        uint previousState,
        uint newState
    );

    event FundValueGiven(
        uint fundGiven,
        uint valueToGoal,
        string greetingMessage
    );

    function fundProject() public payable differentToTheOwner{
        require(CrowFundingProject.state != 1, "Sorry, this project is closed and cannot receive funds");
        require(msg.value > 0, "Sorry, fund value must be greater than 0, try again");
        CrowFundingProject.ownerWallet.transfer(msg.value);
        CrowFundingProject.currentFund += msg.value;
        emit FundValueGiven(msg.value, (CrowFundingProject.goal - msg.value), "Thanks for your contribution");
    }

    function changeProjectState(uint newState) public onlyOwner{
        require(CrowFundingProject.state != newState, "Sorry, to change the state you must put a different state from the current one");
        emit ChangeState(CrowFundingProject.state, newState);
        CrowFundingProject.state = newState;
    }
}

Reto #5

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.8.0;

contract CrowdFunding {
    struct Project {
        string id;
        string name;
        address payable authorAddress;
        bool isFundable;
        uint targetAmount;
        uint amountFunded;
    }
    
    Project public project;

    // Events
    event projectFunded(
        address sender,
        uint amount
    );
    
    event projectStateChanged (
        string projectName,
        string message
    );
    // ./Events
    
    constructor(string memory _id, string memory _projectName, uint _targetAmount){
        project = Project(_id, _projectName, payable(msg.sender), true, _targetAmount, 0);
    }
    
    function fundProject() public payable isNotAuthor canFund {
        require(msg.value > 0, 'The funded amount must be greater than 0');
        project.authorAddress.transfer(msg.value);
        project.amountFunded += msg.value;
        emit projectFunded(msg.sender, msg.value);
    }
    
    function changeProjectState(string calldata newState) public isAuthor {
        string memory currentState = project.isFundable ? string('opened') : string('closed');
        
        require(keccak256(abi.encode(newState)) == keccak256(abi.encode('opened')) || keccak256(abi.encode(newState)) == keccak256(abi.encode('closed')), 'This state is not defined');
        require(keccak256(abi.encode(newState)) != keccak256(abi.encode(currentState)), string(abi.encodePacked('This project is already ', currentState )));
        
        if(keccak256(abi.encode(newState)) == keccak256(abi.encode('opened'))){
            project.isFundable = true;
        } else if(keccak256(abi.encode(newState)) == keccak256(abi.encode('closed'))) {
            project.isFundable = false;
        }
        
        emit projectStateChanged(project.name, project.isFundable ? 'Project opened' : 'Project closed');
    }
    
    // Function modifiers
    modifier isAuthor(){
        require(project.authorAddress == msg.sender, "You must be the project author!");
        _;
    }
     
    modifier isNotAuthor() {
        require(project.authorAddress != msg.sender, "As author you can not fund your own project!");
        _;
    }
    
    modifier canFund(){
        require(project.isFundable == true, "This project is not available for funding!");
        _;
    }
    // ./Function modifiers
    
    function getGoal() public view returns(uint){
        return project.targetAmount;
    }
    
    function getFunds() public view returns(uint){
        return project.amountFunded;
    }
    
    function getStatus() public view returns(string memory){
        return project.isFundable ? 'Opened': 'Closed';
    }
}

Mi resultado al migrar todo a un struct type para el reto #5

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

contract CrowdFunding {
    
    struct Project {
        string name;
        string description;
        uint fundraisingGoal;
        address payable wallet;
        address owner;
        uint funds;
        uint status;
    }

    // Project Struct
    Project project;

    // Funds starts in zero.
    uint private initialFunds = 0;

    // Project Status:
    // status 0 = "Opened"
    // status 1 = "Closed"
    uint private initialState = 0;
    
    constructor(string memory _name, string memory _description, uint _fundraisingGoal) {
        project = Project(
            _name,
            _description,
            _fundraisingGoal,
            payable(msg.sender),
            msg.sender,
            initialFunds,
            initialState
        );
    }

    function fundProject() public payable ownerNotSendFunds {
        require(project.status == 0, "This project state is Closed!");
        require(msg.value > 0, "Funds can not be Zero!");

        project.wallet.transfer(msg.value);
        project.funds += msg.value;
        emit NewFundNotification(msg.sender, msg.value);
    }

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

    event NewFundNotification(
        address sender,
        uint fundAmount
    );
    
    event NewStatusChange(
        uint 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!");
         _;
    }

    function getGoal() public view returns(uint) {
        return project.fundraisingGoal;
    }
    
    function getStatus() public view returns(uint) {
        return project.status;
    }
    
    function getFunds() public view returns (uint) {
        return project.funds;
    }
    
}

Reto de clase anterior

pragma solidity ^0.7.0;


contract KickstarterPoor {
        
    string name;
    string description;
   
    bool opened;
    uint256 goal;
    uint256 funds;
     address private owner;
    
    address payable creatorPayable;
    
   
    
    constructor(string memory _name, string memory _description, uint256 _goal) payable{
        
        name = _name;
        description = _description;
        owner = msg.sender;
        creatorPayable = payable(owner);
        goal = _goal;
        funds = 0;
        opened = true;
    }
   
    
    event newFunds(
        address sender,
        uint256 funds
        );
        
    event projectState(
        uint256 goal,
        uint256 funds,
        string state
        );
    
    
    function checkStatus () public view returns (bool){
        
    return opened;
    }
    
    function getFunds() public view returns (uint256){
        return funds;
    }
        
    
    
    modifier noFundOwner (){
          require(msg.sender != owner, "You can't donate to yourself, I'll call the police.");
        _;
    }
    
   modifier onlyOwner (){
        require(msg.sender == owner, "You are not admin, go away");
        _;
    }
    
     function FundProyect() public payable noFundOwner{
         require(opened, "The project is already closed");
         require(msg.value > 0 , "Funds > 0");
     
              creatorPayable.transfer(msg.value);
        funds += msg.value;
        emit newFunds(msg.sender,msg.value);
        
       
    }
    
    
    function changeProjectState(bool  newState) public  onlyOwner{
        require(opened != newState, "The same status cannot be reapplied");
        
        opened = newState;
        
        emit projectState(goal,funds,opened ? "Opened" : "Closed");
    }
    
    
}```

Reto #5

struct Project {
        string public id;
        string public name;
        string public description;
        address payable public author;
        uint256 public state;
        uint256 public funds;
        uint256 public fundraisingGoal;
    }
    
    Project public project;

waaao los struc se usan en todos lados