Verified contractExactMIT
SUSDeBuyer
Address0x714530e9d5bded17009cef8376a252c9dc3e2a9a
- Balance
0 ETH
- Transactions
32
- Compiler
solc 0.8.28
- Same code
Only this contract
Details
- Compiler
- solc 0.8.28+commit.7893614a
- Language
- solidity
- Optimization
- Yes, 200 runs
- EVM version
- cancun
- License
- MIT
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 26,338,192
- Creation transaction
- 0xab9344bdf7…63df2f9ac1
2 files
- src/SUSDeBuyer.solmain4.75 KB
- src/SUSDeUnstaker.sol5.1 KB
src/SUSDeBuyer.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;
import {Rescuable} from "./SUSDeUnstaker.sol";
interface IERC20 {
function balanceOf(address account) external view returns (uint256);
function transfer(address to, uint256 amount) external returns (bool);
function transferFrom(address from, address to, uint256 amount) external returns (bool);
function approve(address spender, uint256 amount) external returns (bool);
}
/// The parts of Ethena's sUSDe (StakedUSDeV2) that price a buy.
interface ISUSDe is IERC20 {
function previewRedeem(uint256 shares) external view returns (uint256 assets);
function cooldownDuration() external view returns (uint24);
}
IERC20 constant USDE = IERC20(0x4c9EDD5852cd905f086C759E8383e09bff1E68B3);
ISUSDe constant SUSDE = ISUSDe(0x9D39A5DE30e57443BfF2A8307A4256c8797A3497);
/// Your sUSDe buyer: swaps the owner's USDe for sUSDe through whatever DEX route the caller
/// brings, but only at 10% APR or better. The sUSDe has to redeem, after sUSDe's cooldown, for the
/// USDe spent plus 10% a year over that cooldown. sUSDe itself says what it redeems for and how
/// long the cooldown is, so no caller can get a worse trade through.
///
/// The owner, who deployed it, approves the USDe it may spend and names executors: wallets of
/// theirs, such as a bot holding only ETH for gas, that may call buy() too. An executor picks the
/// route and may insist on more sUSDe than the 10% floor, never less. It can't take any money: the
/// USDe only comes from the owner and the sUSDe only goes to the owner. Approving 0, or
/// setExecutor(executor, false), stops it.
contract SUSDeBuyer is Rescuable {
uint256 public constant MIN_APR_PERCENT = 10;
mapping(address account => bool) public isExecutor;
bool private transient buying;
event ExecutorSet(address indexed executor, bool allowed);
event Bought(address indexed caller, address indexed router, uint256 usdeIn, uint256 susdeReceived);
/// One buy at a time: a route can't call back in and start another one midway.
modifier nonReentrant() {
require(!buying, "already buying");
buying = true;
_;
buying = false;
}
constructor() Rescuable(msg.sender) {}
function setExecutor(address executor, bool allowed) external onlyOwner {
isExecutor[executor] = allowed;
emit ExecutorSet(executor, allowed);
}
/// Spend `usdeIn` of the owner's USDe by calling `router` with `routerData`, letting `spender`
/// take it, and send the owner the sUSDe that comes back. It all reverts unless the route spends
/// all of `usdeIn`, and the sUSDe is at least `minSusdeOut` and worth 10% APR.
function buy(address router, address spender, uint256 usdeIn, bytes calldata routerData, uint256 minSusdeOut)
external
nonReentrant
returns (uint256 received)
{
require(msg.sender == owner || isExecutor[msg.sender], "only the owner or an executor");
// Called from here, USDe would let the caller transferFrom or burnFrom the owner's whole
// approval, and sUSDe would let it approve away the shares held here.
require(router != address(USDE) && router != address(SUSDE) && router != address(this), "not a router");
// Tokens sent here by anyone else don't count as traded; they wait here for the owner.
uint256 usdeBefore = USDE.balanceOf(address(this));
uint256 susdeBefore = SUSDE.balanceOf(address(this));
// The owner's USDe whoever calls: an executor only pays the gas.
// forge-lint: disable-next-line(arbitrary-send-erc20)
require(USDE.transferFrom(owner, address(this), usdeIn), "USDe transfer failed");
require(USDE.approve(spender, usdeIn), "approve failed");
(bool ok, bytes memory reason) = router.call(routerData);
if (!ok) {
assembly ("memory-safe") {
revert(add(reason, 0x20), mload(reason))
}
}
require(USDE.approve(spender, 0), "approve failed");
// A buy that spent nothing would leave nothing to check, and a caller free to make this
// contract call anything.
require(usdeIn > 0 && USDE.balanceOf(address(this)) == usdeBefore, "not a full buy");
received = SUSDE.balanceOf(address(this)) - susdeBefore;
require(received >= minSusdeOut, "less than minSusdeOut");
uint256 interest = usdeIn * MIN_APR_PERCENT * SUSDE.cooldownDuration() / (100 * 365 days);
require(SUSDE.previewRedeem(received) >= usdeIn + interest, "under 10% APR");
require(SUSDE.transfer(owner, received), "sUSDe transfer failed");
emit Bought(msg.sender, router, usdeIn, received);
}
}
src/SUSDeUnstaker.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;
/// The parts of Ethena's sUSDe (StakedUSDeV2) used here.
interface IStakedUSDe {
function transferFrom(address from, address to, uint256 shares) external returns (bool);
function cooldownShares(uint256 shares) external returns (uint256 assets);
function unstake(address receiver) external;
}
IStakedUSDe constant SUSDE = IStakedUSDe(0x9D39A5DE30e57443BfF2A8307A4256c8797A3497);
/// An owner who can make this contract do anything a wallet of theirs could: call any contract
/// with any data and ETH. It's the way out of whatever the code below didn't foresee, such as
/// tokens sent here by mistake, an airdrop, or a change at Ethena.
abstract contract Rescuable {
address public immutable owner;
constructor(address owner_) {
owner = owner_;
}
modifier onlyOwner() {
require(msg.sender == owner, "only the owner");
_;
}
/// Call `target` from this contract, sending `value` wei and `data`, and return what it
/// returns. If the call reverts, this reverts with the same reason. Data only goes to a
/// contract: sent to an address without code, as with a mistyped token, it would "succeed".
function execute(address target, uint256 value, bytes calldata data)
external
payable
onlyOwner
returns (bytes memory result)
{
require(target.code.length > 0 || data.length == 0, "no code at target");
bool ok;
(ok, result) = target.call{value: value}(data);
if (!ok) {
assembly ("memory-safe") {
revert(add(result, 0x20), mload(result))
}
}
}
receive() external payable {}
}
/// Your sUSDe unstaker: redeems sUSDe for USDe, with every redemption in its own Cooldown
/// contract.
///
/// sUSDe keeps one cooldown per address: starting another adds to it and restarts the clock for
/// all of it. Giving each redemption its own address keeps redemptions independent.
///
/// The owner, who deployed it, owns every Cooldown too. A Cooldown's USDe waits in Ethena's silo
/// until it's claimed, and claim() sends it to the owner.
///
/// Executors, other wallets the owner names, can start redemptions and pay the gas for them, but
/// can't touch the money: start() only takes the owner's sUSDe, up to what the owner has approved
/// this contract for, and the USDe only goes to the owner. Approving 0 stops all of them at once.
contract SUSDeUnstaker is Rescuable {
/// No redemption under 1 sUSDe, so no one, an executor included, can litter cooldowns() with dust.
uint256 public constant MIN_SHARES = 1e18;
mapping(address account => bool) public isExecutor;
Cooldown[] private _cooldowns;
event ExecutorSet(address indexed executor, bool allowed);
event Started(Cooldown indexed cooldown, uint256 shares, uint256 assets);
constructor() Rescuable(msg.sender) {}
/// Let `executor` start redemptions (`allowed` true), or stop it (false).
function setExecutor(address executor, bool allowed) external onlyOwner {
isExecutor[executor] = allowed;
emit ExecutorSet(executor, allowed);
}
/// Redeem `shares` of the owner's sUSDe: move them into a new Cooldown and start its
/// cooldown. The owner or an executor may call it, and it needs an sUSDe allowance from the
/// owner for this contract. Once the cooldown is over, cooldown.claim() sends the USDe to the
/// owner.
function start(uint256 shares) external returns (Cooldown cooldown) {
require(msg.sender == owner || isExecutor[msg.sender], "only the owner or an executor");
require(shares >= MIN_SHARES, "less than 1 sUSDe");
cooldown = new Cooldown(owner);
// The owner's sUSDe whoever calls: an executor only pays the gas.
// forge-lint: disable-next-line(arbitrary-send-erc20)
require(SUSDE.transferFrom(owner, address(cooldown), shares), "sUSDe transfer failed");
uint256 assets = cooldown.begin(shares);
_cooldowns.push(cooldown);
emit Started(cooldown, shares, assets);
}
/// Every Cooldown started, oldest first, claimed or not.
function cooldowns() external view returns (Cooldown[] memory) {
return _cooldowns;
}
}
/// One redemption, made by SUSDeUnstaker.start(). sUSDe tracks it under this contract's
/// address: sUSDe.cooldowns(cooldown) is (when it can be claimed, USDe to claim).
contract Cooldown is Rescuable {
address private immutable unstaker = msg.sender;
constructor(address owner_) Rescuable(owner_) {}
/// Start the cooldown of `shares` sUSDe held here. The unstaker calls it right after
/// creating and funding this contract.
function begin(uint256 shares) external returns (uint256 assets) {
require(msg.sender == unstaker, "only the unstaker");
return SUSDE.cooldownShares(shares);
}
/// Once the cooldown is over, send the USDe to the owner. Anyone may call it.
function claim() external {
SUSDE.unstake(owner);
}
}
Compiler settingsShow
{
"viaIR": false,
"metadata": {
"appendCBOR": true,
"bytecodeHash": "ipfs",
"useLiteralContent": false
},
"libraries": {},
"optimizer": {
"runs": 200,
"enabled": true
},
"evmVersion": "cancun",
"remappings": [
"forge-std/=lib/forge-std/src/"
]
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.