Balance
4.4896 $ZTH
Type
Contract
Transactions
1407
This contract's methods and events are decoded across the explorer.
Prefer source verification? Verify from Foundry or Hardhat against the Etherscan-compatible API to publish the full source too.
// File: RoyaSwap.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;
interface IERC20Swap {
function transfer(address to, uint256 value) external returns (bool);
function transferFrom(address from, address to, uint256 value) external returns (bool);
function balanceOf(address who) external view returns (uint256);
}
contract RoyaSwap {
address public owner;
address public factory;
uint256 private _unlocked = 1;
mapping(address => uint256) public rateOf;
event Swapped(address indexed user, address indexed tokenIn, address indexed tokenOut, uint256 amountIn, uint256 amountOut);
event RateSet(address indexed token, uint256 rate);
event OwnerUpdated(address indexed oldOwner, address indexed newOwner);
error NotOwner();
error NotFactory();
error Reentrancy();
error ZeroAddress();
error ZeroAmount();
error ZeroRate();
error SameToken();
error RateNotSet(address token);
error RateAlreadySet();
error ZeroOutput();
error TooLittleOut(uint256 expected, uint256 actual);
error NotEnoughInventory(address token, uint256 needed, uint256 available);
modifier onlyOwner() {
if (msg.sender != owner) revert NotOwner();
_;
}
modifier lock() {
if (_unlocked != 1) revert Reentrancy();
_unlocked = 2;
_;
_unlocked = 1;
}
constructor() {
owner = msg.sender;
}
receive() external payable {}
function setRate(address token, uint256 rate) external onlyOwner {
if (token == address(0)) revert ZeroAddress();
if (rate == 0) revert ZeroRate();
rateOf[token] = rate;
emit RateSet(token, rate);
}
function setFactory(address factory_) external onlyOwner {
if (factory_ == address(0)) revert ZeroAddress();
factory = factory_;
}
function setTokenRate(address token, uint256 rate) external {
if (msg.sender != factory && msg.sender != owner) revert NotFactory();
if (token == address(0)) revert ZeroAddress();
if (rate == 0) revert ZeroRate();
if (rateOf[token] != 0) revert RateAlreadySet();
rateOf[token] = rate;
emit RateSet(token, rate);
}
function transferOwner(address newOwner) external onlyOwner {
if (newOwner == address(0)) revert ZeroAddress();
emit OwnerUpdated(owner, newOwner);
owner = newOwner;
}
function swapExactZthForToken(address token, uint256 minOut) external payable lock {
uint256 amountIn = msg.value;
if (token == address(0)) revert ZeroAddress();
if (amountIn == 0) revert ZeroAmount();
uint256 rate = rateOf[token];
require(rate != 0, "No rate set");
uint256 out = (amountIn * rate) / 1e18;
if (out == 0) revert ZeroOutput();
if (out < minOut) revert TooLittleOut(minOut, out);
uint256 available = IERC20Swap(token).balanceOf(address(this));
if (available < out) revert NotEnoughInventory(token, out, available);
if (!IERC20Swap(token).transfer(msg.sender, out)) revert NotEnoughInventory(token, out, available);
emit Swapped(msg.sender, address(0), token, amountIn, out);
}
function swapExactTokenForZth(address token, uint256 amountIn, uint256 minOut) external lock {
if (token == address(0)) revert ZeroAddress();
if (amountIn == 0) revert ZeroAmount();
uint256 rate = rateOf[token];
require(rate != 0, "No rate set");
uint256 out = (amountIn * 1e18) / rate;
if (out == 0) revert ZeroOutput();
if (out < minOut) revert TooLittleOut(minOut, out);
require(address(this).balance >= out, "Insufficient ZTH liquidity in contract");
if (!IERC20Swap(token).transferFrom(msg.sender, address(this), amountIn)) revert ZeroAmount();
(bool sent, ) = msg.sender.call{value: out}("");
if (!sent) revert NotEnoughInventory(address(0), out, address(this).balance);
emit Swapped(msg.sender, token, address(0), amountIn, out);
}
function swapTokenForToken(address tokenIn, address tokenOut, uint256 amountIn, uint256 minOut) external lock {
if (tokenIn == address(0) || tokenOut == address(0)) revert ZeroAddress();
if (tokenIn == tokenOut) revert SameToken();
if (amountIn == 0) revert ZeroAmount();
uint256 rateIn = rateOf[tokenIn];
uint256 rateOut = rateOf[tokenOut];
require(rateIn != 0 && rateOut != 0, "No rate set");
uint256 out = (amountIn * rateOut) / rateIn;
if (out == 0) revert ZeroOutput();
if (out < minOut) revert TooLittleOut(minOut, out);
uint256 available = IERC20Swap(tokenOut).balanceOf(address(this));
if (available < out) revert NotEnoughInventory(tokenOut, out, available);
if (!IERC20Swap(tokenIn).transferFrom(msg.sender, address(this), amountIn)) revert ZeroAmount();
if (!IERC20Swap(tokenOut).transfer(msg.sender, out)) revert NotEnoughInventory(tokenOut, out, available);
emit Swapped(msg.sender, tokenIn, tokenOut, amountIn, out);
}
function withdrawZth(address to, uint256 amount) external onlyOwner {
if (to == address(0)) revert ZeroAddress();
(bool sent, ) = to.call{value: amount}("");
if (!sent) revert ZeroAmount();
}
function withdrawToken(address token, address to, uint256 amount) external onlyOwner {
if (to == address(0)) revert ZeroAddress();
if (!IERC20Swap(token).transfer(to, amount)) revert ZeroAmount();
}
}
No transactions found
This address has no recorded transactions yet