Smart Contract Code
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract EthOrUsdtTo4Art is Ownable {
IERC20 public fourArtToken;
address public priceOracle;
uint256 public constant BONUS_RATE = 10; // 10% bonus
event ExchangeCompleted(address indexed user, uint256 fourArtAmount);
constructor(
address _fourArtToken,
address _priceOracle
) Ownable(msg.sender) {
fourArtToken = IERC20(_fourArtToken);
priceOracle = _priceOracle;
}
function getPrices() public view returns (uint256, uint256) {
(bool success, bytes memory data) = priceOracle.staticcall(
abi.encodeWithSignature("getPrices()")
);
require(success, "Failed to fetch prices from oracle");
return abi.decode(data, (uint256, uint256));
}
function calculate4ArtAmount(
uint256 baseAmount
) internal pure returns (uint256) {
uint256 bonusAmount = (baseAmount * BONUS_RATE) / 100;
return baseAmount + bonusAmount;
}
receive() external payable {
if (msg.sender == owner()) {
return;
}
require(msg.value > 0, "Send some ETH");
// Get prices
(uint256 ethToUsdtPrice, uint256 usdtTo4ArtPrice) = getPrices();
// Calculate USDT equivalent
uint256 ethToUsdt = (msg.value * ethToUsdtPrice) / 1 ether;
// Calculate 4ART amount with bonus
uint256 base4ArtAmount = (ethToUsdt * 1 ether) / usdtTo4ArtPrice;
uint256 fourArtAmount = calculate4ArtAmount(base4ArtAmount);
require(
fourArtToken.transfer(msg.sender, fourArtAmount),
"4ART transfer failed"
);
emit ExchangeCompleted(msg.sender, fourArtAmount);
}
function withdraw4Art(uint256 amount) external onlyOwner {
require(
fourArtToken.transfer(owner(), amount),
"4ART withdrawal failed"
);
}
function withdrawEth() external onlyOwner {
(bool sent, ) = owner().call{value: address(this).balance}("");
require(sent, "ETH withdrawal failed");
}
}
Detailed Functionality Analysis
The EthOrUsdtTo4Art smart contract facilitates the swapping of ETH for 4ART tokens with a 10% bonus. Below is a breakdown of its features:
- getPrices(): Retrieves the current conversion rates from the price oracle for ETH to USDT and USDT to 4ART.
-
receive(): Handles ETH sent to the contract:
- Converts ETH to USDT using the oracle rate.
- Calculates the equivalent 4ART tokens, adding a 10% bonus.
- Transfers the calculated tokens to the sender.
- withdraw4Art(uint256 amount): Allows the owner to withdraw a specified amount of 4ART tokens.
- withdrawEth(): Lets the owner withdraw all ETH held by the contract.
-
Events:
- ExchangeCompleted logs successful swaps, providing transparency.
Why Use This Contract?
Users can trust this contract for the following reasons:
- Transparent Logic: The code is open-source and auditable.
- Bonus Incentive: Users receive a 10% bonus on every swap.
- Oracle Integration: Ensures real-time, accurate conversion rates.
- Secure Withdrawals: Only the owner can withdraw tokens or ETH.