I have a Lottery smart contract where users can purchase a lottery ticket with Ether and Ticket Price is 0.1 ether.
When the winning limit (0.4 ether) is reached, the lottery ends and 25% of funds send to the winner and 75 will be used to add liquidity in uniswap.
As we can add liquidity in 1:1 so, I divided 75% by 2 and get its value in two variables.
swapETHForExactTokens with first half and adding Liqudity with other eth + daiTokens.
I am getting error when calling addLiquidity function. I called approve function also and approved dai tokens to be used by uniRouter.
This is my function:
function Participate() payable public {
require(msg.value == ticketPrice,"please set correct price");
if(address(this).balance < winningsLimit || address(this).balance == winningsLimit) {
pot += msg.value;
participants.push(msg.sender);
}
if (pot > winningsLimit || pot == winningsLimit) {
EndLottery();
}
}
function EndLottery() internal{
//total payments in pot
uint totalPayout = pot;
pot = 0;
// Take 75% for the Liquidity Pool (rounded down by int-division)
uint liquidityAmount = totalPayout.mul(liquidityPercentage).div(100);
// Pay 25% to the winner
uint payoutToWinner = totalPayout.sub(liquidityAmount);
winner = pickWinner();
winner.transfer(payoutToWinner);
if(liqState == LiquidityState.EthToToken){
// split the contract balance into halves
uint half = liquidityAmount.div(2);
uint otherHalf = liquidityAmount.sub(half);
// capture the contract's current ETH balance.
// this is so that we can capture exactly the amount of ETH that the
// swap creates, and not make the liquidity event include any ETH that
// has been manually sent to the contract
uint256 initialBalance = address(this).balance;
//swao ETH for tokens
require(convertEthToDai(half) == true,"convertEthToDai: Failed");
//how much ETH did we just swap into?
uint256 newBalance = address(this).balance;
//using remaining half to add liqdity
addLiquidityEthereum(otherHalf, newBalance);
}
}
function convertEthToDai(uint daiAmountToBuy, uint ethAmount) internal returns(bool){
uint deadline = block.timestamp; // using 'now' for convenience, for mainnet pass deadline from frontend!
uniswapRouter.swapETHForExactTokens{ value: ethAmount }(daiAmountToBuy, getPathForETHtoDAI(), address(this), deadline);
// refund leftover ETH to user
(bool success,) = msg.sender.call{ value: address(this).balance }("");
require(success, "refund failed");
return true;
}
function addLiquidityEthereum(uint256 tokenAmount, uint256 ethAmount) internal returns(bool){
// uint bal = payable(address(this)).balance;
//approving tokens to uniswapRouter
require(daiToken.approve(multiDaiKovan,tokenAmount),'approve failed');
require(daiToken.allowance(multiDaiKovan,address(this)) != 0,"zero allowance, pleae approve DAI first to be used by this contract");
uniswapRouter.addLiquidityETH{value: ethAmount}(
address(daiToken),
tokenAmount,//tkn amount
tokenAmount, // slippage is unavoidable
ethAmount, // slippage is unavoidable
address(this),
block.timestamp + 15
);
// refund leftover ETH to user
(bool success,) = msg.sender.call{ value: address(this).balance }("");
require(success, "refund failed");
}
When ConvertEthToDai function is called, it uses all ether’s in contract. I want it to use only half of ethers.