0

I am having a persistent issue programming receive fallback function on an smart contract where I am programming an staged pre-sale of a Token. When I try on Remix transaction looks as done, but it doesn't update the state as needed, and in truffle, I can't get any transaction ID to debug as it always fails.

https://github.com/artfortressonlinenft/master_solidityv2/blob/master/contracts/Ancestry.sol

On line 437 is the funcion buyTokens called by receive, and buyTokens call preValidatePurchase where I think is the fault. I am not sure.

I am a very begineer solidity developer already learning... and I'll be grateful if could you provide some advice. Thanks in advance.

/**
     * @dev Extend parent behavior requiring purchase to respect investor min/max funding cap.
     * @param _beneficiary Token purchaser
     * @param _weiAmount Amount of wei contributed
     */
    function _preValidatePurchase(address _beneficiary, uint256 _weiAmount)
        internal
    {
        uint256 _existingContribution;
        uint256 _newContribution;

        require(
            _beneficiary != address(0),
            "Address 0x000...000 can't purchase"
        );

        if (stage == CrowdsaleStage.PreICO1) {
            require(
                investorTrack[_beneficiary].isWhtlP1 == true,
                "Sorry your address has not been whitelisted to enter Private Sale 1"
            );

            _existingContribution = investorTrack[_beneficiary].contributionP1;
            _newContribution = _existingContribution.add(_weiAmount);
            require(
                _newContribution >= investorMinCap &&
                    _newContribution <= investorMaxCap,
                "You can contribute from 0.2 to 1 BNB, no more as total"
            );
            investorTrack[_beneficiary].contributionP1 = _newContribution;
        } else if (stage == CrowdsaleStage.PreICO2) {
            require(
                investorTrack[_beneficiary].isWhtlP2 == true,
                "Sorry your address has not been whitelisted to enter Private Sale 2"
            );

            _existingContribution = investorTrack[_beneficiary].contributionP2;
            _newContribution = _existingContribution.add(_existingContribution);
            _newContribution = _newContribution.add(_weiAmount);
            require(
                _newContribution >= investorMinCap &&
                    _newContribution <= investorMaxCap,
                "You can contribute from 0.2 to 1 BNB, no more as total on both presales"
            );
            investorTrack[_beneficiary].contributionP2 = _newContribution;
        } else revert("Private Sales phases are both ended");
    }
5
  • Did you try debuggin in Remix? I'll suggest to comment and uncomment code until you arrive to the line that causes the revert.
    – Ismael
    Commented Jan 5, 2022 at 23:09
  • 1
    Thanks for you answer. I have seen that receive function has a fix low gas quantity to spent and the solution was on turning mere receive function into an external payable one...
    – ealco85
    Commented Jan 6, 2022 at 12:59
  • Great! The famous gas stipend limit of 2300. It will be nice if you add that as the answer so the question can be marked as resolved.
    – Ismael
    Commented Jan 6, 2022 at 14:56
  • Of course! could you please tell me how can I do that?
    – ealco85
    Commented Jan 6, 2022 at 19:23
  • There's a text area below the question where you can write how did you solve the problem. You will find more details about the gas stipend in this question ethereum.stackexchange.com/questions/77521/….
    – Ismael
    Commented Jan 7, 2022 at 3:44

1 Answer 1

1

I have seen that receive function has a fix low gas quantity to spent and the solution was on turning mere receive function into an external payable one...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.