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");
}