I have a crowdsale campaign that i've created but cant seem to find the root cause of this.
Basically, when the campaign is expired, the buyer able to make a refund the token that they've bought during launch but they need to return the token in order to get back the ETH that they're purchased. When buying the token , the eth will transfer to the contract balance and it will revert back once the contract has received the token.
function buyTokens(
uint256 _amount
) public payable onlyWhiteListed onlyWhileOpen {
uint256 requiredEth = (_amount / 1e18) * price;
require(msg.value == requiredEth, "Invalid amount of Ether");
require(_amount <= maxPurchase && _amount >= minPurchase);
require(dappToken.balanceOf(address(this)) >= _amount);
purchaseAmount[msg.sender] += _amount;
require(dappToken.transfer(msg.sender, _amount));
tokensSold += _amount;
(bool success, ) = owner.call{value: requiredEth}("");
require(success, "Failed to send Ether");
emit Buy(_amount, msg.sender);
}
function refundCampaign() public onlyCampaignEnd {
require(refundStatus == true, "Refund is not enabled");
uint256 refundAmount = purchaseAmount[msg.sender];
require(refundAmount > 0, "You have not purchased any tokens");
purchaseAmount[msg.sender] = 0;
tokensSold -= refundAmount;
dappToken.transferFrom(msg.sender, address(this), refundAmount);
uint256 ethAmount = refundAmount * price;
require(address(this).balance >= ethAmount, "Insufficient balance");
(bool ethSuccess, ) = msg.sender.call{value: ethAmount}("");
require(ethSuccess, "Failed to send Ether");
emit Refund(msg.sender, refundAmount);
}
I can see the eth increased in deployer wallet but I'm still getting an error "Insufficient balance" when sending back eth to the buyer