Verified contractExactMIT
PsmOperatorSpell
Address0x7d6b503f723cefc79a7da4be4a17469aae22412f
- Balance
0 ETH
- Transactions
1
- Compiler
solc 0.8.26
- Same code
Only this contract
Details
- Compiler
- solc 0.8.26+commit.8a97fa7a
- Language
- solidity
- Optimization
- Yes, 200 runs
- EVM version
- istanbul
- License
- MIT
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 26,346,002
- Creation transaction
- 0x4968021848…0059d07a4a
2 files
- contracts/PsmOperatorSpell.solmain2.62 KB
- contracts/PsmOperator.sol11.34 KB
contracts/PsmOperatorSpell.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
import {PsmOperator} from "./PsmOperator.sol";
interface OperatorPermissionLike {
function rely(address account) external;
function hope(address account) external;
function mate(address account) external;
function kiss(address account) external;
}
/// @notice Grant withdrawal, repayment and receiver-management permissions.
/// @dev Execute setup() through GovActionsProxy.execSpell / DSPauseProxy delegatecall.
/// PauseProxy must already be a ward on PSM, RwaUrn2 and OutputConduit.
/// No storage writes to the delegatecall context, and no Vat ward grant.
contract PsmOperatorSpell {
PsmOperator public immutable withdrawal;
address public immutable pauseProxy;
address public immutable psm;
address public immutable rwaUrn;
address public immutable outputConduit;
error InvalidAddress(address account);
error WrongExecutionContext(address actual);
error InvalidConfiguration();
event WithdrawalConfigured(address indexed withdrawal, address indexed receiver);
constructor(address withdrawal_, address pauseProxy_) {
if (withdrawal_.code.length == 0) revert InvalidAddress(withdrawal_);
if (pauseProxy_.code.length == 0) revert InvalidAddress(pauseProxy_);
withdrawal = PsmOperator(withdrawal_);
pauseProxy = pauseProxy_;
psm = address(withdrawal.psm());
rwaUrn = address(withdrawal.rwaUrn());
outputConduit = address(withdrawal.outputConduit());
}
function setup() external {
if (address(this) != pauseProxy) revert WrongExecutionContext(address(this));
if (withdrawal.rwaUrn().vat() != withdrawal.vat()
|| withdrawal.rwaUrn().outputConduit() != outputConduit
|| withdrawal.outputConduit().psm() != psm
|| withdrawal.outputConduit().gem() != address(withdrawal.gem())
|| withdrawal.psm().gemJoin() != withdrawal.gemJoin()) revert InvalidConfiguration();
address account = address(withdrawal);
address receiver = withdrawal.receiver();
OperatorPermissionLike(psm).rely(account);
OperatorPermissionLike(rwaUrn).hope(account);
OperatorPermissionLike(outputConduit).hope(account);
OperatorPermissionLike(outputConduit).mate(account);
// kiss() is ward-only: receiver management grants broad Conduit administration.
OperatorPermissionLike(outputConduit).rely(account);
OperatorPermissionLike(outputConduit).kiss(receiver);
emit WithdrawalConfigured(account, receiver);
}
}
contracts/PsmOperator.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
interface OperatorPsmLike {
function vat() external view returns (address);
function ilk() external view returns (bytes32);
function gemJoin() external view returns (address);
function buyEnabled() external view returns (uint256);
function tout() external view returns (uint256);
function tin() external view returns (uint256);
function sellEnabled() external view returns (uint256);
function usdd() external view returns (address);
function sellGem(address recipient, uint256 amount) external;
function file(bytes32 what, uint256 value) external;
}
interface OperatorVatLike {
function urns(bytes32 ilk, address urn) external view returns (uint256 ink, uint256 art);
}
interface OperatorJoinLike {
function gem() external view returns (address);
function dec() external view returns (uint256);
}
interface OperatorTokenLike {
function balanceOf(address account) external view returns (uint256);
}
interface OperatorRwaUrnLike {
function vat() external view returns (address);
function outputConduit() external view returns (address);
function draw(uint256 wad) external;
function usddJoin() external view returns (address);
function wipe(uint256 wad) external;
}
interface OperatorUsddJoinLike {
function usdd() external view returns (address);
function vat() external view returns (address);
}
interface OperatorOutputConduitLike {
function psm() external view returns (address);
function gem() external view returns (address);
function requiredUsddWad(uint256 amount) external view returns (uint256);
function expectedGemAmt(uint256 wad) external view returns (uint256);
function pick(address receiver) external;
function push(uint256 wad) external;
function kiss(address receiver) external;
function bud(address receiver) external view returns (uint256);
}
/// @notice Withdraw collateral or return it to PSM and repay the bound RWA vault.
/// @dev Receiver management requires OutputConduit ward. Never changes buy/sell switches.
contract PsmOperator {
OperatorPsmLike public immutable psm;
OperatorRwaUrnLike public immutable rwaUrn;
OperatorOutputConduitLike public immutable outputConduit;
address public immutable vat;
address public immutable gemJoin;
OperatorTokenLike public immutable gem;
uint256 public immutable to18ConversionFactor;
address public receiver;
mapping(address => uint256) public wards;
bool private entered;
error InvalidAddress(address account);
error InvalidConfiguration();
error NotAuthorized(address account);
error ReentrantCall();
error BuyDisabled();
error InvalidAmount();
error UnexpectedWithdrawalAmount();
error FeeNotRestored();
error SellDisabled();
error UnexpectedRepaymentAmount();
error TokenCallFailed();
event Rely(address indexed account);
event Deny(address indexed account);
event ReceiverUpdated(address indexed previousReceiver, address indexed newReceiver);
event Withdrawn(address indexed receiver, uint256 gemAmount, uint256 usddDrawn, uint256 restoredTout);
event Repaid(address indexed rwaUrn, uint256 gemAmount, uint256 usddRepaid, uint256 restoredTin);
event TokenWithdrawn(address indexed token, address indexed receiver, uint256 amount);
constructor(address psm_, address rwaUrn_, address receiver_, address operator_) {
if (psm_.code.length == 0) revert InvalidAddress(psm_);
if (rwaUrn_.code.length == 0) revert InvalidAddress(rwaUrn_);
if (operator_ == address(0)) revert InvalidAddress(operator_);
psm = OperatorPsmLike(psm_);
rwaUrn = OperatorRwaUrnLike(rwaUrn_);
vat = psm.vat();
if (vat.code.length == 0) revert InvalidAddress(vat);
gemJoin = psm.gemJoin();
if (gemJoin.code.length == 0) revert InvalidAddress(gemJoin);
address token = OperatorJoinLike(gemJoin).gem();
if (token.code.length == 0) revert InvalidAddress(token);
gem = OperatorTokenLike(token);
uint256 decimals = OperatorJoinLike(gemJoin).dec();
if (decimals > 18) revert InvalidConfiguration();
to18ConversionFactor = 10 ** (18 - decimals);
address conduit = rwaUrn.outputConduit();
if (conduit.code.length == 0) revert InvalidAddress(conduit);
outputConduit = OperatorOutputConduitLike(conduit);
_checkConfiguration();
_setReceiver(receiver_);
wards[operator_] = 1;
emit Rely(operator_);
}
modifier auth() {
if (wards[msg.sender] != 1) revert NotAuthorized(msg.sender);
_;
}
modifier nonReentrant() {
if (entered) revert ReentrantCall();
entered = true;
_;
entered = false;
}
function rely(address account) external auth {
if (account == address(0)) revert InvalidAddress(account);
wards[account] = 1;
emit Rely(account);
}
function deny(address account) external auth {
wards[account] = 0;
emit Deny(account);
}
function setReceiver(address receiver_) external auth nonReentrant {
_checkConfiguration();
_setReceiver(receiver_);
outputConduit.kiss(receiver_);
if (outputConduit.bud(receiver_) != 1) revert InvalidConfiguration();
}
function _setReceiver(address receiver_) private {
if (receiver_ == address(0) || receiver_ == address(this) || receiver_ == gemJoin || receiver_ == address(psm)) revert InvalidAddress(receiver_);
emit ReceiverUpdated(receiver, receiver_);
receiver = receiver_;
}
function _checkConfiguration() private view {
if (rwaUrn.vat() != vat || rwaUrn.outputConduit() != address(outputConduit)
|| outputConduit.psm() != address(psm) || outputConduit.gem() != address(gem)
|| psm.gemJoin() != gemJoin) revert InvalidConfiguration();
}
/// @notice Reserve-side withdrawal limit in collateral raw units, not a guarantee of execution.
/// @dev Other constraints, including RWA debt headroom and permissions, still apply.
function maxWithdrawAmount() external view returns (uint256) {
_checkConfiguration();
return _withdrawLimit(gem.balanceOf(gemJoin));
}
function _withdrawLimit(uint256 joinBalance) private view returns (uint256) {
(uint256 ink,) = OperatorVatLike(vat).urns(psm.ilk(), address(psm));
uint256 accountedBalance = ink / to18ConversionFactor;
return accountedBalance < joinBalance ? accountedBalance : joinBalance;
}
/// @param amount Collateral raw units: e.g. 100 USDC = 100 * 10**6.
function withdraw(uint256 amount) external auth nonReentrant {
_checkConfiguration();
if (psm.buyEnabled() != 1) revert BuyDisabled();
uint256 joinBalance = gem.balanceOf(gemJoin);
if (amount == 0 || amount > _withdrawLimit(joinBalance)) revert InvalidAmount();
address recipient = receiver;
uint256 receiverBalance = gem.balanceOf(recipient);
uint256 previousTout = psm.tout();
if (previousTout != 0) psm.file("tout", 0);
if (psm.tout() != 0) revert InvalidConfiguration();
uint256 wad = amount * to18ConversionFactor;
if (outputConduit.requiredUsddWad(amount) != wad || outputConduit.expectedGemAmt(wad) != amount) {
revert UnexpectedWithdrawalAmount();
}
rwaUrn.draw(wad);
outputConduit.pick(recipient);
outputConduit.push(wad);
if (previousTout != 0) psm.file("tout", previousTout);
if (psm.tout() != previousTout) revert FeeNotRestored();
if (gem.balanceOf(gemJoin) != joinBalance - amount || gem.balanceOf(recipient) != receiverBalance + amount) {
revert UnexpectedWithdrawalAmount();
}
emit Withdrawn(recipient, amount, wad, previousTout);
}
/// @notice Return already funded collateral and repay the corresponding USDD amount.
/// @dev Does not promise to clear accrued interest or all normalized debt. Excess repayment reverts in RwaUrn2.
function repay(uint256 amount) external auth nonReentrant {
_checkConfiguration();
if (psm.sellEnabled() != 1) revert SellDisabled();
uint256 balance = gem.balanceOf(address(this));
if (amount == 0 || amount > balance) revert InvalidAmount();
address usdd = psm.usdd();
address usddJoin = rwaUrn.usddJoin();
if (usdd.code.length == 0 || usddJoin.code.length == 0
|| OperatorUsddJoinLike(usddJoin).usdd() != usdd
|| OperatorUsddJoinLike(usddJoin).vat() != vat) revert InvalidConfiguration();
uint256 joinBalance = gem.balanceOf(gemJoin);
uint256 urnBalance = OperatorTokenLike(usdd).balanceOf(address(rwaUrn));
uint256 wad = amount * to18ConversionFactor;
uint256 previousTin = psm.tin();
if (previousTin != 0) psm.file("tin", 0);
if (psm.tin() != 0) revert InvalidConfiguration();
// GemJoin, not PSM, pulls the collateral in sellGem().
_approveGem(0);
_approveGem(amount);
psm.sellGem(address(rwaUrn), amount);
_approveGem(0);
if (gem.balanceOf(address(this)) != balance - amount || gem.balanceOf(gemJoin) != joinBalance + amount
|| OperatorTokenLike(usdd).balanceOf(address(rwaUrn)) != urnBalance + wad) revert UnexpectedRepaymentAmount();
rwaUrn.wipe(wad);
if (OperatorTokenLike(usdd).balanceOf(address(rwaUrn)) != urnBalance) revert UnexpectedRepaymentAmount();
if (previousTin != 0) psm.file("tin", previousTin);
if (psm.tin() != previousTin) revert FeeNotRestored();
emit Repaid(address(rwaUrn), amount, wad, previousTin);
}
/// @notice Recover tokens held by this contract to the configured receiver.
/// @dev Does not withdraw PSM reserves or repay RWA debt. No per-depositor accounting.
function withdrawToken(address token, uint256 amount) external auth nonReentrant {
if (token.code.length == 0) revert InvalidAddress(token);
if (amount == 0 || amount > OperatorTokenLike(token).balanceOf(address(this))) revert InvalidAmount();
address recipient = receiver;
_callToken(token, abi.encodeWithSignature("transfer(address,uint256)", recipient, amount));
emit TokenWithdrawn(token, recipient, amount);
}
function _approveGem(uint256 amount) private {
_callToken(address(gem), abi.encodeWithSignature("approve(address,uint256)", gemJoin, amount));
}
function _callToken(address token, bytes memory data) private {
(bool ok, bytes memory result) = token.call(data);
if (!ok) {
if (result.length == 0) revert TokenCallFailed();
// Preserve the token's revert string, custom error or panic for callers.
assembly ("memory-safe") {
revert(add(result, 32), mload(result))
}
}
if (result.length != 0) {
if (result.length != 32) revert TokenCallFailed();
// Only the canonical ABI encoding of true is accepted.
if (abi.decode(result, (uint256)) != 1) revert TokenCallFailed();
}
}
}
Compiler settingsShow
{
"optimizer": {
"runs": 200,
"enabled": true
},
"evmVersion": "istanbul"
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.