|
| 1 | +// SPDX-License-Identifier: BUSL-1.1 |
| 2 | +pragma solidity 0.8.26; |
| 3 | + |
| 4 | +import { Ownable, Ownable2Step } from "@openzeppelin/contracts/access/Ownable2Step.sol"; |
| 5 | +import { IERC20, SafeERC20 } from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; |
| 6 | +import { ERC165 } from "@openzeppelin/contracts/utils/introspection/ERC165.sol"; |
| 7 | +import { IERC165 } from "@openzeppelin/contracts/utils/introspection/IERC165.sol"; |
| 8 | +import { ISmardexPair } from "@smardex-dex-contracts/contracts/ethereum/core/v2/interfaces/ISmardexPair.sol"; |
| 9 | +import { ISmardexSwapCallback } from |
| 10 | + "@smardex-dex-contracts/contracts/ethereum/core/v2/interfaces/ISmardexSwapCallback.sol"; |
| 11 | +import { SmardexLibrary } from "@smardex-dex-contracts/contracts/ethereum/core/v2/libraries/SmardexLibrary.sol"; |
| 12 | +import { FixedPointMathLib } from "solady/src/utils/FixedPointMathLib.sol"; |
| 13 | + |
| 14 | +import { IFeeCollectorCallback } from "./../interfaces/UsdnProtocol/IFeeCollectorCallback.sol"; |
| 15 | +import { IAutoSwapperWusdnSdex } from "./../interfaces/Utils/IAutoSwapperWusdnSdex.sol"; |
| 16 | + |
| 17 | +/** |
| 18 | + * @title SDEX buy-back and burn AutoSwapper |
| 19 | + * @notice Automates protocol fee conversion from WUSDN to SDEX via Smardex. |
| 20 | + */ |
| 21 | +contract AutoSwapperWusdnSdex is |
| 22 | + Ownable2Step, |
| 23 | + IAutoSwapperWusdnSdex, |
| 24 | + IFeeCollectorCallback, |
| 25 | + ERC165, |
| 26 | + ISmardexSwapCallback |
| 27 | +{ |
| 28 | + using SafeERC20 for IERC20; |
| 29 | + |
| 30 | + /// @notice Decimal points for basis points (bps). |
| 31 | + uint16 internal constant BPS_DIVISOR = 10_000; |
| 32 | + |
| 33 | + /// @notice Wrapped USDN token address. |
| 34 | + IERC20 internal constant WUSDN = IERC20(0x99999999999999Cc837C997B882957daFdCb1Af9); |
| 35 | + |
| 36 | + /// @notice SmarDex pair address for WUSDN/SDEX swaps. |
| 37 | + ISmardexPair internal constant SMARDEX_WUSDN_SDEX_PAIR = ISmardexPair(0x11443f5B134c37903705e64129BEFc20e35a3725); |
| 38 | + |
| 39 | + /// @notice Fee rates for LP on the SmarDex pair. |
| 40 | + uint128 internal immutable FEE_LP; |
| 41 | + |
| 42 | + /// @notice Fee rates for the pool on the SmarDex pair. |
| 43 | + uint128 internal immutable FEE_POOL; |
| 44 | + |
| 45 | + /// @notice Allowed slippage for swaps (in basis points). |
| 46 | + uint256 internal _swapSlippage = 100; // 1% |
| 47 | + |
| 48 | + constructor() Ownable(msg.sender) { |
| 49 | + (FEE_LP, FEE_POOL) = SMARDEX_WUSDN_SDEX_PAIR.getPairFees(); |
| 50 | + } |
| 51 | + |
| 52 | + /// @inheritdoc IFeeCollectorCallback |
| 53 | + function feeCollectorCallback(uint256) external { |
| 54 | + try this.swapWusdnToSdex() { } |
| 55 | + catch { |
| 56 | + emit FailedSwap(); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /// @inheritdoc IAutoSwapperWusdnSdex |
| 61 | + function swapWusdnToSdex() external { |
| 62 | + uint256 wusdnAmount = WUSDN.balanceOf(address(this)); |
| 63 | + |
| 64 | + uint256 quoteAmountSdexOut = _quoteAmountOut(wusdnAmount); |
| 65 | + uint256 minSdexAmount = FixedPointMathLib.mulDiv(quoteAmountSdexOut, BPS_DIVISOR - _swapSlippage, BPS_DIVISOR); |
| 66 | + (int256 amountSdexOut,) = SMARDEX_WUSDN_SDEX_PAIR.swap(address(0xdead), false, int256(wusdnAmount), ""); |
| 67 | + |
| 68 | + if (uint256(-amountSdexOut) < minSdexAmount) { |
| 69 | + revert AutoSwapperSwapFailed(); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /// @inheritdoc ISmardexSwapCallback |
| 74 | + function smardexSwapCallback(int256, int256 amountWusdnIn, bytes calldata) external { |
| 75 | + if (msg.sender != address(SMARDEX_WUSDN_SDEX_PAIR)) { |
| 76 | + revert AutoSwapperInvalidCaller(); |
| 77 | + } |
| 78 | + WUSDN.safeTransfer(msg.sender, uint256(amountWusdnIn)); |
| 79 | + } |
| 80 | + |
| 81 | + /// @inheritdoc IAutoSwapperWusdnSdex |
| 82 | + function sweep(address token, address to, uint256 amount) external onlyOwner { |
| 83 | + IERC20(token).safeTransfer(to, amount); |
| 84 | + } |
| 85 | + |
| 86 | + /// @inheritdoc IAutoSwapperWusdnSdex |
| 87 | + function getSwapSlippage() external view returns (uint256) { |
| 88 | + return _swapSlippage; |
| 89 | + } |
| 90 | + |
| 91 | + /// @inheritdoc IAutoSwapperWusdnSdex |
| 92 | + function updateSwapSlippage(uint256 newSwapSlippage) external onlyOwner { |
| 93 | + if (newSwapSlippage == 0) { |
| 94 | + revert AutoSwapperInvalidSwapSlippage(); |
| 95 | + } |
| 96 | + _swapSlippage = newSwapSlippage; |
| 97 | + emit SwapSlippageUpdated(newSwapSlippage); |
| 98 | + } |
| 99 | + |
| 100 | + /// @inheritdoc ERC165 |
| 101 | + function supportsInterface(bytes4 interfaceId) public view override(ERC165, IERC165) returns (bool) { |
| 102 | + if (interfaceId == type(IFeeCollectorCallback).interfaceId) { |
| 103 | + return true; |
| 104 | + } |
| 105 | + return super.supportsInterface(interfaceId); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @notice Calculates the amount of SDEX that can be obtained from a given amount of WUSDN. |
| 110 | + * @param amountIn The amount of WUSDN to swap |
| 111 | + * @return amountOut_ The amount of SDEX that can be obtained |
| 112 | + */ |
| 113 | + function _quoteAmountOut(uint256 amountIn) internal view returns (uint256 amountOut_) { |
| 114 | + (uint256 fictiveReserveSdex, uint256 fictiveReserveWusdn) = SMARDEX_WUSDN_SDEX_PAIR.getFictiveReserves(); |
| 115 | + (uint256 reservesSdex, uint256 reservesWusdn) = SMARDEX_WUSDN_SDEX_PAIR.getReserves(); |
| 116 | + (uint256 priceAvSdex, uint256 priceAvWusdn, uint256 priceAvTimestamp) = |
| 117 | + SMARDEX_WUSDN_SDEX_PAIR.getPriceAverage(); |
| 118 | + |
| 119 | + (priceAvWusdn, priceAvSdex) = SmardexLibrary.getUpdatedPriceAverage( |
| 120 | + fictiveReserveWusdn, fictiveReserveSdex, priceAvTimestamp, priceAvWusdn, priceAvSdex, block.timestamp |
| 121 | + ); |
| 122 | + |
| 123 | + (fictiveReserveWusdn, fictiveReserveSdex) = |
| 124 | + SmardexLibrary.computeFictiveReserves(reservesWusdn, reservesSdex, fictiveReserveWusdn, fictiveReserveSdex); |
| 125 | + |
| 126 | + (amountOut_,,,,) = SmardexLibrary.applyKConstRuleOut( |
| 127 | + SmardexLibrary.GetAmountParameters({ |
| 128 | + amount: amountIn, |
| 129 | + reserveIn: reservesWusdn, |
| 130 | + reserveOut: reservesSdex, |
| 131 | + fictiveReserveIn: fictiveReserveWusdn, |
| 132 | + fictiveReserveOut: fictiveReserveSdex, |
| 133 | + priceAverageIn: priceAvWusdn, |
| 134 | + priceAverageOut: priceAvSdex, |
| 135 | + feesLP: FEE_LP, |
| 136 | + feesPool: FEE_POOL |
| 137 | + }) |
| 138 | + ); |
| 139 | + } |
| 140 | +} |
0 commit comments