-1

I tried compile my contract for about 2 days now. I keep getting the error: ParserError: Expected pragma, import directive or contract/interface/library/struct/enum/constant/function/error definition.--> project:/contracts/Donation.sol:57:5:|57 | }| ^but i can't figure out what is wrong with my code. Anyone to assist me.

my code:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

contract DonationContract {
    struct Donation {
        string pickupAddress;
        string pickupDate;
        string pickupTime;
        string availabilityDate;
        string pickupHours;
        string itemType;
        string otherItem;
        string itemDescription;
        uint quantity;
        string requiresRefrigeration;
        string bestConsumedDate;
        string partialDonation;
    }

    Donation[] public donations;

    function createDonation(
        string memory _pickupAddress,
        string memory _pickupDate,
        string memory _pickupTime,
        string memory _availabilityDate,
        string memory _pickupHours,
        string memory _itemType,
        string memory _otherItem,
        string memory _itemDescription,
        uint _quantity,
        string memory _requiresRefrigeration,
        string memory _bestConsumedDate,
        string memory _partialDonation 
    )

    public {
        Donation memory newDonation = Donation(
            _pickupAddress,
            _pickupDate,
            _pickupTime,
            _availabilityDate,
            _pickupHours,
            _itemType,
            _otherItem,
            _itemDescription,
            _quantity,
            _requiresRefrigeration,
            _bestConsumedDate,
            _partialDonation
        );
        donations.push(newDonation);
    }
    function getDonationCount() public view returns (uint){
        return donations.length;
    }
}          
            
            
            
           
           
       

trying to save the content of a form to blockchain after a user fill it. i am new solidity

Tinawaa1
  • 7
  • 2

1 Answers1

1

(1) Switch to solc version 0.8.20 - the current latest.

(2) If you compile, you will get a different compiler error: "Stack too deep."

CompilerError: Stack too deep. Try compiling with `--via-ir` (cli) or the equivalent `viaIR: true` (standard JSON) while enabling the optimizer. Otherwise, try removing local variables. When compiling inline assembly: Variable headStart is 1 slot(s) too deep inside the stack. Stack too deep. Try compiling with `--via-ir` (cli) or the equivalent `viaIR: true` (standard JSON) while enabling the optimizer. Otherwise, try removing local variables.

(3) Reduce the size of your createDonation function parameters to fix the above. There is a maximum possible size, and you have exceeded it.

Commenting out just one appears to be sufficient in your contract:

// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;

contract DonationContract {
    struct Donation {
        // string pickupAddress;
        string pickupDate;
        string pickupTime;
        string availabilityDate;
        string pickupHours;
        string itemType;
        string otherItem;
        string itemDescription;
        uint quantity;
        string requiresRefrigeration;
        string bestConsumedDate;
        string partialDonation;
    }

    Donation[] public donations;

    function createDonation(
        // string memory _pickupAddress,
        string memory _pickupDate,
        string memory _pickupTime,
        string memory _availabilityDate,
        string memory _pickupHours,
        string memory _itemType,
        string memory _otherItem,
        string memory _itemDescription,
        uint _quantity,
        string memory _requiresRefrigeration,
        string memory _bestConsumedDate,
        string memory _partialDonation
    )

    public {
        Donation memory newDonation = Donation(
            // _pickupAddress,
            _pickupDate,
            _pickupTime,
            _availabilityDate,
            _pickupHours,
            _itemType,
            _otherItem,
            _itemDescription,
            _quantity,
            _requiresRefrigeration,
            _bestConsumedDate,
            _partialDonation
        );
        donations.push(newDonation);
    }
    function getDonationCount() public view returns (uint){
        return donations.length;
    }
}

This does compile successfully.


Further details

(1) Note that switching to the latest version of solc isn't strictly necessary. Just a recommendation if you're starting a new project.

(2) The reason that there is a max size is that the EVM stack only has 16 slots, and that is (sometimes) insufficient to fit all the variables/ parameters/ return values. Having too many parameters is generally not considered to be good Solidity programming style, and therefore to be avoided through refactoring.

(3) So in this case, how would to refactor your code? I would recommend instead of strings you may want to consider other data types for the values used in your struct. Ones that are more compact, and more specific to the types of information you are trying to represent. For example you could:

  • Combine _pickupDate and _pickupTime into a single uint256 by using that to store the number of seconds since epoch. See Unix time.
  • Do the same for the other dates.
  • Switch _requiresRefrigeration and _partialDonation to bool if these are only true or false.
bguiz
  • 27,371
  • 47
  • 154
  • 243