I try to create a contract which reads the current price of a token. This contract will receive eth in deposit. Then reading the amount received it will send the corresponding amount of tokens to the address that sent those eth to the contract.
A kind of swap but internal to the contract.
What I really want to do is that the addresses can exchange eth for a specific token but only by executing orders to send that token, so the user who sends eth only pays the sending fee and the contract sends that token that it already has previously.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@uniswap/v2-core/contracts/interfaces/IUniswapV2Pair.sol";
contract Mother is Ownable {
address private constant MUZZ_ADDRESS = 0xef3dAa5fDa8Ad7aabFF4658f1F78061fd626B8f0;
address private constant MUZZ_ETH_PAIR = 0x06D9CdDD7FCaF2e93573D04bE5DE2fc4dEA45F45;
address private muzzTokenAddress; // declare the variable muzzTokenAddress
address private muzzEthPairAddress; // declare the variable muzzTokenAddress
constructor() Ownable(msg.sender) {
// Constructor de la clase Ownable con la dirección del propietario inicial
}
function setMuzzTokenAddress(address _muzzTokenAddress) external onlyOwner {
muzzTokenAddress = _muzzTokenAddress;
}
function setMuzzEthPairAddress(address _muzzEthPairAddress) external onlyOwner {
muzzEthPairAddress = _muzzEthPairAddress;
}
function deposit() external payable {
// Función para recibir ETH
}
function depositToken(address token, uint256 amount) external {
IERC20(token).transferFrom(msg.sender, address(this), amount);
}
function withdrawEth(uint256 amount) external onlyOwner {
require(amount <= address(this).balance, "Insufficient ETH balance");
payable(owner()).transfer(amount);
}
function withdrawToken(address token, uint256 amount) external onlyOwner {
require(IERC20(token).balanceOf(address(this)) >= amount, "Insufficient token balance");
IERC20(token).transfer(owner(), amount);
}
function autoSendTokens() external onlyOwner {
uint256 ethReceived = address(this).balance;
require(ethReceived > 0, "No ETH received");
uint256 muzzPrice = getUniswapPrice(MUZZ_ETH_PAIR);
uint256 tokensToSend = calculateTokens(ethReceived, muzzPrice);
sendTokens(MUZZ_ADDRESS, tokensToSend, msg.sender);
}
function getUniswapPrice(address pairAddress) internal view returns (uint256 price) {
IUniswapV2Pair pair = IUniswapV2Pair(pairAddress);
(uint112 reserve0, uint112 reserve1, ) = pair.getReserves();
if (reserve0 > 0) {
price = (uint256(reserve1) * 1e18) / uint256(reserve0);
}
}
function calculateTokens(uint256 ethAmount, uint256 price) internal pure returns (uint256) {
uint256 tokensToSend = (ethAmount * price * 98) / (100 * 1e18);
return tokensToSend;
}
function sendTokens(address token, uint256 amount, address recipient) internal {
require(IERC20(token).balanceOf(address(this)) >= amount, "Insufficient token balance");
IERC20(token).transfer(recipient, amount);
}
}