Source Code
Overview
ETH Balance
0 ETH
Eth Value
$0.00Latest 1 internal transaction
Advanced mode:
| Parent Transaction Hash | Method | Block |
From
|
|
To
|
||
|---|---|---|---|---|---|---|---|
| 0x60e06040 | 23493963 | 142 days ago | Contract Creation | 0 ETH |
Loading...
Loading
Loading...
Loading
Cross-Chain Transactions
Loading...
Loading
This contract may be a proxy contract. Click on More Options and select Is this a proxy? to confirm and enable the "Read as Proxy" & "Write as Proxy" tabs.
Contract Name:
BtcVaultStrategy
Compiler Version
v0.8.25+commit.b61c2a91
Optimization Enabled:
Yes with 30 runs
Other Settings:
cancun EvmVersion
Contract Source Code (Solidity Standard Json-Input format)
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.25;
import {ManagedWithdrawReportedStrategy} from "./ManagedWithdrawRWAStrategy.sol";
import {BtcVaultToken} from "../token/BtcVaultToken.sol";
import {SafeTransferLib} from "solady/utils/SafeTransferLib.sol";
import {CollateralManagementLib} from "../libraries/CollateralManagementLib.sol";
import {CollateralViewLib} from "../libraries/CollateralViewLib.sol";
/// @title BtcVaultStrategy
/// @notice Multi-collateral BTC vault strategy with managed withdrawals
contract BtcVaultStrategy is ManagedWithdrawReportedStrategy {
using SafeTransferLib for address;
uint8 public constant COLLATERAL_DECIMALS = 8;
mapping(address => bool) public supportedAssets;
address[] public collateralTokens;
/// @notice Initialize the strategy
function initialize(
string calldata name_,
string calldata symbol_,
address roleManager_,
address manager_,
address sovaBTC_,
uint8 assetDecimals_,
bytes memory initData
) public virtual override {
super.initialize(name_, symbol_, roleManager_, manager_, sovaBTC_, assetDecimals_, initData);
require(assetDecimals_ == 8);
supportedAssets[sovaBTC_] = true;
collateralTokens.push(sovaBTC_);
}
/// @notice Deploy a new BtcVaultToken for this strategy
function _deployToken(string calldata name_, string calldata symbol_, address asset_, uint8 /* assetDecimals_ */ )
internal
virtual
override
returns (address)
{
return address(new BtcVaultToken(name_, symbol_, asset_, address(this)));
}
/// @notice Add a new supported collateral token
function addCollateral(address token) external onlyManager {
CollateralManagementLib.addCollateral(token, supportedAssets, collateralTokens, COLLATERAL_DECIMALS);
}
/// @notice Remove a supported collateral token
function removeCollateral(address token) external onlyManager {
CollateralManagementLib.removeCollateral(token, asset, supportedAssets, collateralTokens);
}
/// @notice Deposit collateral directly to the strategy
function depositCollateral(address token, uint256 amount) external {
CollateralManagementLib.depositCollateral(token, amount, supportedAssets);
}
/// @notice Add sovaBTC for redemptions
function addLiquidity(uint256 amount) external onlyManager {
CollateralManagementLib.addLiquidity(asset, amount);
}
/// @notice Remove sovaBTC from strategy
function removeLiquidity(uint256 amount, address to) external onlyManager {
CollateralManagementLib.removeLiquidity(asset, amount, to);
}
/// @notice Withdraw collateral to admin
function withdrawCollateral(address token, uint256 amount, address to) external onlyManager {
CollateralManagementLib.withdrawCollateral(token, amount, to, supportedAssets);
}
/// @notice Approve token to withdraw assets during redemptions
function approveTokenWithdrawal(uint256 amount) external onlyManager {
asset.safeApprove(sToken, 0);
asset.safeApprove(sToken, amount);
}
/// @notice Check if an asset is supported
function isSupportedAsset(address token) external view returns (bool) {
return supportedAssets[token];
}
/// @notice Get list of all supported collateral tokens
function getSupportedCollaterals() external view returns (address[] memory) {
return collateralTokens;
}
/// @notice Get total collateral assets value
function totalCollateralAssets() external view returns (uint256) {
return CollateralViewLib.totalCollateralAssets(collateralTokens);
}
/// @notice Get balance of a specific collateral token
function collateralBalance(address token) external view returns (uint256) {
return CollateralViewLib.collateralBalance(token, supportedAssets);
}
/// @notice Get available sovaBTC balance for redemptions
function availableLiquidity() external view returns (uint256) {
return CollateralViewLib.availableLiquidity(asset);
}
/*//////////////////////////////////////////////////////////////
EIP-712 OVERRIDE
//////////////////////////////////////////////////////////////*/
/// @notice EIP-712 Type Hash Constants
bytes32 private constant EIP712_DOMAIN_TYPEHASH =
keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)");
/**
* @notice Calculate the EIP-712 domain separator with correct contract name
* @return The domain separator
*/
function _domainSeparator() internal view override returns (bytes32) {
return keccak256(
abi.encode(
EIP712_DOMAIN_TYPEHASH,
keccak256(bytes("BtcVaultStrategy")),
keccak256(bytes("V1")),
block.chainid,
address(this)
)
);
}
}// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.25;
import {ECDSA} from "solady/utils/ECDSA.sol";
import {ManagedWithdrawRWA} from "../token/ManagedWithdrawRWA.sol";
import {ReportedStrategy} from "./ReportedStrategy.sol";
/**
* @title ManagedWithdrawReportedStrategy
* @notice Extension of ReportedStrategy that deploys and configures ManagedWithdrawRWA tokens
*/
contract ManagedWithdrawReportedStrategy is ReportedStrategy {
/*//////////////////////////////////////////////////////////////
ERRORS
//////////////////////////////////////////////////////////////*/
error WithdrawalRequestExpired();
error WithdrawNonceReuse();
error WithdrawInvalidSignature();
error InvalidArrayLengths();
/*//////////////////////////////////////////////////////////////
EVENTS
//////////////////////////////////////////////////////////////*/
event WithdrawalNonceUsed(address indexed owner, uint96 nonce);
/*//////////////////////////////////////////////////////////////
EIP-712 DATA
//////////////////////////////////////////////////////////////*/
/// @notice Signature argument struct
struct Signature {
uint8 v;
bytes32 r;
bytes32 s;
}
// EIP-712 Type Hash Constants
bytes32 private constant EIP712_DOMAIN_TYPEHASH =
keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)");
bytes32 private constant WITHDRAWAL_REQUEST_TYPEHASH = keccak256(
"WithdrawalRequest(address owner,address to,uint256 shares,uint256 minAssets,uint96 nonce,uint96 expirationTime)"
);
/*//////////////////////////////////////////////////////////////
STATE
//////////////////////////////////////////////////////////////*/
/// @notice Struct to track withdrawal requests
struct WithdrawalRequest {
uint256 shares;
uint256 minAssets;
address owner;
uint96 nonce;
address to;
uint96 expirationTime;
}
// Tracking of used nonces
mapping(address => mapping(uint96 => bool)) public usedNonces;
/*//////////////////////////////////////////////////////////////
INITIALIZATION
//////////////////////////////////////////////////////////////*/
/**
* @notice Initialize the strategy with ManagedWithdrawRWA token
* @param name_ Name of the token
* @param symbol_ Symbol of the token
* @param roleManager_ Address of the role manager
* @param manager_ Address of the manager
* @param asset_ Address of the underlying asset
* @param assetDecimals_ Decimals of the asset
* @param initData Additional initialization data (unused)
*/
function initialize(
string calldata name_,
string calldata symbol_,
address roleManager_,
address manager_,
address asset_,
uint8 assetDecimals_,
bytes memory initData
) public virtual override {
super.initialize(name_, symbol_, roleManager_, manager_, asset_, assetDecimals_, initData);
}
/**
* @notice Deploy a new ManagedWithdrawRWA token
* @param name_ Name of the token
* @param symbol_ Symbol of the token
* @param asset_ Address of the underlying asset
* @param assetDecimals_ Decimals of the asset
*/
function _deployToken(string calldata name_, string calldata symbol_, address asset_, uint8 assetDecimals_)
internal
virtual
override
returns (address)
{
ManagedWithdrawRWA newToken = new ManagedWithdrawRWA(name_, symbol_, asset_, assetDecimals_, address(this));
return address(newToken);
}
/*//////////////////////////////////////////////////////////////
REDEMPTION LOGIC
//////////////////////////////////////////////////////////////*/
/**
* @notice Process a user-requested withdrawal
* @param request The withdrawal request
* @param userSig The signature of the request
* @return assets The amount of assets received
*/
function redeem(WithdrawalRequest calldata request, Signature calldata userSig)
external
onlyManager
returns (uint256 assets)
{
_validateRedeem(request);
// Verify signature
_verifySignature(request, userSig);
assets = ManagedWithdrawRWA(sToken).redeem(request.shares, request.to, request.owner, request.minAssets);
}
/**
* @notice Process a batch of user-requested withdrawals
* @param requests The withdrawal requests
* @param signatures The signatures of the requests
* @return assets The amount of assets received
*/
function batchRedeem(WithdrawalRequest[] calldata requests, Signature[] calldata signatures)
external
onlyManager
returns (uint256[] memory assets)
{
if (requests.length != signatures.length) revert InvalidArrayLengths();
uint256[] memory shares = new uint256[](requests.length);
address[] memory recipients = new address[](requests.length);
address[] memory owners = new address[](requests.length);
uint256[] memory minAssets = new uint256[](requests.length);
for (uint256 i = 0; i < requests.length;) {
_validateRedeem(requests[i]);
_verifySignature(requests[i], signatures[i]);
shares[i] = requests[i].shares;
recipients[i] = requests[i].to;
owners[i] = requests[i].owner;
minAssets[i] = requests[i].minAssets;
unchecked {
++i;
}
}
assets = ManagedWithdrawRWA(sToken).batchRedeemShares(shares, recipients, owners, minAssets);
}
/*//////////////////////////////////////////////////////////////
INTERNAL FUNCTIONS
//////////////////////////////////////////////////////////////*/
/**
* @notice Validate a withdrawal request's arguments and consume the nonce
* @param request The withdrawal request
*/
function _validateRedeem(WithdrawalRequest calldata request) internal {
if (request.expirationTime < block.timestamp) revert WithdrawalRequestExpired();
// Cache the nonce status to avoid duplicate storage read
mapping(uint96 => bool) storage userNonces = usedNonces[request.owner];
if (userNonces[request.nonce]) revert WithdrawNonceReuse();
// Consume the nonce
userNonces[request.nonce] = true;
emit WithdrawalNonceUsed(request.owner, request.nonce);
}
/**
* @notice Verify a signature using EIP-712
* @param request The withdrawal request
* @param signature The signature
*/
function _verifySignature(WithdrawalRequest calldata request, Signature calldata signature) internal view {
// Verify signature
bytes32 structHash = keccak256(
abi.encode(
WITHDRAWAL_REQUEST_TYPEHASH,
request.owner,
request.to,
request.shares,
request.minAssets,
request.nonce,
request.expirationTime
)
);
bytes32 digest = keccak256(abi.encodePacked("\x19\x01", _domainSeparator(), structHash));
// Recover signer address from signature
address signer = ECDSA.recover(digest, signature.v, signature.r, signature.s);
// Verify the signer is the owner of the shares
if (signer != request.owner) revert WithdrawInvalidSignature();
}
/**
* @notice Calculate the EIP-712 domain separator
* @return The domain separator
*/
function _domainSeparator() internal view virtual returns (bytes32) {
return keccak256(
abi.encode(
EIP712_DOMAIN_TYPEHASH,
keccak256(bytes("ManagedWithdrawReportedStrategy")),
keccak256(bytes("V1")),
block.chainid,
address(this)
)
);
}
}// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.25;
import {ManagedWithdrawRWA} from "./ManagedWithdrawRWA.sol";
import {IERC20} from "forge-std/interfaces/IERC20.sol";
import {SafeTransferLib} from "solady/utils/SafeTransferLib.sol";
import {FixedPointMathLib} from "solady/utils/FixedPointMathLib.sol";
import {IBtcVaultStrategy} from "../interfaces/IBtcVaultStrategy.sol";
/**
* @title BtcVaultToken
* @notice Multi-collateral BTC vault token that extends ManagedWithdrawRWA
* @dev Supports deposits of multiple BTC collateral types, redeems only in sovaBTC
*/
contract BtcVaultToken is ManagedWithdrawRWA {
using FixedPointMathLib for uint256;
using SafeTransferLib for address;
/*//////////////////////////////////////////////////////////////
ERRORS
//////////////////////////////////////////////////////////////*/
error TokenNotSupported();
error InsufficientAmount();
error ZeroShares();
error StandardDepositDisabled();
/*//////////////////////////////////////////////////////////////
EVENTS
//////////////////////////////////////////////////////////////*/
event CollateralDeposited(
address indexed depositor, address indexed token, uint256 amount, uint256 shares, address indexed receiver
);
/*//////////////////////////////////////////////////////////////
CONSTANTS
//////////////////////////////////////////////////////////////*/
/// @notice Minimum deposit amount (0.001 BTC in 8 decimals)
uint256 public constant MIN_DEPOSIT = 1e5;
/*//////////////////////////////////////////////////////////////
CONSTRUCTOR
//////////////////////////////////////////////////////////////*/
/**
* @notice Constructor
* @param name_ Token name
* @param symbol_ Token symbol
* @param sovaBTC_ Address of sovaBTC (redemption asset, 8 decimals)
* @param strategy_ Address of BtcVaultStrategy
*/
constructor(string memory name_, string memory symbol_, address sovaBTC_, address strategy_)
ManagedWithdrawRWA(name_, symbol_, sovaBTC_, 8, strategy_)
{
// ManagedWithdrawRWA handles all initialization
}
/*//////////////////////////////////////////////////////////////
MULTI-COLLATERAL DEPOSITS
//////////////////////////////////////////////////////////////*/
/**
* @notice Deposit BTC collateral tokens for shares
* @param token Address of the BTC collateral token
* @param amount Amount of collateral to deposit (8 decimals)
* @param receiver Address to receive shares
* @return shares Amount of shares minted (18 decimals)
*/
function depositCollateral(address token, uint256 amount, address receiver)
external
nonReentrant
returns (uint256 shares)
{
if (!IBtcVaultStrategy(strategy).isSupportedAsset(token)) revert TokenNotSupported();
if (amount < MIN_DEPOSIT) revert InsufficientAmount();
if (receiver == address(0)) revert InvalidAddress();
// IMPORTANT: Since all collateral tokens are enforced to be 8 decimals (same as sovaBTC),
// and treated as 1:1 with sovaBTC, we can pass amount directly to previewDeposit
// which expects asset-denominated units (8 decimals). The ERC-4626 math handles
// the conversion to 18-decimal shares internally.
shares = previewDeposit(amount);
if (shares == 0) revert ZeroShares();
// Transfer collateral directly to strategy
token.safeTransferFrom(msg.sender, strategy, amount);
// Mint shares to receiver
_mint(receiver, shares);
emit CollateralDeposited(msg.sender, token, amount, shares, receiver);
}
/**
* @notice Preview shares for collateral deposit
* @param token Address of the BTC collateral token
* @param amount Amount of collateral to deposit
* @return shares Amount of shares that would be minted
*/
function previewDepositCollateral(address token, uint256 amount) external view returns (uint256 shares) {
if (!IBtcVaultStrategy(strategy).isSupportedAsset(token)) return 0;
if (amount < MIN_DEPOSIT) return 0;
// - Calculates shares = (amount * totalSupply) / totalAssets
// - Where totalAssets comes from ReportedStrategy.balance() using current pricePerShare
// - This ensures preview matches actual minting logic and respects vault NAV
shares = previewDeposit(amount);
}
/*//////////////////////////////////////////////////////////////
ERC-4626 OVERRIDES
//////////////////////////////////////////////////////////////*/
/**
* @notice Standard ERC-4626 deposit is disabled
* @dev Use depositCollateral for all deposits including sovaBTC
*/
function deposit(uint256, address) public virtual override returns (uint256) {
revert StandardDepositDisabled();
}
/**
* @notice Standard ERC-4626 mint is disabled
* @dev Use depositCollateral for all deposits including sovaBTC
*/
function mint(uint256, address) public virtual override returns (uint256) {
revert StandardDepositDisabled();
}
// Note: withdraw and redeem are already restricted in ManagedWithdrawRWA parent class
// They require onlyStrategy modifier, so users cannot call them directly
}// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
/// @notice Safe ETH and ERC20 transfer library that gracefully handles missing return values.
/// @author Solady (https://github.com/vectorized/solady/blob/main/src/utils/SafeTransferLib.sol)
/// @author Modified from Solmate (https://github.com/transmissions11/solmate/blob/main/src/utils/SafeTransferLib.sol)
/// @author Permit2 operations from (https://github.com/Uniswap/permit2/blob/main/src/libraries/Permit2Lib.sol)
///
/// @dev Note:
/// - For ETH transfers, please use `forceSafeTransferETH` for DoS protection.
library SafeTransferLib {
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CUSTOM ERRORS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev The ETH transfer has failed.
error ETHTransferFailed();
/// @dev The ERC20 `transferFrom` has failed.
error TransferFromFailed();
/// @dev The ERC20 `transfer` has failed.
error TransferFailed();
/// @dev The ERC20 `approve` has failed.
error ApproveFailed();
/// @dev The ERC20 `totalSupply` query has failed.
error TotalSupplyQueryFailed();
/// @dev The Permit2 operation has failed.
error Permit2Failed();
/// @dev The Permit2 amount must be less than `2**160 - 1`.
error Permit2AmountOverflow();
/// @dev The Permit2 approve operation has failed.
error Permit2ApproveFailed();
/// @dev The Permit2 lockdown operation has failed.
error Permit2LockdownFailed();
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CONSTANTS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Suggested gas stipend for contract receiving ETH that disallows any storage writes.
uint256 internal constant GAS_STIPEND_NO_STORAGE_WRITES = 2300;
/// @dev Suggested gas stipend for contract receiving ETH to perform a few
/// storage reads and writes, but low enough to prevent griefing.
uint256 internal constant GAS_STIPEND_NO_GRIEF = 100000;
/// @dev The unique EIP-712 domain domain separator for the DAI token contract.
bytes32 internal constant DAI_DOMAIN_SEPARATOR =
0xdbb8cf42e1ecb028be3f3dbc922e1d878b963f411dc388ced501601c60f7c6f7;
/// @dev The address for the WETH9 contract on Ethereum mainnet.
address internal constant WETH9 = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
/// @dev The canonical Permit2 address.
/// [Github](https://github.com/Uniswap/permit2)
/// [Etherscan](https://etherscan.io/address/0x000000000022D473030F116dDEE9F6B43aC78BA3)
address internal constant PERMIT2 = 0x000000000022D473030F116dDEE9F6B43aC78BA3;
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* ETH OPERATIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
// If the ETH transfer MUST succeed with a reasonable gas budget, use the force variants.
//
// The regular variants:
// - Forwards all remaining gas to the target.
// - Reverts if the target reverts.
// - Reverts if the current contract has insufficient balance.
//
// The force variants:
// - Forwards with an optional gas stipend
// (defaults to `GAS_STIPEND_NO_GRIEF`, which is sufficient for most cases).
// - If the target reverts, or if the gas stipend is exhausted,
// creates a temporary contract to force send the ETH via `SELFDESTRUCT`.
// Future compatible with `SENDALL`: https://eips.ethereum.org/EIPS/eip-4758.
// - Reverts if the current contract has insufficient balance.
//
// The try variants:
// - Forwards with a mandatory gas stipend.
// - Instead of reverting, returns whether the transfer succeeded.
/// @dev Sends `amount` (in wei) ETH to `to`.
function safeTransferETH(address to, uint256 amount) internal {
/// @solidity memory-safe-assembly
assembly {
if iszero(call(gas(), to, amount, codesize(), 0x00, codesize(), 0x00)) {
mstore(0x00, 0xb12d13eb) // `ETHTransferFailed()`.
revert(0x1c, 0x04)
}
}
}
/// @dev Sends all the ETH in the current contract to `to`.
function safeTransferAllETH(address to) internal {
/// @solidity memory-safe-assembly
assembly {
// Transfer all the ETH and check if it succeeded or not.
if iszero(call(gas(), to, selfbalance(), codesize(), 0x00, codesize(), 0x00)) {
mstore(0x00, 0xb12d13eb) // `ETHTransferFailed()`.
revert(0x1c, 0x04)
}
}
}
/// @dev Force sends `amount` (in wei) ETH to `to`, with a `gasStipend`.
function forceSafeTransferETH(address to, uint256 amount, uint256 gasStipend) internal {
/// @solidity memory-safe-assembly
assembly {
if lt(selfbalance(), amount) {
mstore(0x00, 0xb12d13eb) // `ETHTransferFailed()`.
revert(0x1c, 0x04)
}
if iszero(call(gasStipend, to, amount, codesize(), 0x00, codesize(), 0x00)) {
mstore(0x00, to) // Store the address in scratch space.
mstore8(0x0b, 0x73) // Opcode `PUSH20`.
mstore8(0x20, 0xff) // Opcode `SELFDESTRUCT`.
if iszero(create(amount, 0x0b, 0x16)) { revert(codesize(), codesize()) } // For gas estimation.
}
}
}
/// @dev Force sends all the ETH in the current contract to `to`, with a `gasStipend`.
function forceSafeTransferAllETH(address to, uint256 gasStipend) internal {
/// @solidity memory-safe-assembly
assembly {
if iszero(call(gasStipend, to, selfbalance(), codesize(), 0x00, codesize(), 0x00)) {
mstore(0x00, to) // Store the address in scratch space.
mstore8(0x0b, 0x73) // Opcode `PUSH20`.
mstore8(0x20, 0xff) // Opcode `SELFDESTRUCT`.
if iszero(create(selfbalance(), 0x0b, 0x16)) { revert(codesize(), codesize()) } // For gas estimation.
}
}
}
/// @dev Force sends `amount` (in wei) ETH to `to`, with `GAS_STIPEND_NO_GRIEF`.
function forceSafeTransferETH(address to, uint256 amount) internal {
/// @solidity memory-safe-assembly
assembly {
if lt(selfbalance(), amount) {
mstore(0x00, 0xb12d13eb) // `ETHTransferFailed()`.
revert(0x1c, 0x04)
}
if iszero(call(GAS_STIPEND_NO_GRIEF, to, amount, codesize(), 0x00, codesize(), 0x00)) {
mstore(0x00, to) // Store the address in scratch space.
mstore8(0x0b, 0x73) // Opcode `PUSH20`.
mstore8(0x20, 0xff) // Opcode `SELFDESTRUCT`.
if iszero(create(amount, 0x0b, 0x16)) { revert(codesize(), codesize()) } // For gas estimation.
}
}
}
/// @dev Force sends all the ETH in the current contract to `to`, with `GAS_STIPEND_NO_GRIEF`.
function forceSafeTransferAllETH(address to) internal {
/// @solidity memory-safe-assembly
assembly {
// forgefmt: disable-next-item
if iszero(call(GAS_STIPEND_NO_GRIEF, to, selfbalance(), codesize(), 0x00, codesize(), 0x00)) {
mstore(0x00, to) // Store the address in scratch space.
mstore8(0x0b, 0x73) // Opcode `PUSH20`.
mstore8(0x20, 0xff) // Opcode `SELFDESTRUCT`.
if iszero(create(selfbalance(), 0x0b, 0x16)) { revert(codesize(), codesize()) } // For gas estimation.
}
}
}
/// @dev Sends `amount` (in wei) ETH to `to`, with a `gasStipend`.
function trySafeTransferETH(address to, uint256 amount, uint256 gasStipend)
internal
returns (bool success)
{
/// @solidity memory-safe-assembly
assembly {
success := call(gasStipend, to, amount, codesize(), 0x00, codesize(), 0x00)
}
}
/// @dev Sends all the ETH in the current contract to `to`, with a `gasStipend`.
function trySafeTransferAllETH(address to, uint256 gasStipend)
internal
returns (bool success)
{
/// @solidity memory-safe-assembly
assembly {
success := call(gasStipend, to, selfbalance(), codesize(), 0x00, codesize(), 0x00)
}
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* ERC20 OPERATIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Sends `amount` of ERC20 `token` from `from` to `to`.
/// Reverts upon failure.
///
/// The `from` account must have at least `amount` approved for
/// the current contract to manage.
function safeTransferFrom(address token, address from, address to, uint256 amount) internal {
/// @solidity memory-safe-assembly
assembly {
let m := mload(0x40) // Cache the free memory pointer.
mstore(0x60, amount) // Store the `amount` argument.
mstore(0x40, to) // Store the `to` argument.
mstore(0x2c, shl(96, from)) // Store the `from` argument.
mstore(0x0c, 0x23b872dd000000000000000000000000) // `transferFrom(address,address,uint256)`.
let success := call(gas(), token, 0, 0x1c, 0x64, 0x00, 0x20)
if iszero(and(eq(mload(0x00), 1), success)) {
if iszero(lt(or(iszero(extcodesize(token)), returndatasize()), success)) {
mstore(0x00, 0x7939f424) // `TransferFromFailed()`.
revert(0x1c, 0x04)
}
}
mstore(0x60, 0) // Restore the zero slot to zero.
mstore(0x40, m) // Restore the free memory pointer.
}
}
/// @dev Sends `amount` of ERC20 `token` from `from` to `to`.
///
/// The `from` account must have at least `amount` approved for the current contract to manage.
function trySafeTransferFrom(address token, address from, address to, uint256 amount)
internal
returns (bool success)
{
/// @solidity memory-safe-assembly
assembly {
let m := mload(0x40) // Cache the free memory pointer.
mstore(0x60, amount) // Store the `amount` argument.
mstore(0x40, to) // Store the `to` argument.
mstore(0x2c, shl(96, from)) // Store the `from` argument.
mstore(0x0c, 0x23b872dd000000000000000000000000) // `transferFrom(address,address,uint256)`.
success := call(gas(), token, 0, 0x1c, 0x64, 0x00, 0x20)
if iszero(and(eq(mload(0x00), 1), success)) {
success := lt(or(iszero(extcodesize(token)), returndatasize()), success)
}
mstore(0x60, 0) // Restore the zero slot to zero.
mstore(0x40, m) // Restore the free memory pointer.
}
}
/// @dev Sends all of ERC20 `token` from `from` to `to`.
/// Reverts upon failure.
///
/// The `from` account must have their entire balance approved for the current contract to manage.
function safeTransferAllFrom(address token, address from, address to)
internal
returns (uint256 amount)
{
/// @solidity memory-safe-assembly
assembly {
let m := mload(0x40) // Cache the free memory pointer.
mstore(0x40, to) // Store the `to` argument.
mstore(0x2c, shl(96, from)) // Store the `from` argument.
mstore(0x0c, 0x70a08231000000000000000000000000) // `balanceOf(address)`.
// Read the balance, reverting upon failure.
if iszero(
and( // The arguments of `and` are evaluated from right to left.
gt(returndatasize(), 0x1f), // At least 32 bytes returned.
staticcall(gas(), token, 0x1c, 0x24, 0x60, 0x20)
)
) {
mstore(0x00, 0x7939f424) // `TransferFromFailed()`.
revert(0x1c, 0x04)
}
mstore(0x00, 0x23b872dd) // `transferFrom(address,address,uint256)`.
amount := mload(0x60) // The `amount` is already at 0x60. We'll need to return it.
// Perform the transfer, reverting upon failure.
let success := call(gas(), token, 0, 0x1c, 0x64, 0x00, 0x20)
if iszero(and(eq(mload(0x00), 1), success)) {
if iszero(lt(or(iszero(extcodesize(token)), returndatasize()), success)) {
mstore(0x00, 0x7939f424) // `TransferFromFailed()`.
revert(0x1c, 0x04)
}
}
mstore(0x60, 0) // Restore the zero slot to zero.
mstore(0x40, m) // Restore the free memory pointer.
}
}
/// @dev Sends `amount` of ERC20 `token` from the current contract to `to`.
/// Reverts upon failure.
function safeTransfer(address token, address to, uint256 amount) internal {
/// @solidity memory-safe-assembly
assembly {
mstore(0x14, to) // Store the `to` argument.
mstore(0x34, amount) // Store the `amount` argument.
mstore(0x00, 0xa9059cbb000000000000000000000000) // `transfer(address,uint256)`.
// Perform the transfer, reverting upon failure.
let success := call(gas(), token, 0, 0x10, 0x44, 0x00, 0x20)
if iszero(and(eq(mload(0x00), 1), success)) {
if iszero(lt(or(iszero(extcodesize(token)), returndatasize()), success)) {
mstore(0x00, 0x90b8ec18) // `TransferFailed()`.
revert(0x1c, 0x04)
}
}
mstore(0x34, 0) // Restore the part of the free memory pointer that was overwritten.
}
}
/// @dev Sends all of ERC20 `token` from the current contract to `to`.
/// Reverts upon failure.
function safeTransferAll(address token, address to) internal returns (uint256 amount) {
/// @solidity memory-safe-assembly
assembly {
mstore(0x00, 0x70a08231) // Store the function selector of `balanceOf(address)`.
mstore(0x20, address()) // Store the address of the current contract.
// Read the balance, reverting upon failure.
if iszero(
and( // The arguments of `and` are evaluated from right to left.
gt(returndatasize(), 0x1f), // At least 32 bytes returned.
staticcall(gas(), token, 0x1c, 0x24, 0x34, 0x20)
)
) {
mstore(0x00, 0x90b8ec18) // `TransferFailed()`.
revert(0x1c, 0x04)
}
mstore(0x14, to) // Store the `to` argument.
amount := mload(0x34) // The `amount` is already at 0x34. We'll need to return it.
mstore(0x00, 0xa9059cbb000000000000000000000000) // `transfer(address,uint256)`.
// Perform the transfer, reverting upon failure.
let success := call(gas(), token, 0, 0x10, 0x44, 0x00, 0x20)
if iszero(and(eq(mload(0x00), 1), success)) {
if iszero(lt(or(iszero(extcodesize(token)), returndatasize()), success)) {
mstore(0x00, 0x90b8ec18) // `TransferFailed()`.
revert(0x1c, 0x04)
}
}
mstore(0x34, 0) // Restore the part of the free memory pointer that was overwritten.
}
}
/// @dev Sets `amount` of ERC20 `token` for `to` to manage on behalf of the current contract.
/// Reverts upon failure.
function safeApprove(address token, address to, uint256 amount) internal {
/// @solidity memory-safe-assembly
assembly {
mstore(0x14, to) // Store the `to` argument.
mstore(0x34, amount) // Store the `amount` argument.
mstore(0x00, 0x095ea7b3000000000000000000000000) // `approve(address,uint256)`.
let success := call(gas(), token, 0, 0x10, 0x44, 0x00, 0x20)
if iszero(and(eq(mload(0x00), 1), success)) {
if iszero(lt(or(iszero(extcodesize(token)), returndatasize()), success)) {
mstore(0x00, 0x3e3f8f73) // `ApproveFailed()`.
revert(0x1c, 0x04)
}
}
mstore(0x34, 0) // Restore the part of the free memory pointer that was overwritten.
}
}
/// @dev Sets `amount` of ERC20 `token` for `to` to manage on behalf of the current contract.
/// If the initial attempt to approve fails, attempts to reset the approved amount to zero,
/// then retries the approval again (some tokens, e.g. USDT, requires this).
/// Reverts upon failure.
function safeApproveWithRetry(address token, address to, uint256 amount) internal {
/// @solidity memory-safe-assembly
assembly {
mstore(0x14, to) // Store the `to` argument.
mstore(0x34, amount) // Store the `amount` argument.
mstore(0x00, 0x095ea7b3000000000000000000000000) // `approve(address,uint256)`.
// Perform the approval, retrying upon failure.
let success := call(gas(), token, 0, 0x10, 0x44, 0x00, 0x20)
if iszero(and(eq(mload(0x00), 1), success)) {
if iszero(lt(or(iszero(extcodesize(token)), returndatasize()), success)) {
mstore(0x34, 0) // Store 0 for the `amount`.
mstore(0x00, 0x095ea7b3000000000000000000000000) // `approve(address,uint256)`.
pop(call(gas(), token, 0, 0x10, 0x44, codesize(), 0x00)) // Reset the approval.
mstore(0x34, amount) // Store back the original `amount`.
// Retry the approval, reverting upon failure.
success := call(gas(), token, 0, 0x10, 0x44, 0x00, 0x20)
if iszero(and(eq(mload(0x00), 1), success)) {
// Check the `extcodesize` again just in case the token selfdestructs lol.
if iszero(lt(or(iszero(extcodesize(token)), returndatasize()), success)) {
mstore(0x00, 0x3e3f8f73) // `ApproveFailed()`.
revert(0x1c, 0x04)
}
}
}
}
mstore(0x34, 0) // Restore the part of the free memory pointer that was overwritten.
}
}
/// @dev Returns the amount of ERC20 `token` owned by `account`.
/// Returns zero if the `token` does not exist.
function balanceOf(address token, address account) internal view returns (uint256 amount) {
/// @solidity memory-safe-assembly
assembly {
mstore(0x14, account) // Store the `account` argument.
mstore(0x00, 0x70a08231000000000000000000000000) // `balanceOf(address)`.
amount :=
mul( // The arguments of `mul` are evaluated from right to left.
mload(0x20),
and( // The arguments of `and` are evaluated from right to left.
gt(returndatasize(), 0x1f), // At least 32 bytes returned.
staticcall(gas(), token, 0x10, 0x24, 0x20, 0x20)
)
)
}
}
/// @dev Returns the total supply of the `token`.
/// Reverts if the token does not exist or does not implement `totalSupply()`.
function totalSupply(address token) internal view returns (uint256 result) {
/// @solidity memory-safe-assembly
assembly {
mstore(0x00, 0x18160ddd) // `totalSupply()`.
if iszero(
and(gt(returndatasize(), 0x1f), staticcall(gas(), token, 0x1c, 0x04, 0x00, 0x20))
) {
mstore(0x00, 0x54cd9435) // `TotalSupplyQueryFailed()`.
revert(0x1c, 0x04)
}
result := mload(0x00)
}
}
/// @dev Sends `amount` of ERC20 `token` from `from` to `to`.
/// If the initial attempt fails, try to use Permit2 to transfer the token.
/// Reverts upon failure.
///
/// The `from` account must have at least `amount` approved for the current contract to manage.
function safeTransferFrom2(address token, address from, address to, uint256 amount) internal {
if (!trySafeTransferFrom(token, from, to, amount)) {
permit2TransferFrom(token, from, to, amount);
}
}
/// @dev Sends `amount` of ERC20 `token` from `from` to `to` via Permit2.
/// Reverts upon failure.
function permit2TransferFrom(address token, address from, address to, uint256 amount)
internal
{
/// @solidity memory-safe-assembly
assembly {
let m := mload(0x40)
mstore(add(m, 0x74), shr(96, shl(96, token)))
mstore(add(m, 0x54), amount)
mstore(add(m, 0x34), to)
mstore(add(m, 0x20), shl(96, from))
// `transferFrom(address,address,uint160,address)`.
mstore(m, 0x36c78516000000000000000000000000)
let p := PERMIT2
let exists := eq(chainid(), 1)
if iszero(exists) { exists := iszero(iszero(extcodesize(p))) }
if iszero(
and(
call(gas(), p, 0, add(m, 0x10), 0x84, codesize(), 0x00),
lt(iszero(extcodesize(token)), exists) // Token has code and Permit2 exists.
)
) {
mstore(0x00, 0x7939f4248757f0fd) // `TransferFromFailed()` or `Permit2AmountOverflow()`.
revert(add(0x18, shl(2, iszero(iszero(shr(160, amount))))), 0x04)
}
}
}
/// @dev Permit a user to spend a given amount of
/// another user's tokens via native EIP-2612 permit if possible, falling
/// back to Permit2 if native permit fails or is not implemented on the token.
function permit2(
address token,
address owner,
address spender,
uint256 amount,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) internal {
bool success;
/// @solidity memory-safe-assembly
assembly {
for {} shl(96, xor(token, WETH9)) {} {
mstore(0x00, 0x3644e515) // `DOMAIN_SEPARATOR()`.
if iszero(
and( // The arguments of `and` are evaluated from right to left.
lt(iszero(mload(0x00)), eq(returndatasize(), 0x20)), // Returns 1 non-zero word.
// Gas stipend to limit gas burn for tokens that don't refund gas when
// an non-existing function is called. 5K should be enough for a SLOAD.
staticcall(5000, token, 0x1c, 0x04, 0x00, 0x20)
)
) { break }
// After here, we can be sure that token is a contract.
let m := mload(0x40)
mstore(add(m, 0x34), spender)
mstore(add(m, 0x20), shl(96, owner))
mstore(add(m, 0x74), deadline)
if eq(mload(0x00), DAI_DOMAIN_SEPARATOR) {
mstore(0x14, owner)
mstore(0x00, 0x7ecebe00000000000000000000000000) // `nonces(address)`.
mstore(
add(m, 0x94),
lt(iszero(amount), staticcall(gas(), token, 0x10, 0x24, add(m, 0x54), 0x20))
)
mstore(m, 0x8fcbaf0c000000000000000000000000) // `IDAIPermit.permit`.
// `nonces` is already at `add(m, 0x54)`.
// `amount != 0` is already stored at `add(m, 0x94)`.
mstore(add(m, 0xb4), and(0xff, v))
mstore(add(m, 0xd4), r)
mstore(add(m, 0xf4), s)
success := call(gas(), token, 0, add(m, 0x10), 0x104, codesize(), 0x00)
break
}
mstore(m, 0xd505accf000000000000000000000000) // `IERC20Permit.permit`.
mstore(add(m, 0x54), amount)
mstore(add(m, 0x94), and(0xff, v))
mstore(add(m, 0xb4), r)
mstore(add(m, 0xd4), s)
success := call(gas(), token, 0, add(m, 0x10), 0xe4, codesize(), 0x00)
break
}
}
if (!success) simplePermit2(token, owner, spender, amount, deadline, v, r, s);
}
/// @dev Simple permit on the Permit2 contract.
function simplePermit2(
address token,
address owner,
address spender,
uint256 amount,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) internal {
/// @solidity memory-safe-assembly
assembly {
let m := mload(0x40)
mstore(m, 0x927da105) // `allowance(address,address,address)`.
{
let addressMask := shr(96, not(0))
mstore(add(m, 0x20), and(addressMask, owner))
mstore(add(m, 0x40), and(addressMask, token))
mstore(add(m, 0x60), and(addressMask, spender))
mstore(add(m, 0xc0), and(addressMask, spender))
}
let p := mul(PERMIT2, iszero(shr(160, amount)))
if iszero(
and( // The arguments of `and` are evaluated from right to left.
gt(returndatasize(), 0x5f), // Returns 3 words: `amount`, `expiration`, `nonce`.
staticcall(gas(), p, add(m, 0x1c), 0x64, add(m, 0x60), 0x60)
)
) {
mstore(0x00, 0x6b836e6b8757f0fd) // `Permit2Failed()` or `Permit2AmountOverflow()`.
revert(add(0x18, shl(2, iszero(p))), 0x04)
}
mstore(m, 0x2b67b570) // `Permit2.permit` (PermitSingle variant).
// `owner` is already `add(m, 0x20)`.
// `token` is already at `add(m, 0x40)`.
mstore(add(m, 0x60), amount)
mstore(add(m, 0x80), 0xffffffffffff) // `expiration = type(uint48).max`.
// `nonce` is already at `add(m, 0xa0)`.
// `spender` is already at `add(m, 0xc0)`.
mstore(add(m, 0xe0), deadline)
mstore(add(m, 0x100), 0x100) // `signature` offset.
mstore(add(m, 0x120), 0x41) // `signature` length.
mstore(add(m, 0x140), r)
mstore(add(m, 0x160), s)
mstore(add(m, 0x180), shl(248, v))
if iszero( // Revert if token does not have code, or if the call fails.
mul(extcodesize(token), call(gas(), p, 0, add(m, 0x1c), 0x184, codesize(), 0x00))) {
mstore(0x00, 0x6b836e6b) // `Permit2Failed()`.
revert(0x1c, 0x04)
}
}
}
/// @dev Approves `spender` to spend `amount` of `token` for `address(this)`.
function permit2Approve(address token, address spender, uint160 amount, uint48 expiration)
internal
{
/// @solidity memory-safe-assembly
assembly {
let addressMask := shr(96, not(0))
let m := mload(0x40)
mstore(m, 0x87517c45) // `approve(address,address,uint160,uint48)`.
mstore(add(m, 0x20), and(addressMask, token))
mstore(add(m, 0x40), and(addressMask, spender))
mstore(add(m, 0x60), and(addressMask, amount))
mstore(add(m, 0x80), and(0xffffffffffff, expiration))
if iszero(call(gas(), PERMIT2, 0, add(m, 0x1c), 0xa0, codesize(), 0x00)) {
mstore(0x00, 0x324f14ae) // `Permit2ApproveFailed()`.
revert(0x1c, 0x04)
}
}
}
/// @dev Revokes an approval for `token` and `spender` for `address(this)`.
function permit2Lockdown(address token, address spender) internal {
/// @solidity memory-safe-assembly
assembly {
let m := mload(0x40)
mstore(m, 0xcc53287f) // `Permit2.lockdown`.
mstore(add(m, 0x20), 0x20) // Offset of the `approvals`.
mstore(add(m, 0x40), 1) // `approvals.length`.
mstore(add(m, 0x60), shr(96, shl(96, token)))
mstore(add(m, 0x80), shr(96, shl(96, spender)))
if iszero(call(gas(), PERMIT2, 0, add(m, 0x1c), 0xa0, codesize(), 0x00)) {
mstore(0x00, 0x96b3de23) // `Permit2LockdownFailed()`.
revert(0x1c, 0x04)
}
}
}
}// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.25;
import {IERC20} from "forge-std/interfaces/IERC20.sol";
import {SafeTransferLib} from "solady/utils/SafeTransferLib.sol";
// Minimal interface for decimals check
interface IERC20Decimals {
function decimals() external view returns (uint8);
}
/**
* @title CollateralManagementLib
* @notice Library for managing multi-collateral operations in BtcVaultStrategy
*/
library CollateralManagementLib {
using SafeTransferLib for address;
/*//////////////////////////////////////////////////////////////
ERRORS
//////////////////////////////////////////////////////////////*/
error AssetNotSupported();
error AssetAlreadySupported();
error InvalidDecimals();
error InsufficientLiquidity();
error InvalidAmount();
error InvalidAddress();
/*//////////////////////////////////////////////////////////////
EVENTS
//////////////////////////////////////////////////////////////*/
event CollateralAdded(address indexed token, uint8 decimals);
event CollateralRemoved(address indexed token);
event LiquidityAdded(uint256 amount);
event LiquidityRemoved(uint256 amount);
event CollateralWithdrawn(address indexed token, uint256 amount, address indexed to);
event CollateralDeposited(address indexed depositor, address indexed token, uint256 amount);
/*//////////////////////////////////////////////////////////////
COLLATERAL MANAGEMENT
//////////////////////////////////////////////////////////////*/
/**
* @notice Add a new supported collateral token
* @param token Address of the BTC collateral token
* @param supportedAssets Mapping of supported assets
* @param collateralTokens Array of collateral tokens
* @param requiredDecimals Required decimals for collateral
*/
function addCollateral(
address token,
mapping(address => bool) storage supportedAssets,
address[] storage collateralTokens,
uint8 requiredDecimals
) external {
if (token == address(0)) revert InvalidAddress();
if (supportedAssets[token]) revert AssetAlreadySupported();
// Verify the token has required decimals
try IERC20Decimals(token).decimals() returns (uint8 decimals) {
if (decimals != requiredDecimals) revert InvalidDecimals();
} catch {
revert InvalidDecimals();
}
supportedAssets[token] = true;
collateralTokens.push(token);
emit CollateralAdded(token, requiredDecimals);
}
/**
* @notice Remove a supported collateral token
* @param token Address of the collateral token to remove
* @param asset The main asset address (cannot be removed)
* @param supportedAssets Mapping of supported assets
* @param collateralTokens Array of collateral tokens
*/
function removeCollateral(
address token,
address asset,
mapping(address => bool) storage supportedAssets,
address[] storage collateralTokens
) external {
if (!supportedAssets[token]) revert AssetNotSupported();
if (token == asset) revert InvalidAddress();
supportedAssets[token] = false;
// Remove from array
for (uint256 i = 0; i < collateralTokens.length;) {
if (collateralTokens[i] == token) {
collateralTokens[i] = collateralTokens[collateralTokens.length - 1];
collateralTokens.pop();
break;
}
unchecked {
++i;
}
}
emit CollateralRemoved(token);
}
/**
* @notice Deposit collateral directly to the strategy
* @param token The collateral token to deposit
* @param amount The amount to deposit
* @param supportedAssets Mapping of supported assets
*/
function depositCollateral(address token, uint256 amount, mapping(address => bool) storage supportedAssets)
external
{
if (!supportedAssets[token]) revert AssetNotSupported();
if (amount == 0) revert InvalidAmount();
// Transfer collateral from sender to this contract
token.safeTransferFrom(msg.sender, address(this), amount);
emit CollateralDeposited(msg.sender, token, amount);
}
/**
* @notice Withdraw collateral to admin (emergency or rebalancing)
* @param token Collateral token to withdraw
* @param amount Amount to withdraw
* @param to Recipient address
* @param supportedAssets Mapping of supported assets
*/
function withdrawCollateral(
address token,
uint256 amount,
address to,
mapping(address => bool) storage supportedAssets
) external {
if (!supportedAssets[token]) revert AssetNotSupported();
if (amount == 0) revert InvalidAmount();
if (to == address(0)) revert InvalidAddress();
uint256 tokenBalance = IERC20(token).balanceOf(address(this));
if (amount > tokenBalance) revert InsufficientLiquidity();
token.safeTransfer(to, amount);
emit CollateralWithdrawn(token, amount, to);
}
/*//////////////////////////////////////////////////////////////
LIQUIDITY MANAGEMENT
//////////////////////////////////////////////////////////////*/
/**
* @notice Add sovaBTC for redemptions
* @param asset The asset to add liquidity for
* @param amount Amount to add
*/
function addLiquidity(address asset, uint256 amount) external {
if (amount == 0) revert InvalidAmount();
// Transfer asset from manager
asset.safeTransferFrom(msg.sender, address(this), amount);
emit LiquidityAdded(amount);
}
/**
* @notice Remove sovaBTC from strategy
* @param asset The asset to remove liquidity for
* @param amount Amount to remove
* @param to Address to send the asset
*/
function removeLiquidity(address asset, uint256 amount, address to) external {
if (amount == 0) revert InvalidAmount();
uint256 balance = IERC20(asset).balanceOf(address(this));
if (amount > balance) revert InsufficientLiquidity();
if (to == address(0)) revert InvalidAddress();
asset.safeTransfer(to, amount);
emit LiquidityRemoved(amount);
}
}// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.25;
import {IERC20} from "forge-std/interfaces/IERC20.sol";
/**
* @title CollateralViewLib
* @notice Library for view functions in BtcVaultStrategy
*/
library CollateralViewLib {
/**
* @notice Get total collateral assets value in sovaBTC terms (1:1 for all BTC variants)
* @dev This sums raw collateral balances without NAV adjustment
* @param collateralTokens Array of collateral token addresses
* @return Total value of all collateral in 8 decimal units
*/
function totalCollateralAssets(address[] storage collateralTokens) external view returns (uint256) {
uint256 total = 0;
// Sum all collateral balances (all have 1:1 value with sovaBTC)
for (uint256 i = 0; i < collateralTokens.length;) {
address token = collateralTokens[i];
uint256 tokenBalance = IERC20(token).balanceOf(address(this));
total += tokenBalance; // All BTC tokens are 8 decimals, 1:1 with sovaBTC
unchecked {
++i;
}
}
return total;
}
/**
* @notice Get balance of a specific collateral token
* @param token Address of the collateral token
* @param supportedAssets Mapping of supported assets
* @return Balance of the token held by strategy
*/
function collateralBalance(address token, mapping(address => bool) storage supportedAssets)
external
view
returns (uint256)
{
if (!supportedAssets[token]) return 0;
return IERC20(token).balanceOf(address(this));
}
/**
* @notice Get available sovaBTC balance for redemptions
* @param asset The asset address
* @return Current sovaBTC balance in the strategy
*/
function availableLiquidity(address asset) external view returns (uint256) {
return IERC20(asset).balanceOf(address(this));
}
}// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
/// @notice Gas optimized ECDSA wrapper.
/// @author Solady (https://github.com/vectorized/solady/blob/main/src/utils/ECDSA.sol)
/// @author Modified from Solmate (https://github.com/transmissions11/solmate/blob/main/src/utils/ECDSA.sol)
/// @author Modified from OpenZeppelin (https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/cryptography/ECDSA.sol)
///
/// @dev Note:
/// - The recovery functions use the ecrecover precompile (0x1).
/// - As of Solady version 0.0.68, the `recover` variants will revert upon recovery failure.
/// This is for more safety by default.
/// Use the `tryRecover` variants if you need to get the zero address back
/// upon recovery failure instead.
/// - As of Solady version 0.0.134, all `bytes signature` variants accept both
/// regular 65-byte `(r, s, v)` and EIP-2098 `(r, vs)` short form signatures.
/// See: https://eips.ethereum.org/EIPS/eip-2098
/// This is for calldata efficiency on smart accounts prevalent on L2s.
///
/// WARNING! Do NOT directly use signatures as unique identifiers:
/// - The recovery operations do NOT check if a signature is non-malleable.
/// - Use a nonce in the digest to prevent replay attacks on the same contract.
/// - Use EIP-712 for the digest to prevent replay attacks across different chains and contracts.
/// EIP-712 also enables readable signing of typed data for better user safety.
/// - If you need a unique hash from a signature, please use the `canonicalHash` functions.
library ECDSA {
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CONSTANTS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev The order of the secp256k1 elliptic curve.
uint256 internal constant N = 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141;
/// @dev `N/2 + 1`. Used for checking the malleability of the signature.
uint256 private constant _HALF_N_PLUS_1 =
0x7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a1;
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CUSTOM ERRORS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev The signature is invalid.
error InvalidSignature();
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* RECOVERY OPERATIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Recovers the signer's address from a message digest `hash`, and the `signature`.
function recover(bytes32 hash, bytes memory signature) internal view returns (address result) {
/// @solidity memory-safe-assembly
assembly {
for { let m := mload(0x40) } 1 {
mstore(0x00, 0x8baa579f) // `InvalidSignature()`.
revert(0x1c, 0x04)
} {
switch mload(signature)
case 64 {
let vs := mload(add(signature, 0x40))
mstore(0x20, add(shr(255, vs), 27)) // `v`.
mstore(0x60, shr(1, shl(1, vs))) // `s`.
}
case 65 {
mstore(0x20, byte(0, mload(add(signature, 0x60)))) // `v`.
mstore(0x60, mload(add(signature, 0x40))) // `s`.
}
default { continue }
mstore(0x00, hash)
mstore(0x40, mload(add(signature, 0x20))) // `r`.
result := mload(staticcall(gas(), 1, 0x00, 0x80, 0x01, 0x20))
mstore(0x60, 0) // Restore the zero slot.
mstore(0x40, m) // Restore the free memory pointer.
// `returndatasize()` will be `0x20` upon success, and `0x00` otherwise.
if returndatasize() { break }
}
}
}
/// @dev Recovers the signer's address from a message digest `hash`, and the `signature`.
function recoverCalldata(bytes32 hash, bytes calldata signature)
internal
view
returns (address result)
{
/// @solidity memory-safe-assembly
assembly {
for { let m := mload(0x40) } 1 {
mstore(0x00, 0x8baa579f) // `InvalidSignature()`.
revert(0x1c, 0x04)
} {
switch signature.length
case 64 {
let vs := calldataload(add(signature.offset, 0x20))
mstore(0x20, add(shr(255, vs), 27)) // `v`.
mstore(0x40, calldataload(signature.offset)) // `r`.
mstore(0x60, shr(1, shl(1, vs))) // `s`.
}
case 65 {
mstore(0x20, byte(0, calldataload(add(signature.offset, 0x40)))) // `v`.
calldatacopy(0x40, signature.offset, 0x40) // Copy `r` and `s`.
}
default { continue }
mstore(0x00, hash)
result := mload(staticcall(gas(), 1, 0x00, 0x80, 0x01, 0x20))
mstore(0x60, 0) // Restore the zero slot.
mstore(0x40, m) // Restore the free memory pointer.
// `returndatasize()` will be `0x20` upon success, and `0x00` otherwise.
if returndatasize() { break }
}
}
}
/// @dev Recovers the signer's address from a message digest `hash`,
/// and the EIP-2098 short form signature defined by `r` and `vs`.
function recover(bytes32 hash, bytes32 r, bytes32 vs) internal view returns (address result) {
/// @solidity memory-safe-assembly
assembly {
let m := mload(0x40) // Cache the free memory pointer.
mstore(0x00, hash)
mstore(0x20, add(shr(255, vs), 27)) // `v`.
mstore(0x40, r)
mstore(0x60, shr(1, shl(1, vs))) // `s`.
result := mload(staticcall(gas(), 1, 0x00, 0x80, 0x01, 0x20))
// `returndatasize()` will be `0x20` upon success, and `0x00` otherwise.
if iszero(returndatasize()) {
mstore(0x00, 0x8baa579f) // `InvalidSignature()`.
revert(0x1c, 0x04)
}
mstore(0x60, 0) // Restore the zero slot.
mstore(0x40, m) // Restore the free memory pointer.
}
}
/// @dev Recovers the signer's address from a message digest `hash`,
/// and the signature defined by `v`, `r`, `s`.
function recover(bytes32 hash, uint8 v, bytes32 r, bytes32 s)
internal
view
returns (address result)
{
/// @solidity memory-safe-assembly
assembly {
let m := mload(0x40) // Cache the free memory pointer.
mstore(0x00, hash)
mstore(0x20, and(v, 0xff))
mstore(0x40, r)
mstore(0x60, s)
result := mload(staticcall(gas(), 1, 0x00, 0x80, 0x01, 0x20))
// `returndatasize()` will be `0x20` upon success, and `0x00` otherwise.
if iszero(returndatasize()) {
mstore(0x00, 0x8baa579f) // `InvalidSignature()`.
revert(0x1c, 0x04)
}
mstore(0x60, 0) // Restore the zero slot.
mstore(0x40, m) // Restore the free memory pointer.
}
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* TRY-RECOVER OPERATIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
// WARNING!
// These functions will NOT revert upon recovery failure.
// Instead, they will return the zero address upon recovery failure.
// It is critical that the returned address is NEVER compared against
// a zero address (e.g. an uninitialized address variable).
/// @dev Recovers the signer's address from a message digest `hash`, and the `signature`.
function tryRecover(bytes32 hash, bytes memory signature)
internal
view
returns (address result)
{
/// @solidity memory-safe-assembly
assembly {
for { let m := mload(0x40) } 1 {} {
switch mload(signature)
case 64 {
let vs := mload(add(signature, 0x40))
mstore(0x20, add(shr(255, vs), 27)) // `v`.
mstore(0x60, shr(1, shl(1, vs))) // `s`.
}
case 65 {
mstore(0x20, byte(0, mload(add(signature, 0x60)))) // `v`.
mstore(0x60, mload(add(signature, 0x40))) // `s`.
}
default { break }
mstore(0x00, hash)
mstore(0x40, mload(add(signature, 0x20))) // `r`.
pop(staticcall(gas(), 1, 0x00, 0x80, 0x40, 0x20))
mstore(0x60, 0) // Restore the zero slot.
// `returndatasize()` will be `0x20` upon success, and `0x00` otherwise.
result := mload(xor(0x60, returndatasize()))
mstore(0x40, m) // Restore the free memory pointer.
break
}
}
}
/// @dev Recovers the signer's address from a message digest `hash`, and the `signature`.
function tryRecoverCalldata(bytes32 hash, bytes calldata signature)
internal
view
returns (address result)
{
/// @solidity memory-safe-assembly
assembly {
for { let m := mload(0x40) } 1 {} {
switch signature.length
case 64 {
let vs := calldataload(add(signature.offset, 0x20))
mstore(0x20, add(shr(255, vs), 27)) // `v`.
mstore(0x40, calldataload(signature.offset)) // `r`.
mstore(0x60, shr(1, shl(1, vs))) // `s`.
}
case 65 {
mstore(0x20, byte(0, calldataload(add(signature.offset, 0x40)))) // `v`.
calldatacopy(0x40, signature.offset, 0x40) // Copy `r` and `s`.
}
default { break }
mstore(0x00, hash)
pop(staticcall(gas(), 1, 0x00, 0x80, 0x40, 0x20))
mstore(0x60, 0) // Restore the zero slot.
// `returndatasize()` will be `0x20` upon success, and `0x00` otherwise.
result := mload(xor(0x60, returndatasize()))
mstore(0x40, m) // Restore the free memory pointer.
break
}
}
}
/// @dev Recovers the signer's address from a message digest `hash`,
/// and the EIP-2098 short form signature defined by `r` and `vs`.
function tryRecover(bytes32 hash, bytes32 r, bytes32 vs)
internal
view
returns (address result)
{
/// @solidity memory-safe-assembly
assembly {
let m := mload(0x40) // Cache the free memory pointer.
mstore(0x00, hash)
mstore(0x20, add(shr(255, vs), 27)) // `v`.
mstore(0x40, r)
mstore(0x60, shr(1, shl(1, vs))) // `s`.
pop(staticcall(gas(), 1, 0x00, 0x80, 0x40, 0x20))
mstore(0x60, 0) // Restore the zero slot.
// `returndatasize()` will be `0x20` upon success, and `0x00` otherwise.
result := mload(xor(0x60, returndatasize()))
mstore(0x40, m) // Restore the free memory pointer.
}
}
/// @dev Recovers the signer's address from a message digest `hash`,
/// and the signature defined by `v`, `r`, `s`.
function tryRecover(bytes32 hash, uint8 v, bytes32 r, bytes32 s)
internal
view
returns (address result)
{
/// @solidity memory-safe-assembly
assembly {
let m := mload(0x40) // Cache the free memory pointer.
mstore(0x00, hash)
mstore(0x20, and(v, 0xff))
mstore(0x40, r)
mstore(0x60, s)
pop(staticcall(gas(), 1, 0x00, 0x80, 0x40, 0x20))
mstore(0x60, 0) // Restore the zero slot.
// `returndatasize()` will be `0x20` upon success, and `0x00` otherwise.
result := mload(xor(0x60, returndatasize()))
mstore(0x40, m) // Restore the free memory pointer.
}
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* HASHING OPERATIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Returns an Ethereum Signed Message, created from a `hash`.
/// This produces a hash corresponding to the one signed with the
/// [`eth_sign`](https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_sign)
/// JSON-RPC method as part of EIP-191.
function toEthSignedMessageHash(bytes32 hash) internal pure returns (bytes32 result) {
/// @solidity memory-safe-assembly
assembly {
mstore(0x20, hash) // Store into scratch space for keccak256.
mstore(0x00, "\x00\x00\x00\x00\x19Ethereum Signed Message:\n32") // 28 bytes.
result := keccak256(0x04, 0x3c) // `32 * 2 - (32 - 28) = 60 = 0x3c`.
}
}
/// @dev Returns an Ethereum Signed Message, created from `s`.
/// This produces a hash corresponding to the one signed with the
/// [`eth_sign`](https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_sign)
/// JSON-RPC method as part of EIP-191.
/// Note: Supports lengths of `s` up to 999999 bytes.
function toEthSignedMessageHash(bytes memory s) internal pure returns (bytes32 result) {
/// @solidity memory-safe-assembly
assembly {
let sLength := mload(s)
let o := 0x20
mstore(o, "\x19Ethereum Signed Message:\n") // 26 bytes, zero-right-padded.
mstore(0x00, 0x00)
// Convert the `s.length` to ASCII decimal representation: `base10(s.length)`.
for { let temp := sLength } 1 {} {
o := sub(o, 1)
mstore8(o, add(48, mod(temp, 10)))
temp := div(temp, 10)
if iszero(temp) { break }
}
let n := sub(0x3a, o) // Header length: `26 + 32 - o`.
// Throw an out-of-offset error (consumes all gas) if the header exceeds 32 bytes.
returndatacopy(returndatasize(), returndatasize(), gt(n, 0x20))
mstore(s, or(mload(0x00), mload(n))) // Temporarily store the header.
result := keccak256(add(s, sub(0x20, n)), add(n, sLength))
mstore(s, sLength) // Restore the length.
}
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CANONICAL HASH FUNCTIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
// The following functions returns the hash of the signature in it's canonicalized format,
// which is the 65-byte `abi.encodePacked(r, s, uint8(v))`, where `v` is either 27 or 28.
// If `s` is greater than `N / 2` then it will be converted to `N - s`
// and the `v` value will be flipped.
// If the signature has an invalid length, or if `v` is invalid,
// a uniquely corrupt hash will be returned.
// These functions are useful for "poor-mans-VRF".
/// @dev Returns the canonical hash of `signature`.
function canonicalHash(bytes memory signature) internal pure returns (bytes32 result) {
/// @solidity memory-safe-assembly
assembly {
let l := mload(signature)
for {} 1 {} {
mstore(0x00, mload(add(signature, 0x20))) // `r`.
let s := mload(add(signature, 0x40))
let v := mload(add(signature, 0x41))
if eq(l, 64) {
v := add(shr(255, s), 27)
s := shr(1, shl(1, s))
}
if iszero(lt(s, _HALF_N_PLUS_1)) {
v := xor(v, 7)
s := sub(N, s)
}
mstore(0x21, v)
mstore(0x20, s)
result := keccak256(0x00, 0x41)
mstore(0x21, 0) // Restore the overwritten part of the free memory pointer.
break
}
// If the length is neither 64 nor 65, return a uniquely corrupted hash.
if iszero(lt(sub(l, 64), 2)) {
// `bytes4(keccak256("InvalidSignatureLength"))`.
result := xor(keccak256(add(signature, 0x20), l), 0xd62f1ab2)
}
}
}
/// @dev Returns the canonical hash of `signature`.
function canonicalHashCalldata(bytes calldata signature)
internal
pure
returns (bytes32 result)
{
/// @solidity memory-safe-assembly
assembly {
for {} 1 {} {
mstore(0x00, calldataload(signature.offset)) // `r`.
let s := calldataload(add(signature.offset, 0x20))
let v := calldataload(add(signature.offset, 0x21))
if eq(signature.length, 64) {
v := add(shr(255, s), 27)
s := shr(1, shl(1, s))
}
if iszero(lt(s, _HALF_N_PLUS_1)) {
v := xor(v, 7)
s := sub(N, s)
}
mstore(0x21, v)
mstore(0x20, s)
result := keccak256(0x00, 0x41)
mstore(0x21, 0) // Restore the overwritten part of the free memory pointer.
break
}
// If the length is neither 64 nor 65, return a uniquely corrupted hash.
if iszero(lt(sub(signature.length, 64), 2)) {
calldatacopy(mload(0x40), signature.offset, signature.length)
// `bytes4(keccak256("InvalidSignatureLength"))`.
result := xor(keccak256(mload(0x40), signature.length), 0xd62f1ab2)
}
}
}
/// @dev Returns the canonical hash of `signature`.
function canonicalHash(bytes32 r, bytes32 vs) internal pure returns (bytes32 result) {
/// @solidity memory-safe-assembly
assembly {
mstore(0x00, r) // `r`.
let v := add(shr(255, vs), 27)
let s := shr(1, shl(1, vs))
mstore(0x21, v)
mstore(0x20, s)
result := keccak256(0x00, 0x41)
mstore(0x21, 0) // Restore the overwritten part of the free memory pointer.
}
}
/// @dev Returns the canonical hash of `signature`.
function canonicalHash(uint8 v, bytes32 r, bytes32 s) internal pure returns (bytes32 result) {
/// @solidity memory-safe-assembly
assembly {
mstore(0x00, r) // `r`.
if iszero(lt(s, _HALF_N_PLUS_1)) {
v := xor(v, 7)
s := sub(N, s)
}
mstore(0x21, v)
mstore(0x20, s)
result := keccak256(0x00, 0x41)
mstore(0x21, 0) // Restore the overwritten part of the free memory pointer.
}
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* EMPTY CALLDATA HELPERS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Returns an empty calldata bytes.
function emptySignature() internal pure returns (bytes calldata signature) {
/// @solidity memory-safe-assembly
assembly {
signature.length := 0
}
}
}// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.25;
import {SafeTransferLib} from "solady/utils/SafeTransferLib.sol";
import {tRWA} from "./tRWA.sol";
import {IStrategy} from "../strategy/IStrategy.sol";
import {IHook} from "../hooks/IHook.sol";
/**
* @title ManagedWithdrawRWA
* @notice Extension of tRWA that implements manager-initiated withdrawals
*/
contract ManagedWithdrawRWA is tRWA {
/*//////////////////////////////////////////////////////////////
ERRORS
//////////////////////////////////////////////////////////////*/
error UseRedeem();
error InvalidArrayLengths();
error InsufficientOutputAssets();
/*//////////////////////////////////////////////////////////////
CONSTRUCTOR
//////////////////////////////////////////////////////////////*/
/**
* @notice Constructor
* @param name_ Token name
* @param symbol_ Token symbol
* @param asset_ Asset address
* @param assetDecimals_ Decimals of the asset token
* @param strategy_ Strategy address
*/
constructor(string memory name_, string memory symbol_, address asset_, uint8 assetDecimals_, address strategy_)
tRWA(name_, symbol_, asset_, assetDecimals_, strategy_)
{}
/*//////////////////////////////////////////////////////////////
REDEMPTION FUNCTIONS
//////////////////////////////////////////////////////////////*/
/**
* @notice Redeem shares from the strategy with minimum assets check
* @param shares The amount of shares to redeem
* @param to The address to send the assets to
* @param owner The owner of the shares
* @param minAssets The minimum amount of assets to receive
* @return assets The amount of assets received
*/
function redeem(uint256 shares, address to, address owner, uint256 minAssets)
public
onlyStrategy
returns (uint256 assets)
{
if (shares > maxRedeem(owner)) revert RedeemMoreThanMax();
assets = previewRedeem(shares);
if (assets < minAssets) revert InsufficientOutputAssets();
// User must token-approve strategy for withdrawal
_withdraw(strategy, to, owner, assets, shares);
}
/**
* @notice Process a batch of user-requested withdrawals with minimum assets check
* @param shares The amount of shares to redeem
* @param to The address to send the assets to
* @param owner The owner of the shares
* @param minAssets The minimum amount of assets for each withdrawal
* @return assets The amount of assets received
*/
function batchRedeemShares(
uint256[] calldata shares,
address[] calldata to,
address[] calldata owner,
uint256[] calldata minAssets
) external onlyStrategy nonReentrant returns (uint256[] memory assets) {
// Validate array lengths
uint256 len = shares.length;
if (len != to.length || len != owner.length || len != minAssets.length) {
revert InvalidArrayLengths();
}
// Prepare memory array and accumulate total assets required
assets = new uint256[](len);
uint256 totalAssets;
for (uint256 i; i < len; ++i) {
// Validate share amount for each owner
if (shares[i] > maxRedeem(owner[i])) revert RedeemMoreThanMax();
uint256 amt = previewRedeem(shares[i]);
if (amt < minAssets[i]) revert InsufficientOutputAssets();
assets[i] = amt;
totalAssets += amt;
}
// Pull all assets from the strategy in a single transfer
_collect(totalAssets);
// Cache the OP_WITHDRAW hooks once to save gas / avoid stack depth issues
HookInfo[] memory opHooks = operationHooks[OP_WITHDRAW];
// Execute each individual withdrawal (includes hooks)
for (uint256 i; i < len; ++i) {
uint256 userShares = shares[i];
uint256 userAssets = assets[i];
address recipient = to[i];
address shareOwner = owner[i];
// Accounting and transfers (mirrors _withdraw logic sans nonReentrant)
_beforeWithdraw(userAssets, userShares);
_burn(shareOwner, userShares);
// Call hooks (same logic as in _withdraw)
for (uint256 j; j < opHooks.length; ++j) {
IHook.HookOutput memory hookOut =
opHooks[j].hook.onBeforeWithdraw(address(this), strategy, userAssets, recipient, shareOwner);
if (!hookOut.approved) revert HookCheckFailed(hookOut.reason);
}
// Update last executed block for this operation type if hooks were called
if (opHooks.length > 0) {
lastExecutedBlock[OP_WITHDRAW] = block.number;
}
SafeTransferLib.safeTransfer(asset(), recipient, userAssets);
emit Withdraw(strategy, recipient, shareOwner, userAssets, userShares);
}
}
/*//////////////////////////////////////////////////////////////
ERC4626 OVERRIDES
//////////////////////////////////////////////////////////////*/
/**
* @notice Withdraw assets from the strategy - must be called by the manager
* @dev Use redeem instead - all accounting is share-based
* @return shares The amount of shares burned
*/
function withdraw(uint256, address, address) public view override onlyStrategy returns (uint256) {
revert UseRedeem();
}
/**
* @notice Redeem shares from the strategy - must be called by the manager
* @param shares The amount of shares to redeem
* @param to The address to send the assets to
* @param owner The owner of the shares
* @return assets The amount of assets received
*/
function redeem(uint256 shares, address to, address owner) public override onlyStrategy returns (uint256 assets) {
if (shares > maxRedeem(owner)) revert RedeemMoreThanMax();
assets = previewRedeem(shares);
// User must token-approve strategy for withdrawal
_withdraw(strategy, to, owner, assets, shares);
}
}// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.25;
import {BasicStrategy} from "./BasicStrategy.sol";
import {IReporter} from "../reporter/IReporter.sol";
import {IERC20} from "forge-std/interfaces/IERC20.sol";
import {FixedPointMathLib} from "solady/utils/FixedPointMathLib.sol";
/**
* @title ReportedStrategy
* @notice A strategy contract that reports its underlying asset balance through an external oracle using price per share
*/
contract ReportedStrategy is BasicStrategy {
using FixedPointMathLib for uint256;
/*//////////////////////////////////////////////////////////////
ERRORS
//////////////////////////////////////////////////////////////*/
error InvalidReporter();
/*//////////////////////////////////////////////////////////////
EVENTS
//////////////////////////////////////////////////////////////*/
event SetReporter(address indexed reporter);
/*//////////////////////////////////////////////////////////////
STATE
//////////////////////////////////////////////////////////////*/
/// @notice The reporter contract
IReporter public reporter;
/*//////////////////////////////////////////////////////////////
INITIALIZATION
//////////////////////////////////////////////////////////////*/
/**
* @notice Initialize the strategy
* @param name_ The name of the strategy
* @param symbol_ The symbol of the strategy
* @param roleManager_ The role manager address
* @param manager_ The manager address
* @param asset_ The asset address
* @param assetDecimals_ The asset decimals
* @param initData Initialization data
*/
function initialize(
string calldata name_,
string calldata symbol_,
address roleManager_,
address manager_,
address asset_,
uint8 assetDecimals_,
bytes memory initData
) public virtual override {
super.initialize(name_, symbol_, roleManager_, manager_, asset_, assetDecimals_, initData);
address reporter_ = abi.decode(initData, (address));
if (reporter_ == address(0)) revert InvalidReporter();
reporter = IReporter(reporter_);
emit SetReporter(reporter_);
}
/*//////////////////////////////////////////////////////////////
ASSET MANAGEMENT
//////////////////////////////////////////////////////////////*/
/**
* @notice Get the balance of the strategy
* @return The balance of the strategy in the underlying asset
*/
function balance() external view override returns (uint256) {
uint256 _pricePerShare = abi.decode(reporter.report(), (uint256));
uint256 totalSupply = IERC20(sToken).totalSupply();
// Get sToken decimals dynamically
uint8 sTokenDecimals = IERC20(sToken).decimals();
// pricePerShare (18 decimals) * totalSupply (sTokenDecimals) needs to be scaled to asset decimals
// We divide by 10^(18 + sTokenDecimals - assetDecimals) to get the result in asset decimals
uint256 scalingFactor = 10 ** (18 + sTokenDecimals - assetDecimals);
// Scale to asset decimals
return _pricePerShare.mulDiv(totalSupply, scalingFactor);
}
/**
* @notice Get the current price per share from the reporter
* @return The price per share in 18 decimal format
*/
function pricePerShare() external view returns (uint256) {
return abi.decode(reporter.report(), (uint256));
}
/*//////////////////////////////////////////////////////////////
REPORTING MANAGEMENT
//////////////////////////////////////////////////////////////*/
/**
* @notice Set the reporter contract
* @param _reporter The new reporter contract
*/
function setReporter(address _reporter) external onlyManager {
if (_reporter == address(0)) revert InvalidReporter();
reporter = IReporter(_reporter);
emit SetReporter(_reporter);
}
}// SPDX-License-Identifier: MIT
pragma solidity >=0.6.2;
/// @dev Interface of the ERC20 standard as defined in the EIP.
/// @dev This includes the optional name, symbol, and decimals metadata.
interface IERC20 {
/// @dev Emitted when `value` tokens are moved from one account (`from`) to another (`to`).
event Transfer(address indexed from, address indexed to, uint256 value);
/// @dev Emitted when the allowance of a `spender` for an `owner` is set, where `value`
/// is the new allowance.
event Approval(address indexed owner, address indexed spender, uint256 value);
/// @notice Returns the amount of tokens in existence.
function totalSupply() external view returns (uint256);
/// @notice Returns the amount of tokens owned by `account`.
function balanceOf(address account) external view returns (uint256);
/// @notice Moves `amount` tokens from the caller's account to `to`.
function transfer(address to, uint256 amount) external returns (bool);
/// @notice Returns the remaining number of tokens that `spender` is allowed
/// to spend on behalf of `owner`
function allowance(address owner, address spender) external view returns (uint256);
/// @notice Sets `amount` as the allowance of `spender` over the caller's tokens.
/// @dev Be aware of front-running risks: https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
function approve(address spender, uint256 amount) external returns (bool);
/// @notice Moves `amount` tokens from `from` to `to` using the allowance mechanism.
/// `amount` is then deducted from the caller's allowance.
function transferFrom(address from, address to, uint256 amount) external returns (bool);
/// @notice Returns the name of the token.
function name() external view returns (string memory);
/// @notice Returns the symbol of the token.
function symbol() external view returns (string memory);
/// @notice Returns the decimals places of the token.
function decimals() external view returns (uint8);
}// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
/// @notice Arithmetic library with operations for fixed-point numbers.
/// @author Solady (https://github.com/vectorized/solady/blob/main/src/utils/FixedPointMathLib.sol)
/// @author Modified from Solmate (https://github.com/transmissions11/solmate/blob/main/src/utils/FixedPointMathLib.sol)
library FixedPointMathLib {
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CUSTOM ERRORS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev The operation failed, as the output exceeds the maximum value of uint256.
error ExpOverflow();
/// @dev The operation failed, as the output exceeds the maximum value of uint256.
error FactorialOverflow();
/// @dev The operation failed, due to an overflow.
error RPowOverflow();
/// @dev The mantissa is too big to fit.
error MantissaOverflow();
/// @dev The operation failed, due to an multiplication overflow.
error MulWadFailed();
/// @dev The operation failed, due to an multiplication overflow.
error SMulWadFailed();
/// @dev The operation failed, either due to a multiplication overflow, or a division by a zero.
error DivWadFailed();
/// @dev The operation failed, either due to a multiplication overflow, or a division by a zero.
error SDivWadFailed();
/// @dev The operation failed, either due to a multiplication overflow, or a division by a zero.
error MulDivFailed();
/// @dev The division failed, as the denominator is zero.
error DivFailed();
/// @dev The full precision multiply-divide operation failed, either due
/// to the result being larger than 256 bits, or a division by a zero.
error FullMulDivFailed();
/// @dev The output is undefined, as the input is less-than-or-equal to zero.
error LnWadUndefined();
/// @dev The input outside the acceptable domain.
error OutOfDomain();
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CONSTANTS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev The scalar of ETH and most ERC20s.
uint256 internal constant WAD = 1e18;
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* SIMPLIFIED FIXED POINT OPERATIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Equivalent to `(x * y) / WAD` rounded down.
function mulWad(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
// Equivalent to `require(y == 0 || x <= type(uint256).max / y)`.
if gt(x, div(not(0), y)) {
if y {
mstore(0x00, 0xbac65e5b) // `MulWadFailed()`.
revert(0x1c, 0x04)
}
}
z := div(mul(x, y), WAD)
}
}
/// @dev Equivalent to `(x * y) / WAD` rounded down.
function sMulWad(int256 x, int256 y) internal pure returns (int256 z) {
/// @solidity memory-safe-assembly
assembly {
z := mul(x, y)
// Equivalent to `require((x == 0 || z / x == y) && !(x == -1 && y == type(int256).min))`.
if iszero(gt(or(iszero(x), eq(sdiv(z, x), y)), lt(not(x), eq(y, shl(255, 1))))) {
mstore(0x00, 0xedcd4dd4) // `SMulWadFailed()`.
revert(0x1c, 0x04)
}
z := sdiv(z, WAD)
}
}
/// @dev Equivalent to `(x * y) / WAD` rounded down, but without overflow checks.
function rawMulWad(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := div(mul(x, y), WAD)
}
}
/// @dev Equivalent to `(x * y) / WAD` rounded down, but without overflow checks.
function rawSMulWad(int256 x, int256 y) internal pure returns (int256 z) {
/// @solidity memory-safe-assembly
assembly {
z := sdiv(mul(x, y), WAD)
}
}
/// @dev Equivalent to `(x * y) / WAD` rounded up.
function mulWadUp(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := mul(x, y)
// Equivalent to `require(y == 0 || x <= type(uint256).max / y)`.
if iszero(eq(div(z, y), x)) {
if y {
mstore(0x00, 0xbac65e5b) // `MulWadFailed()`.
revert(0x1c, 0x04)
}
}
z := add(iszero(iszero(mod(z, WAD))), div(z, WAD))
}
}
/// @dev Equivalent to `(x * y) / WAD` rounded up, but without overflow checks.
function rawMulWadUp(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := add(iszero(iszero(mod(mul(x, y), WAD))), div(mul(x, y), WAD))
}
}
/// @dev Equivalent to `(x * WAD) / y` rounded down.
function divWad(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
// Equivalent to `require(y != 0 && x <= type(uint256).max / WAD)`.
if iszero(mul(y, lt(x, add(1, div(not(0), WAD))))) {
mstore(0x00, 0x7c5f487d) // `DivWadFailed()`.
revert(0x1c, 0x04)
}
z := div(mul(x, WAD), y)
}
}
/// @dev Equivalent to `(x * WAD) / y` rounded down.
function sDivWad(int256 x, int256 y) internal pure returns (int256 z) {
/// @solidity memory-safe-assembly
assembly {
z := mul(x, WAD)
// Equivalent to `require(y != 0 && ((x * WAD) / WAD == x))`.
if iszero(mul(y, eq(sdiv(z, WAD), x))) {
mstore(0x00, 0x5c43740d) // `SDivWadFailed()`.
revert(0x1c, 0x04)
}
z := sdiv(z, y)
}
}
/// @dev Equivalent to `(x * WAD) / y` rounded down, but without overflow and divide by zero checks.
function rawDivWad(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := div(mul(x, WAD), y)
}
}
/// @dev Equivalent to `(x * WAD) / y` rounded down, but without overflow and divide by zero checks.
function rawSDivWad(int256 x, int256 y) internal pure returns (int256 z) {
/// @solidity memory-safe-assembly
assembly {
z := sdiv(mul(x, WAD), y)
}
}
/// @dev Equivalent to `(x * WAD) / y` rounded up.
function divWadUp(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
// Equivalent to `require(y != 0 && x <= type(uint256).max / WAD)`.
if iszero(mul(y, lt(x, add(1, div(not(0), WAD))))) {
mstore(0x00, 0x7c5f487d) // `DivWadFailed()`.
revert(0x1c, 0x04)
}
z := add(iszero(iszero(mod(mul(x, WAD), y))), div(mul(x, WAD), y))
}
}
/// @dev Equivalent to `(x * WAD) / y` rounded up, but without overflow and divide by zero checks.
function rawDivWadUp(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := add(iszero(iszero(mod(mul(x, WAD), y))), div(mul(x, WAD), y))
}
}
/// @dev Equivalent to `x` to the power of `y`.
/// because `x ** y = (e ** ln(x)) ** y = e ** (ln(x) * y)`.
/// Note: This function is an approximation.
function powWad(int256 x, int256 y) internal pure returns (int256) {
// Using `ln(x)` means `x` must be greater than 0.
return expWad((lnWad(x) * y) / int256(WAD));
}
/// @dev Returns `exp(x)`, denominated in `WAD`.
/// Credit to Remco Bloemen under MIT license: https://2π.com/22/exp-ln
/// Note: This function is an approximation. Monotonically increasing.
function expWad(int256 x) internal pure returns (int256 r) {
unchecked {
// When the result is less than 0.5 we return zero.
// This happens when `x <= (log(1e-18) * 1e18) ~ -4.15e19`.
if (x <= -41446531673892822313) return r;
/// @solidity memory-safe-assembly
assembly {
// When the result is greater than `(2**255 - 1) / 1e18` we can not represent it as
// an int. This happens when `x >= floor(log((2**255 - 1) / 1e18) * 1e18) ≈ 135`.
if iszero(slt(x, 135305999368893231589)) {
mstore(0x00, 0xa37bfec9) // `ExpOverflow()`.
revert(0x1c, 0x04)
}
}
// `x` is now in the range `(-42, 136) * 1e18`. Convert to `(-42, 136) * 2**96`
// for more intermediate precision and a binary basis. This base conversion
// is a multiplication by 1e18 / 2**96 = 5**18 / 2**78.
x = (x << 78) / 5 ** 18;
// Reduce range of x to (-½ ln 2, ½ ln 2) * 2**96 by factoring out powers
// of two such that exp(x) = exp(x') * 2**k, where k is an integer.
// Solving this gives k = round(x / log(2)) and x' = x - k * log(2).
int256 k = ((x << 96) / 54916777467707473351141471128 + 2 ** 95) >> 96;
x = x - k * 54916777467707473351141471128;
// `k` is in the range `[-61, 195]`.
// Evaluate using a (6, 7)-term rational approximation.
// `p` is made monic, we'll multiply by a scale factor later.
int256 y = x + 1346386616545796478920950773328;
y = ((y * x) >> 96) + 57155421227552351082224309758442;
int256 p = y + x - 94201549194550492254356042504812;
p = ((p * y) >> 96) + 28719021644029726153956944680412240;
p = p * x + (4385272521454847904659076985693276 << 96);
// We leave `p` in `2**192` basis so we don't need to scale it back up for the division.
int256 q = x - 2855989394907223263936484059900;
q = ((q * x) >> 96) + 50020603652535783019961831881945;
q = ((q * x) >> 96) - 533845033583426703283633433725380;
q = ((q * x) >> 96) + 3604857256930695427073651918091429;
q = ((q * x) >> 96) - 14423608567350463180887372962807573;
q = ((q * x) >> 96) + 26449188498355588339934803723976023;
/// @solidity memory-safe-assembly
assembly {
// Div in assembly because solidity adds a zero check despite the unchecked.
// The q polynomial won't have zeros in the domain as all its roots are complex.
// No scaling is necessary because p is already `2**96` too large.
r := sdiv(p, q)
}
// r should be in the range `(0.09, 0.25) * 2**96`.
// We now need to multiply r by:
// - The scale factor `s ≈ 6.031367120`.
// - The `2**k` factor from the range reduction.
// - The `1e18 / 2**96` factor for base conversion.
// We do this all at once, with an intermediate result in `2**213`
// basis, so the final right shift is always by a positive amount.
r = int256(
(uint256(r) * 3822833074963236453042738258902158003155416615667) >> uint256(195 - k)
);
}
}
/// @dev Returns `ln(x)`, denominated in `WAD`.
/// Credit to Remco Bloemen under MIT license: https://2π.com/22/exp-ln
/// Note: This function is an approximation. Monotonically increasing.
function lnWad(int256 x) internal pure returns (int256 r) {
/// @solidity memory-safe-assembly
assembly {
// We want to convert `x` from `10**18` fixed point to `2**96` fixed point.
// We do this by multiplying by `2**96 / 10**18`. But since
// `ln(x * C) = ln(x) + ln(C)`, we can simply do nothing here
// and add `ln(2**96 / 10**18)` at the end.
// Compute `k = log2(x) - 96`, `r = 159 - k = 255 - log2(x) = 255 ^ log2(x)`.
r := shl(7, lt(0xffffffffffffffffffffffffffffffff, x))
r := or(r, shl(6, lt(0xffffffffffffffff, shr(r, x))))
r := or(r, shl(5, lt(0xffffffff, shr(r, x))))
r := or(r, shl(4, lt(0xffff, shr(r, x))))
r := or(r, shl(3, lt(0xff, shr(r, x))))
// We place the check here for more optimal stack operations.
if iszero(sgt(x, 0)) {
mstore(0x00, 0x1615e638) // `LnWadUndefined()`.
revert(0x1c, 0x04)
}
// forgefmt: disable-next-item
r := xor(r, byte(and(0x1f, shr(shr(r, x), 0x8421084210842108cc6318c6db6d54be)),
0xf8f9f9faf9fdfafbf9fdfcfdfafbfcfef9fafdfafcfcfbfefafafcfbffffffff))
// Reduce range of x to (1, 2) * 2**96
// ln(2^k * x) = k * ln(2) + ln(x)
x := shr(159, shl(r, x))
// Evaluate using a (8, 8)-term rational approximation.
// `p` is made monic, we will multiply by a scale factor later.
// forgefmt: disable-next-item
let p := sub( // This heavily nested expression is to avoid stack-too-deep for via-ir.
sar(96, mul(add(43456485725739037958740375743393,
sar(96, mul(add(24828157081833163892658089445524,
sar(96, mul(add(3273285459638523848632254066296,
x), x))), x))), x)), 11111509109440967052023855526967)
p := sub(sar(96, mul(p, x)), 45023709667254063763336534515857)
p := sub(sar(96, mul(p, x)), 14706773417378608786704636184526)
p := sub(mul(p, x), shl(96, 795164235651350426258249787498))
// We leave `p` in `2**192` basis so we don't need to scale it back up for the division.
// `q` is monic by convention.
let q := add(5573035233440673466300451813936, x)
q := add(71694874799317883764090561454958, sar(96, mul(x, q)))
q := add(283447036172924575727196451306956, sar(96, mul(x, q)))
q := add(401686690394027663651624208769553, sar(96, mul(x, q)))
q := add(204048457590392012362485061816622, sar(96, mul(x, q)))
q := add(31853899698501571402653359427138, sar(96, mul(x, q)))
q := add(909429971244387300277376558375, sar(96, mul(x, q)))
// `p / q` is in the range `(0, 0.125) * 2**96`.
// Finalization, we need to:
// - Multiply by the scale factor `s = 5.549…`.
// - Add `ln(2**96 / 10**18)`.
// - Add `k * ln(2)`.
// - Multiply by `10**18 / 2**96 = 5**18 >> 78`.
// The q polynomial is known not to have zeros in the domain.
// No scaling required because p is already `2**96` too large.
p := sdiv(p, q)
// Multiply by the scaling factor: `s * 5**18 * 2**96`, base is now `5**18 * 2**192`.
p := mul(1677202110996718588342820967067443963516166, p)
// Add `ln(2) * k * 5**18 * 2**192`.
// forgefmt: disable-next-item
p := add(mul(16597577552685614221487285958193947469193820559219878177908093499208371, sub(159, r)), p)
// Add `ln(2**96 / 10**18) * 5**18 * 2**192`.
p := add(600920179829731861736702779321621459595472258049074101567377883020018308, p)
// Base conversion: mul `2**18 / 2**192`.
r := sar(174, p)
}
}
/// @dev Returns `W_0(x)`, denominated in `WAD`.
/// See: https://en.wikipedia.org/wiki/Lambert_W_function
/// a.k.a. Product log function. This is an approximation of the principal branch.
/// Note: This function is an approximation. Monotonically increasing.
function lambertW0Wad(int256 x) internal pure returns (int256 w) {
// forgefmt: disable-next-item
unchecked {
if ((w = x) <= -367879441171442322) revert OutOfDomain(); // `x` less than `-1/e`.
(int256 wad, int256 p) = (int256(WAD), x);
uint256 c; // Whether we need to avoid catastrophic cancellation.
uint256 i = 4; // Number of iterations.
if (w <= 0x1ffffffffffff) {
if (-0x4000000000000 <= w) {
i = 1; // Inputs near zero only take one step to converge.
} else if (w <= -0x3ffffffffffffff) {
i = 32; // Inputs near `-1/e` take very long to converge.
}
} else if (uint256(w >> 63) == uint256(0)) {
/// @solidity memory-safe-assembly
assembly {
// Inline log2 for more performance, since the range is small.
let v := shr(49, w)
let l := shl(3, lt(0xff, v))
l := add(or(l, byte(and(0x1f, shr(shr(l, v), 0x8421084210842108cc6318c6db6d54be)),
0x0706060506020504060203020504030106050205030304010505030400000000)), 49)
w := sdiv(shl(l, 7), byte(sub(l, 31), 0x0303030303030303040506080c13))
c := gt(l, 60)
i := add(2, add(gt(l, 53), c))
}
} else {
int256 ll = lnWad(w = lnWad(w));
/// @solidity memory-safe-assembly
assembly {
// `w = ln(x) - ln(ln(x)) + b * ln(ln(x)) / ln(x)`.
w := add(sdiv(mul(ll, 1023715080943847266), w), sub(w, ll))
i := add(3, iszero(shr(68, x)))
c := iszero(shr(143, x))
}
if (c == uint256(0)) {
do { // If `x` is big, use Newton's so that intermediate values won't overflow.
int256 e = expWad(w);
/// @solidity memory-safe-assembly
assembly {
let t := mul(w, div(e, wad))
w := sub(w, sdiv(sub(t, x), div(add(e, t), wad)))
}
if (p <= w) break;
p = w;
} while (--i != uint256(0));
/// @solidity memory-safe-assembly
assembly {
w := sub(w, sgt(w, 2))
}
return w;
}
}
do { // Otherwise, use Halley's for faster convergence.
int256 e = expWad(w);
/// @solidity memory-safe-assembly
assembly {
let t := add(w, wad)
let s := sub(mul(w, e), mul(x, wad))
w := sub(w, sdiv(mul(s, wad), sub(mul(e, t), sdiv(mul(add(t, wad), s), add(t, t)))))
}
if (p <= w) break;
p = w;
} while (--i != c);
/// @solidity memory-safe-assembly
assembly {
w := sub(w, sgt(w, 2))
}
// For certain ranges of `x`, we'll use the quadratic-rate recursive formula of
// R. Iacono and J.P. Boyd for the last iteration, to avoid catastrophic cancellation.
if (c == uint256(0)) return w;
int256 t = w | 1;
/// @solidity memory-safe-assembly
assembly {
x := sdiv(mul(x, wad), t)
}
x = (t * (wad + lnWad(x)));
/// @solidity memory-safe-assembly
assembly {
w := sdiv(x, add(wad, t))
}
}
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* GENERAL NUMBER UTILITIES */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Returns `a * b == x * y`, with full precision.
function fullMulEq(uint256 a, uint256 b, uint256 x, uint256 y)
internal
pure
returns (bool result)
{
/// @solidity memory-safe-assembly
assembly {
result := and(eq(mul(a, b), mul(x, y)), eq(mulmod(x, y, not(0)), mulmod(a, b, not(0))))
}
}
/// @dev Calculates `floor(x * y / d)` with full precision.
/// Throws if result overflows a uint256 or when `d` is zero.
/// Credit to Remco Bloemen under MIT license: https://2π.com/21/muldiv
function fullMulDiv(uint256 x, uint256 y, uint256 d) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
// 512-bit multiply `[p1 p0] = x * y`.
// Compute the product mod `2**256` and mod `2**256 - 1`
// then use the Chinese Remainder Theorem to reconstruct
// the 512 bit result. The result is stored in two 256
// variables such that `product = p1 * 2**256 + p0`.
// Temporarily use `z` as `p0` to save gas.
z := mul(x, y) // Lower 256 bits of `x * y`.
for {} 1 {} {
// If overflows.
if iszero(mul(or(iszero(x), eq(div(z, x), y)), d)) {
let mm := mulmod(x, y, not(0))
let p1 := sub(mm, add(z, lt(mm, z))) // Upper 256 bits of `x * y`.
/*------------------- 512 by 256 division --------------------*/
// Make division exact by subtracting the remainder from `[p1 p0]`.
let r := mulmod(x, y, d) // Compute remainder using mulmod.
let t := and(d, sub(0, d)) // The least significant bit of `d`. `t >= 1`.
// Make sure `z` is less than `2**256`. Also prevents `d == 0`.
// Placing the check here seems to give more optimal stack operations.
if iszero(gt(d, p1)) {
mstore(0x00, 0xae47f702) // `FullMulDivFailed()`.
revert(0x1c, 0x04)
}
d := div(d, t) // Divide `d` by `t`, which is a power of two.
// Invert `d mod 2**256`
// Now that `d` is an odd number, it has an inverse
// modulo `2**256` such that `d * inv = 1 mod 2**256`.
// Compute the inverse by starting with a seed that is correct
// correct for four bits. That is, `d * inv = 1 mod 2**4`.
let inv := xor(2, mul(3, d))
// Now use Newton-Raphson iteration to improve the precision.
// Thanks to Hensel's lifting lemma, this also works in modular
// arithmetic, doubling the correct bits in each step.
inv := mul(inv, sub(2, mul(d, inv))) // inverse mod 2**8
inv := mul(inv, sub(2, mul(d, inv))) // inverse mod 2**16
inv := mul(inv, sub(2, mul(d, inv))) // inverse mod 2**32
inv := mul(inv, sub(2, mul(d, inv))) // inverse mod 2**64
inv := mul(inv, sub(2, mul(d, inv))) // inverse mod 2**128
z :=
mul(
// Divide [p1 p0] by the factors of two.
// Shift in bits from `p1` into `p0`. For this we need
// to flip `t` such that it is `2**256 / t`.
or(mul(sub(p1, gt(r, z)), add(div(sub(0, t), t), 1)), div(sub(z, r), t)),
mul(sub(2, mul(d, inv)), inv) // inverse mod 2**256
)
break
}
z := div(z, d)
break
}
}
}
/// @dev Calculates `floor(x * y / d)` with full precision.
/// Behavior is undefined if `d` is zero or the final result cannot fit in 256 bits.
/// Performs the full 512 bit calculation regardless.
function fullMulDivUnchecked(uint256 x, uint256 y, uint256 d)
internal
pure
returns (uint256 z)
{
/// @solidity memory-safe-assembly
assembly {
z := mul(x, y)
let mm := mulmod(x, y, not(0))
let p1 := sub(mm, add(z, lt(mm, z)))
let t := and(d, sub(0, d))
let r := mulmod(x, y, d)
d := div(d, t)
let inv := xor(2, mul(3, d))
inv := mul(inv, sub(2, mul(d, inv)))
inv := mul(inv, sub(2, mul(d, inv)))
inv := mul(inv, sub(2, mul(d, inv)))
inv := mul(inv, sub(2, mul(d, inv)))
inv := mul(inv, sub(2, mul(d, inv)))
z :=
mul(
or(mul(sub(p1, gt(r, z)), add(div(sub(0, t), t), 1)), div(sub(z, r), t)),
mul(sub(2, mul(d, inv)), inv)
)
}
}
/// @dev Calculates `floor(x * y / d)` with full precision, rounded up.
/// Throws if result overflows a uint256 or when `d` is zero.
/// Credit to Uniswap-v3-core under MIT license:
/// https://github.com/Uniswap/v3-core/blob/main/contracts/libraries/FullMath.sol
function fullMulDivUp(uint256 x, uint256 y, uint256 d) internal pure returns (uint256 z) {
z = fullMulDiv(x, y, d);
/// @solidity memory-safe-assembly
assembly {
if mulmod(x, y, d) {
z := add(z, 1)
if iszero(z) {
mstore(0x00, 0xae47f702) // `FullMulDivFailed()`.
revert(0x1c, 0x04)
}
}
}
}
/// @dev Calculates `floor(x * y / 2 ** n)` with full precision.
/// Throws if result overflows a uint256.
/// Credit to Philogy under MIT license:
/// https://github.com/SorellaLabs/angstrom/blob/main/contracts/src/libraries/X128MathLib.sol
function fullMulDivN(uint256 x, uint256 y, uint8 n) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
// Temporarily use `z` as `p0` to save gas.
z := mul(x, y) // Lower 256 bits of `x * y`. We'll call this `z`.
for {} 1 {} {
if iszero(or(iszero(x), eq(div(z, x), y))) {
let k := and(n, 0xff) // `n`, cleaned.
let mm := mulmod(x, y, not(0))
let p1 := sub(mm, add(z, lt(mm, z))) // Upper 256 bits of `x * y`.
// | p1 | z |
// Before: | p1_0 ¦ p1_1 | z_0 ¦ z_1 |
// Final: | 0 ¦ p1_0 | p1_1 ¦ z_0 |
// Check that final `z` doesn't overflow by checking that p1_0 = 0.
if iszero(shr(k, p1)) {
z := add(shl(sub(256, k), p1), shr(k, z))
break
}
mstore(0x00, 0xae47f702) // `FullMulDivFailed()`.
revert(0x1c, 0x04)
}
z := shr(and(n, 0xff), z)
break
}
}
}
/// @dev Returns `floor(x * y / d)`.
/// Reverts if `x * y` overflows, or `d` is zero.
function mulDiv(uint256 x, uint256 y, uint256 d) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := mul(x, y)
// Equivalent to `require(d != 0 && (y == 0 || x <= type(uint256).max / y))`.
if iszero(mul(or(iszero(x), eq(div(z, x), y)), d)) {
mstore(0x00, 0xad251c27) // `MulDivFailed()`.
revert(0x1c, 0x04)
}
z := div(z, d)
}
}
/// @dev Returns `ceil(x * y / d)`.
/// Reverts if `x * y` overflows, or `d` is zero.
function mulDivUp(uint256 x, uint256 y, uint256 d) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := mul(x, y)
// Equivalent to `require(d != 0 && (y == 0 || x <= type(uint256).max / y))`.
if iszero(mul(or(iszero(x), eq(div(z, x), y)), d)) {
mstore(0x00, 0xad251c27) // `MulDivFailed()`.
revert(0x1c, 0x04)
}
z := add(iszero(iszero(mod(z, d))), div(z, d))
}
}
/// @dev Returns `x`, the modular multiplicative inverse of `a`, such that `(a * x) % n == 1`.
function invMod(uint256 a, uint256 n) internal pure returns (uint256 x) {
/// @solidity memory-safe-assembly
assembly {
let g := n
let r := mod(a, n)
for { let y := 1 } 1 {} {
let q := div(g, r)
let t := g
g := r
r := sub(t, mul(r, q))
let u := x
x := y
y := sub(u, mul(y, q))
if iszero(r) { break }
}
x := mul(eq(g, 1), add(x, mul(slt(x, 0), n)))
}
}
/// @dev Returns `ceil(x / d)`.
/// Reverts if `d` is zero.
function divUp(uint256 x, uint256 d) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
if iszero(d) {
mstore(0x00, 0x65244e4e) // `DivFailed()`.
revert(0x1c, 0x04)
}
z := add(iszero(iszero(mod(x, d))), div(x, d))
}
}
/// @dev Returns `max(0, x - y)`. Alias for `saturatingSub`.
function zeroFloorSub(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := mul(gt(x, y), sub(x, y))
}
}
/// @dev Returns `max(0, x - y)`.
function saturatingSub(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := mul(gt(x, y), sub(x, y))
}
}
/// @dev Returns `min(2 ** 256 - 1, x + y)`.
function saturatingAdd(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := or(sub(0, lt(add(x, y), x)), add(x, y))
}
}
/// @dev Returns `min(2 ** 256 - 1, x * y)`.
function saturatingMul(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := or(sub(or(iszero(x), eq(div(mul(x, y), x), y)), 1), mul(x, y))
}
}
/// @dev Returns `condition ? x : y`, without branching.
function ternary(bool condition, uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := xor(x, mul(xor(x, y), iszero(condition)))
}
}
/// @dev Returns `condition ? x : y`, without branching.
function ternary(bool condition, bytes32 x, bytes32 y) internal pure returns (bytes32 z) {
/// @solidity memory-safe-assembly
assembly {
z := xor(x, mul(xor(x, y), iszero(condition)))
}
}
/// @dev Returns `condition ? x : y`, without branching.
function ternary(bool condition, address x, address y) internal pure returns (address z) {
/// @solidity memory-safe-assembly
assembly {
z := xor(x, mul(xor(x, y), iszero(condition)))
}
}
/// @dev Returns `x != 0 ? x : y`, without branching.
function coalesce(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := or(x, mul(y, iszero(x)))
}
}
/// @dev Returns `x != bytes32(0) ? x : y`, without branching.
function coalesce(bytes32 x, bytes32 y) internal pure returns (bytes32 z) {
/// @solidity memory-safe-assembly
assembly {
z := or(x, mul(y, iszero(x)))
}
}
/// @dev Returns `x != address(0) ? x : y`, without branching.
function coalesce(address x, address y) internal pure returns (address z) {
/// @solidity memory-safe-assembly
assembly {
z := or(x, mul(y, iszero(shl(96, x))))
}
}
/// @dev Exponentiate `x` to `y` by squaring, denominated in base `b`.
/// Reverts if the computation overflows.
function rpow(uint256 x, uint256 y, uint256 b) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := mul(b, iszero(y)) // `0 ** 0 = 1`. Otherwise, `0 ** n = 0`.
if x {
z := xor(b, mul(xor(b, x), and(y, 1))) // `z = isEven(y) ? scale : x`
let half := shr(1, b) // Divide `b` by 2.
// Divide `y` by 2 every iteration.
for { y := shr(1, y) } y { y := shr(1, y) } {
let xx := mul(x, x) // Store x squared.
let xxRound := add(xx, half) // Round to the nearest number.
// Revert if `xx + half` overflowed, or if `x ** 2` overflows.
if or(lt(xxRound, xx), shr(128, x)) {
mstore(0x00, 0x49f7642b) // `RPowOverflow()`.
revert(0x1c, 0x04)
}
x := div(xxRound, b) // Set `x` to scaled `xxRound`.
// If `y` is odd:
if and(y, 1) {
let zx := mul(z, x) // Compute `z * x`.
let zxRound := add(zx, half) // Round to the nearest number.
// If `z * x` overflowed or `zx + half` overflowed:
if or(xor(div(zx, x), z), lt(zxRound, zx)) {
// Revert if `x` is non-zero.
if x {
mstore(0x00, 0x49f7642b) // `RPowOverflow()`.
revert(0x1c, 0x04)
}
}
z := div(zxRound, b) // Return properly scaled `zxRound`.
}
}
}
}
}
/// @dev Returns the square root of `x`, rounded down.
function sqrt(uint256 x) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
// `floor(sqrt(2**15)) = 181`. `sqrt(2**15) - 181 = 2.84`.
z := 181 // The "correct" value is 1, but this saves a multiplication later.
// This segment is to get a reasonable initial estimate for the Babylonian method. With a bad
// start, the correct # of bits increases ~linearly each iteration instead of ~quadratically.
// Let `y = x / 2**r`. We check `y >= 2**(k + 8)`
// but shift right by `k` bits to ensure that if `x >= 256`, then `y >= 256`.
let r := shl(7, lt(0xffffffffffffffffffffffffffffffffff, x))
r := or(r, shl(6, lt(0xffffffffffffffffff, shr(r, x))))
r := or(r, shl(5, lt(0xffffffffff, shr(r, x))))
r := or(r, shl(4, lt(0xffffff, shr(r, x))))
z := shl(shr(1, r), z)
// Goal was to get `z*z*y` within a small factor of `x`. More iterations could
// get y in a tighter range. Currently, we will have y in `[256, 256*(2**16))`.
// We ensured `y >= 256` so that the relative difference between `y` and `y+1` is small.
// That's not possible if `x < 256` but we can just verify those cases exhaustively.
// Now, `z*z*y <= x < z*z*(y+1)`, and `y <= 2**(16+8)`, and either `y >= 256`, or `x < 256`.
// Correctness can be checked exhaustively for `x < 256`, so we assume `y >= 256`.
// Then `z*sqrt(y)` is within `sqrt(257)/sqrt(256)` of `sqrt(x)`, or about 20bps.
// For `s` in the range `[1/256, 256]`, the estimate `f(s) = (181/1024) * (s+1)`
// is in the range `(1/2.84 * sqrt(s), 2.84 * sqrt(s))`,
// with largest error when `s = 1` and when `s = 256` or `1/256`.
// Since `y` is in `[256, 256*(2**16))`, let `a = y/65536`, so that `a` is in `[1/256, 256)`.
// Then we can estimate `sqrt(y)` using
// `sqrt(65536) * 181/1024 * (a + 1) = 181/4 * (y + 65536)/65536 = 181 * (y + 65536)/2**18`.
// There is no overflow risk here since `y < 2**136` after the first branch above.
z := shr(18, mul(z, add(shr(r, x), 65536))) // A `mul()` is saved from starting `z` at 181.
// Given the worst case multiplicative error of 2.84 above, 7 iterations should be enough.
z := shr(1, add(z, div(x, z)))
z := shr(1, add(z, div(x, z)))
z := shr(1, add(z, div(x, z)))
z := shr(1, add(z, div(x, z)))
z := shr(1, add(z, div(x, z)))
z := shr(1, add(z, div(x, z)))
z := shr(1, add(z, div(x, z)))
// If `x+1` is a perfect square, the Babylonian method cycles between
// `floor(sqrt(x))` and `ceil(sqrt(x))`. This statement ensures we return floor.
// See: https://en.wikipedia.org/wiki/Integer_square_root#Using_only_integer_division
z := sub(z, lt(div(x, z), z))
}
}
/// @dev Returns the cube root of `x`, rounded down.
/// Credit to bout3fiddy and pcaversaccio under AGPLv3 license:
/// https://github.com/pcaversaccio/snekmate/blob/main/src/utils/Math.vy
/// Formally verified by xuwinnie:
/// https://github.com/vectorized/solady/blob/main/audits/xuwinnie-solady-cbrt-proof.pdf
function cbrt(uint256 x) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
let r := shl(7, lt(0xffffffffffffffffffffffffffffffff, x))
r := or(r, shl(6, lt(0xffffffffffffffff, shr(r, x))))
r := or(r, shl(5, lt(0xffffffff, shr(r, x))))
r := or(r, shl(4, lt(0xffff, shr(r, x))))
r := or(r, shl(3, lt(0xff, shr(r, x))))
// Makeshift lookup table to nudge the approximate log2 result.
z := div(shl(div(r, 3), shl(lt(0xf, shr(r, x)), 0xf)), xor(7, mod(r, 3)))
// Newton-Raphson's.
z := div(add(add(div(x, mul(z, z)), z), z), 3)
z := div(add(add(div(x, mul(z, z)), z), z), 3)
z := div(add(add(div(x, mul(z, z)), z), z), 3)
z := div(add(add(div(x, mul(z, z)), z), z), 3)
z := div(add(add(div(x, mul(z, z)), z), z), 3)
z := div(add(add(div(x, mul(z, z)), z), z), 3)
z := div(add(add(div(x, mul(z, z)), z), z), 3)
// Round down.
z := sub(z, lt(div(x, mul(z, z)), z))
}
}
/// @dev Returns the square root of `x`, denominated in `WAD`, rounded down.
function sqrtWad(uint256 x) internal pure returns (uint256 z) {
unchecked {
if (x <= type(uint256).max / 10 ** 18) return sqrt(x * 10 ** 18);
z = (1 + sqrt(x)) * 10 ** 9;
z = (fullMulDivUnchecked(x, 10 ** 18, z) + z) >> 1;
}
/// @solidity memory-safe-assembly
assembly {
z := sub(z, gt(999999999999999999, sub(mulmod(z, z, x), 1))) // Round down.
}
}
/// @dev Returns the cube root of `x`, denominated in `WAD`, rounded down.
/// Formally verified by xuwinnie:
/// https://github.com/vectorized/solady/blob/main/audits/xuwinnie-solady-cbrt-proof.pdf
function cbrtWad(uint256 x) internal pure returns (uint256 z) {
unchecked {
if (x <= type(uint256).max / 10 ** 36) return cbrt(x * 10 ** 36);
z = (1 + cbrt(x)) * 10 ** 12;
z = (fullMulDivUnchecked(x, 10 ** 36, z * z) + z + z) / 3;
}
/// @solidity memory-safe-assembly
assembly {
let p := x
for {} 1 {} {
if iszero(shr(229, p)) {
if iszero(shr(199, p)) {
p := mul(p, 100000000000000000) // 10 ** 17.
break
}
p := mul(p, 100000000) // 10 ** 8.
break
}
if iszero(shr(249, p)) { p := mul(p, 100) }
break
}
let t := mulmod(mul(z, z), z, p)
z := sub(z, gt(lt(t, shr(1, p)), iszero(t))) // Round down.
}
}
/// @dev Returns the factorial of `x`.
function factorial(uint256 x) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := 1
if iszero(lt(x, 58)) {
mstore(0x00, 0xaba0f2a2) // `FactorialOverflow()`.
revert(0x1c, 0x04)
}
for {} x { x := sub(x, 1) } { z := mul(z, x) }
}
}
/// @dev Returns the log2 of `x`.
/// Equivalent to computing the index of the most significant bit (MSB) of `x`.
/// Returns 0 if `x` is zero.
function log2(uint256 x) internal pure returns (uint256 r) {
/// @solidity memory-safe-assembly
assembly {
r := shl(7, lt(0xffffffffffffffffffffffffffffffff, x))
r := or(r, shl(6, lt(0xffffffffffffffff, shr(r, x))))
r := or(r, shl(5, lt(0xffffffff, shr(r, x))))
r := or(r, shl(4, lt(0xffff, shr(r, x))))
r := or(r, shl(3, lt(0xff, shr(r, x))))
// forgefmt: disable-next-item
r := or(r, byte(and(0x1f, shr(shr(r, x), 0x8421084210842108cc6318c6db6d54be)),
0x0706060506020504060203020504030106050205030304010505030400000000))
}
}
/// @dev Returns the log2 of `x`, rounded up.
/// Returns 0 if `x` is zero.
function log2Up(uint256 x) internal pure returns (uint256 r) {
r = log2(x);
/// @solidity memory-safe-assembly
assembly {
r := add(r, lt(shl(r, 1), x))
}
}
/// @dev Returns the log10 of `x`.
/// Returns 0 if `x` is zero.
function log10(uint256 x) internal pure returns (uint256 r) {
/// @solidity memory-safe-assembly
assembly {
if iszero(lt(x, 100000000000000000000000000000000000000)) {
x := div(x, 100000000000000000000000000000000000000)
r := 38
}
if iszero(lt(x, 100000000000000000000)) {
x := div(x, 100000000000000000000)
r := add(r, 20)
}
if iszero(lt(x, 10000000000)) {
x := div(x, 10000000000)
r := add(r, 10)
}
if iszero(lt(x, 100000)) {
x := div(x, 100000)
r := add(r, 5)
}
r := add(r, add(gt(x, 9), add(gt(x, 99), add(gt(x, 999), gt(x, 9999)))))
}
}
/// @dev Returns the log10 of `x`, rounded up.
/// Returns 0 if `x` is zero.
function log10Up(uint256 x) internal pure returns (uint256 r) {
r = log10(x);
/// @solidity memory-safe-assembly
assembly {
r := add(r, lt(exp(10, r), x))
}
}
/// @dev Returns the log256 of `x`.
/// Returns 0 if `x` is zero.
function log256(uint256 x) internal pure returns (uint256 r) {
/// @solidity memory-safe-assembly
assembly {
r := shl(7, lt(0xffffffffffffffffffffffffffffffff, x))
r := or(r, shl(6, lt(0xffffffffffffffff, shr(r, x))))
r := or(r, shl(5, lt(0xffffffff, shr(r, x))))
r := or(r, shl(4, lt(0xffff, shr(r, x))))
r := or(shr(3, r), lt(0xff, shr(r, x)))
}
}
/// @dev Returns the log256 of `x`, rounded up.
/// Returns 0 if `x` is zero.
function log256Up(uint256 x) internal pure returns (uint256 r) {
r = log256(x);
/// @solidity memory-safe-assembly
assembly {
r := add(r, lt(shl(shl(3, r), 1), x))
}
}
/// @dev Returns the scientific notation format `mantissa * 10 ** exponent` of `x`.
/// Useful for compressing prices (e.g. using 25 bit mantissa and 7 bit exponent).
function sci(uint256 x) internal pure returns (uint256 mantissa, uint256 exponent) {
/// @solidity memory-safe-assembly
assembly {
mantissa := x
if mantissa {
if iszero(mod(mantissa, 1000000000000000000000000000000000)) {
mantissa := div(mantissa, 1000000000000000000000000000000000)
exponent := 33
}
if iszero(mod(mantissa, 10000000000000000000)) {
mantissa := div(mantissa, 10000000000000000000)
exponent := add(exponent, 19)
}
if iszero(mod(mantissa, 1000000000000)) {
mantissa := div(mantissa, 1000000000000)
exponent := add(exponent, 12)
}
if iszero(mod(mantissa, 1000000)) {
mantissa := div(mantissa, 1000000)
exponent := add(exponent, 6)
}
if iszero(mod(mantissa, 10000)) {
mantissa := div(mantissa, 10000)
exponent := add(exponent, 4)
}
if iszero(mod(mantissa, 100)) {
mantissa := div(mantissa, 100)
exponent := add(exponent, 2)
}
if iszero(mod(mantissa, 10)) {
mantissa := div(mantissa, 10)
exponent := add(exponent, 1)
}
}
}
}
/// @dev Convenience function for packing `x` into a smaller number using `sci`.
/// The `mantissa` will be in bits [7..255] (the upper 249 bits).
/// The `exponent` will be in bits [0..6] (the lower 7 bits).
/// Use `SafeCastLib` to safely ensure that the `packed` number is small
/// enough to fit in the desired unsigned integer type:
/// ```
/// uint32 packed = SafeCastLib.toUint32(FixedPointMathLib.packSci(777 ether));
/// ```
function packSci(uint256 x) internal pure returns (uint256 packed) {
(x, packed) = sci(x); // Reuse for `mantissa` and `exponent`.
/// @solidity memory-safe-assembly
assembly {
if shr(249, x) {
mstore(0x00, 0xce30380c) // `MantissaOverflow()`.
revert(0x1c, 0x04)
}
packed := or(shl(7, x), packed)
}
}
/// @dev Convenience function for unpacking a packed number from `packSci`.
function unpackSci(uint256 packed) internal pure returns (uint256 unpacked) {
unchecked {
unpacked = (packed >> 7) * 10 ** (packed & 0x7f);
}
}
/// @dev Returns the average of `x` and `y`. Rounds towards zero.
function avg(uint256 x, uint256 y) internal pure returns (uint256 z) {
unchecked {
z = (x & y) + ((x ^ y) >> 1);
}
}
/// @dev Returns the average of `x` and `y`. Rounds towards negative infinity.
function avg(int256 x, int256 y) internal pure returns (int256 z) {
unchecked {
z = (x >> 1) + (y >> 1) + (x & y & 1);
}
}
/// @dev Returns the absolute value of `x`.
function abs(int256 x) internal pure returns (uint256 z) {
unchecked {
z = (uint256(x) + uint256(x >> 255)) ^ uint256(x >> 255);
}
}
/// @dev Returns the absolute distance between `x` and `y`.
function dist(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := add(xor(sub(0, gt(x, y)), sub(y, x)), gt(x, y))
}
}
/// @dev Returns the absolute distance between `x` and `y`.
function dist(int256 x, int256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := add(xor(sub(0, sgt(x, y)), sub(y, x)), sgt(x, y))
}
}
/// @dev Returns the minimum of `x` and `y`.
function min(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := xor(x, mul(xor(x, y), lt(y, x)))
}
}
/// @dev Returns the minimum of `x` and `y`.
function min(int256 x, int256 y) internal pure returns (int256 z) {
/// @solidity memory-safe-assembly
assembly {
z := xor(x, mul(xor(x, y), slt(y, x)))
}
}
/// @dev Returns the maximum of `x` and `y`.
function max(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := xor(x, mul(xor(x, y), gt(y, x)))
}
}
/// @dev Returns the maximum of `x` and `y`.
function max(int256 x, int256 y) internal pure returns (int256 z) {
/// @solidity memory-safe-assembly
assembly {
z := xor(x, mul(xor(x, y), sgt(y, x)))
}
}
/// @dev Returns `x`, bounded to `minValue` and `maxValue`.
function clamp(uint256 x, uint256 minValue, uint256 maxValue)
internal
pure
returns (uint256 z)
{
/// @solidity memory-safe-assembly
assembly {
z := xor(x, mul(xor(x, minValue), gt(minValue, x)))
z := xor(z, mul(xor(z, maxValue), lt(maxValue, z)))
}
}
/// @dev Returns `x`, bounded to `minValue` and `maxValue`.
function clamp(int256 x, int256 minValue, int256 maxValue) internal pure returns (int256 z) {
/// @solidity memory-safe-assembly
assembly {
z := xor(x, mul(xor(x, minValue), sgt(minValue, x)))
z := xor(z, mul(xor(z, maxValue), slt(maxValue, z)))
}
}
/// @dev Returns greatest common divisor of `x` and `y`.
function gcd(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
for { z := x } y {} {
let t := y
y := mod(z, y)
z := t
}
}
}
/// @dev Returns `a + (b - a) * (t - begin) / (end - begin)`,
/// with `t` clamped between `begin` and `end` (inclusive).
/// Agnostic to the order of (`a`, `b`) and (`end`, `begin`).
/// If `begins == end`, returns `t <= begin ? a : b`.
function lerp(uint256 a, uint256 b, uint256 t, uint256 begin, uint256 end)
internal
pure
returns (uint256)
{
if (begin > end) (t, begin, end) = (~t, ~begin, ~end);
if (t <= begin) return a;
if (t >= end) return b;
unchecked {
if (b >= a) return a + fullMulDiv(b - a, t - begin, end - begin);
return a - fullMulDiv(a - b, t - begin, end - begin);
}
}
/// @dev Returns `a + (b - a) * (t - begin) / (end - begin)`.
/// with `t` clamped between `begin` and `end` (inclusive).
/// Agnostic to the order of (`a`, `b`) and (`end`, `begin`).
/// If `begins == end`, returns `t <= begin ? a : b`.
function lerp(int256 a, int256 b, int256 t, int256 begin, int256 end)
internal
pure
returns (int256)
{
if (begin > end) (t, begin, end) = (~t, ~begin, ~end);
if (t <= begin) return a;
if (t >= end) return b;
// forgefmt: disable-next-item
unchecked {
if (b >= a) return int256(uint256(a) + fullMulDiv(uint256(b - a),
uint256(t - begin), uint256(end - begin)));
return int256(uint256(a) - fullMulDiv(uint256(a - b),
uint256(t - begin), uint256(end - begin)));
}
}
/// @dev Returns if `x` is an even number. Some people may need this.
function isEven(uint256 x) internal pure returns (bool) {
return x & uint256(1) == uint256(0);
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* RAW NUMBER OPERATIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Returns `x + y`, without checking for overflow.
function rawAdd(uint256 x, uint256 y) internal pure returns (uint256 z) {
unchecked {
z = x + y;
}
}
/// @dev Returns `x + y`, without checking for overflow.
function rawAdd(int256 x, int256 y) internal pure returns (int256 z) {
unchecked {
z = x + y;
}
}
/// @dev Returns `x - y`, without checking for underflow.
function rawSub(uint256 x, uint256 y) internal pure returns (uint256 z) {
unchecked {
z = x - y;
}
}
/// @dev Returns `x - y`, without checking for underflow.
function rawSub(int256 x, int256 y) internal pure returns (int256 z) {
unchecked {
z = x - y;
}
}
/// @dev Returns `x * y`, without checking for overflow.
function rawMul(uint256 x, uint256 y) internal pure returns (uint256 z) {
unchecked {
z = x * y;
}
}
/// @dev Returns `x * y`, without checking for overflow.
function rawMul(int256 x, int256 y) internal pure returns (int256 z) {
unchecked {
z = x * y;
}
}
/// @dev Returns `x / y`, returning 0 if `y` is zero.
function rawDiv(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := div(x, y)
}
}
/// @dev Returns `x / y`, returning 0 if `y` is zero.
function rawSDiv(int256 x, int256 y) internal pure returns (int256 z) {
/// @solidity memory-safe-assembly
assembly {
z := sdiv(x, y)
}
}
/// @dev Returns `x % y`, returning 0 if `y` is zero.
function rawMod(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := mod(x, y)
}
}
/// @dev Returns `x % y`, returning 0 if `y` is zero.
function rawSMod(int256 x, int256 y) internal pure returns (int256 z) {
/// @solidity memory-safe-assembly
assembly {
z := smod(x, y)
}
}
/// @dev Returns `(x + y) % d`, return 0 if `d` if zero.
function rawAddMod(uint256 x, uint256 y, uint256 d) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := addmod(x, y, d)
}
}
/// @dev Returns `(x * y) % d`, return 0 if `d` if zero.
function rawMulMod(uint256 x, uint256 y, uint256 d) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
z := mulmod(x, y, d)
}
}
}// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.25;
/**
* @title IBtcVaultStrategy
* @notice Interface for the BTC vault strategy managing multi-collateral assets
*/
interface IBtcVaultStrategy {
/*//////////////////////////////////////////////////////////////
EVENTS
//////////////////////////////////////////////////////////////*/
event CollateralAdded(address indexed token, uint8 decimals);
event CollateralRemoved(address indexed token);
event LiquidityAdded(uint256 amount);
event LiquidityRemoved(uint256 amount);
event CollateralWithdrawn(address indexed token, uint256 amount, address indexed to);
/*//////////////////////////////////////////////////////////////
ERRORS
//////////////////////////////////////////////////////////////*/
error AssetNotSupported();
error AssetAlreadySupported();
error InvalidDecimals();
error InsufficientLiquidity();
error InvalidAmount();
error InvalidAddress();
error UnauthorizedCaller();
/*//////////////////////////////////////////////////////////////
COLLATERAL MANAGEMENT
//////////////////////////////////////////////////////////////*/
/**
* @notice Add a new supported collateral token
* @dev All collateral must have 8 decimals
* @param token Address of the BTC collateral token
*/
function addCollateral(address token) external;
/**
* @notice Remove a supported collateral token
* @param token Address of the collateral token to remove
*/
function removeCollateral(address token) external;
/**
* @notice Check if an asset is supported
* @param token Address of the token to check
* @return Whether the token is supported
*/
function isSupportedAsset(address token) external view returns (bool);
/**
* @notice Get list of all supported collateral tokens
* @return Array of supported token addresses
*/
function getSupportedCollaterals() external view returns (address[] memory);
/*//////////////////////////////////////////////////////////////
LIQUIDITY MANAGEMENT
//////////////////////////////////////////////////////////////*/
/**
* @notice Add sovaBTC liquidity for redemptions
* @param amount Amount of sovaBTC to add
*/
function addLiquidity(uint256 amount) external;
/**
* @notice Remove excess sovaBTC liquidity
* @param amount Amount of sovaBTC to remove
* @param to Address to send the sovaBTC
*/
function removeLiquidity(uint256 amount, address to) external;
/*//////////////////////////////////////////////////////////////
VAULT OPERATIONS
//////////////////////////////////////////////////////////////*/
/**
* @notice Withdraw collateral to admin
* @param token Collateral token to withdraw
* @param amount Amount to withdraw
* @param to Recipient address
*/
function withdrawCollateral(address token, uint256 amount, address to) external;
/*//////////////////////////////////////////////////////////////
VIEW FUNCTIONS
//////////////////////////////////////////////////////////////*/
/**
* @notice Get total collateral assets value in sovaBTC terms (1:1 for all BTC variants)
* @dev This sums raw collateral balances without NAV adjustment
* @return Total value of all collateral in 8 decimal units
*/
function totalCollateralAssets() external view returns (uint256);
/**
* @notice Get balance of a specific collateral token
* @param token Address of the collateral token
* @return Balance of the token held by strategy
*/
function collateralBalance(address token) external view returns (uint256);
/**
* @notice Get available sovaBTC balance for redemptions
* @return Current sovaBTC balance in the strategy
*/
function availableLiquidity() external view returns (uint256);
}// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.25;
import {ERC4626} from "solady/tokens/ERC4626.sol";
import {SafeTransferLib} from "solady/utils/SafeTransferLib.sol";
import {FixedPointMathLib} from "solady/utils/FixedPointMathLib.sol";
import {ReentrancyGuard} from "solady/utils/ReentrancyGuard.sol";
import {IHook} from "../hooks/IHook.sol";
import {IStrategy} from "../strategy/IStrategy.sol";
import {ItRWA} from "./ItRWA.sol";
import {Conduit} from "../conduit/Conduit.sol";
import {RoleManaged} from "../auth/RoleManaged.sol";
import {IRegistry} from "../registry/IRegistry.sol";
/**
* @title tRWA
* @notice Tokenized Real World Asset (tRWA) inheriting ERC4626 standard
* @dev Each token represents a share in the underlying real-world fund
*/
contract tRWA is ERC4626, ItRWA, ReentrancyGuard {
using FixedPointMathLib for uint256;
using SafeTransferLib for address;
/*//////////////////////////////////////////////////////////////
ERRORS
//////////////////////////////////////////////////////////////*/
error HookCheckFailed(string reason);
error NotStrategyAdmin();
error HookAddressZero();
error ReorderInvalidLength();
error ReorderIndexOutOfBounds();
error ReorderDuplicateIndex();
error HookHasProcessedOperations();
error HookIndexOutOfBounds();
error InvalidDecimals();
/*//////////////////////////////////////////////////////////////
EVENTS
//////////////////////////////////////////////////////////////*/
// Events for operation hooks
event HookAdded(bytes32 indexed operationType, address indexed hookAddress, uint256 index);
event HookRemoved(bytes32 indexed operationType, address indexed hookAddress);
event HooksReordered(bytes32 indexed operationType, uint256[] newIndices);
/*//////////////////////////////////////////////////////////////
TOKEN STATE
//////////////////////////////////////////////////////////////*/
/// @notice Internal storage for token metadata
string private _symbol;
string private _name;
address private immutable _asset;
uint8 private immutable _assetDecimals;
/// @notice The strategy contract
address public immutable strategy;
/*//////////////////////////////////////////////////////////////
HOOK STATE
//////////////////////////////////////////////////////////////*/
/// @notice Operation type identifiers
bytes32 public constant OP_DEPOSIT = keccak256("DEPOSIT_OPERATION");
bytes32 public constant OP_WITHDRAW = keccak256("WITHDRAW_OPERATION");
bytes32 public constant OP_TRANSFER = keccak256("TRANSFER_OPERATION");
/// @notice Hook information structure
struct HookInfo {
IHook hook;
uint256 addedAtBlock;
}
/// @notice Mapping of operation type to hook information
mapping(bytes32 => HookInfo[]) public operationHooks;
/// @notice Mapping of operation type to the last block number it was executed
mapping(bytes32 => uint256) public lastExecutedBlock;
/*//////////////////////////////////////////////////////////////
CONSTRUCTOR
//////////////////////////////////////////////////////////////*/
/**
* @notice Contract constructor
* @param name_ Token name
* @param symbol_ Token symbol
* @param asset_ Asset address
* @param assetDecimals_ Decimals of the asset token
* @param strategy_ Strategy address
*/
constructor(string memory name_, string memory symbol_, address asset_, uint8 assetDecimals_, address strategy_) {
// Validate configuration parameters
if (asset_ == address(0)) revert InvalidAddress();
if (strategy_ == address(0)) revert InvalidAddress();
if (assetDecimals_ > _DEFAULT_UNDERLYING_DECIMALS) revert InvalidDecimals();
_name = name_;
_symbol = symbol_;
_asset = asset_;
_assetDecimals = assetDecimals_;
strategy = strategy_;
}
/*//////////////////////////////////////////////////////////////
VIEW FUNCTIONS
//////////////////////////////////////////////////////////////*/
/**
* @notice Returns the name of the token
* @return Name of the token
*/
function name() public view virtual override returns (string memory) {
return _name;
}
/**
* @notice Returns the symbol of the token
* @return Symbol of the token
*/
function symbol() public view virtual override returns (string memory) {
return _symbol;
}
/**
* @notice Returns the asset of the token
* @return Asset of the token
*/
function asset() public view virtual override(ERC4626, ItRWA) returns (address) {
return _asset;
}
/*//////////////////////////////////////////////////////////////
ERC4626 OVERRIDE VIEW FUNCTIONS
//////////////////////////////////////////////////////////////*/
/**
* @dev Returns the decimals of the underlying asset token.
*/
function _underlyingDecimals() internal view virtual override returns (uint8) {
return _assetDecimals;
}
/**
* @dev Returns the offset to adjust share decimals relative to asset decimals.
* Ensures that `_underlyingDecimals() + _decimalsOffset()` equals `decimals()` (18 for tRWA shares).
*/
function _decimalsOffset() internal view virtual override returns (uint8) {
return _DEFAULT_UNDERLYING_DECIMALS - _assetDecimals;
}
/**
* @notice Returns the total amount of the underlying asset managed by the Vault.
* @dev This value is expected by the base ERC4626 implementation to be in terms of asset's native decimals.
* @return Total assets in terms of _asset
*/
function totalAssets() public view override returns (uint256) {
// Use the strategy's balance which implements price-per-share calculation
return IStrategy(strategy).balance();
}
/*//////////////////////////////////////////////////////////////
ERC4626 OVERRIDE LOGIC FUNCTIONS
//////////////////////////////////////////////////////////////*/
/**
* @notice Deposit assets into the token
* @param by Address of the sender
* @param to Address of the receiver
* @param assets Amount of assets to deposit
* @param shares Amount of shares to mint
*/
function _deposit(address by, address to, uint256 assets, uint256 shares) internal virtual override nonReentrant {
HookInfo[] storage opHooks = operationHooks[OP_DEPOSIT];
for (uint256 i = 0; i < opHooks.length;) {
IHook.HookOutput memory hookOutput = opHooks[i].hook.onBeforeDeposit(address(this), by, assets, to);
if (!hookOutput.approved) {
revert HookCheckFailed(hookOutput.reason);
}
unchecked {
++i;
}
}
// Update last executed block for this operation type if hooks were called
if (opHooks.length > 0) {
lastExecutedBlock[OP_DEPOSIT] = block.number;
}
Conduit(IRegistry(RoleManaged(strategy).registry()).conduit()).collectDeposit(asset(), by, strategy, assets);
_mint(to, shares);
_afterDeposit(assets, shares);
emit Deposit(by, to, assets, shares);
}
/**
* @notice Withdraw assets from the token
* @param by Address of the sender
* @param to Address of the receiver
* @param owner Address of the owner
* @param assets Amount of assets to withdraw
* @param shares Amount of shares to withdraw
*/
function _withdraw(address by, address to, address owner, uint256 assets, uint256 shares)
internal
virtual
override
nonReentrant
{
if (by != owner) _spendAllowance(owner, by, shares);
_beforeWithdraw(assets, shares);
_burn(owner, shares);
// Get assets from strategy
_collect(assets);
// Call hooks after state changes but before final transfer
HookInfo[] storage opHooks = operationHooks[OP_WITHDRAW];
for (uint256 i = 0; i < opHooks.length;) {
IHook.HookOutput memory hookOutput = opHooks[i].hook.onBeforeWithdraw(address(this), by, assets, to, owner);
if (!hookOutput.approved) {
revert HookCheckFailed(hookOutput.reason);
}
unchecked {
++i;
}
}
// Update last executed block for this operation type if hooks were called
if (opHooks.length > 0) {
lastExecutedBlock[OP_WITHDRAW] = block.number;
}
// Transfer the assets to the recipient
_asset.safeTransfer(to, assets);
emit Withdraw(by, to, owner, assets, shares);
}
/*//////////////////////////////////////////////////////////////
HOOK MANAGEMENT FUNCTIONS
//////////////////////////////////////////////////////////////*/
/**
* @notice Adds a new operation hook to the end of the list for a specific operation type.
* @dev Callable only by the strategy contract.
* @param operationType The type of operation this hook applies to (e.g., OP_DEPOSIT).
* @param newHookAddress The address of the new hook contract to add.
*/
function addOperationHook(bytes32 operationType, address newHookAddress) external onlyStrategy {
if (newHookAddress == address(0)) revert HookAddressZero();
HookInfo memory newHookInfo = HookInfo({hook: IHook(newHookAddress), addedAtBlock: block.number});
HookInfo[] storage opHooks = operationHooks[operationType];
opHooks.push(newHookInfo);
emit HookAdded(operationType, newHookAddress, opHooks.length - 1);
}
/**
* @notice Removes an operation hook from a specific operation type.
* @dev Callable only by the strategy contract. Can only remove hooks that haven't processed operations.
* @param operationType The type of operation to remove the hook from.
* @param index The index of the hook to remove.
*/
function removeOperationHook(bytes32 operationType, uint256 index) external onlyStrategy nonReentrant {
HookInfo[] storage opHooks = operationHooks[operationType];
uint256 opHooksLen = opHooks.length;
if (index >= opHooksLen) revert HookIndexOutOfBounds();
// Cache the hook info to avoid multiple storage reads
HookInfo storage hookToRemove = opHooks[index];
// Check if this hook was added before the last execution of this operation type
if (hookToRemove.addedAtBlock <= lastExecutedBlock[operationType]) {
revert HookHasProcessedOperations();
}
address removedHookAddress = address(hookToRemove.hook);
// Remove by swapping with last element and popping (more gas efficient)
if (index != opHooksLen - 1) {
opHooks[index] = opHooks[opHooksLen - 1];
}
opHooks.pop();
emit HookRemoved(operationType, removedHookAddress);
}
/**
* @notice Reorders the existing operation hooks for a specific operation type.
* @dev Callable only by the strategy contract. The newOrderIndices array must be a permutation
* of the current hook indices (0 to length-1) for the given operation type.
* @param operationType The type of operation for which hooks are being reordered.
* @param newOrderIndices An array where newOrderIndices[i] specifies the OLD index of the hook
* that should now be at NEW position i.
*/
function reorderOperationHooks(bytes32 operationType, uint256[] calldata newOrderIndices)
external
onlyStrategy
nonReentrant
{
HookInfo[] storage opTypeHooks = operationHooks[operationType];
uint256 numHooks = opTypeHooks.length;
if (newOrderIndices.length != numHooks) revert ReorderInvalidLength();
// Create a temporary copy of all hooks
HookInfo[] memory tempHooks = new HookInfo[](numHooks);
for (uint256 i = 0; i < numHooks;) {
tempHooks[i] = opTypeHooks[i];
unchecked {
++i;
}
}
bool[] memory indexSeen = new bool[](numHooks);
// Reorder by copying from temp array back to storage
for (uint256 i = 0; i < numHooks;) {
uint256 oldIndex = newOrderIndices[i];
if (oldIndex >= numHooks) revert ReorderIndexOutOfBounds();
if (indexSeen[oldIndex]) revert ReorderDuplicateIndex();
opTypeHooks[i] = tempHooks[oldIndex];
indexSeen[oldIndex] = true;
unchecked {
++i;
}
}
emit HooksReordered(operationType, newOrderIndices);
}
/*//////////////////////////////////////////////////////////////
HOOK VIEW FUNCTIONS
//////////////////////////////////////////////////////////////*/
/**
* @notice Gets all registered hook addresses for a specific operation type.
* @param operationType The type of operation.
* @return An array of hook contract addresses.
*/
function getHooksForOperation(bytes32 operationType) external view returns (address[] memory) {
HookInfo[] storage opTypeHooks = operationHooks[operationType];
address[] memory hookAddresses = new address[](opTypeHooks.length);
for (uint256 i = 0; i < opTypeHooks.length;) {
hookAddresses[i] = address(opTypeHooks[i].hook);
unchecked {
++i;
}
}
return hookAddresses;
}
/**
* @notice Gets detailed information about all hooks for a specific operation type.
* @param operationType The type of operation.
* @return hookInfos Array of HookInfo structs containing hook details.
*/
function getHookInfoForOperation(bytes32 operationType) external view returns (HookInfo[] memory) {
return operationHooks[operationType];
}
/*//////////////////////////////////////////////////////////////
ERC20 HOOK OVERRIDES
//////////////////////////////////////////////////////////////*/
/**
* @dev Hook that is called before any token transfer, including mints and burns.
* We use this to apply OP_TRANSFER hooks.
*/
function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual override {
super._beforeTokenTransfer(from, to, amount); // Call to parent ERC20/ERC4626 _beforeTokenTransfer if it exists
HookInfo[] storage opHooks = operationHooks[OP_TRANSFER];
if (opHooks.length > 0) {
// Optimization to save gas if no hooks registered for OP_TRANSFER
for (uint256 i = 0; i < opHooks.length;) {
IHook.HookOutput memory hookOutput = opHooks[i].hook.onBeforeTransfer(address(this), from, to, amount);
if (!hookOutput.approved) {
revert HookCheckFailed(hookOutput.reason);
}
unchecked {
++i;
}
}
// Update last executed block for this operation type
lastExecutedBlock[OP_TRANSFER] = block.number;
}
}
/*//////////////////////////////////////////////////////////////
INTERNAL FUNCTIONS
//////////////////////////////////////////////////////////////*/
/**
* @notice Collect assets from the strategy
* @param assets The amount of assets to collect
*/
function _collect(uint256 assets) internal {
_asset.safeTransferFrom(strategy, address(this), assets);
}
modifier onlyStrategy() {
if (msg.sender != strategy) revert NotStrategyAdmin();
_;
}
}// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.25;
/**
* @title IStrategy
* @notice Interface for tRWA investment strategies
* @dev Defines the interface for strategies that manage tRWA token assets
*/
interface IStrategy {
/*//////////////////////////////////////////////////////////////
ERRORS
//////////////////////////////////////////////////////////////*/
error InvalidAddress();
error InvalidRules();
error Unauthorized();
error CallRevert(bytes returnData);
error AlreadyInitialized();
error TokenAlreadyDeployed();
error CannotCallToken();
/*//////////////////////////////////////////////////////////////
EVENTS
//////////////////////////////////////////////////////////////*/
event PendingAdminChange(address indexed oldAdmin, address indexed newAdmin);
event AdminChange(address indexed oldAdmin, address indexed newAdmin);
event NoAdminChange(address indexed oldAdmin, address indexed cancelledAdmin);
event ManagerChange(address indexed oldManager, address indexed newManager);
event Call(address indexed target, uint256 value, bytes data);
event StrategyInitialized(address indexed admin, address indexed manager, address indexed asset, address sToken);
event ControllerConfigured(address indexed controller);
/*//////////////////////////////////////////////////////////////
INITIALIZATION
//////////////////////////////////////////////////////////////*/
function initialize(
string calldata name,
string calldata symbol,
address roleManager,
address manager,
address asset,
uint8 assetDecimals,
bytes memory initData
) external;
/*//////////////////////////////////////////////////////////////
MANAGEMENT
//////////////////////////////////////////////////////////////*/
function manager() external view returns (address);
function setManager(address newManager) external;
/*//////////////////////////////////////////////////////////////
VIEW FUNCTIONS
//////////////////////////////////////////////////////////////*/
function asset() external view returns (address);
function assetDecimals() external view returns (uint8);
function sToken() external view returns (address);
function balance() external view returns (uint256);
}// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.25;
/**
* @title IHook
* @notice Interface for operation hooks in the tRWA system
* @dev Operation hooks are called before key operations (deposit, withdraw, transfer)
* and can approve or reject the operation with a reason
*/
interface IHook {
/*//////////////////////////////////////////////////////////////
DATA STRUCTS
//////////////////////////////////////////////////////////////*/
/**
* @title HookOutput
* @notice Structure representing the result of a hook evaluation
* @param approved Whether the operation is approved by this hook
* @param reason Reason for approval/rejection (for logging or error messages)
*/
struct HookOutput {
bool approved;
string reason;
}
/*//////////////////////////////////////////////////////////////
VIEW FUNCTIONS
//////////////////////////////////////////////////////////////*/
/**
* @notice Returns the unique identifier for this hook
* @return Hook identifier
*/
function hookId() external view returns (bytes32);
/**
* @notice Returns the human readable name of this hook
* @return Hook name
*/
function name() external view returns (string memory);
/*//////////////////////////////////////////////////////////////
HOOK LOGIC
//////////////////////////////////////////////////////////////*/
/**
* @notice Called before a deposit operation
* @param token Address of the token
* @param user Address of the user
* @param assets Amount of assets to deposit
* @param receiver Address of the receiver
* @return HookOutput Result of the hook evaluation
*/
function onBeforeDeposit(address token, address user, uint256 assets, address receiver)
external
returns (HookOutput memory);
/**
* @notice Called before a withdraw operation
* @param token Address of the token
* @param by Address of the sender
* @param assets Amount of assets to withdraw
* @param to Address of the receiver
* @param owner Address of the owner
* @return HookOutput Result of the hook evaluation
*/
function onBeforeWithdraw(address token, address by, uint256 assets, address to, address owner)
external
returns (HookOutput memory);
/**
* @notice Called before a transfer operation
* @param token Address of the token
* @param from Address of the sender
* @param to Address of the receiver
* @param amount Amount of assets to transfer
* @return HookOutput Result of the hook evaluation
*/
function onBeforeTransfer(address token, address from, address to, uint256 amount)
external
returns (HookOutput memory);
}// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.25;
import {SafeTransferLib} from "solady/utils/SafeTransferLib.sol";
import {IStrategy} from "./IStrategy.sol";
import {tRWA} from "../token/tRWA.sol";
import {CloneableRoleManaged} from "../auth/CloneableRoleManaged.sol";
/**
* @title BasicStrategy
* @notice A basic strategy contract for managing tRWA assets
* @dev Each strategy deploys its own tRWA token (sToken)
*
* Consider for future: Making BasicStrategy an ERC4337-compatible smart account
*/
abstract contract BasicStrategy is IStrategy, CloneableRoleManaged {
using SafeTransferLib for address;
/*//////////////////////////////////////////////////////////////
STATE
//////////////////////////////////////////////////////////////*/
/// @notice The manager of the strategy
address public manager;
/// @notice The asset of the strategy
address public asset;
/// @notice The decimals of the asset
uint8 public assetDecimals;
/// @notice The sToken of the strategy
address public sToken;
/// @notice Initialization flags to prevent re-initialization
bool internal _initialized;
/*//////////////////////////////////////////////////////////////
INITIALIZATION
//////////////////////////////////////////////////////////////*/
/**
* @notice Initialize the strategy
* @param name_ Name of the token
* @param symbol_ Symbol of the token
* @param roleManager_ Address of the role manager
* @param manager_ Address of the manager
* @param asset_ Address of the underlying asset
* @param assetDecimals_ Decimals of the asset
*/
function initialize(
string calldata name_,
string calldata symbol_,
address roleManager_,
address manager_,
address asset_,
uint8 assetDecimals_,
bytes memory // initData
) public virtual override {
// Prevent re-initialization
if (_initialized) revert AlreadyInitialized();
_initialized = true;
if (manager_ == address(0)) revert InvalidAddress();
if (asset_ == address(0)) revert InvalidAddress();
// Set up strategy configuration
// Unlike other protocol roles, only a single manager is allowed
manager = manager_;
asset = asset_;
assetDecimals = assetDecimals_;
_initializeRoleManager(roleManager_);
sToken = _deployToken(name_, symbol_, asset, assetDecimals_);
emit StrategyInitialized(address(0), manager, asset, sToken);
}
/**
* @notice Deploy a new tRWA token
* @param name_ Name of the token
* @param symbol_ Symbol of the token
* @param asset_ Address of the underlying asset
* @param assetDecimals_ Decimals of the asset
*/
function _deployToken(string calldata name_, string calldata symbol_, address asset_, uint8 assetDecimals_)
internal
virtual
returns (address)
{
tRWA newToken = new tRWA(name_, symbol_, asset_, assetDecimals_, address(this));
return address(newToken);
}
/*//////////////////////////////////////////////////////////////
ADMIN MANAGEMENT
//////////////////////////////////////////////////////////////*/
/**
* @notice Allow admin to change the manager
* @param newManager The new manager
*/
function setManager(address newManager) external onlyRoles(roleManager.STRATEGY_ADMIN()) {
// Can set to 0 to disable manager
manager = newManager;
emit ManagerChange(manager, newManager);
}
/*//////////////////////////////////////////////////////////////
ASSET MANAGEMENT
//////////////////////////////////////////////////////////////*/
/**
* @notice Get the balance of the strategy
* @return The balance of the strategy in the underlying asset
*/
function balance() external view virtual returns (uint256);
/**
* @notice Send owned ETH to an address
* @param to The address to send the ETH to
*/
function sendETH(address to) external onlyManager {
(bool success,) = to.call{value: address(this).balance}("");
require(success, "ETH transfer failed");
}
/**
* @notice Send owned ERC20 tokens to an address
* @param tokenAddr The address of the ERC20 token to send
* @param to The address to send the tokens to
* @param amount The amount of tokens to send
*/
function sendToken(address tokenAddr, address to, uint256 amount) external onlyManager {
tokenAddr.safeTransfer(to, amount);
}
/**
* @notice Pull ERC20 tokens from an external contract into this contract
* @param tokenAddr The address of the ERC20 token to pull
* @param from The address to pull the tokens from
* @param amount The amount of tokens to pull
*/
function pullToken(address tokenAddr, address from, uint256 amount) external onlyManager {
tokenAddr.safeTransferFrom(from, address(this), amount);
}
/**
* @notice Set the allowance for an ERC20 token
* @param tokenAddr The address of the ERC20 token to set the allowance for
* @param spender The address to set the allowance for
* @param amount The amount of allowance to set
*/
function setAllowance(address tokenAddr, address spender, uint256 amount) external onlyManager {
tokenAddr.safeApproveWithRetry(spender, amount);
}
/**
* @notice Call the strategy token
* @dev Used for configuring token hooks
* @param data The calldata to call the strategy token with
*/
function callStrategyToken(bytes calldata data) external onlyRoles(roleManager.STRATEGY_ADMIN()) {
(bool success, bytes memory returnData) = sToken.call(data);
if (!success) {
revert CallRevert(returnData);
}
emit Call(sToken, 0, data);
}
/**
* @notice Execute arbitrary transactions on behalf of the strategy
* @param target Address of the contract to call
* @param value Amount of ETH to send
* @param data Calldata to send
* @return success Whether the call succeeded
* @return returnData The return data from the call
*/
function call(address target, uint256 value, bytes calldata data)
external
onlyManager
returns (bool success, bytes memory returnData)
{
if (target == address(0) || target == address(this)) revert InvalidAddress();
if (target == sToken) revert CannotCallToken();
(success, returnData) = target.call{value: value}(data);
if (!success) {
revert CallRevert(returnData);
}
emit Call(target, value, data);
}
/*//////////////////////////////////////////////////////////////
MODIFIERS
//////////////////////////////////////////////////////////////*/
modifier onlyManager() {
if (msg.sender != manager) revert Unauthorized();
_;
}
/*//////////////////////////////////////////////////////////////
FALLBACK
//////////////////////////////////////////////////////////////*/
receive() external payable {}
}// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.25;
/**
* @title IReporter
* @notice Interface for reporters that return strategy info
*/
interface IReporter {
/*//////////////////////////////////////////////////////////////
FUNCTIONS
//////////////////////////////////////////////////////////////*/
/**
* @notice Report the current value of an asset
* @return the content of the report
*/
function report() external view returns (bytes memory);
}// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import {ERC20} from "./ERC20.sol";
import {FixedPointMathLib} from "../utils/FixedPointMathLib.sol";
import {SafeTransferLib} from "../utils/SafeTransferLib.sol";
/// @notice Simple ERC4626 tokenized Vault implementation.
/// @author Solady (https://github.com/vectorized/solady/blob/main/src/tokens/ERC4626.sol)
/// @author Modified from Solmate (https://github.com/transmissions11/solmate/blob/main/src/mixins/ERC4626.sol)
/// @author Modified from OpenZeppelin (https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/extensions/ERC4626.sol)
abstract contract ERC4626 is ERC20 {
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CONSTANTS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev The default underlying decimals.
uint8 internal constant _DEFAULT_UNDERLYING_DECIMALS = 18;
/// @dev The default decimals offset.
uint8 internal constant _DEFAULT_DECIMALS_OFFSET = 0;
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CUSTOM ERRORS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Cannot deposit more than the max limit.
error DepositMoreThanMax();
/// @dev Cannot mint more than the max limit.
error MintMoreThanMax();
/// @dev Cannot withdraw more than the max limit.
error WithdrawMoreThanMax();
/// @dev Cannot redeem more than the max limit.
error RedeemMoreThanMax();
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* EVENTS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Emitted during a mint call or deposit call.
event Deposit(address indexed by, address indexed owner, uint256 assets, uint256 shares);
/// @dev Emitted during a withdraw call or redeem call.
event Withdraw(
address indexed by,
address indexed to,
address indexed owner,
uint256 assets,
uint256 shares
);
/// @dev `keccak256(bytes("Deposit(address,address,uint256,uint256)"))`.
uint256 private constant _DEPOSIT_EVENT_SIGNATURE =
0xdcbc1c05240f31ff3ad067ef1ee35ce4997762752e3a095284754544f4c709d7;
/// @dev `keccak256(bytes("Withdraw(address,address,address,uint256,uint256)"))`.
uint256 private constant _WITHDRAW_EVENT_SIGNATURE =
0xfbde797d201c681b91056529119e0b02407c7bb96a4a2c75c01fc9667232c8db;
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* ERC4626 CONSTANTS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev To be overridden to return the address of the underlying asset.
///
/// - MUST be an ERC20 token contract.
/// - MUST NOT revert.
function asset() public view virtual returns (address);
/// @dev To be overridden to return the number of decimals of the underlying asset.
/// Default: 18.
///
/// - MUST NOT revert.
function _underlyingDecimals() internal view virtual returns (uint8) {
return _DEFAULT_UNDERLYING_DECIMALS;
}
/// @dev Override to return a non-zero value to make the inflation attack even more unfeasible.
/// Only used when {_useVirtualShares} returns true.
/// Default: 0.
///
/// - MUST NOT revert.
function _decimalsOffset() internal view virtual returns (uint8) {
return _DEFAULT_DECIMALS_OFFSET;
}
/// @dev Returns whether virtual shares will be used to mitigate the inflation attack.
/// See: https://github.com/OpenZeppelin/openzeppelin-contracts/issues/3706
/// Override to return true or false.
/// Default: true.
///
/// - MUST NOT revert.
function _useVirtualShares() internal view virtual returns (bool) {
return true;
}
/// @dev Returns the decimals places of the token.
///
/// - MUST NOT revert.
function decimals() public view virtual override(ERC20) returns (uint8) {
if (!_useVirtualShares()) return _underlyingDecimals();
return _underlyingDecimals() + _decimalsOffset();
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* ASSET DECIMALS GETTER HELPER */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Helper function to get the decimals of the underlying asset.
/// Useful for setting the return value of `_underlyingDecimals` during initialization.
/// If the retrieval succeeds, `success` will be true, and `result` will hold the result.
/// Otherwise, `success` will be false, and `result` will be zero.
///
/// Example usage:
/// ```
/// (bool success, uint8 result) = _tryGetAssetDecimals(underlying);
/// _decimals = success ? result : _DEFAULT_UNDERLYING_DECIMALS;
/// ```
function _tryGetAssetDecimals(address underlying)
internal
view
returns (bool success, uint8 result)
{
/// @solidity memory-safe-assembly
assembly {
// Store the function selector of `decimals()`.
mstore(0x00, 0x313ce567)
// Arguments are evaluated last to first.
success :=
and(
// Returned value is less than 256, at left-padded to 32 bytes.
and(lt(mload(0x00), 0x100), gt(returndatasize(), 0x1f)),
// The staticcall succeeds.
staticcall(gas(), underlying, 0x1c, 0x04, 0x00, 0x20)
)
result := mul(mload(0x00), success)
}
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* ACCOUNTING LOGIC */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Returns the total amount of the underlying asset managed by the Vault.
///
/// - SHOULD include any compounding that occurs from the yield.
/// - MUST be inclusive of any fees that are charged against assets in the Vault.
/// - MUST NOT revert.
function totalAssets() public view virtual returns (uint256 assets) {
assets = SafeTransferLib.balanceOf(asset(), address(this));
}
/// @dev Returns the amount of shares that the Vault will exchange for the amount of
/// assets provided, in an ideal scenario where all conditions are met.
///
/// - MUST NOT be inclusive of any fees that are charged against assets in the Vault.
/// - MUST NOT show any variations depending on the caller.
/// - MUST NOT reflect slippage or other on-chain conditions, during the actual exchange.
/// - MUST NOT revert.
///
/// Note: This calculation MAY NOT reflect the "per-user" price-per-share, and instead
/// should reflect the "average-user's" price-per-share, i.e. what the average user should
/// expect to see when exchanging to and from.
function convertToShares(uint256 assets) public view virtual returns (uint256 shares) {
if (!_useVirtualShares()) {
uint256 supply = totalSupply();
return _eitherIsZero(assets, supply)
? _initialConvertToShares(assets)
: FixedPointMathLib.fullMulDiv(assets, supply, totalAssets());
}
uint256 o = _decimalsOffset();
if (o == uint256(0)) {
return FixedPointMathLib.fullMulDiv(assets, totalSupply() + 1, _inc(totalAssets()));
}
return FixedPointMathLib.fullMulDiv(assets, totalSupply() + 10 ** o, _inc(totalAssets()));
}
/// @dev Returns the amount of assets that the Vault will exchange for the amount of
/// shares provided, in an ideal scenario where all conditions are met.
///
/// - MUST NOT be inclusive of any fees that are charged against assets in the Vault.
/// - MUST NOT show any variations depending on the caller.
/// - MUST NOT reflect slippage or other on-chain conditions, during the actual exchange.
/// - MUST NOT revert.
///
/// Note: This calculation MAY NOT reflect the "per-user" price-per-share, and instead
/// should reflect the "average-user's" price-per-share, i.e. what the average user should
/// expect to see when exchanging to and from.
function convertToAssets(uint256 shares) public view virtual returns (uint256 assets) {
if (!_useVirtualShares()) {
uint256 supply = totalSupply();
return supply == uint256(0)
? _initialConvertToAssets(shares)
: FixedPointMathLib.fullMulDiv(shares, totalAssets(), supply);
}
uint256 o = _decimalsOffset();
if (o == uint256(0)) {
return FixedPointMathLib.fullMulDiv(shares, totalAssets() + 1, _inc(totalSupply()));
}
return FixedPointMathLib.fullMulDiv(shares, totalAssets() + 1, totalSupply() + 10 ** o);
}
/// @dev Allows an on-chain or off-chain user to simulate the effects of their deposit
/// at the current block, given current on-chain conditions.
///
/// - MUST return as close to and no more than the exact amount of Vault shares that
/// will be minted in a deposit call in the same transaction, i.e. deposit should
/// return the same or more shares as `previewDeposit` if call in the same transaction.
/// - MUST NOT account for deposit limits like those returned from `maxDeposit` and should
/// always act as if the deposit will be accepted, regardless of approvals, etc.
/// - MUST be inclusive of deposit fees. Integrators should be aware of this.
/// - MUST not revert.
///
/// Note: Any unfavorable discrepancy between `convertToShares` and `previewDeposit` SHOULD
/// be considered slippage in share price or some other type of condition, meaning
/// the depositor will lose assets by depositing.
function previewDeposit(uint256 assets) public view virtual returns (uint256 shares) {
shares = convertToShares(assets);
}
/// @dev Allows an on-chain or off-chain user to simulate the effects of their mint
/// at the current block, given current on-chain conditions.
///
/// - MUST return as close to and no fewer than the exact amount of assets that
/// will be deposited in a mint call in the same transaction, i.e. mint should
/// return the same or fewer assets as `previewMint` if called in the same transaction.
/// - MUST NOT account for mint limits like those returned from `maxMint` and should
/// always act as if the mint will be accepted, regardless of approvals, etc.
/// - MUST be inclusive of deposit fees. Integrators should be aware of this.
/// - MUST not revert.
///
/// Note: Any unfavorable discrepancy between `convertToAssets` and `previewMint` SHOULD
/// be considered slippage in share price or some other type of condition,
/// meaning the depositor will lose assets by minting.
function previewMint(uint256 shares) public view virtual returns (uint256 assets) {
if (!_useVirtualShares()) {
uint256 supply = totalSupply();
return supply == uint256(0)
? _initialConvertToAssets(shares)
: FixedPointMathLib.fullMulDivUp(shares, totalAssets(), supply);
}
uint256 o = _decimalsOffset();
if (o == uint256(0)) {
return FixedPointMathLib.fullMulDivUp(shares, totalAssets() + 1, _inc(totalSupply()));
}
return FixedPointMathLib.fullMulDivUp(shares, totalAssets() + 1, totalSupply() + 10 ** o);
}
/// @dev Allows an on-chain or off-chain user to simulate the effects of their withdrawal
/// at the current block, given the current on-chain conditions.
///
/// - MUST return as close to and no fewer than the exact amount of Vault shares that
/// will be burned in a withdraw call in the same transaction, i.e. withdraw should
/// return the same or fewer shares as `previewWithdraw` if call in the same transaction.
/// - MUST NOT account for withdrawal limits like those returned from `maxWithdraw` and should
/// always act as if the withdrawal will be accepted, regardless of share balance, etc.
/// - MUST be inclusive of withdrawal fees. Integrators should be aware of this.
/// - MUST not revert.
///
/// Note: Any unfavorable discrepancy between `convertToShares` and `previewWithdraw` SHOULD
/// be considered slippage in share price or some other type of condition,
/// meaning the depositor will lose assets by depositing.
function previewWithdraw(uint256 assets) public view virtual returns (uint256 shares) {
if (!_useVirtualShares()) {
uint256 supply = totalSupply();
return _eitherIsZero(assets, supply)
? _initialConvertToShares(assets)
: FixedPointMathLib.fullMulDivUp(assets, supply, totalAssets());
}
uint256 o = _decimalsOffset();
if (o == uint256(0)) {
return FixedPointMathLib.fullMulDivUp(assets, totalSupply() + 1, _inc(totalAssets()));
}
return FixedPointMathLib.fullMulDivUp(assets, totalSupply() + 10 ** o, _inc(totalAssets()));
}
/// @dev Allows an on-chain or off-chain user to simulate the effects of their redemption
/// at the current block, given current on-chain conditions.
///
/// - MUST return as close to and no more than the exact amount of assets that
/// will be withdrawn in a redeem call in the same transaction, i.e. redeem should
/// return the same or more assets as `previewRedeem` if called in the same transaction.
/// - MUST NOT account for redemption limits like those returned from `maxRedeem` and should
/// always act as if the redemption will be accepted, regardless of approvals, etc.
/// - MUST be inclusive of withdrawal fees. Integrators should be aware of this.
/// - MUST NOT revert.
///
/// Note: Any unfavorable discrepancy between `convertToAssets` and `previewRedeem` SHOULD
/// be considered slippage in share price or some other type of condition,
/// meaning the depositor will lose assets by depositing.
function previewRedeem(uint256 shares) public view virtual returns (uint256 assets) {
assets = convertToAssets(shares);
}
/// @dev Private helper to return if either value is zero.
function _eitherIsZero(uint256 a, uint256 b) private pure returns (bool result) {
/// @solidity memory-safe-assembly
assembly {
result := or(iszero(a), iszero(b))
}
}
/// @dev Private helper to return `x + 1` without the overflow check.
/// Used for computing the denominator input to `FixedPointMathLib.fullMulDiv(a, b, x + 1)`.
/// When `x == type(uint256).max`, we get `x + 1 == 0` (mod 2**256 - 1),
/// and `FixedPointMathLib.fullMulDiv` will revert as the denominator is zero.
function _inc(uint256 x) private pure returns (uint256) {
unchecked {
return x + 1;
}
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* DEPOSIT / WITHDRAWAL LIMIT LOGIC */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Returns the maximum amount of the underlying asset that can be deposited
/// into the Vault for `to`, via a deposit call.
///
/// - MUST return a limited value if `to` is subject to some deposit limit.
/// - MUST return `2**256-1` if there is no maximum limit.
/// - MUST NOT revert.
function maxDeposit(address to) public view virtual returns (uint256 maxAssets) {
to = to; // Silence unused variable warning.
maxAssets = type(uint256).max;
}
/// @dev Returns the maximum amount of the Vault shares that can be minter for `to`,
/// via a mint call.
///
/// - MUST return a limited value if `to` is subject to some mint limit.
/// - MUST return `2**256-1` if there is no maximum limit.
/// - MUST NOT revert.
function maxMint(address to) public view virtual returns (uint256 maxShares) {
to = to; // Silence unused variable warning.
maxShares = type(uint256).max;
}
/// @dev Returns the maximum amount of the underlying asset that can be withdrawn
/// from the `owner`'s balance in the Vault, via a withdraw call.
///
/// - MUST return a limited value if `owner` is subject to some withdrawal limit or timelock.
/// - MUST NOT revert.
function maxWithdraw(address owner) public view virtual returns (uint256 maxAssets) {
maxAssets = convertToAssets(balanceOf(owner));
}
/// @dev Returns the maximum amount of Vault shares that can be redeemed
/// from the `owner`'s balance in the Vault, via a redeem call.
///
/// - MUST return a limited value if `owner` is subject to some withdrawal limit or timelock.
/// - MUST return `balanceOf(owner)` otherwise.
/// - MUST NOT revert.
function maxRedeem(address owner) public view virtual returns (uint256 maxShares) {
maxShares = balanceOf(owner);
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* DEPOSIT / WITHDRAWAL LOGIC */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Mints `shares` Vault shares to `to` by depositing exactly `assets`
/// of underlying tokens.
///
/// - MUST emit the {Deposit} event.
/// - MAY support an additional flow in which the underlying tokens are owned by the Vault
/// contract before the deposit execution, and are accounted for during deposit.
/// - MUST revert if all of `assets` cannot be deposited, such as due to deposit limit,
/// slippage, insufficient approval, etc.
///
/// Note: Most implementations will require pre-approval of the Vault with the
/// Vault's underlying `asset` token.
function deposit(uint256 assets, address to) public virtual returns (uint256 shares) {
if (assets > maxDeposit(to)) _revert(0xb3c61a83); // `DepositMoreThanMax()`.
shares = previewDeposit(assets);
_deposit(msg.sender, to, assets, shares);
}
/// @dev Mints exactly `shares` Vault shares to `to` by depositing `assets`
/// of underlying tokens.
///
/// - MUST emit the {Deposit} event.
/// - MAY support an additional flow in which the underlying tokens are owned by the Vault
/// contract before the mint execution, and are accounted for during mint.
/// - MUST revert if all of `shares` cannot be deposited, such as due to deposit limit,
/// slippage, insufficient approval, etc.
///
/// Note: Most implementations will require pre-approval of the Vault with the
/// Vault's underlying `asset` token.
function mint(uint256 shares, address to) public virtual returns (uint256 assets) {
if (shares > maxMint(to)) _revert(0x6a695959); // `MintMoreThanMax()`.
assets = previewMint(shares);
_deposit(msg.sender, to, assets, shares);
}
/// @dev Burns `shares` from `owner` and sends exactly `assets` of underlying tokens to `to`.
///
/// - MUST emit the {Withdraw} event.
/// - MAY support an additional flow in which the underlying tokens are owned by the Vault
/// contract before the withdraw execution, and are accounted for during withdraw.
/// - MUST revert if all of `assets` cannot be withdrawn, such as due to withdrawal limit,
/// slippage, insufficient balance, etc.
///
/// Note: Some implementations will require pre-requesting to the Vault before a withdrawal
/// may be performed. Those methods should be performed separately.
function withdraw(uint256 assets, address to, address owner)
public
virtual
returns (uint256 shares)
{
if (assets > maxWithdraw(owner)) _revert(0x936941fc); // `WithdrawMoreThanMax()`.
shares = previewWithdraw(assets);
_withdraw(msg.sender, to, owner, assets, shares);
}
/// @dev Burns exactly `shares` from `owner` and sends `assets` of underlying tokens to `to`.
///
/// - MUST emit the {Withdraw} event.
/// - MAY support an additional flow in which the underlying tokens are owned by the Vault
/// contract before the redeem execution, and are accounted for during redeem.
/// - MUST revert if all of shares cannot be redeemed, such as due to withdrawal limit,
/// slippage, insufficient balance, etc.
///
/// Note: Some implementations will require pre-requesting to the Vault before a redeem
/// may be performed. Those methods should be performed separately.
function redeem(uint256 shares, address to, address owner)
public
virtual
returns (uint256 assets)
{
if (shares > maxRedeem(owner)) _revert(0x4656425a); // `RedeemMoreThanMax()`.
assets = previewRedeem(shares);
_withdraw(msg.sender, to, owner, assets, shares);
}
/// @dev Internal helper for reverting efficiently.
function _revert(uint256 s) private pure {
/// @solidity memory-safe-assembly
assembly {
mstore(0x00, s)
revert(0x1c, 0x04)
}
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* INTERNAL HELPERS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev For deposits and mints.
///
/// Emits a {Deposit} event.
function _deposit(address by, address to, uint256 assets, uint256 shares) internal virtual {
SafeTransferLib.safeTransferFrom(asset(), by, address(this), assets);
_mint(to, shares);
/// @solidity memory-safe-assembly
assembly {
// Emit the {Deposit} event.
mstore(0x00, assets)
mstore(0x20, shares)
let m := shr(96, not(0))
log3(0x00, 0x40, _DEPOSIT_EVENT_SIGNATURE, and(m, by), and(m, to))
}
_afterDeposit(assets, shares);
}
/// @dev For withdrawals and redemptions.
///
/// Emits a {Withdraw} event.
function _withdraw(address by, address to, address owner, uint256 assets, uint256 shares)
internal
virtual
{
if (by != owner) _spendAllowance(owner, by, shares);
_beforeWithdraw(assets, shares);
_burn(owner, shares);
SafeTransferLib.safeTransfer(asset(), to, assets);
/// @solidity memory-safe-assembly
assembly {
// Emit the {Withdraw} event.
mstore(0x00, assets)
mstore(0x20, shares)
let m := shr(96, not(0))
log4(0x00, 0x40, _WITHDRAW_EVENT_SIGNATURE, and(m, by), and(m, to), and(m, owner))
}
}
/// @dev Internal conversion function (from assets to shares) to apply when the Vault is empty.
/// Only used when {_useVirtualShares} returns false.
///
/// Note: Make sure to keep this function consistent with {_initialConvertToAssets}
/// when overriding it.
function _initialConvertToShares(uint256 assets)
internal
view
virtual
returns (uint256 shares)
{
shares = assets;
}
/// @dev Internal conversion function (from shares to assets) to apply when the Vault is empty.
/// Only used when {_useVirtualShares} returns false.
///
/// Note: Make sure to keep this function consistent with {_initialConvertToShares}
/// when overriding it.
function _initialConvertToAssets(uint256 shares)
internal
view
virtual
returns (uint256 assets)
{
assets = shares;
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* HOOKS TO OVERRIDE */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Hook that is called before any withdrawal or redemption.
function _beforeWithdraw(uint256 assets, uint256 shares) internal virtual {}
/// @dev Hook that is called after any deposit or mint.
function _afterDeposit(uint256 assets, uint256 shares) internal virtual {}
}// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
/// @notice Reentrancy guard mixin.
/// @author Solady (https://github.com/vectorized/solady/blob/main/src/utils/ReentrancyGuard.sol)
abstract contract ReentrancyGuard {
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CUSTOM ERRORS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Unauthorized reentrant call.
error Reentrancy();
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* STORAGE */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Equivalent to: `uint72(bytes9(keccak256("_REENTRANCY_GUARD_SLOT")))`.
/// 9 bytes is large enough to avoid collisions with lower slots,
/// but not too large to result in excessive bytecode bloat.
uint256 private constant _REENTRANCY_GUARD_SLOT = 0x929eee149b4bd21268;
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* REENTRANCY GUARD */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Guards a function from reentrancy.
modifier nonReentrant() virtual {
/// @solidity memory-safe-assembly
assembly {
if eq(sload(_REENTRANCY_GUARD_SLOT), address()) {
mstore(0x00, 0xab143c06) // `Reentrancy()`.
revert(0x1c, 0x04)
}
sstore(_REENTRANCY_GUARD_SLOT, address())
}
_;
/// @solidity memory-safe-assembly
assembly {
sstore(_REENTRANCY_GUARD_SLOT, codesize())
}
}
/// @dev Guards a view function from read-only reentrancy.
modifier nonReadReentrant() virtual {
/// @solidity memory-safe-assembly
assembly {
if eq(sload(_REENTRANCY_GUARD_SLOT), address()) {
mstore(0x00, 0xab143c06) // `Reentrancy()`.
revert(0x1c, 0x04)
}
}
_;
}
}// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.25;
/**
* @title ItRWA
* @notice Interface for Tokenized Real World Asset (tRWA)
* @dev Defines the interface with all events and errors for the tRWA contract
* This is an extension interface (does not duplicate ERC4626 methods)
*/
interface ItRWA {
/*//////////////////////////////////////////////////////////////
ERRORS
//////////////////////////////////////////////////////////////*/
error InvalidAddress();
error AssetMismatch();
error RuleCheckFailed(string reason);
/*//////////////////////////////////////////////////////////////
VIEW FUNCTIONS
//////////////////////////////////////////////////////////////*/
/// @notice Returns the address of the strategy
function strategy() external view returns (address);
/// @notice Returns the address of the underlying asset
function asset() external view returns (address);
// Note: Standard ERC4626 operations are defined in the ERC4626 interface
// and are not redefined here to avoid conflicts
}// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.25;
import {SafeTransferLib} from "solady/utils/SafeTransferLib.sol";
import {RoleManaged} from "../auth/RoleManaged.sol";
import {ItRWA} from "../token/ItRWA.sol";
import {IRegistry} from "../registry/IRegistry.sol";
/**
* @title Conduit
* @notice Contract to collect deposits on behalf of tRWA contracts
* @dev This contract is used to collect deposits from users, and transfer them
* to strategy contracts. This allows users to make single global approvals
* to the Conduit contract, and then deposit into any strategy.
*/
contract Conduit is RoleManaged {
using SafeTransferLib for address;
/*//////////////////////////////////////////////////////////////
ERRORS
//////////////////////////////////////////////////////////////*/
error InvalidAmount();
error InvalidToken();
error InvalidDestination();
/*//////////////////////////////////////////////////////////////
INITIALIZATION
//////////////////////////////////////////////////////////////*/
/**
* @notice Constructor
* @dev Constructor is called by the registry contract
* @param _roleManager Address of the role manager contract
*/
constructor(address _roleManager) RoleManaged(_roleManager) {}
/*//////////////////////////////////////////////////////////////
FUNCTIONS
//////////////////////////////////////////////////////////////*/
/**
* @notice Executes a token transfer on behalf of an approved tRWA contract.
* @dev The user (`_from`) must have approved this Conduit contract to spend `_amount` of `_token`.
* Only callable by an `approvedTRWAContracts`.
* @param token The address of the ERC20 token to transfer.
* @param from The address of the user whose tokens are being transferred.
* @param to The address to transfer the tokens to (e.g., the tRWA contract or a designated vault).
* @param amount The amount of tokens to transfer.
*/
function collectDeposit(address token, address from, address to, uint256 amount) external returns (bool) {
if (amount == 0) revert InvalidAmount();
if (!IRegistry(registry()).isStrategyToken(msg.sender)) revert InvalidDestination();
if (ItRWA(msg.sender).asset() != token) revert InvalidToken();
if (ItRWA(msg.sender).strategy() != to) revert InvalidDestination();
// Transfer tokens from the user to specific destination
token.safeTransferFrom(from, to, amount);
return true;
}
/**
* @notice Rescues ERC20 tokens from the conduit
* @param tokenAddress The address of the ERC20 token to rescue
* @param to The address to transfer the tokens to
* @param amount The amount of tokens to transfer
*/
function rescueERC20(address tokenAddress, address to, uint256 amount)
external
onlyRoles(roleManager.PROTOCOL_ADMIN())
{
tokenAddress.safeTransfer(to, amount);
}
}// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.25;
import {RoleManager} from "./RoleManager.sol";
import {LibRoleManaged} from "./LibRoleManaged.sol";
/**
* @title RoleManaged
* @notice Base contract for role-managed contracts in the Fountfi protocol
* @dev Provides role checking functionality for contracts
*/
abstract contract RoleManaged is LibRoleManaged {
/*//////////////////////////////////////////////////////////////
ERRORS
//////////////////////////////////////////////////////////////*/
error InvalidRoleManager();
/*//////////////////////////////////////////////////////////////
CONSTRUCTOR
//////////////////////////////////////////////////////////////*/
/**
* @notice Constructor
* @param _roleManager Address of the role manager contract
*/
constructor(address _roleManager) {
if (_roleManager == address(0)) revert InvalidRoleManager();
roleManager = RoleManager(_roleManager);
}
}// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.25;
/**
* @title IRegistry
* @notice Interface for the Registry contract
* @dev The Registry contract is used to register strategies, hooks, and assets
* and to deploy new strategies and tokens.
*/
interface IRegistry {
/*//////////////////////////////////////////////////////////////
ERRORS
//////////////////////////////////////////////////////////////*/
error ZeroAddress();
error UnauthorizedStrategy();
error UnauthorizedHook();
error UnauthorizedAsset();
error InvalidInitialization();
/*//////////////////////////////////////////////////////////////
EVENTS
//////////////////////////////////////////////////////////////*/
event SetStrategy(address indexed implementation, bool allowed);
event SetHook(address indexed implementation, bool allowed);
event SetAsset(address indexed asset, uint8 decimals);
event Deploy(address indexed strategy, address indexed sToken, address indexed asset);
event DeployWithController(address indexed strategy, address indexed sToken, address indexed controller);
/*//////////////////////////////////////////////////////////////
VIEW FUNCTIONS
//////////////////////////////////////////////////////////////*/
function conduit() external view returns (address);
function allowedStrategies(address implementation) external view returns (bool);
function allowedHooks(address implementation) external view returns (bool);
function allowedAssets(address asset) external view returns (uint8);
function isStrategy(address implementation) external view returns (bool);
function allStrategies() external view returns (address[] memory);
function isStrategyToken(address token) external view returns (bool);
function allStrategyTokens() external view returns (address[] memory tokens);
/*//////////////////////////////////////////////////////////////
DEPLOYMENT
//////////////////////////////////////////////////////////////*/
function deploy(
address _implementation,
string memory _name,
string memory _symbol,
address _asset,
address _manager,
bytes memory _initData
) external returns (address strategy, address token);
}// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.25;
import {RoleManager} from "./RoleManager.sol";
import {LibRoleManaged} from "./LibRoleManaged.sol";
/**
* @title CloneableRoleManaged
* @notice Clone-compatible base contract for role-managed contracts in the Fountfi protocol
* @dev Provides role checking functionality for contracts that will be deployed as clones
*/
abstract contract CloneableRoleManaged is LibRoleManaged {
/*//////////////////////////////////////////////////////////////
ERRORS
//////////////////////////////////////////////////////////////*/
error InvalidRoleManager();
/*//////////////////////////////////////////////////////////////
EVENTS
//////////////////////////////////////////////////////////////*/
event RoleManagerInitialized(address indexed roleManager);
/*//////////////////////////////////////////////////////////////
INITIALIZATION
//////////////////////////////////////////////////////////////*/
/**
* @notice Initialize the role manager (for use with clones)
* @param _roleManager Address of the role manager contract
*/
function _initializeRoleManager(address _roleManager) internal {
if (_roleManager == address(0)) revert InvalidRoleManager();
roleManager = RoleManager(_roleManager);
emit RoleManagerInitialized(_roleManager);
}
}// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
/// @notice Simple ERC20 + EIP-2612 implementation.
/// @author Solady (https://github.com/vectorized/solady/blob/main/src/tokens/ERC20.sol)
/// @author Modified from Solmate (https://github.com/transmissions11/solmate/blob/main/src/tokens/ERC20.sol)
/// @author Modified from OpenZeppelin (https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol)
///
/// @dev Note:
/// - The ERC20 standard allows minting and transferring to and from the zero address,
/// minting and transferring zero tokens, as well as self-approvals.
/// For performance, this implementation WILL NOT revert for such actions.
/// Please add any checks with overrides if desired.
/// - The `permit` function uses the ecrecover precompile (0x1).
///
/// If you are overriding:
/// - NEVER violate the ERC20 invariant:
/// the total sum of all balances must be equal to `totalSupply()`.
/// - Check that the overridden function is actually used in the function you want to
/// change the behavior of. Much of the code has been manually inlined for performance.
abstract contract ERC20 {
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CUSTOM ERRORS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev The total supply has overflowed.
error TotalSupplyOverflow();
/// @dev The allowance has overflowed.
error AllowanceOverflow();
/// @dev The allowance has underflowed.
error AllowanceUnderflow();
/// @dev Insufficient balance.
error InsufficientBalance();
/// @dev Insufficient allowance.
error InsufficientAllowance();
/// @dev The permit is invalid.
error InvalidPermit();
/// @dev The permit has expired.
error PermitExpired();
/// @dev The allowance of Permit2 is fixed at infinity.
error Permit2AllowanceIsFixedAtInfinity();
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* EVENTS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Emitted when `amount` tokens is transferred from `from` to `to`.
event Transfer(address indexed from, address indexed to, uint256 amount);
/// @dev Emitted when `amount` tokens is approved by `owner` to be used by `spender`.
event Approval(address indexed owner, address indexed spender, uint256 amount);
/// @dev `keccak256(bytes("Transfer(address,address,uint256)"))`.
uint256 private constant _TRANSFER_EVENT_SIGNATURE =
0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef;
/// @dev `keccak256(bytes("Approval(address,address,uint256)"))`.
uint256 private constant _APPROVAL_EVENT_SIGNATURE =
0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925;
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* STORAGE */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev The storage slot for the total supply.
uint256 private constant _TOTAL_SUPPLY_SLOT = 0x05345cdf77eb68f44c;
/// @dev The balance slot of `owner` is given by:
/// ```
/// mstore(0x0c, _BALANCE_SLOT_SEED)
/// mstore(0x00, owner)
/// let balanceSlot := keccak256(0x0c, 0x20)
/// ```
uint256 private constant _BALANCE_SLOT_SEED = 0x87a211a2;
/// @dev The allowance slot of (`owner`, `spender`) is given by:
/// ```
/// mstore(0x20, spender)
/// mstore(0x0c, _ALLOWANCE_SLOT_SEED)
/// mstore(0x00, owner)
/// let allowanceSlot := keccak256(0x0c, 0x34)
/// ```
uint256 private constant _ALLOWANCE_SLOT_SEED = 0x7f5e9f20;
/// @dev The nonce slot of `owner` is given by:
/// ```
/// mstore(0x0c, _NONCES_SLOT_SEED)
/// mstore(0x00, owner)
/// let nonceSlot := keccak256(0x0c, 0x20)
/// ```
uint256 private constant _NONCES_SLOT_SEED = 0x38377508;
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CONSTANTS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev `(_NONCES_SLOT_SEED << 16) | 0x1901`.
uint256 private constant _NONCES_SLOT_SEED_WITH_SIGNATURE_PREFIX = 0x383775081901;
/// @dev `keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)")`.
bytes32 private constant _DOMAIN_TYPEHASH =
0x8b73c3c69bb8fe3d512ecc4cf759cc79239f7b179b0ffacaa9a75d522b39400f;
/// @dev `keccak256("1")`.
/// If you need to use a different version, override `_versionHash`.
bytes32 private constant _DEFAULT_VERSION_HASH =
0xc89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc6;
/// @dev `keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)")`.
bytes32 private constant _PERMIT_TYPEHASH =
0x6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9;
/// @dev The canonical Permit2 address.
/// For signature-based allowance granting for single transaction ERC20 `transferFrom`.
/// Enabled by default. To disable, override `_givePermit2InfiniteAllowance()`.
/// [Github](https://github.com/Uniswap/permit2)
/// [Etherscan](https://etherscan.io/address/0x000000000022D473030F116dDEE9F6B43aC78BA3)
address internal constant _PERMIT2 = 0x000000000022D473030F116dDEE9F6B43aC78BA3;
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* ERC20 METADATA */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Returns the name of the token.
function name() public view virtual returns (string memory);
/// @dev Returns the symbol of the token.
function symbol() public view virtual returns (string memory);
/// @dev Returns the decimals places of the token.
function decimals() public view virtual returns (uint8) {
return 18;
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* ERC20 */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Returns the amount of tokens in existence.
function totalSupply() public view virtual returns (uint256 result) {
/// @solidity memory-safe-assembly
assembly {
result := sload(_TOTAL_SUPPLY_SLOT)
}
}
/// @dev Returns the amount of tokens owned by `owner`.
function balanceOf(address owner) public view virtual returns (uint256 result) {
/// @solidity memory-safe-assembly
assembly {
mstore(0x0c, _BALANCE_SLOT_SEED)
mstore(0x00, owner)
result := sload(keccak256(0x0c, 0x20))
}
}
/// @dev Returns the amount of tokens that `spender` can spend on behalf of `owner`.
function allowance(address owner, address spender)
public
view
virtual
returns (uint256 result)
{
if (_givePermit2InfiniteAllowance()) {
if (spender == _PERMIT2) return type(uint256).max;
}
/// @solidity memory-safe-assembly
assembly {
mstore(0x20, spender)
mstore(0x0c, _ALLOWANCE_SLOT_SEED)
mstore(0x00, owner)
result := sload(keccak256(0x0c, 0x34))
}
}
/// @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
///
/// Emits a {Approval} event.
function approve(address spender, uint256 amount) public virtual returns (bool) {
if (_givePermit2InfiniteAllowance()) {
/// @solidity memory-safe-assembly
assembly {
// If `spender == _PERMIT2 && amount != type(uint256).max`.
if iszero(or(xor(shr(96, shl(96, spender)), _PERMIT2), iszero(not(amount)))) {
mstore(0x00, 0x3f68539a) // `Permit2AllowanceIsFixedAtInfinity()`.
revert(0x1c, 0x04)
}
}
}
/// @solidity memory-safe-assembly
assembly {
// Compute the allowance slot and store the amount.
mstore(0x20, spender)
mstore(0x0c, _ALLOWANCE_SLOT_SEED)
mstore(0x00, caller())
sstore(keccak256(0x0c, 0x34), amount)
// Emit the {Approval} event.
mstore(0x00, amount)
log3(0x00, 0x20, _APPROVAL_EVENT_SIGNATURE, caller(), shr(96, mload(0x2c)))
}
return true;
}
/// @dev Transfer `amount` tokens from the caller to `to`.
///
/// Requirements:
/// - `from` must at least have `amount`.
///
/// Emits a {Transfer} event.
function transfer(address to, uint256 amount) public virtual returns (bool) {
_beforeTokenTransfer(msg.sender, to, amount);
/// @solidity memory-safe-assembly
assembly {
// Compute the balance slot and load its value.
mstore(0x0c, _BALANCE_SLOT_SEED)
mstore(0x00, caller())
let fromBalanceSlot := keccak256(0x0c, 0x20)
let fromBalance := sload(fromBalanceSlot)
// Revert if insufficient balance.
if gt(amount, fromBalance) {
mstore(0x00, 0xf4d678b8) // `InsufficientBalance()`.
revert(0x1c, 0x04)
}
// Subtract and store the updated balance.
sstore(fromBalanceSlot, sub(fromBalance, amount))
// Compute the balance slot of `to`.
mstore(0x00, to)
let toBalanceSlot := keccak256(0x0c, 0x20)
// Add and store the updated balance of `to`.
// Will not overflow because the sum of all user balances
// cannot exceed the maximum uint256 value.
sstore(toBalanceSlot, add(sload(toBalanceSlot), amount))
// Emit the {Transfer} event.
mstore(0x20, amount)
log3(0x20, 0x20, _TRANSFER_EVENT_SIGNATURE, caller(), shr(96, mload(0x0c)))
}
_afterTokenTransfer(msg.sender, to, amount);
return true;
}
/// @dev Transfers `amount` tokens from `from` to `to`.
///
/// Note: Does not update the allowance if it is the maximum uint256 value.
///
/// Requirements:
/// - `from` must at least have `amount`.
/// - The caller must have at least `amount` of allowance to transfer the tokens of `from`.
///
/// Emits a {Transfer} event.
function transferFrom(address from, address to, uint256 amount) public virtual returns (bool) {
_beforeTokenTransfer(from, to, amount);
// Code duplication is for zero-cost abstraction if possible.
if (_givePermit2InfiniteAllowance()) {
/// @solidity memory-safe-assembly
assembly {
let from_ := shl(96, from)
if iszero(eq(caller(), _PERMIT2)) {
// Compute the allowance slot and load its value.
mstore(0x20, caller())
mstore(0x0c, or(from_, _ALLOWANCE_SLOT_SEED))
let allowanceSlot := keccak256(0x0c, 0x34)
let allowance_ := sload(allowanceSlot)
// If the allowance is not the maximum uint256 value.
if not(allowance_) {
// Revert if the amount to be transferred exceeds the allowance.
if gt(amount, allowance_) {
mstore(0x00, 0x13be252b) // `InsufficientAllowance()`.
revert(0x1c, 0x04)
}
// Subtract and store the updated allowance.
sstore(allowanceSlot, sub(allowance_, amount))
}
}
// Compute the balance slot and load its value.
mstore(0x0c, or(from_, _BALANCE_SLOT_SEED))
let fromBalanceSlot := keccak256(0x0c, 0x20)
let fromBalance := sload(fromBalanceSlot)
// Revert if insufficient balance.
if gt(amount, fromBalance) {
mstore(0x00, 0xf4d678b8) // `InsufficientBalance()`.
revert(0x1c, 0x04)
}
// Subtract and store the updated balance.
sstore(fromBalanceSlot, sub(fromBalance, amount))
// Compute the balance slot of `to`.
mstore(0x00, to)
let toBalanceSlot := keccak256(0x0c, 0x20)
// Add and store the updated balance of `to`.
// Will not overflow because the sum of all user balances
// cannot exceed the maximum uint256 value.
sstore(toBalanceSlot, add(sload(toBalanceSlot), amount))
// Emit the {Transfer} event.
mstore(0x20, amount)
log3(0x20, 0x20, _TRANSFER_EVENT_SIGNATURE, shr(96, from_), shr(96, mload(0x0c)))
}
} else {
/// @solidity memory-safe-assembly
assembly {
let from_ := shl(96, from)
// Compute the allowance slot and load its value.
mstore(0x20, caller())
mstore(0x0c, or(from_, _ALLOWANCE_SLOT_SEED))
let allowanceSlot := keccak256(0x0c, 0x34)
let allowance_ := sload(allowanceSlot)
// If the allowance is not the maximum uint256 value.
if not(allowance_) {
// Revert if the amount to be transferred exceeds the allowance.
if gt(amount, allowance_) {
mstore(0x00, 0x13be252b) // `InsufficientAllowance()`.
revert(0x1c, 0x04)
}
// Subtract and store the updated allowance.
sstore(allowanceSlot, sub(allowance_, amount))
}
// Compute the balance slot and load its value.
mstore(0x0c, or(from_, _BALANCE_SLOT_SEED))
let fromBalanceSlot := keccak256(0x0c, 0x20)
let fromBalance := sload(fromBalanceSlot)
// Revert if insufficient balance.
if gt(amount, fromBalance) {
mstore(0x00, 0xf4d678b8) // `InsufficientBalance()`.
revert(0x1c, 0x04)
}
// Subtract and store the updated balance.
sstore(fromBalanceSlot, sub(fromBalance, amount))
// Compute the balance slot of `to`.
mstore(0x00, to)
let toBalanceSlot := keccak256(0x0c, 0x20)
// Add and store the updated balance of `to`.
// Will not overflow because the sum of all user balances
// cannot exceed the maximum uint256 value.
sstore(toBalanceSlot, add(sload(toBalanceSlot), amount))
// Emit the {Transfer} event.
mstore(0x20, amount)
log3(0x20, 0x20, _TRANSFER_EVENT_SIGNATURE, shr(96, from_), shr(96, mload(0x0c)))
}
}
_afterTokenTransfer(from, to, amount);
return true;
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* EIP-2612 */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev For more performance, override to return the constant value
/// of `keccak256(bytes(name()))` if `name()` will never change.
function _constantNameHash() internal view virtual returns (bytes32 result) {}
/// @dev If you need a different value, override this function.
function _versionHash() internal view virtual returns (bytes32 result) {
result = _DEFAULT_VERSION_HASH;
}
/// @dev For inheriting contracts to increment the nonce.
function _incrementNonce(address owner) internal virtual {
/// @solidity memory-safe-assembly
assembly {
mstore(0x0c, _NONCES_SLOT_SEED)
mstore(0x00, owner)
let nonceSlot := keccak256(0x0c, 0x20)
sstore(nonceSlot, add(1, sload(nonceSlot)))
}
}
/// @dev Returns the current nonce for `owner`.
/// This value is used to compute the signature for EIP-2612 permit.
function nonces(address owner) public view virtual returns (uint256 result) {
/// @solidity memory-safe-assembly
assembly {
// Compute the nonce slot and load its value.
mstore(0x0c, _NONCES_SLOT_SEED)
mstore(0x00, owner)
result := sload(keccak256(0x0c, 0x20))
}
}
/// @dev Sets `value` as the allowance of `spender` over the tokens of `owner`,
/// authorized by a signed approval by `owner`.
///
/// Emits a {Approval} event.
function permit(
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) public virtual {
if (_givePermit2InfiniteAllowance()) {
/// @solidity memory-safe-assembly
assembly {
// If `spender == _PERMIT2 && value != type(uint256).max`.
if iszero(or(xor(shr(96, shl(96, spender)), _PERMIT2), iszero(not(value)))) {
mstore(0x00, 0x3f68539a) // `Permit2AllowanceIsFixedAtInfinity()`.
revert(0x1c, 0x04)
}
}
}
bytes32 nameHash = _constantNameHash();
// We simply calculate it on-the-fly to allow for cases where the `name` may change.
if (nameHash == bytes32(0)) nameHash = keccak256(bytes(name()));
bytes32 versionHash = _versionHash();
/// @solidity memory-safe-assembly
assembly {
// Revert if the block timestamp is greater than `deadline`.
if gt(timestamp(), deadline) {
mstore(0x00, 0x1a15a3cc) // `PermitExpired()`.
revert(0x1c, 0x04)
}
let m := mload(0x40) // Grab the free memory pointer.
// Clean the upper 96 bits.
owner := shr(96, shl(96, owner))
spender := shr(96, shl(96, spender))
// Compute the nonce slot and load its value.
mstore(0x0e, _NONCES_SLOT_SEED_WITH_SIGNATURE_PREFIX)
mstore(0x00, owner)
let nonceSlot := keccak256(0x0c, 0x20)
let nonceValue := sload(nonceSlot)
// Prepare the domain separator.
mstore(m, _DOMAIN_TYPEHASH)
mstore(add(m, 0x20), nameHash)
mstore(add(m, 0x40), versionHash)
mstore(add(m, 0x60), chainid())
mstore(add(m, 0x80), address())
mstore(0x2e, keccak256(m, 0xa0))
// Prepare the struct hash.
mstore(m, _PERMIT_TYPEHASH)
mstore(add(m, 0x20), owner)
mstore(add(m, 0x40), spender)
mstore(add(m, 0x60), value)
mstore(add(m, 0x80), nonceValue)
mstore(add(m, 0xa0), deadline)
mstore(0x4e, keccak256(m, 0xc0))
// Prepare the ecrecover calldata.
mstore(0x00, keccak256(0x2c, 0x42))
mstore(0x20, and(0xff, v))
mstore(0x40, r)
mstore(0x60, s)
let t := staticcall(gas(), 1, 0x00, 0x80, 0x20, 0x20)
// If the ecrecover fails, the returndatasize will be 0x00,
// `owner` will be checked if it equals the hash at 0x00,
// which evaluates to false (i.e. 0), and we will revert.
// If the ecrecover succeeds, the returndatasize will be 0x20,
// `owner` will be compared against the returned address at 0x20.
if iszero(eq(mload(returndatasize()), owner)) {
mstore(0x00, 0xddafbaef) // `InvalidPermit()`.
revert(0x1c, 0x04)
}
// Increment and store the updated nonce.
sstore(nonceSlot, add(nonceValue, t)) // `t` is 1 if ecrecover succeeds.
// Compute the allowance slot and store the value.
// The `owner` is already at slot 0x20.
mstore(0x40, or(shl(160, _ALLOWANCE_SLOT_SEED), spender))
sstore(keccak256(0x2c, 0x34), value)
// Emit the {Approval} event.
log3(add(m, 0x60), 0x20, _APPROVAL_EVENT_SIGNATURE, owner, spender)
mstore(0x40, m) // Restore the free memory pointer.
mstore(0x60, 0) // Restore the zero pointer.
}
}
/// @dev Returns the EIP-712 domain separator for the EIP-2612 permit.
function DOMAIN_SEPARATOR() public view virtual returns (bytes32 result) {
bytes32 nameHash = _constantNameHash();
// We simply calculate it on-the-fly to allow for cases where the `name` may change.
if (nameHash == bytes32(0)) nameHash = keccak256(bytes(name()));
bytes32 versionHash = _versionHash();
/// @solidity memory-safe-assembly
assembly {
let m := mload(0x40) // Grab the free memory pointer.
mstore(m, _DOMAIN_TYPEHASH)
mstore(add(m, 0x20), nameHash)
mstore(add(m, 0x40), versionHash)
mstore(add(m, 0x60), chainid())
mstore(add(m, 0x80), address())
result := keccak256(m, 0xa0)
}
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* INTERNAL MINT FUNCTIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Mints `amount` tokens to `to`, increasing the total supply.
///
/// Emits a {Transfer} event.
function _mint(address to, uint256 amount) internal virtual {
_beforeTokenTransfer(address(0), to, amount);
/// @solidity memory-safe-assembly
assembly {
let totalSupplyBefore := sload(_TOTAL_SUPPLY_SLOT)
let totalSupplyAfter := add(totalSupplyBefore, amount)
// Revert if the total supply overflows.
if lt(totalSupplyAfter, totalSupplyBefore) {
mstore(0x00, 0xe5cfe957) // `TotalSupplyOverflow()`.
revert(0x1c, 0x04)
}
// Store the updated total supply.
sstore(_TOTAL_SUPPLY_SLOT, totalSupplyAfter)
// Compute the balance slot and load its value.
mstore(0x0c, _BALANCE_SLOT_SEED)
mstore(0x00, to)
let toBalanceSlot := keccak256(0x0c, 0x20)
// Add and store the updated balance.
sstore(toBalanceSlot, add(sload(toBalanceSlot), amount))
// Emit the {Transfer} event.
mstore(0x20, amount)
log3(0x20, 0x20, _TRANSFER_EVENT_SIGNATURE, 0, shr(96, mload(0x0c)))
}
_afterTokenTransfer(address(0), to, amount);
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* INTERNAL BURN FUNCTIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Burns `amount` tokens from `from`, reducing the total supply.
///
/// Emits a {Transfer} event.
function _burn(address from, uint256 amount) internal virtual {
_beforeTokenTransfer(from, address(0), amount);
/// @solidity memory-safe-assembly
assembly {
// Compute the balance slot and load its value.
mstore(0x0c, _BALANCE_SLOT_SEED)
mstore(0x00, from)
let fromBalanceSlot := keccak256(0x0c, 0x20)
let fromBalance := sload(fromBalanceSlot)
// Revert if insufficient balance.
if gt(amount, fromBalance) {
mstore(0x00, 0xf4d678b8) // `InsufficientBalance()`.
revert(0x1c, 0x04)
}
// Subtract and store the updated balance.
sstore(fromBalanceSlot, sub(fromBalance, amount))
// Subtract and store the updated total supply.
sstore(_TOTAL_SUPPLY_SLOT, sub(sload(_TOTAL_SUPPLY_SLOT), amount))
// Emit the {Transfer} event.
mstore(0x00, amount)
log3(0x00, 0x20, _TRANSFER_EVENT_SIGNATURE, shr(96, shl(96, from)), 0)
}
_afterTokenTransfer(from, address(0), amount);
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* INTERNAL TRANSFER FUNCTIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Moves `amount` of tokens from `from` to `to`.
function _transfer(address from, address to, uint256 amount) internal virtual {
_beforeTokenTransfer(from, to, amount);
/// @solidity memory-safe-assembly
assembly {
let from_ := shl(96, from)
// Compute the balance slot and load its value.
mstore(0x0c, or(from_, _BALANCE_SLOT_SEED))
let fromBalanceSlot := keccak256(0x0c, 0x20)
let fromBalance := sload(fromBalanceSlot)
// Revert if insufficient balance.
if gt(amount, fromBalance) {
mstore(0x00, 0xf4d678b8) // `InsufficientBalance()`.
revert(0x1c, 0x04)
}
// Subtract and store the updated balance.
sstore(fromBalanceSlot, sub(fromBalance, amount))
// Compute the balance slot of `to`.
mstore(0x00, to)
let toBalanceSlot := keccak256(0x0c, 0x20)
// Add and store the updated balance of `to`.
// Will not overflow because the sum of all user balances
// cannot exceed the maximum uint256 value.
sstore(toBalanceSlot, add(sload(toBalanceSlot), amount))
// Emit the {Transfer} event.
mstore(0x20, amount)
log3(0x20, 0x20, _TRANSFER_EVENT_SIGNATURE, shr(96, from_), shr(96, mload(0x0c)))
}
_afterTokenTransfer(from, to, amount);
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* INTERNAL ALLOWANCE FUNCTIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Updates the allowance of `owner` for `spender` based on spent `amount`.
function _spendAllowance(address owner, address spender, uint256 amount) internal virtual {
if (_givePermit2InfiniteAllowance()) {
if (spender == _PERMIT2) return; // Do nothing, as allowance is infinite.
}
/// @solidity memory-safe-assembly
assembly {
// Compute the allowance slot and load its value.
mstore(0x20, spender)
mstore(0x0c, _ALLOWANCE_SLOT_SEED)
mstore(0x00, owner)
let allowanceSlot := keccak256(0x0c, 0x34)
let allowance_ := sload(allowanceSlot)
// If the allowance is not the maximum uint256 value.
if not(allowance_) {
// Revert if the amount to be transferred exceeds the allowance.
if gt(amount, allowance_) {
mstore(0x00, 0x13be252b) // `InsufficientAllowance()`.
revert(0x1c, 0x04)
}
// Subtract and store the updated allowance.
sstore(allowanceSlot, sub(allowance_, amount))
}
}
}
/// @dev Sets `amount` as the allowance of `spender` over the tokens of `owner`.
///
/// Emits a {Approval} event.
function _approve(address owner, address spender, uint256 amount) internal virtual {
if (_givePermit2InfiniteAllowance()) {
/// @solidity memory-safe-assembly
assembly {
// If `spender == _PERMIT2 && amount != type(uint256).max`.
if iszero(or(xor(shr(96, shl(96, spender)), _PERMIT2), iszero(not(amount)))) {
mstore(0x00, 0x3f68539a) // `Permit2AllowanceIsFixedAtInfinity()`.
revert(0x1c, 0x04)
}
}
}
/// @solidity memory-safe-assembly
assembly {
let owner_ := shl(96, owner)
// Compute the allowance slot and store the amount.
mstore(0x20, spender)
mstore(0x0c, or(owner_, _ALLOWANCE_SLOT_SEED))
sstore(keccak256(0x0c, 0x34), amount)
// Emit the {Approval} event.
mstore(0x00, amount)
log3(0x00, 0x20, _APPROVAL_EVENT_SIGNATURE, shr(96, owner_), shr(96, mload(0x2c)))
}
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* HOOKS TO OVERRIDE */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Hook that is called before any transfer of tokens.
/// This includes minting and burning.
function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual {}
/// @dev Hook that is called after any transfer of tokens.
/// This includes minting and burning.
function _afterTokenTransfer(address from, address to, uint256 amount) internal virtual {}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* PERMIT2 */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Returns whether to fix the Permit2 contract's allowance at infinity.
///
/// This value should be kept constant after contract initialization,
/// or else the actual allowance values may not match with the {Approval} events.
/// For best performance, return a compile-time constant for zero-cost abstraction.
function _givePermit2InfiniteAllowance() internal view virtual returns (bool) {
return true;
}
}// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.25;
import {OwnableRoles} from "solady/auth/OwnableRoles.sol";
import {IRoleManager} from "./IRoleManager.sol";
/**
* @title RoleManager
* @notice Central role management contract for the Fountfi protocol
* @dev Uses hierarchical bitmasks for core roles. Owner/PROTOCOL_ADMIN have override.
*/
contract RoleManager is OwnableRoles, IRoleManager {
/*//////////////////////////////////////////////////////////////
ROLE DEFINITIONS
//////////////////////////////////////////////////////////////*/
uint256 public constant PROTOCOL_ADMIN = 1 << 1; // Bit 1 = Protocol Admin Authority
uint256 public constant STRATEGY_ADMIN = 1 << 2; // Bit 2 = Strategy Admin Authority
uint256 public constant RULES_ADMIN = 1 << 3; // Bit 3 = Rules Admin Authority
uint256 public constant STRATEGY_OPERATOR = 1 << 4; // Bit 4 = Strategy Operator Authority
uint256 public constant KYC_OPERATOR = 1 << 5; // Bit 5 = KYC Operator Authority
/*//////////////////////////////////////////////////////////////
STATE
//////////////////////////////////////////////////////////////*/
/// @notice Mapping from a target role to the specific (admin) role required to manage it.
/// @dev If a role maps to 0, only owner or PROTOCOL_ADMIN can manage it.
mapping(uint256 => uint256) public roleAdminRole;
/// @notice The address of the registry contract, used as global reference
address public registry;
/*//////////////////////////////////////////////////////////////
INITIALIZATION
//////////////////////////////////////////////////////////////*/
/**
* @notice Constructor that sets up the initial roles
* @dev Initializes the owner and grants all roles to the deployer
*/
constructor() {
_initializeOwner(msg.sender);
// Grant all roles to deployer
uint256 rolesAll = PROTOCOL_ADMIN | STRATEGY_ADMIN | RULES_ADMIN;
_grantRoles(msg.sender, rolesAll);
// Emit event for easier off-chain tracking
emit RoleGranted(msg.sender, rolesAll, address(0));
// Set initial management hierarchy
_setInitialAdminRole(STRATEGY_OPERATOR, STRATEGY_ADMIN);
_setInitialAdminRole(KYC_OPERATOR, RULES_ADMIN);
_setInitialAdminRole(STRATEGY_ADMIN, PROTOCOL_ADMIN);
_setInitialAdminRole(RULES_ADMIN, PROTOCOL_ADMIN);
}
/**
* @notice Initialize the role manager with the registry contract
* @param _registry The address of the registry
*/
function initializeRegistry(address _registry) external {
if (msg.sender != owner()) revert Unauthorized();
if (registry != address(0)) revert AlreadyInitialized();
if (_registry == address(0)) revert ZeroAddress();
registry = _registry;
}
/*//////////////////////////////////////////////////////////////
PUBLIC FUNCTIONS
//////////////////////////////////////////////////////////////*/
/**
* @notice Grants a role to a user
* @param user The address of the user to grant the role to
* @param role The role to grant
*/
function grantRole(address user, uint256 role) public virtual override {
// Check authorization using the hierarchical logic
if (!_canManageRole(msg.sender, role)) {
revert Unauthorized();
}
if (role == 0) revert InvalidRole(); // Prevent granting role 0
// Grant the role
_grantRoles(user, role);
// Emit event
emit RoleGranted(user, role, msg.sender);
}
/**
* @notice Revokes a role from a user
* @param user The address of the user to revoke the role from
* @param role The role to revoke
*/
function revokeRole(address user, uint256 role) public virtual override {
// Check authorization using the hierarchical logic
if (!_canManageRole(msg.sender, role)) {
revert Unauthorized();
}
if (role == 0) revert InvalidRole(); // Prevent revoking role 0
// Revoke the role
_removeRoles(user, role);
// Emit event
emit RoleRevoked(user, role, msg.sender);
}
/**
* @notice Sets the specific role required to manage a target role
* @dev Requires the caller to have the PROTOCOL_ADMIN role or be the owner
* @param targetRole The role whose admin role is to be set
* @param adminRole The role that will be required to manage the targetRole
*/
function setRoleAdmin(uint256 targetRole, uint256 adminRole) external virtual {
// Authorization: Only Owner or PROTOCOL_ADMIN
// Use hasAllRoles for the strict check against the composite PROTOCOL_ADMIN role
if (msg.sender != owner() && !hasAllRoles(msg.sender, PROTOCOL_ADMIN)) {
revert Unauthorized();
}
// Prevent managing PROTOCOL_ADMIN itself via this mechanism or setting role 0
if (targetRole == 0 || targetRole == PROTOCOL_ADMIN) revert InvalidRole();
roleAdminRole[targetRole] = adminRole;
emit RoleAdminSet(targetRole, adminRole, msg.sender);
}
/*//////////////////////////////////////////////////////////////
INTERNAL FUNCTIONS
//////////////////////////////////////////////////////////////*/
/**
* @notice Internal function to check if an address can manage a specific role
* @dev Leverages hierarchical bitmasks. Manager must possess all target role bits plus additional bits.
* @param manager The address to check for management permission
* @param role The role being managed
* @return True if the manager can grant/revoke the role
*/
function _canManageRole(address manager, uint256 role) internal view virtual returns (bool) {
// Owner can always manage any role.
if (manager == owner()) {
return true;
}
// PROTOCOL_ADMIN can manage any role *except* PROTOCOL_ADMIN itself.
if (hasAllRoles(manager, PROTOCOL_ADMIN)) {
return role != PROTOCOL_ADMIN;
}
// --- Check Explicit Mapping ---
uint256 requiredAdminRole = roleAdminRole[role];
return requiredAdminRole != 0 && hasAllRoles(manager, requiredAdminRole);
}
/**
* @notice Internal helper to set initial admin roles during construction
* @dev Does not perform authorization checks.
* @param targetRole The role whose admin role is to be set
* @param adminRole The role that will be required to manage the targetRole
*/
function _setInitialAdminRole(uint256 targetRole, uint256 adminRole) internal {
roleAdminRole[targetRole] = adminRole;
// Emit event with contract address as sender for setup clarity
emit RoleAdminSet(targetRole, adminRole, address(this));
}
}// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.25;
import {RoleManager} from "./RoleManager.sol";
/**
* @title LibRoleManaged
* @notice Logical library for role-managed contracts. Can be inherited by
* both deployable and cloneable versions of RoleManaged.
*/
abstract contract LibRoleManaged {
/*//////////////////////////////////////////////////////////////
ERRORS
//////////////////////////////////////////////////////////////*/
error UnauthorizedRole(address caller, uint256 roleRequired);
/*//////////////////////////////////////////////////////////////
STATE
//////////////////////////////////////////////////////////////*/
/// @notice The role manager contract
RoleManager public roleManager;
/*//////////////////////////////////////////////////////////////
ROLE MANAGED LOGIC
//////////////////////////////////////////////////////////////*/
/**
* @notice Get the registry contract
* @return The address of the registry contract
*/
function registry() public view returns (address) {
return roleManager.registry();
}
/**
* @notice Modifier to restrict access to addresses with a specific role
* @param role The role required to access the function
*/
modifier onlyRoles(uint256 role) {
if (!roleManager.hasAnyRole(msg.sender, role)) {
revert UnauthorizedRole(msg.sender, role);
}
_;
}
}// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import {Ownable} from "./Ownable.sol";
/// @notice Simple single owner and multiroles authorization mixin.
/// @author Solady (https://github.com/vectorized/solady/blob/main/src/auth/OwnableRoles.sol)
///
/// @dev Note:
/// This implementation does NOT auto-initialize the owner to `msg.sender`.
/// You MUST call the `_initializeOwner` in the constructor / initializer.
///
/// While the ownable portion follows
/// [EIP-173](https://eips.ethereum.org/EIPS/eip-173) for compatibility,
/// the nomenclature for the 2-step ownership handover may be unique to this codebase.
abstract contract OwnableRoles is Ownable {
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* EVENTS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev The `user`'s roles is updated to `roles`.
/// Each bit of `roles` represents whether the role is set.
event RolesUpdated(address indexed user, uint256 indexed roles);
/// @dev `keccak256(bytes("RolesUpdated(address,uint256)"))`.
uint256 private constant _ROLES_UPDATED_EVENT_SIGNATURE =
0x715ad5ce61fc9595c7b415289d59cf203f23a94fa06f04af7e489a0a76e1fe26;
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* STORAGE */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev The role slot of `user` is given by:
/// ```
/// mstore(0x00, or(shl(96, user), _ROLE_SLOT_SEED))
/// let roleSlot := keccak256(0x00, 0x20)
/// ```
/// This automatically ignores the upper bits of the `user` in case
/// they are not clean, as well as keep the `keccak256` under 32-bytes.
///
/// Note: This is equivalent to `uint32(bytes4(keccak256("_OWNER_SLOT_NOT")))`.
uint256 private constant _ROLE_SLOT_SEED = 0x8b78c6d8;
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* INTERNAL FUNCTIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Overwrite the roles directly without authorization guard.
function _setRoles(address user, uint256 roles) internal virtual {
/// @solidity memory-safe-assembly
assembly {
mstore(0x0c, _ROLE_SLOT_SEED)
mstore(0x00, user)
// Store the new value.
sstore(keccak256(0x0c, 0x20), roles)
// Emit the {RolesUpdated} event.
log3(0, 0, _ROLES_UPDATED_EVENT_SIGNATURE, shr(96, mload(0x0c)), roles)
}
}
/// @dev Updates the roles directly without authorization guard.
/// If `on` is true, each set bit of `roles` will be turned on,
/// otherwise, each set bit of `roles` will be turned off.
function _updateRoles(address user, uint256 roles, bool on) internal virtual {
/// @solidity memory-safe-assembly
assembly {
mstore(0x0c, _ROLE_SLOT_SEED)
mstore(0x00, user)
let roleSlot := keccak256(0x0c, 0x20)
// Load the current value.
let current := sload(roleSlot)
// Compute the updated roles if `on` is true.
let updated := or(current, roles)
// Compute the updated roles if `on` is false.
// Use `and` to compute the intersection of `current` and `roles`,
// `xor` it with `current` to flip the bits in the intersection.
if iszero(on) { updated := xor(current, and(current, roles)) }
// Then, store the new value.
sstore(roleSlot, updated)
// Emit the {RolesUpdated} event.
log3(0, 0, _ROLES_UPDATED_EVENT_SIGNATURE, shr(96, mload(0x0c)), updated)
}
}
/// @dev Grants the roles directly without authorization guard.
/// Each bit of `roles` represents the role to turn on.
function _grantRoles(address user, uint256 roles) internal virtual {
_updateRoles(user, roles, true);
}
/// @dev Removes the roles directly without authorization guard.
/// Each bit of `roles` represents the role to turn off.
function _removeRoles(address user, uint256 roles) internal virtual {
_updateRoles(user, roles, false);
}
/// @dev Throws if the sender does not have any of the `roles`.
function _checkRoles(uint256 roles) internal view virtual {
/// @solidity memory-safe-assembly
assembly {
// Compute the role slot.
mstore(0x0c, _ROLE_SLOT_SEED)
mstore(0x00, caller())
// Load the stored value, and if the `and` intersection
// of the value and `roles` is zero, revert.
if iszero(and(sload(keccak256(0x0c, 0x20)), roles)) {
mstore(0x00, 0x82b42900) // `Unauthorized()`.
revert(0x1c, 0x04)
}
}
}
/// @dev Throws if the sender is not the owner,
/// and does not have any of the `roles`.
/// Checks for ownership first, then lazily checks for roles.
function _checkOwnerOrRoles(uint256 roles) internal view virtual {
/// @solidity memory-safe-assembly
assembly {
// If the caller is not the stored owner.
// Note: `_ROLE_SLOT_SEED` is equal to `_OWNER_SLOT_NOT`.
if iszero(eq(caller(), sload(not(_ROLE_SLOT_SEED)))) {
// Compute the role slot.
mstore(0x0c, _ROLE_SLOT_SEED)
mstore(0x00, caller())
// Load the stored value, and if the `and` intersection
// of the value and `roles` is zero, revert.
if iszero(and(sload(keccak256(0x0c, 0x20)), roles)) {
mstore(0x00, 0x82b42900) // `Unauthorized()`.
revert(0x1c, 0x04)
}
}
}
}
/// @dev Throws if the sender does not have any of the `roles`,
/// and is not the owner.
/// Checks for roles first, then lazily checks for ownership.
function _checkRolesOrOwner(uint256 roles) internal view virtual {
/// @solidity memory-safe-assembly
assembly {
// Compute the role slot.
mstore(0x0c, _ROLE_SLOT_SEED)
mstore(0x00, caller())
// Load the stored value, and if the `and` intersection
// of the value and `roles` is zero, revert.
if iszero(and(sload(keccak256(0x0c, 0x20)), roles)) {
// If the caller is not the stored owner.
// Note: `_ROLE_SLOT_SEED` is equal to `_OWNER_SLOT_NOT`.
if iszero(eq(caller(), sload(not(_ROLE_SLOT_SEED)))) {
mstore(0x00, 0x82b42900) // `Unauthorized()`.
revert(0x1c, 0x04)
}
}
}
}
/// @dev Convenience function to return a `roles` bitmap from an array of `ordinals`.
/// This is meant for frontends like Etherscan, and is therefore not fully optimized.
/// Not recommended to be called on-chain.
/// Made internal to conserve bytecode. Wrap it in a public function if needed.
function _rolesFromOrdinals(uint8[] memory ordinals) internal pure returns (uint256 roles) {
/// @solidity memory-safe-assembly
assembly {
for { let i := shl(5, mload(ordinals)) } i { i := sub(i, 0x20) } {
// We don't need to mask the values of `ordinals`, as Solidity
// cleans dirty upper bits when storing variables into memory.
roles := or(shl(mload(add(ordinals, i)), 1), roles)
}
}
}
/// @dev Convenience function to return an array of `ordinals` from the `roles` bitmap.
/// This is meant for frontends like Etherscan, and is therefore not fully optimized.
/// Not recommended to be called on-chain.
/// Made internal to conserve bytecode. Wrap it in a public function if needed.
function _ordinalsFromRoles(uint256 roles) internal pure returns (uint8[] memory ordinals) {
/// @solidity memory-safe-assembly
assembly {
// Grab the pointer to the free memory.
ordinals := mload(0x40)
let ptr := add(ordinals, 0x20)
let o := 0
// The absence of lookup tables, De Bruijn, etc., here is intentional for
// smaller bytecode, as this function is not meant to be called on-chain.
for { let t := roles } 1 {} {
mstore(ptr, o)
// `shr` 5 is equivalent to multiplying by 0x20.
// Push back into the ordinals array if the bit is set.
ptr := add(ptr, shl(5, and(t, 1)))
o := add(o, 1)
t := shr(o, roles)
if iszero(t) { break }
}
// Store the length of `ordinals`.
mstore(ordinals, shr(5, sub(ptr, add(ordinals, 0x20))))
// Allocate the memory.
mstore(0x40, ptr)
}
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* PUBLIC UPDATE FUNCTIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Allows the owner to grant `user` `roles`.
/// If the `user` already has a role, then it will be an no-op for the role.
function grantRoles(address user, uint256 roles) public payable virtual onlyOwner {
_grantRoles(user, roles);
}
/// @dev Allows the owner to remove `user` `roles`.
/// If the `user` does not have a role, then it will be an no-op for the role.
function revokeRoles(address user, uint256 roles) public payable virtual onlyOwner {
_removeRoles(user, roles);
}
/// @dev Allow the caller to remove their own roles.
/// If the caller does not have a role, then it will be an no-op for the role.
function renounceRoles(uint256 roles) public payable virtual {
_removeRoles(msg.sender, roles);
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* PUBLIC READ FUNCTIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Returns the roles of `user`.
function rolesOf(address user) public view virtual returns (uint256 roles) {
/// @solidity memory-safe-assembly
assembly {
// Compute the role slot.
mstore(0x0c, _ROLE_SLOT_SEED)
mstore(0x00, user)
// Load the stored value.
roles := sload(keccak256(0x0c, 0x20))
}
}
/// @dev Returns whether `user` has any of `roles`.
function hasAnyRole(address user, uint256 roles) public view virtual returns (bool) {
return rolesOf(user) & roles != 0;
}
/// @dev Returns whether `user` has all of `roles`.
function hasAllRoles(address user, uint256 roles) public view virtual returns (bool) {
return rolesOf(user) & roles == roles;
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* MODIFIERS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Marks a function as only callable by an account with `roles`.
modifier onlyRoles(uint256 roles) virtual {
_checkRoles(roles);
_;
}
/// @dev Marks a function as only callable by the owner or by an account
/// with `roles`. Checks for ownership first, then lazily checks for roles.
modifier onlyOwnerOrRoles(uint256 roles) virtual {
_checkOwnerOrRoles(roles);
_;
}
/// @dev Marks a function as only callable by an account with `roles`
/// or the owner. Checks for roles first, then lazily checks for ownership.
modifier onlyRolesOrOwner(uint256 roles) virtual {
_checkRolesOrOwner(roles);
_;
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* ROLE CONSTANTS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
// IYKYK
uint256 internal constant _ROLE_0 = 1 << 0;
uint256 internal constant _ROLE_1 = 1 << 1;
uint256 internal constant _ROLE_2 = 1 << 2;
uint256 internal constant _ROLE_3 = 1 << 3;
uint256 internal constant _ROLE_4 = 1 << 4;
uint256 internal constant _ROLE_5 = 1 << 5;
uint256 internal constant _ROLE_6 = 1 << 6;
uint256 internal constant _ROLE_7 = 1 << 7;
uint256 internal constant _ROLE_8 = 1 << 8;
uint256 internal constant _ROLE_9 = 1 << 9;
uint256 internal constant _ROLE_10 = 1 << 10;
uint256 internal constant _ROLE_11 = 1 << 11;
uint256 internal constant _ROLE_12 = 1 << 12;
uint256 internal constant _ROLE_13 = 1 << 13;
uint256 internal constant _ROLE_14 = 1 << 14;
uint256 internal constant _ROLE_15 = 1 << 15;
uint256 internal constant _ROLE_16 = 1 << 16;
uint256 internal constant _ROLE_17 = 1 << 17;
uint256 internal constant _ROLE_18 = 1 << 18;
uint256 internal constant _ROLE_19 = 1 << 19;
uint256 internal constant _ROLE_20 = 1 << 20;
uint256 internal constant _ROLE_21 = 1 << 21;
uint256 internal constant _ROLE_22 = 1 << 22;
uint256 internal constant _ROLE_23 = 1 << 23;
uint256 internal constant _ROLE_24 = 1 << 24;
uint256 internal constant _ROLE_25 = 1 << 25;
uint256 internal constant _ROLE_26 = 1 << 26;
uint256 internal constant _ROLE_27 = 1 << 27;
uint256 internal constant _ROLE_28 = 1 << 28;
uint256 internal constant _ROLE_29 = 1 << 29;
uint256 internal constant _ROLE_30 = 1 << 30;
uint256 internal constant _ROLE_31 = 1 << 31;
uint256 internal constant _ROLE_32 = 1 << 32;
uint256 internal constant _ROLE_33 = 1 << 33;
uint256 internal constant _ROLE_34 = 1 << 34;
uint256 internal constant _ROLE_35 = 1 << 35;
uint256 internal constant _ROLE_36 = 1 << 36;
uint256 internal constant _ROLE_37 = 1 << 37;
uint256 internal constant _ROLE_38 = 1 << 38;
uint256 internal constant _ROLE_39 = 1 << 39;
uint256 internal constant _ROLE_40 = 1 << 40;
uint256 internal constant _ROLE_41 = 1 << 41;
uint256 internal constant _ROLE_42 = 1 << 42;
uint256 internal constant _ROLE_43 = 1 << 43;
uint256 internal constant _ROLE_44 = 1 << 44;
uint256 internal constant _ROLE_45 = 1 << 45;
uint256 internal constant _ROLE_46 = 1 << 46;
uint256 internal constant _ROLE_47 = 1 << 47;
uint256 internal constant _ROLE_48 = 1 << 48;
uint256 internal constant _ROLE_49 = 1 << 49;
uint256 internal constant _ROLE_50 = 1 << 50;
uint256 internal constant _ROLE_51 = 1 << 51;
uint256 internal constant _ROLE_52 = 1 << 52;
uint256 internal constant _ROLE_53 = 1 << 53;
uint256 internal constant _ROLE_54 = 1 << 54;
uint256 internal constant _ROLE_55 = 1 << 55;
uint256 internal constant _ROLE_56 = 1 << 56;
uint256 internal constant _ROLE_57 = 1 << 57;
uint256 internal constant _ROLE_58 = 1 << 58;
uint256 internal constant _ROLE_59 = 1 << 59;
uint256 internal constant _ROLE_60 = 1 << 60;
uint256 internal constant _ROLE_61 = 1 << 61;
uint256 internal constant _ROLE_62 = 1 << 62;
uint256 internal constant _ROLE_63 = 1 << 63;
uint256 internal constant _ROLE_64 = 1 << 64;
uint256 internal constant _ROLE_65 = 1 << 65;
uint256 internal constant _ROLE_66 = 1 << 66;
uint256 internal constant _ROLE_67 = 1 << 67;
uint256 internal constant _ROLE_68 = 1 << 68;
uint256 internal constant _ROLE_69 = 1 << 69;
uint256 internal constant _ROLE_70 = 1 << 70;
uint256 internal constant _ROLE_71 = 1 << 71;
uint256 internal constant _ROLE_72 = 1 << 72;
uint256 internal constant _ROLE_73 = 1 << 73;
uint256 internal constant _ROLE_74 = 1 << 74;
uint256 internal constant _ROLE_75 = 1 << 75;
uint256 internal constant _ROLE_76 = 1 << 76;
uint256 internal constant _ROLE_77 = 1 << 77;
uint256 internal constant _ROLE_78 = 1 << 78;
uint256 internal constant _ROLE_79 = 1 << 79;
uint256 internal constant _ROLE_80 = 1 << 80;
uint256 internal constant _ROLE_81 = 1 << 81;
uint256 internal constant _ROLE_82 = 1 << 82;
uint256 internal constant _ROLE_83 = 1 << 83;
uint256 internal constant _ROLE_84 = 1 << 84;
uint256 internal constant _ROLE_85 = 1 << 85;
uint256 internal constant _ROLE_86 = 1 << 86;
uint256 internal constant _ROLE_87 = 1 << 87;
uint256 internal constant _ROLE_88 = 1 << 88;
uint256 internal constant _ROLE_89 = 1 << 89;
uint256 internal constant _ROLE_90 = 1 << 90;
uint256 internal constant _ROLE_91 = 1 << 91;
uint256 internal constant _ROLE_92 = 1 << 92;
uint256 internal constant _ROLE_93 = 1 << 93;
uint256 internal constant _ROLE_94 = 1 << 94;
uint256 internal constant _ROLE_95 = 1 << 95;
uint256 internal constant _ROLE_96 = 1 << 96;
uint256 internal constant _ROLE_97 = 1 << 97;
uint256 internal constant _ROLE_98 = 1 << 98;
uint256 internal constant _ROLE_99 = 1 << 99;
uint256 internal constant _ROLE_100 = 1 << 100;
uint256 internal constant _ROLE_101 = 1 << 101;
uint256 internal constant _ROLE_102 = 1 << 102;
uint256 internal constant _ROLE_103 = 1 << 103;
uint256 internal constant _ROLE_104 = 1 << 104;
uint256 internal constant _ROLE_105 = 1 << 105;
uint256 internal constant _ROLE_106 = 1 << 106;
uint256 internal constant _ROLE_107 = 1 << 107;
uint256 internal constant _ROLE_108 = 1 << 108;
uint256 internal constant _ROLE_109 = 1 << 109;
uint256 internal constant _ROLE_110 = 1 << 110;
uint256 internal constant _ROLE_111 = 1 << 111;
uint256 internal constant _ROLE_112 = 1 << 112;
uint256 internal constant _ROLE_113 = 1 << 113;
uint256 internal constant _ROLE_114 = 1 << 114;
uint256 internal constant _ROLE_115 = 1 << 115;
uint256 internal constant _ROLE_116 = 1 << 116;
uint256 internal constant _ROLE_117 = 1 << 117;
uint256 internal constant _ROLE_118 = 1 << 118;
uint256 internal constant _ROLE_119 = 1 << 119;
uint256 internal constant _ROLE_120 = 1 << 120;
uint256 internal constant _ROLE_121 = 1 << 121;
uint256 internal constant _ROLE_122 = 1 << 122;
uint256 internal constant _ROLE_123 = 1 << 123;
uint256 internal constant _ROLE_124 = 1 << 124;
uint256 internal constant _ROLE_125 = 1 << 125;
uint256 internal constant _ROLE_126 = 1 << 126;
uint256 internal constant _ROLE_127 = 1 << 127;
uint256 internal constant _ROLE_128 = 1 << 128;
uint256 internal constant _ROLE_129 = 1 << 129;
uint256 internal constant _ROLE_130 = 1 << 130;
uint256 internal constant _ROLE_131 = 1 << 131;
uint256 internal constant _ROLE_132 = 1 << 132;
uint256 internal constant _ROLE_133 = 1 << 133;
uint256 internal constant _ROLE_134 = 1 << 134;
uint256 internal constant _ROLE_135 = 1 << 135;
uint256 internal constant _ROLE_136 = 1 << 136;
uint256 internal constant _ROLE_137 = 1 << 137;
uint256 internal constant _ROLE_138 = 1 << 138;
uint256 internal constant _ROLE_139 = 1 << 139;
uint256 internal constant _ROLE_140 = 1 << 140;
uint256 internal constant _ROLE_141 = 1 << 141;
uint256 internal constant _ROLE_142 = 1 << 142;
uint256 internal constant _ROLE_143 = 1 << 143;
uint256 internal constant _ROLE_144 = 1 << 144;
uint256 internal constant _ROLE_145 = 1 << 145;
uint256 internal constant _ROLE_146 = 1 << 146;
uint256 internal constant _ROLE_147 = 1 << 147;
uint256 internal constant _ROLE_148 = 1 << 148;
uint256 internal constant _ROLE_149 = 1 << 149;
uint256 internal constant _ROLE_150 = 1 << 150;
uint256 internal constant _ROLE_151 = 1 << 151;
uint256 internal constant _ROLE_152 = 1 << 152;
uint256 internal constant _ROLE_153 = 1 << 153;
uint256 internal constant _ROLE_154 = 1 << 154;
uint256 internal constant _ROLE_155 = 1 << 155;
uint256 internal constant _ROLE_156 = 1 << 156;
uint256 internal constant _ROLE_157 = 1 << 157;
uint256 internal constant _ROLE_158 = 1 << 158;
uint256 internal constant _ROLE_159 = 1 << 159;
uint256 internal constant _ROLE_160 = 1 << 160;
uint256 internal constant _ROLE_161 = 1 << 161;
uint256 internal constant _ROLE_162 = 1 << 162;
uint256 internal constant _ROLE_163 = 1 << 163;
uint256 internal constant _ROLE_164 = 1 << 164;
uint256 internal constant _ROLE_165 = 1 << 165;
uint256 internal constant _ROLE_166 = 1 << 166;
uint256 internal constant _ROLE_167 = 1 << 167;
uint256 internal constant _ROLE_168 = 1 << 168;
uint256 internal constant _ROLE_169 = 1 << 169;
uint256 internal constant _ROLE_170 = 1 << 170;
uint256 internal constant _ROLE_171 = 1 << 171;
uint256 internal constant _ROLE_172 = 1 << 172;
uint256 internal constant _ROLE_173 = 1 << 173;
uint256 internal constant _ROLE_174 = 1 << 174;
uint256 internal constant _ROLE_175 = 1 << 175;
uint256 internal constant _ROLE_176 = 1 << 176;
uint256 internal constant _ROLE_177 = 1 << 177;
uint256 internal constant _ROLE_178 = 1 << 178;
uint256 internal constant _ROLE_179 = 1 << 179;
uint256 internal constant _ROLE_180 = 1 << 180;
uint256 internal constant _ROLE_181 = 1 << 181;
uint256 internal constant _ROLE_182 = 1 << 182;
uint256 internal constant _ROLE_183 = 1 << 183;
uint256 internal constant _ROLE_184 = 1 << 184;
uint256 internal constant _ROLE_185 = 1 << 185;
uint256 internal constant _ROLE_186 = 1 << 186;
uint256 internal constant _ROLE_187 = 1 << 187;
uint256 internal constant _ROLE_188 = 1 << 188;
uint256 internal constant _ROLE_189 = 1 << 189;
uint256 internal constant _ROLE_190 = 1 << 190;
uint256 internal constant _ROLE_191 = 1 << 191;
uint256 internal constant _ROLE_192 = 1 << 192;
uint256 internal constant _ROLE_193 = 1 << 193;
uint256 internal constant _ROLE_194 = 1 << 194;
uint256 internal constant _ROLE_195 = 1 << 195;
uint256 internal constant _ROLE_196 = 1 << 196;
uint256 internal constant _ROLE_197 = 1 << 197;
uint256 internal constant _ROLE_198 = 1 << 198;
uint256 internal constant _ROLE_199 = 1 << 199;
uint256 internal constant _ROLE_200 = 1 << 200;
uint256 internal constant _ROLE_201 = 1 << 201;
uint256 internal constant _ROLE_202 = 1 << 202;
uint256 internal constant _ROLE_203 = 1 << 203;
uint256 internal constant _ROLE_204 = 1 << 204;
uint256 internal constant _ROLE_205 = 1 << 205;
uint256 internal constant _ROLE_206 = 1 << 206;
uint256 internal constant _ROLE_207 = 1 << 207;
uint256 internal constant _ROLE_208 = 1 << 208;
uint256 internal constant _ROLE_209 = 1 << 209;
uint256 internal constant _ROLE_210 = 1 << 210;
uint256 internal constant _ROLE_211 = 1 << 211;
uint256 internal constant _ROLE_212 = 1 << 212;
uint256 internal constant _ROLE_213 = 1 << 213;
uint256 internal constant _ROLE_214 = 1 << 214;
uint256 internal constant _ROLE_215 = 1 << 215;
uint256 internal constant _ROLE_216 = 1 << 216;
uint256 internal constant _ROLE_217 = 1 << 217;
uint256 internal constant _ROLE_218 = 1 << 218;
uint256 internal constant _ROLE_219 = 1 << 219;
uint256 internal constant _ROLE_220 = 1 << 220;
uint256 internal constant _ROLE_221 = 1 << 221;
uint256 internal constant _ROLE_222 = 1 << 222;
uint256 internal constant _ROLE_223 = 1 << 223;
uint256 internal constant _ROLE_224 = 1 << 224;
uint256 internal constant _ROLE_225 = 1 << 225;
uint256 internal constant _ROLE_226 = 1 << 226;
uint256 internal constant _ROLE_227 = 1 << 227;
uint256 internal constant _ROLE_228 = 1 << 228;
uint256 internal constant _ROLE_229 = 1 << 229;
uint256 internal constant _ROLE_230 = 1 << 230;
uint256 internal constant _ROLE_231 = 1 << 231;
uint256 internal constant _ROLE_232 = 1 << 232;
uint256 internal constant _ROLE_233 = 1 << 233;
uint256 internal constant _ROLE_234 = 1 << 234;
uint256 internal constant _ROLE_235 = 1 << 235;
uint256 internal constant _ROLE_236 = 1 << 236;
uint256 internal constant _ROLE_237 = 1 << 237;
uint256 internal constant _ROLE_238 = 1 << 238;
uint256 internal constant _ROLE_239 = 1 << 239;
uint256 internal constant _ROLE_240 = 1 << 240;
uint256 internal constant _ROLE_241 = 1 << 241;
uint256 internal constant _ROLE_242 = 1 << 242;
uint256 internal constant _ROLE_243 = 1 << 243;
uint256 internal constant _ROLE_244 = 1 << 244;
uint256 internal constant _ROLE_245 = 1 << 245;
uint256 internal constant _ROLE_246 = 1 << 246;
uint256 internal constant _ROLE_247 = 1 << 247;
uint256 internal constant _ROLE_248 = 1 << 248;
uint256 internal constant _ROLE_249 = 1 << 249;
uint256 internal constant _ROLE_250 = 1 << 250;
uint256 internal constant _ROLE_251 = 1 << 251;
uint256 internal constant _ROLE_252 = 1 << 252;
uint256 internal constant _ROLE_253 = 1 << 253;
uint256 internal constant _ROLE_254 = 1 << 254;
uint256 internal constant _ROLE_255 = 1 << 255;
}// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.25;
/**
* @title IRoleManager
* @notice Interface for the RoleManager contract
*/
interface IRoleManager {
/*//////////////////////////////////////////////////////////////
ERRORS
//////////////////////////////////////////////////////////////*/
/// @notice Emitted for 0 role in arguments
error InvalidRole();
/// @notice Emitted for 0 address in arguments
error ZeroAddress();
/*//////////////////////////////////////////////////////////////
EVENTS
//////////////////////////////////////////////////////////////*/
/**
* @notice Emitted when a role is granted to a user
* @param user The address of the user
* @param role The role that was granted
* @param sender The address that granted the role
*/
event RoleGranted(address indexed user, uint256 indexed role, address indexed sender);
/**
* @notice Emitted when a role is revoked from a user
* @param user The address of the user
* @param role The role that was revoked
* @param sender The address that revoked the role
*/
event RoleRevoked(address indexed user, uint256 indexed role, address indexed sender);
/**
* @notice Emitted when the admin role for a target role is updated.
* @param targetRole The role whose admin is being changed.
* @param adminRole The new role required to manage the targetRole (0 means revert to owner/PROTOCOL_ADMIN).
* @param sender The address that performed the change.
*/
event RoleAdminSet(uint256 indexed targetRole, uint256 indexed adminRole, address indexed sender);
/*//////////////////////////////////////////////////////////////
FUNCTIONS
//////////////////////////////////////////////////////////////*/
/// @notice Grants a role to a user
/// @param user The address of the user to grant the role to
/// @param role The role to grant
function grantRole(address user, uint256 role) external;
/// @notice Revokes a role from a user
/// @param user The address of the user to revoke the role from
/// @param role The role to revoke
function revokeRole(address user, uint256 role) external;
/// @notice Sets the specific role required to manage a target role.
/// @dev Requires the caller to have the PROTOCOL_ADMIN role or be the owner.
/// @param targetRole The role whose admin role is to be set. Cannot be PROTOCOL_ADMIN.
/// @param adminRole The role that will be required to manage the targetRole. Set to 0 to require owner/PROTOCOL_ADMIN.
function setRoleAdmin(uint256 targetRole, uint256 adminRole) external;
}// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
/// @notice Simple single owner authorization mixin.
/// @author Solady (https://github.com/vectorized/solady/blob/main/src/auth/Ownable.sol)
///
/// @dev Note:
/// This implementation does NOT auto-initialize the owner to `msg.sender`.
/// You MUST call the `_initializeOwner` in the constructor / initializer.
///
/// While the ownable portion follows
/// [EIP-173](https://eips.ethereum.org/EIPS/eip-173) for compatibility,
/// the nomenclature for the 2-step ownership handover may be unique to this codebase.
abstract contract Ownable {
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CUSTOM ERRORS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev The caller is not authorized to call the function.
error Unauthorized();
/// @dev The `newOwner` cannot be the zero address.
error NewOwnerIsZeroAddress();
/// @dev The `pendingOwner` does not have a valid handover request.
error NoHandoverRequest();
/// @dev Cannot double-initialize.
error AlreadyInitialized();
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* EVENTS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev The ownership is transferred from `oldOwner` to `newOwner`.
/// This event is intentionally kept the same as OpenZeppelin's Ownable to be
/// compatible with indexers and [EIP-173](https://eips.ethereum.org/EIPS/eip-173),
/// despite it not being as lightweight as a single argument event.
event OwnershipTransferred(address indexed oldOwner, address indexed newOwner);
/// @dev An ownership handover to `pendingOwner` has been requested.
event OwnershipHandoverRequested(address indexed pendingOwner);
/// @dev The ownership handover to `pendingOwner` has been canceled.
event OwnershipHandoverCanceled(address indexed pendingOwner);
/// @dev `keccak256(bytes("OwnershipTransferred(address,address)"))`.
uint256 private constant _OWNERSHIP_TRANSFERRED_EVENT_SIGNATURE =
0x8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e0;
/// @dev `keccak256(bytes("OwnershipHandoverRequested(address)"))`.
uint256 private constant _OWNERSHIP_HANDOVER_REQUESTED_EVENT_SIGNATURE =
0xdbf36a107da19e49527a7176a1babf963b4b0ff8cde35ee35d6cd8f1f9ac7e1d;
/// @dev `keccak256(bytes("OwnershipHandoverCanceled(address)"))`.
uint256 private constant _OWNERSHIP_HANDOVER_CANCELED_EVENT_SIGNATURE =
0xfa7b8eab7da67f412cc9575ed43464468f9bfbae89d1675917346ca6d8fe3c92;
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* STORAGE */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev The owner slot is given by:
/// `bytes32(~uint256(uint32(bytes4(keccak256("_OWNER_SLOT_NOT")))))`.
/// It is intentionally chosen to be a high value
/// to avoid collision with lower slots.
/// The choice of manual storage layout is to enable compatibility
/// with both regular and upgradeable contracts.
bytes32 internal constant _OWNER_SLOT =
0xffffffffffffffffffffffffffffffffffffffffffffffffffffffff74873927;
/// The ownership handover slot of `newOwner` is given by:
/// ```
/// mstore(0x00, or(shl(96, user), _HANDOVER_SLOT_SEED))
/// let handoverSlot := keccak256(0x00, 0x20)
/// ```
/// It stores the expiry timestamp of the two-step ownership handover.
uint256 private constant _HANDOVER_SLOT_SEED = 0x389a75e1;
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* INTERNAL FUNCTIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Override to return true to make `_initializeOwner` prevent double-initialization.
function _guardInitializeOwner() internal pure virtual returns (bool guard) {}
/// @dev Initializes the owner directly without authorization guard.
/// This function must be called upon initialization,
/// regardless of whether the contract is upgradeable or not.
/// This is to enable generalization to both regular and upgradeable contracts,
/// and to save gas in case the initial owner is not the caller.
/// For performance reasons, this function will not check if there
/// is an existing owner.
function _initializeOwner(address newOwner) internal virtual {
if (_guardInitializeOwner()) {
/// @solidity memory-safe-assembly
assembly {
let ownerSlot := _OWNER_SLOT
if sload(ownerSlot) {
mstore(0x00, 0x0dc149f0) // `AlreadyInitialized()`.
revert(0x1c, 0x04)
}
// Clean the upper 96 bits.
newOwner := shr(96, shl(96, newOwner))
// Store the new value.
sstore(ownerSlot, or(newOwner, shl(255, iszero(newOwner))))
// Emit the {OwnershipTransferred} event.
log3(0, 0, _OWNERSHIP_TRANSFERRED_EVENT_SIGNATURE, 0, newOwner)
}
} else {
/// @solidity memory-safe-assembly
assembly {
// Clean the upper 96 bits.
newOwner := shr(96, shl(96, newOwner))
// Store the new value.
sstore(_OWNER_SLOT, newOwner)
// Emit the {OwnershipTransferred} event.
log3(0, 0, _OWNERSHIP_TRANSFERRED_EVENT_SIGNATURE, 0, newOwner)
}
}
}
/// @dev Sets the owner directly without authorization guard.
function _setOwner(address newOwner) internal virtual {
if (_guardInitializeOwner()) {
/// @solidity memory-safe-assembly
assembly {
let ownerSlot := _OWNER_SLOT
// Clean the upper 96 bits.
newOwner := shr(96, shl(96, newOwner))
// Emit the {OwnershipTransferred} event.
log3(0, 0, _OWNERSHIP_TRANSFERRED_EVENT_SIGNATURE, sload(ownerSlot), newOwner)
// Store the new value.
sstore(ownerSlot, or(newOwner, shl(255, iszero(newOwner))))
}
} else {
/// @solidity memory-safe-assembly
assembly {
let ownerSlot := _OWNER_SLOT
// Clean the upper 96 bits.
newOwner := shr(96, shl(96, newOwner))
// Emit the {OwnershipTransferred} event.
log3(0, 0, _OWNERSHIP_TRANSFERRED_EVENT_SIGNATURE, sload(ownerSlot), newOwner)
// Store the new value.
sstore(ownerSlot, newOwner)
}
}
}
/// @dev Throws if the sender is not the owner.
function _checkOwner() internal view virtual {
/// @solidity memory-safe-assembly
assembly {
// If the caller is not the stored owner, revert.
if iszero(eq(caller(), sload(_OWNER_SLOT))) {
mstore(0x00, 0x82b42900) // `Unauthorized()`.
revert(0x1c, 0x04)
}
}
}
/// @dev Returns how long a two-step ownership handover is valid for in seconds.
/// Override to return a different value if needed.
/// Made internal to conserve bytecode. Wrap it in a public function if needed.
function _ownershipHandoverValidFor() internal view virtual returns (uint64) {
return 48 * 3600;
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* PUBLIC UPDATE FUNCTIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Allows the owner to transfer the ownership to `newOwner`.
function transferOwnership(address newOwner) public payable virtual onlyOwner {
/// @solidity memory-safe-assembly
assembly {
if iszero(shl(96, newOwner)) {
mstore(0x00, 0x7448fbae) // `NewOwnerIsZeroAddress()`.
revert(0x1c, 0x04)
}
}
_setOwner(newOwner);
}
/// @dev Allows the owner to renounce their ownership.
function renounceOwnership() public payable virtual onlyOwner {
_setOwner(address(0));
}
/// @dev Request a two-step ownership handover to the caller.
/// The request will automatically expire in 48 hours (172800 seconds) by default.
function requestOwnershipHandover() public payable virtual {
unchecked {
uint256 expires = block.timestamp + _ownershipHandoverValidFor();
/// @solidity memory-safe-assembly
assembly {
// Compute and set the handover slot to `expires`.
mstore(0x0c, _HANDOVER_SLOT_SEED)
mstore(0x00, caller())
sstore(keccak256(0x0c, 0x20), expires)
// Emit the {OwnershipHandoverRequested} event.
log2(0, 0, _OWNERSHIP_HANDOVER_REQUESTED_EVENT_SIGNATURE, caller())
}
}
}
/// @dev Cancels the two-step ownership handover to the caller, if any.
function cancelOwnershipHandover() public payable virtual {
/// @solidity memory-safe-assembly
assembly {
// Compute and set the handover slot to 0.
mstore(0x0c, _HANDOVER_SLOT_SEED)
mstore(0x00, caller())
sstore(keccak256(0x0c, 0x20), 0)
// Emit the {OwnershipHandoverCanceled} event.
log2(0, 0, _OWNERSHIP_HANDOVER_CANCELED_EVENT_SIGNATURE, caller())
}
}
/// @dev Allows the owner to complete the two-step ownership handover to `pendingOwner`.
/// Reverts if there is no existing ownership handover requested by `pendingOwner`.
function completeOwnershipHandover(address pendingOwner) public payable virtual onlyOwner {
/// @solidity memory-safe-assembly
assembly {
// Compute and set the handover slot to 0.
mstore(0x0c, _HANDOVER_SLOT_SEED)
mstore(0x00, pendingOwner)
let handoverSlot := keccak256(0x0c, 0x20)
// If the handover does not exist, or has expired.
if gt(timestamp(), sload(handoverSlot)) {
mstore(0x00, 0x6f5e8818) // `NoHandoverRequest()`.
revert(0x1c, 0x04)
}
// Set the handover slot to 0.
sstore(handoverSlot, 0)
}
_setOwner(pendingOwner);
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* PUBLIC READ FUNCTIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Returns the owner of the contract.
function owner() public view virtual returns (address result) {
/// @solidity memory-safe-assembly
assembly {
result := sload(_OWNER_SLOT)
}
}
/// @dev Returns the expiry timestamp for the two-step ownership handover to `pendingOwner`.
function ownershipHandoverExpiresAt(address pendingOwner)
public
view
virtual
returns (uint256 result)
{
/// @solidity memory-safe-assembly
assembly {
// Compute the handover slot.
mstore(0x0c, _HANDOVER_SLOT_SEED)
mstore(0x00, pendingOwner)
// Load the handover slot.
result := sload(keccak256(0x0c, 0x20))
}
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* MODIFIERS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Marks a function as only callable by the owner.
modifier onlyOwner() virtual {
_checkOwner();
_;
}
}{
"remappings": [
"forge-std/=lib/forge-std/src/",
"solady/=lib/solady/src/",
"@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
"@openzeppelin/contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/contracts/",
"ds-test/=lib/openzeppelin-contracts-upgradeable/lib/forge-std/lib/ds-test/src/",
"erc4626-tests/=lib/openzeppelin-contracts-upgradeable/lib/erc4626-tests/",
"halmos-cheatcodes/=lib/openzeppelin-contracts-upgradeable/lib/halmos-cheatcodes/src/",
"openzeppelin-contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/",
"openzeppelin-contracts/=lib/openzeppelin-contracts/"
],
"optimizer": {
"enabled": true,
"runs": 30
},
"metadata": {
"useLiteralContent": false,
"bytecodeHash": "ipfs",
"appendCBOR": true
},
"outputSelection": {
"*": {
"*": [
"evm.bytecode",
"evm.deployedBytecode",
"devdoc",
"userdoc",
"metadata",
"abi"
]
}
},
"evmVersion": "cancun",
"viaIR": false,
"libraries": {
"src/strategy/BtcVaultStrategy.sol": {
"CollateralManagementLib": "0xce4de370c7743a72b51edce92534bd98ec0246e7",
"CollateralViewLib": "0x7d39c84e9c009c529348c909e3d2c350b130cc07"
}
}
}Contract Security Audit
- No Contract Security Audit Submitted- Submit Audit Here
Contract ABI
API[{"inputs":[],"name":"AlreadyInitialized","type":"error"},{"inputs":[{"internalType":"bytes","name":"returnData","type":"bytes"}],"name":"CallRevert","type":"error"},{"inputs":[],"name":"CannotCallToken","type":"error"},{"inputs":[],"name":"InvalidAddress","type":"error"},{"inputs":[],"name":"InvalidArrayLengths","type":"error"},{"inputs":[],"name":"InvalidReporter","type":"error"},{"inputs":[],"name":"InvalidRoleManager","type":"error"},{"inputs":[],"name":"InvalidRules","type":"error"},{"inputs":[],"name":"TokenAlreadyDeployed","type":"error"},{"inputs":[],"name":"Unauthorized","type":"error"},{"inputs":[{"internalType":"address","name":"caller","type":"address"},{"internalType":"uint256","name":"roleRequired","type":"uint256"}],"name":"UnauthorizedRole","type":"error"},{"inputs":[],"name":"WithdrawInvalidSignature","type":"error"},{"inputs":[],"name":"WithdrawNonceReuse","type":"error"},{"inputs":[],"name":"WithdrawalRequestExpired","type":"error"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"oldAdmin","type":"address"},{"indexed":true,"internalType":"address","name":"newAdmin","type":"address"}],"name":"AdminChange","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"target","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"},{"indexed":false,"internalType":"bytes","name":"data","type":"bytes"}],"name":"Call","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"controller","type":"address"}],"name":"ControllerConfigured","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"oldManager","type":"address"},{"indexed":true,"internalType":"address","name":"newManager","type":"address"}],"name":"ManagerChange","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"oldAdmin","type":"address"},{"indexed":true,"internalType":"address","name":"cancelledAdmin","type":"address"}],"name":"NoAdminChange","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"oldAdmin","type":"address"},{"indexed":true,"internalType":"address","name":"newAdmin","type":"address"}],"name":"PendingAdminChange","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"roleManager","type":"address"}],"name":"RoleManagerInitialized","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"reporter","type":"address"}],"name":"SetReporter","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"admin","type":"address"},{"indexed":true,"internalType":"address","name":"manager","type":"address"},{"indexed":true,"internalType":"address","name":"asset","type":"address"},{"indexed":false,"internalType":"address","name":"sToken","type":"address"}],"name":"StrategyInitialized","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":false,"internalType":"uint96","name":"nonce","type":"uint96"}],"name":"WithdrawalNonceUsed","type":"event"},{"inputs":[],"name":"COLLATERAL_DECIMALS","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"}],"name":"addCollateral","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"addLiquidity","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"approveTokenWithdrawal","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"asset","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"assetDecimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"availableLiquidity","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"balance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"components":[{"internalType":"uint256","name":"shares","type":"uint256"},{"internalType":"uint256","name":"minAssets","type":"uint256"},{"internalType":"address","name":"owner","type":"address"},{"internalType":"uint96","name":"nonce","type":"uint96"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint96","name":"expirationTime","type":"uint96"}],"internalType":"struct ManagedWithdrawReportedStrategy.WithdrawalRequest[]","name":"requests","type":"tuple[]"},{"components":[{"internalType":"uint8","name":"v","type":"uint8"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"internalType":"struct ManagedWithdrawReportedStrategy.Signature[]","name":"signatures","type":"tuple[]"}],"name":"batchRedeem","outputs":[{"internalType":"uint256[]","name":"assets","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"target","type":"address"},{"internalType":"uint256","name":"value","type":"uint256"},{"internalType":"bytes","name":"data","type":"bytes"}],"name":"call","outputs":[{"internalType":"bool","name":"success","type":"bool"},{"internalType":"bytes","name":"returnData","type":"bytes"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes","name":"data","type":"bytes"}],"name":"callStrategyToken","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"}],"name":"collateralBalance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"collateralTokens","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"depositCollateral","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"getSupportedCollaterals","outputs":[{"internalType":"address[]","name":"","type":"address[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"string","name":"name_","type":"string"},{"internalType":"string","name":"symbol_","type":"string"},{"internalType":"address","name":"roleManager_","type":"address"},{"internalType":"address","name":"manager_","type":"address"},{"internalType":"address","name":"sovaBTC_","type":"address"},{"internalType":"uint8","name":"assetDecimals_","type":"uint8"},{"internalType":"bytes","name":"initData","type":"bytes"}],"name":"initialize","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"}],"name":"isSupportedAsset","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"manager","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"pricePerShare","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"tokenAddr","type":"address"},{"internalType":"address","name":"from","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"pullToken","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"uint256","name":"shares","type":"uint256"},{"internalType":"uint256","name":"minAssets","type":"uint256"},{"internalType":"address","name":"owner","type":"address"},{"internalType":"uint96","name":"nonce","type":"uint96"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint96","name":"expirationTime","type":"uint96"}],"internalType":"struct ManagedWithdrawReportedStrategy.WithdrawalRequest","name":"request","type":"tuple"},{"components":[{"internalType":"uint8","name":"v","type":"uint8"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"internalType":"struct ManagedWithdrawReportedStrategy.Signature","name":"userSig","type":"tuple"}],"name":"redeem","outputs":[{"internalType":"uint256","name":"assets","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"registry","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"}],"name":"removeCollateral","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"address","name":"to","type":"address"}],"name":"removeLiquidity","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"reporter","outputs":[{"internalType":"contract IReporter","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"roleManager","outputs":[{"internalType":"contract RoleManager","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"sToken","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"to","type":"address"}],"name":"sendETH","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"tokenAddr","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"sendToken","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"tokenAddr","type":"address"},{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"setAllowance","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newManager","type":"address"}],"name":"setManager","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_reporter","type":"address"}],"name":"setReporter","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"supportedAssets","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalCollateralAssets","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"uint96","name":"","type":"uint96"}],"name":"usedNonces","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"address","name":"to","type":"address"}],"name":"withdrawCollateral","outputs":[],"stateMutability":"nonpayable","type":"function"},{"stateMutability":"payable","type":"receive"}]Contract Creation Code
6080604052348015600e575f80fd5b50615fb68061001c5f395ff3fe6080604052600436106101af575f3560e01c80636dbf2fa0116100e65780636dbf2fa0146104405780636e551f221461046d57806374375359146104995780637b103999146104ad57806399530b06146104c15780639be918e6146104d55780639ebdf12c1461050c578063a1bf28401461052b578063a5d5db0c1461054a578063b69ef8a814610569578063b70e92421461057d578063bd81579e1461059c578063c2d41601146105ca578063c99d3a06146105ea578063d0ebdbe714610609578063da46098c14610628578063e753178114610647578063f0d2d5a81461065b575f80fd5b8062435da5146101ba578063010ec441146101ee57806305db8a691461020d57806305fe138b1461022e578063081932db1461024f578063172c48c7146102985780631f1088a0146102b757806321b1e5f8146102d65780632bf71fe9146102f55780632e144579146103145780632fdcfbd214610333578063309e9a091461035257806338d52e0f1461037157806347597d9c14610390578063481c6a75146103bd5780634c925032146103dc5780634ddde78d146103fb57806351c6590a14610421575f80fd5b366101b657005b5f80fd5b3480156101c5575f80fd5b505f546101d8906001600160a01b031681565b6040516101e591906122ab565b60405180910390f35b3480156101f9575f80fd5b506004546101d8906001600160a01b031681565b348015610218575f80fd5b5061022161067a565b6040516101e59190612302565b348015610239575f80fd5b5061024d610248366004612338565b6106da565b005b34801561025a575f80fd5b5061028861026936600461237c565b600560209081525f928352604080842090915290825290205460ff1681565b60405190151581526020016101e5565b3480156102a3575f80fd5b506101d86102b23660046123af565b610783565b3480156102c2575f80fd5b5061024d6102d13660046123c6565b6107ab565b3480156102e1575f80fd5b5061024d6102f0366004612405565b610857565b348015610300575f80fd5b5061024d61030f366004612539565b61091f565b34801561031f575f80fd5b5061024d61032e36600461260d565b6109ac565b34801561033e575f80fd5b5061024d61034d36600461260d565b6109f0565b34801561035d575f80fd5b5061024d61036c36600461264b565b610a2e565b34801561037c575f80fd5b506002546101d8906001600160a01b031681565b34801561039b575f80fd5b506103af6103aa366004612689565b610c09565b6040519081526020016101e5565b3480156103c8575f80fd5b506001546101d8906001600160a01b031681565b3480156103e7575f80fd5b5061024d6103f6366004612405565b610d01565b348015610406575f80fd5b5061040f600881565b60405160ff90911681526020016101e5565b34801561042c575f80fd5b5061024d61043b3660046123af565b610d9b565b34801561044b575f80fd5b5061045f61045a3660046126cb565b610e37565b6040516101e5929190612750565b348015610478575f80fd5b5061048c610487366004612772565b610f9f565b6040516101e5919061285d565b3480156104a4575f80fd5b506103af61131a565b3480156104b8575f80fd5b506101d86113a4565b3480156104cc575f80fd5b506103af611418565b3480156104e0575f80fd5b506102886104ef366004612405565b6001600160a01b03165f9081526006602052604090205460ff1690565b348015610517575f80fd5b506003546101d8906001600160a01b031681565b348015610536575f80fd5b506103af610545366004612405565b6114a2565b348015610555575f80fd5b5061024d61056436600461286f565b611523565b348015610574575f80fd5b506103af611571565b348015610588575f80fd5b5061024d6105973660046123af565b611726565b3480156105a7575f80fd5b506102886105b6366004612405565b60066020525f908152604090205460ff1681565b3480156105d5575f80fd5b5060025461040f90600160a01b900460ff1681565b3480156105f5575f80fd5b5061024d610604366004612405565b61178d565b348015610614575f80fd5b5061024d610623366004612405565b611811565b348015610633575f80fd5b5061024d61064236600461260d565b611960565b348015610652575f80fd5b506103af61199e565b348015610666575f80fd5b5061024d610675366004612405565b6119d7565b606060078054806020026020016040519081016040528092919081815260200182805480156106d057602002820191905f5260205f20905b81546001600160a01b031681526001909101906020018083116106b2575b5050505050905090565b6001546001600160a01b03163314610704576040516282b42960e81b815260040160405180910390fd5b6002546040516323fede4760e11b81526001600160a01b03918216600482015260248101849052908216604482015273ce4de370c7743a72b51edce92534bd98ec0246e7906347fdbc8e906064015b5f6040518083038186803b158015610769575f80fd5b505af415801561077b573d5f803e3d5ffd5b505050505050565b60078181548110610792575f80fd5b5f918252602090912001546001600160a01b0316905081565b6001546001600160a01b031633146107d5576040516282b42960e81b815260040160405180910390fd5b6040516326be1dc160e11b81526001600160a01b03808516600483015260248201849052821660448201526006606482015273ce4de370c7743a72b51edce92534bd98ec0246e790634d7c3b82906084015f6040518083038186803b15801561083c575f80fd5b505af415801561084e573d5f803e3d5ffd5b50505050505050565b6001546001600160a01b03163314610881576040516282b42960e81b815260040160405180910390fd5b5f816001600160a01b0316476040515f6040518083038185875af1925050503d805f81146108ca576040519150601f19603f3d011682016040523d82523d5f602084013e6108cf565b606091505b505090508061091b5760405162461bcd60e51b8152602060048201526013602482015272115512081d1c985b9cd9995c8819985a5b1959606a1b60448201526064015b60405180910390fd5b5050565b610930898989898989898989611a56565b8160ff1660081461093f575f80fd5b50506001600160a01b03165f818152600660205260408120805460ff191660019081179091556007805491820181559091527fa66cc928b5edb82af9bd49922954155ab7b0942694bea4ce44661d9a8736c6880180546001600160a01b0319169091179055505050505050565b6001546001600160a01b031633146109d6576040516282b42960e81b815260040160405180910390fd5b6109eb6001600160a01b038416833084611a72565b505050565b6001546001600160a01b03163314610a1a576040516282b42960e81b815260040160405180910390fd5b6109eb6001600160a01b0384168383611acb565b5f8054906101000a90046001600160a01b03166001600160a01b0316638992929c6040518163ffffffff1660e01b8152600401602060405180830381865afa158015610a7c573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190610aa09190612899565b5f5460405163145398bf60e21b81526001600160a01b039091169063514e62fc90610ad190339085906004016128b0565b602060405180830381865afa158015610aec573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190610b1091906128c9565b610b3157338160405163418b5b9d60e11b81526004016109129291906128b0565b6003546040515f9182916001600160a01b0390911690610b5490879087906128e8565b5f604051808303815f865af19150503d805f8114610b8d576040519150601f19603f3d011682016040523d82523d5f602084013e610b92565b606091505b509150915081610bb7578060405163bba5858960e01b815260040161091291906128f7565b6003546040516001600160a01b03909116907f58920bab8ebe20f458895b68243189a021c51741421c3d98eff715b8e5afe1fa90610bfa905f9089908990612931565b60405180910390a25050505050565b6001545f906001600160a01b03163314610c35576040516282b42960e81b815260040160405180910390fd5b610c3e83611b15565b610c488383611c71565b6003546001600160a01b0316639f40a7b38435610c6b60a0870160808801612405565b610c7b6060880160408901612405565b6040516001600160e01b031960e086901b16815260048101939093526001600160a01b039182166024840152166044820152602086013560648201526084016020604051808303815f875af1158015610cd6573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190610cfa9190612899565b9392505050565b6001546001600160a01b03163314610d2b576040516282b42960e81b815260040160405180910390fd5b6001600160a01b038116610d5257604051631d73770560e11b815260040160405180910390fd5b600480546001600160a01b0319166001600160a01b0383169081179091556040517fb4eda453b9dd76fa43a6adc49c2c2ae46d7d3dc55c5906261a33e0decfbec057905f90a250565b6001546001600160a01b03163314610dc5576040516282b42960e81b815260040160405180910390fd5b6002546040516256688760e81b815273ce4de370c7743a72b51edce92534bd98ec0246e791635668870091610e08916001600160a01b03169085906004016128b0565b5f6040518083038186803b158015610e1e575f80fd5b505af4158015610e30573d5f803e3d5ffd5b5050505050565b6001545f906060906001600160a01b03163314610e66576040516282b42960e81b815260040160405180910390fd5b6001600160a01b0386161580610e8457506001600160a01b03861630145b15610ea25760405163e6c4247b60e01b815260040160405180910390fd5b6003546001600160a01b0390811690871603610ed157604051631e47b9cd60e31b815260040160405180910390fd5b856001600160a01b0316858585604051610eec9291906128e8565b5f6040518083038185875af1925050503d805f8114610f26576040519150601f19603f3d011682016040523d82523d5f602084013e610f2b565b606091505b50909250905081610f51578060405163bba5858960e01b815260040161091291906128f7565b856001600160a01b03167f58920bab8ebe20f458895b68243189a021c51741421c3d98eff715b8e5afe1fa868686604051610f8e93929190612931565b60405180910390a294509492505050565b6001546060906001600160a01b03163314610fcc576040516282b42960e81b815260040160405180910390fd5b838214610fec5760405163a9854bc960e01b815260040160405180910390fd5b5f846001600160401b038111156110055761100561247d565b60405190808252806020026020018201604052801561102e578160200160208202803683370190505b5090505f856001600160401b0381111561104a5761104a61247d565b604051908082528060200260200182016040528015611073578160200160208202803683370190505b5090505f866001600160401b0381111561108f5761108f61247d565b6040519080825280602002602001820160405280156110b8578160200160208202803683370190505b5090505f876001600160401b038111156110d4576110d461247d565b6040519080825280602002602001820160405280156110fd578160200160208202803683370190505b5090505f5b888110156112945761112a8a8a8381811061111f5761111f612953565b905060c00201611b15565b6111628a8a8381811061113f5761113f612953565b905060c0020189898481811061115757611157612953565b905060600201611c71565b89898281811061117457611174612953565b905060c002015f013585828151811061118f5761118f612953565b6020026020010181815250508989828181106111ad576111ad612953565b905060c0020160800160208101906111c59190612405565b8482815181106111d7576111d7612953565b60200260200101906001600160a01b031690816001600160a01b03168152505089898281811061120957611209612953565b905060c0020160400160208101906112219190612405565b83828151811061123357611233612953565b60200260200101906001600160a01b031690816001600160a01b03168152505089898281811061126557611265612953565b905060c002016020013582828151811061128157611281612953565b6020908102919091010152600101611102565b50600354604051630cab09d560e21b81526001600160a01b03909116906332ac2754906112cb908790879087908790600401612967565b5f604051808303815f875af11580156112e6573d5f803e3d5ffd5b505050506040513d5f823e601f3d908101601f1916820160405261130d91908101906129be565b9998505050505050505050565b600254604051630303e6f960e31b81525f91737d39c84e9c009c529348c909e3d2c350b130cc079163181f37c891611360916001600160a01b03909116906004016122ab565b602060405180830381865af415801561137b573d5f803e3d5ffd5b505050506040513d601f19601f8201168201806040525081019061139f9190612899565b905090565b5f805f9054906101000a90046001600160a01b03166001600160a01b0316637b1039996040518163ffffffff1660e01b8152600401602060405180830381865afa1580156113f4573d5f803e3d5ffd5b505050506040513d601f19601f8201168201806040525081019061139f9190612a5e565b5f60045f9054906101000a90046001600160a01b03166001600160a01b0316632606a10b6040518163ffffffff1660e01b81526004015f60405180830381865afa158015611468573d5f803e3d5ffd5b505050506040513d5f823e601f3d908101601f1916820160405261148f9190810190612a79565b80602001905181019061139f9190612899565b604051630872156760e31b81525f90737d39c84e9c009c529348c909e3d2c350b130cc0790634390ab38906114de9085906006906004016128b0565b602060405180830381865af41580156114f9573d5f803e3d5ffd5b505050506040513d601f19601f8201168201806040525081019061151d9190612899565b92915050565b6040516318432c6d60e31b81526001600160a01b0383166004820152602481018290526006604482015273ce4de370c7743a72b51edce92534bd98ec0246e79063c219636890606401610753565b6004805460408051632606a10b60e01b815290515f9384936001600160a01b031692632606a10b928183019286928290030181865afa1580156115b6573d5f803e3d5ffd5b505050506040513d5f823e601f3d908101601f191682016040526115dd9190810190612a79565b8060200190518101906115f09190612899565b90505f60035f9054906101000a90046001600160a01b03166001600160a01b03166318160ddd6040518163ffffffff1660e01b8152600401602060405180830381865afa158015611643573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906116679190612899565b90505f60035f9054906101000a90046001600160a01b03166001600160a01b031663313ce5676040518163ffffffff1660e01b8152600401602060405180830381865afa1580156116ba573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906116de9190612aed565b6002549091505f90600160a01b900460ff166116fb836012612b1c565b6117059190612b35565b61171090600a612c2e565b905061171d848483611eba565b94505050505090565b6001546001600160a01b03163314611750576040516282b42960e81b815260040160405180910390fd5b60035460025461176d916001600160a01b0391821691165f611edf565b60035460025461178a916001600160a01b03918216911683611edf565b50565b6001546001600160a01b031633146117b7576040516282b42960e81b815260040160405180910390fd5b60025460405163b32c9a0d60e01b81526001600160a01b0380841660048301529091166024820152600660448201526007606482015273ce4de370c7743a72b51edce92534bd98ec0246e79063b32c9a0d90608401610e08565b5f8054906101000a90046001600160a01b03166001600160a01b0316638992929c6040518163ffffffff1660e01b8152600401602060405180830381865afa15801561185f573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906118839190612899565b5f5460405163145398bf60e21b81526001600160a01b039091169063514e62fc906118b490339085906004016128b0565b602060405180830381865afa1580156118cf573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906118f391906128c9565b61191457338160405163418b5b9d60e11b81526004016109129291906128b0565b600180546001600160a01b0319166001600160a01b03841690811790915560405181907fa860735870bed3facc9fc54c41108b1c5a273ea42e3fd7fb54f5124e990ef87b905f90a35050565b6001546001600160a01b0316331461198a576040516282b42960e81b815260040160405180910390fd5b6109eb6001600160a01b0384168383611f1f565b60405162e4194360e11b8152600760048201525f90737d39c84e9c009c529348c909e3d2c350b130cc07906301c8328690602401611360565b6001546001600160a01b03163314611a01576040516282b42960e81b815260040160405180910390fd5b604051631120af9760e01b81526001600160a01b038216600482015260066024820152600760448201526008606482015273ce4de370c7743a72b51edce92534bd98ec0246e790631120af9790608401610e08565b611a67898989898989898989611f9f565b505050505050505050565b60405181606052826040528360601b602c526323b872dd60601b600c5260205f6064601c5f895af18060015f511416611abd57803d873b151710611abd57637939f4245f526004601cfd5b505f60605260405250505050565b816014528060345263a9059cbb60601b5f5260205f604460105f875af18060015f511416611b0b57803d853b151710611b0b576390b8ec185f526004601cfd5b505f603452505050565b42611b2660c0830160a08401612c3c565b6001600160601b03161015611b4e5760405163271997a960e11b815260040160405180910390fd5b5f600581611b626060850160408601612405565b6001600160a01b0316815260208101919091526040015f90812091508190611b906080850160608601612c3c565b6001600160601b0316815260208101919091526040015f205460ff1615611bca57604051633834e53160e01b815260040160405180910390fd5b6001815f611bde6080860160608701612c3c565b6001600160601b03168152602081019190915260409081015f20805460ff191692151592909217909155611c189060608401908401612405565b6001600160a01b03167f0c5c052a8523c82e037f4c91bf81ce8a8f0df9bc89bdd872c1338e27d6676619611c526080850160608601612c3c565b6040516001600160601b03909116815260200160405180910390a25050565b5f7fe8c5e883f3410c872ea73823d1ffe6b761c739dd31959b30b204854e9ff5cc49611ca36060850160408601612405565b611cb360a0860160808701612405565b85356020870135611cca6080890160608a01612c3c565b611cda60c08a0160a08b01612c3c565b6040805160208101989098526001600160a01b0396871690880152949093166060860152608085019190915260a08401526001600160601b0390811660c08401521660e0820152610100016040516020818303038152906040528051906020012090505f611e19604080518082018252601081526f4274635661756c74537472617465677960801b602091820152815180830183526002815261563160f01b9082015281517f8b73c3c69bb8fe3d512ecc4cf759cc79239f7b179b0ffacaa9a75d522b39400f818301527f4a96b5c733663a175b63f2e78d6d14bb80066ad1db461d589ca24fd9c3ad3456818401527f4c23426613a5dc69e08fbd2787e6210aa679d4522e95a89d4dd88c4fd13a228360608201524660808201523060a0808301919091528351808303909101815260c0909101909252815191012090565b60405161190160f01b60208201526022810191909152604281018390526062016040516020818303038152906040528051906020012090505f611e7782855f016020810190611e689190612c55565b86602001358760400135612040565b9050611e896060860160408701612405565b6001600160a01b0316816001600160a01b031614610e305760405163cb1c3b0960e01b815260040160405180910390fd5b82820283158482048414178202611ed85763ad251c275f526004601cfd5b0492915050565b816014528060345263095ea7b360601b5f5260205f604460105f875af18060015f511416611b0b57803d853b151710611b0b57633e3f8f735f526004601cfd5b816014528060345263095ea7b360601b5f5260205f604460105f875af18060015f511416611b0b57803d853b151710611b0b575f60345263095ea7b360601b5f525f38604460105f885af1508160345260205f604460105f885af190508060015f511416611b0b57803d853b151710611b0b57633e3f8f735f526004601cfd5b611fb0898989898989898989612085565b5f81806020019051810190611fc59190612a5e565b90506001600160a01b038116611fee57604051631d73770560e11b815260040160405180910390fd5b600480546001600160a01b0319166001600160a01b0383169081179091556040517fb4eda453b9dd76fa43a6adc49c2c2ae46d7d3dc55c5906261a33e0decfbec057905f90a250505050505050505050565b5f604051855f5260ff851660205283604052826060526020600160805f60015afa5191503d61207657638baa579f5f526004601cfd5b5f606052604052949350505050565b600354600160a01b900460ff16156120af5760405162dc149f60e41b815260040160405180910390fd5b6003805460ff60a01b1916600160a01b1790556001600160a01b0384166120e95760405163e6c4247b60e01b815260040160405180910390fd5b6001600160a01b0383166121105760405163e6c4247b60e01b815260040160405180910390fd5b600180546001600160a01b0319166001600160a01b0386811691909117909155600280549185166001600160a81b031990921691909117600160a01b60ff85160217905561215d856121e9565b60025461217b908a908a908a908a906001600160a01b031687612257565b600380546001600160a01b0319166001600160a01b039283169081179091556002546001546040519184169316915f917f67d60251e7fb24788566a45d564dd80b11e3a68082dd61c162bb77424d11c252916121d6916122ab565b60405180910390a4505050505050505050565b6001600160a01b038116612210576040516309a53a6560e31b815260040160405180910390fd5b5f80546001600160a01b0319166001600160a01b038316908117825560405190917f6f865f6929ab0789d634d1f4591672186c120e6f5eb4f319efb07a3f894ff86d91a250565b5f86868686863060405161226a9061229e565b61227996959493929190612c70565b604051809103905ff080158015612292573d5f803e3d5ffd5b50979650505050505050565b6132c380612cbe83390190565b6001600160a01b0391909116815260200190565b5f815180845260208085019450602084015f5b838110156122f75781516001600160a01b0316875295820195908201906001016122d2565b509495945050505050565b602081525f610cfa60208301846122bf565b6001600160a01b038116811461178a575f80fd5b803561233381612314565b919050565b5f8060408385031215612349575f80fd5b82359150602083013561235b81612314565b809150509250929050565b80356001600160601b0381168114612333575f80fd5b5f806040838503121561238d575f80fd5b823561239881612314565b91506123a660208401612366565b90509250929050565b5f602082840312156123bf575f80fd5b5035919050565b5f805f606084860312156123d8575f80fd5b83356123e381612314565b92506020840135915060408401356123fa81612314565b809150509250925092565b5f60208284031215612415575f80fd5b8135610cfa81612314565b5f8083601f840112612430575f80fd5b5081356001600160401b03811115612446575f80fd5b60208301915083602082850101111561245d575f80fd5b9250929050565b60ff8116811461178a575f80fd5b803561233381612464565b634e487b7160e01b5f52604160045260245ffd5b604051601f8201601f191681016001600160401b03811182821017156124b9576124b961247d565b604052919050565b5f6001600160401b038211156124d9576124d961247d565b50601f01601f191660200190565b5f82601f8301126124f6575f80fd5b8135612509612504826124c1565b612491565b81815284602083860101111561251d575f80fd5b816020850160208301375f918101602001919091529392505050565b5f805f805f805f805f60e08a8c031215612551575f80fd5b89356001600160401b0380821115612567575f80fd5b6125738d838e01612420565b909b50995060208c013591508082111561258b575f80fd5b6125978d838e01612420565b909950975060408c013591506125ac82612314565b90955060608b0135906125be82612314565b8195506125cd60808d01612328565b94506125db60a08d01612472565b935060c08c01359150808211156125f0575f80fd5b506125fd8c828d016124e7565b9150509295985092959850929598565b5f805f6060848603121561261f575f80fd5b833561262a81612314565b9250602084013561263a81612314565b929592945050506040919091013590565b5f806020838503121561265c575f80fd5b82356001600160401b03811115612671575f80fd5b61267d85828601612420565b90969095509350505050565b5f8082840361012081121561269c575f80fd5b60c08112156126a9575f80fd5b839250606060bf19820112156126bd575f80fd5b5060c0830190509250929050565b5f805f80606085870312156126de575f80fd5b84356126e981612314565b93506020850135925060408501356001600160401b0381111561270a575f80fd5b61271687828801612420565b95989497509550505050565b5f81518084528060208401602086015e5f602082860101526020601f19601f83011685010191505092915050565b8215158152604060208201525f61276a6040830184612722565b949350505050565b5f805f8060408587031215612785575f80fd5b84356001600160401b038082111561279b575f80fd5b818701915087601f8301126127ae575f80fd5b8135818111156127bc575f80fd5b88602060c0830285010111156127d0575f80fd5b6020928301965094509086013590808211156127ea575f80fd5b818701915087601f8301126127fd575f80fd5b81358181111561280b575f80fd5b88602060608302850101111561281f575f80fd5b95989497505060200194505050565b5f815180845260208085019450602084015f5b838110156122f757815187529582019590820190600101612841565b602081525f610cfa602083018461282e565b5f8060408385031215612880575f80fd5b823561288b81612314565b946020939093013593505050565b5f602082840312156128a9575f80fd5b5051919050565b6001600160a01b03929092168252602082015260400190565b5f602082840312156128d9575f80fd5b81518015158114610cfa575f80fd5b818382375f9101908152919050565b602081525f610cfa6020830184612722565b81835281816020850137505f828201602090810191909152601f909101601f19169091010190565b838152604060208201525f61294a604083018486612909565b95945050505050565b634e487b7160e01b5f52603260045260245ffd5b608081525f612979608083018761282e565b828103602084015261298b81876122bf565b9050828103604084015261299f81866122bf565b905082810360608401526129b3818561282e565b979650505050505050565b5f60208083850312156129cf575f80fd5b82516001600160401b03808211156129e5575f80fd5b818501915085601f8301126129f8575f80fd5b815181811115612a0a57612a0a61247d565b8060051b9150612a1b848301612491565b8181529183018401918481019088841115612a34575f80fd5b938501935b83851015612a5257845182529385019390850190612a39565b98975050505050505050565b5f60208284031215612a6e575f80fd5b8151610cfa81612314565b5f60208284031215612a89575f80fd5b81516001600160401b03811115612a9e575f80fd5b8201601f81018413612aae575f80fd5b8051612abc612504826124c1565b818152856020838501011115612ad0575f80fd5b8160208401602083015e5f91810160200191909152949350505050565b5f60208284031215612afd575f80fd5b8151610cfa81612464565b634e487b7160e01b5f52601160045260245ffd5b60ff818116838216019081111561151d5761151d612b08565b60ff828116828216039081111561151d5761151d612b08565b600181815b80851115612b8857815f1904821115612b6e57612b6e612b08565b80851615612b7b57918102915b93841c9390800290612b53565b509250929050565b5f82612b9e5750600161151d565b81612baa57505f61151d565b8160018114612bc05760028114612bca57612be6565b600191505061151d565b60ff841115612bdb57612bdb612b08565b50506001821b61151d565b5060208310610133831016604e8410600b8410161715612c09575081810a61151d565b612c138383612b4e565b805f1904821115612c2657612c26612b08565b029392505050565b5f610cfa60ff841683612b90565b5f60208284031215612c4c575f80fd5b610cfa82612366565b5f60208284031215612c65575f80fd5b8135610cfa81612464565b608081525f612c8360808301888a612909565b8281036020840152612c96818789612909565b6001600160a01b03958616604085015293909416606090920191909152509594505050505056fe60e060405234801561000f575f80fd5b506040516132c33803806132c383398101604081905261002e916101a7565b83838360088484848484846001600160a01b0383166100605760405163e6c4247b60e01b815260040160405180910390fd5b6001600160a01b0381166100875760405163e6c4247b60e01b815260040160405180910390fd5b601260ff831611156100ac57604051630692acc560e51b815260040160405180910390fd5b60016100b886826102aa565b505f6100c485826102aa565b506001600160a01b0392831660805260ff90911660a0521660c052506103699950505050505050505050565b634e487b7160e01b5f52604160045260245ffd5b5f82601f830112610113575f80fd5b81516001600160401b038082111561012d5761012d6100f0565b604051601f8301601f19908116603f01168101908282118183101715610155576101556100f0565b8160405283815286602085880101111561016d575f80fd5b8360208701602083015e5f602085830101528094505050505092915050565b80516001600160a01b03811681146101a2575f80fd5b919050565b5f805f80608085870312156101ba575f80fd5b84516001600160401b03808211156101d0575f80fd5b6101dc88838901610104565b955060208701519150808211156101f1575f80fd5b506101fe87828801610104565b93505061020d6040860161018c565b915061021b6060860161018c565b905092959194509250565b600181811c9082168061023a57607f821691505b60208210810361025857634e487b7160e01b5f52602260045260245ffd5b50919050565b601f8211156102a557805f5260205f20601f840160051c810160208510156102835750805b601f840160051c820191505b818110156102a2575f815560010161028f565b50505b505050565b81516001600160401b038111156102c3576102c36100f0565b6102d7816102d18454610226565b8461025e565b602080601f83116001811461030a575f84156102f35750858301515b5f19600386901b1c1916600185901b178555610361565b5f85815260208120601f198616915b8281101561033857888601518255948401946001909101908401610319565b508582101561035557878501515f19600388901b60f8161c191681555b505060018460011b0185555b505050505050565b60805160a05160c051612eab6104185f395f8181610495015281816105c80152818161096201528181610a7901528181610be601528181610c8b01528181610fdd015281816113440152818161146b015281816115930152818161176e0152818161198401528181611a1c01528181611b3d01528181611ba001528181611c17015261222601525f8181610fac0152611f7b01525f81816115660152818161220401526124e60152612eab5ff3fe608060405234801561000f575f80fd5b5060043610610211575f3560e01c806370a082311161011e57806370a082311461040a5780637ecebe001461041d57806384e21249146104425780638f46049c1461046257806394bf804d146103f757806395d89b41146104755780639f40a7b31461047d578063a8c62e7614610490578063a9059cbb146104b7578063a9adfc2a146104ca578063b3d7f6b9146104de578063b460af94146104f1578063ba08765214610504578063c63d75b61461038a578063c6e6f59214610517578063ce96cb771461052a578063d505accf1461053d578063d905777e14610550578063dc49910914610563578063dd62ed3e14610595578063e1e158a5146105a8578063ef8b30f7146105b2575f80fd5b806301e1d1141461021557806306fdde031461023057806307a2d13a14610245578063095ea7b3146102585780630a28a4771461027b57806318160ddd1461028e5780631af05d101461029e57806322425480146102be57806323b872dd146102d1578063290d6a66146102e45780632bb77571146102f7578063313ce5671461030c578063316fe8c11461032657806332ac27541461034d5780633644e5151461036d57806338d52e0f14610375578063402d267d1461038a578063409547da1461039e578063472c2bd3146103bd5780634cdad506146103d05780635f250f69146103e35780636e553f65146103f7575b5f80fd5b61021d6105c5565b6040519081526020015b60405180910390f35b61023861064b565b60405161022791906125e5565b61021d61025336600461261a565b6106db565b61026b61026636600461264c565b610768565b6040519015158152602001610227565b61021d61028936600461261a565b6107e8565b6805345cdf77eb68f44c5461021d565b6102b16102ac36600461261a565b610855565b6040516102279190612674565b61021d6102cc3660046126c0565b610920565b61026b6102df3660046126f9565b610b06565b61021d6102f236600461264c565b610bcd565b61030a610305366004612779565b610c80565b005b610314610f9d565b60405160ff9091168152602001610227565b61021d7fa8518d2c1c36f837b794f71c4989af91cfa8e2f691ec44d51a2faeb243677cd181565b61036061035b3660046127c0565b610fd0565b604051610227919061287a565b61021d611500565b61037d611564565b60405161022791906128b1565b61021d6103983660046128c5565b505f1990565b61021d6103ac36600461261a565b60036020525f908152604090205481565b61030a6103cb3660046128de565b611588565b61021d6103de36600461261a565b6116a4565b61021d5f80516020612e1683398151915281565b61021d6104053660046128de565b6116ae565b61021d6104183660046128c5565b6116c8565b61021d61042b3660046128c5565b6338377508600c9081525f91909152602090205490565b61045561045036600461261a565b6116df565b6040516102279190612908565b61030a61047036600461295f565b611763565b61023861196a565b61021d61048b36600461297f565b611978565b61037d7f000000000000000000000000000000000000000000000000000000000000000081565b61026b6104c536600461264c565b611a4c565b61021d5f80516020612e5683398151915281565b61021d6104ec36600461261a565b611abb565b61021d6104ff3660046129c0565b611b31565b61021d6105123660046129c0565b611b94565b61021d61052536600461261a565b611c3f565b61021d6105383660046128c5565b611ca2565b61030a61054b3660046129f0565b611caf565b61021d61055e3660046128c5565b611e4d565b61057661057136600461295f565b611e57565b604080516001600160a01b039093168352602083019190915201610227565b61021d6105a3366004612a5d565b611e99565b61021d620186a081565b61021d6105c036600461261a565b611edd565b5f7f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031663b69ef8a86040518163ffffffff1660e01b8152600401602060405180830381865afa158015610622573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906106469190612a85565b905090565b60606001805461065a90612a9c565b80601f016020809104026020016040519081016040528092919081815260200182805461068690612a9c565b80156106d15780601f106106a8576101008083540402835291602001916106d1565b820191905f5260205f20905b8154815290600101906020018083116106b457829003601f168201915b5050505050905090565b5f6106e8565b9392505050565b5f6106f1611f75565b60ff16905080610731576106e1836107076105c5565b610712906001612ae8565b61072c6107266805345cdf77eb68f44c5490565b60010190565b611ee7565b6106e18361073d6105c5565b610748906001612ae8565b61075384600a612bdb565b6805345cdf77eb68f44c5461072c9190612ae8565b5f6001600160a01b0383166e22d473030f116ddee9f6b43ac78ba3188219151761079957633f68539a5f526004601cfd5b82602052637f5e9f20600c52335f52816034600c2055815f52602c5160601c337f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b92560205fa35060015b92915050565b5f6107f3565b611fa1565b5f6107fc611f75565b60ff16905080610831576106e18361081b6805345cdf77eb68f44c5490565b610826906001612ae8565b6107ee6107266105c5565b6106e18361084083600a612bdb565b6805345cdf77eb68f44c546108269190612ae8565b5f8181526002602052604081208054606092906001600160401b0381111561087f5761087f612be6565b6040519080825280602002602001820160405280156108a8578160200160208202803683370190505b5090505f5b8254811015610918578281815481106108c8576108c8612bfa565b5f91825260209091206002909102015482516001600160a01b03909116908390839081106108f8576108f8612bfa565b6001600160a01b03909216602092830291909101909101526001016108ad565b509392505050565b5f3068929eee149b4bd21268540361093f5763ab143c065f526004601cfd5b3068929eee149b4bd2126855604051634df48c7360e11b81526001600160a01b037f00000000000000000000000000000000000000000000000000000000000000001690639be918e6906109979087906004016128b1565b602060405180830381865afa1580156109b2573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906109d69190612c1d565b6109f357604051633dd1b30560e01b815260040160405180910390fd5b620186a0831015610a1757604051632ca2f52b60e11b815260040160405180910390fd5b6001600160a01b038216610a3e5760405163e6c4247b60e01b815260040160405180910390fd5b610a4783611edd565b9050805f03610a6957604051639811e0c760e01b815260040160405180910390fd5b610a9e6001600160a01b038516337f000000000000000000000000000000000000000000000000000000000000000086611fcd565b610aa88282612026565b60408051848152602081018390526001600160a01b03808516929087169133917f5cc049a9dd19f9433d64bdb54e83782424e5d075e63ce9e0dab55fadde1ac10d910160405180910390a43868929eee149b4bd21268559392505050565b5f610b1284848461209a565b8360601b6e22d473030f116ddee9f6b43ac78ba33314610b665733602052637f5e9f208117600c526034600c208054801915610b635780851115610b5d576313be252b5f526004601cfd5b84810382555b50505b6387a211a28117600c526020600c20805480851115610b8c5763f4d678b85f526004601cfd5b84810382555050835f526020600c208381540181555082602052600c5160601c8160601c5f80516020612e36833981519152602080a3505060019392505050565b604051634df48c7360e11b81525f906001600160a01b037f00000000000000000000000000000000000000000000000000000000000000001690639be918e690610c1b9086906004016128b1565b602060405180830381865afa158015610c36573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190610c5a9190612c1d565b610c6557505f6107e2565b620186a0821015610c7757505f6107e2565b6106e182611edd565b336001600160a01b037f00000000000000000000000000000000000000000000000000000000000000001614610cc957604051630b96ee6960e11b815260040160405180910390fd5b3068929eee149b4bd212685403610ce75763ab143c065f526004601cfd5b3068929eee149b4bd21268555f8381526002602052604090208054828114610d225760405163b7cb4a0760e01b815260040160405180910390fd5b5f816001600160401b03811115610d3b57610d3b612be6565b604051908082528060200260200182016040528015610d7f57816020015b604080518082019091525f8082526020820152815260200190600190039081610d595790505b5090505f5b82811015610df757838181548110610d9e57610d9e612bfa565b5f9182526020918290206040805180820190915260029092020180546001600160a01b0316825260010154918101919091528251839083908110610de457610de4612bfa565b6020908102919091010152600101610d84565b505f826001600160401b03811115610e1157610e11612be6565b604051908082528060200260200182016040528015610e3a578160200160208202803683370190505b5090505f5b83811015610f4d575f878783818110610e5a57610e5a612bfa565b905060200201359050848110610e83576040516358d019df60e01b815260040160405180910390fd5b828181518110610e9557610e95612bfa565b602002602001015115610ebb57604051630286b34560e61b815260040160405180910390fd5b838181518110610ecd57610ecd612bfa565b6020026020010151868381548110610ee757610ee7612bfa565b5f91825260209182902083516002929092020180546001600160a01b0319166001600160a01b039092169190911781559101516001918201558351849083908110610f3457610f34612bfa565b9115156020928302919091019091015250600101610e3f565b50867f48aa282ab1222aa00ca8e316801496ebfa102c8347d71be019280b12351b1ea58787604051610f80929190612c36565b60405180910390a2505050503868929eee149b4bd2126855505050565b5f610fa6611f75565b610646907f0000000000000000000000000000000000000000000000000000000000000000612c6d565b6060336001600160a01b037f0000000000000000000000000000000000000000000000000000000000000000161461101b57604051630b96ee6960e11b815260040160405180910390fd5b3068929eee149b4bd2126854036110395763ab143c065f526004601cfd5b3068929eee149b4bd21268558786811415806110555750808514155b806110605750808314155b1561107e5760405163a9854bc960e01b815260040160405180910390fd5b806001600160401b0381111561109657611096612be6565b6040519080825280602002602001820160405280156110bf578160200160208202803683370190505b5091505f805b828110156111bf576110f78888838181106110e2576110e2612bfa565b905060200201602081019061055e91906128c5565b8c8c8381811061110957611109612bfa565b90506020020135111561112f5760405163232b212d60e11b815260040160405180910390fd5b5f6111518d8d8481811061114557611145612bfa565b905060200201356116a4565b905086868381811061116557611165612bfa565b9050602002013581101561118c57604051632044382560e21b815260040160405180910390fd5b8085838151811061119f5761119f612bfa565b60209081029190910101526111b48184612ae8565b9250506001016110c5565b506111c9816121f7565b5f80516020612e168339815191525f908152600260209081527ffa24723fc895b271989df6e47f150e13fcd8fe50dd4e77c49d1f311030683c638054604080518285028101850190915281815292849084015b82821015611263575f848152602090819020604080518082019091526002850290910180546001600160a01b0316825260019081015482840152908352909201910161121c565b5050505090505f5b838110156114e4575f8d8d8381811061128657611286612bfa565b9050602002013590505f8683815181106112a2576112a2612bfa565b602002602001015190505f8d8d858181106112bf576112bf612bfa565b90506020020160208101906112d491906128c5565b90505f8c8c868181106112e9576112e9612bfa565b90506020020160208101906112fe91906128c5565b905061130a818561224f565b5f5b8651811015611402575f87828151811061132857611328612bfa565b60200260200101515f01516001600160a01b03166363812098307f00000000000000000000000000000000000000000000000000000000000000008888886040518663ffffffff1660e01b8152600401611386959493929190612c86565b5f604051808303815f875af11580156113a1573d5f803e3d5ffd5b505050506040513d5f823e601f3d908101601f191682016040526113c89190810190612d10565b80519091506113f9578060200151604051634a84814360e11b81526004016113f091906125e5565b60405180910390fd5b5060010161130c565b50855115611443575f80516020612e168339815191525f526003602052437f9f0385b1ed938db9d43de8083df6144b17af73c79967d96ab37bc282abcfb5c3555b61145561144e611564565b83856122bb565b806001600160a01b0316826001600160a01b03167f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03167ffbde797d201c681b91056529119e0b02407c7bb96a4a2c75c01fc9667232c8db86886040516114cd929190918252602082015260400190565b60405180910390a45050505080600101905061126b565b505050503868929eee149b4bd212685598975050505050505050565b5f8061150a61064b565b8051906020012090505f61151c612305565b604080517f8b73c3c69bb8fe3d512ecc4cf759cc79239f7b179b0ffacaa9a75d522b39400f815260208101949094528301525046606082015230608082015260a09020919050565b7f000000000000000000000000000000000000000000000000000000000000000090565b336001600160a01b037f000000000000000000000000000000000000000000000000000000000000000016146115d157604051630b96ee6960e11b815260040160405180910390fd5b6001600160a01b0381166115f857604051637d9e044960e11b815260040160405180910390fd5b6040805180820182526001600160a01b038381168083524360208085019182525f88815260028083529681208054600180820183558284529390922087519290980290970180546001600160a01b031916919095161784559051928101929092558354929392909186917fb7913938389428c36fa6e3a9ee44f7fdf7e638f3df36e807872f51aa89d516a49161168d91612dd5565b60405190815260200160405180910390a350505050565b5f6107e2826106db565b5f604051633d03393f60e11b815260040160405180910390fd5b6387a211a2600c9081525f91909152602090205490565b606060025f8381526020019081526020015f20805480602002602001604051908101604052809291908181526020015f905b82821015611758575f848152602090819020604080518082019091526002850290910180546001600160a01b03168252600190810154828401529083529092019101611711565b505050509050919050565b336001600160a01b037f000000000000000000000000000000000000000000000000000000000000000016146117ac57604051630b96ee6960e11b815260040160405180910390fd5b3068929eee149b4bd2126854036117ca5763ab143c065f526004601cfd5b3068929eee149b4bd21268555f8281526002602052604090208054808310611805576040516374e428b760e01b815260040160405180910390fd5b5f82848154811061181857611818612bfa565b905f5260205f209060020201905060035f8681526020019081526020015f205481600101541161185b5760405163d9e9fe7f60e01b815260040160405180910390fd5b80546001600160a01b0316611871600184612dd5565b85146118e95783611883600185612dd5565b8154811061189357611893612bfa565b905f5260205f2090600202018486815481106118b1576118b1612bfa565b5f9182526020909120825460029092020180546001600160a01b0319166001600160a01b039092169190911781556001918201549101555b838054806118f9576118f9612de8565b5f8281526020812060025f199093019283020180546001600160a01b031916815560010181905591556040516001600160a01b0383169188917feda906f5392545379935f1070f3c72b72072d8b54440d8d900db211dfcc9c6059190a3505050503868929eee149b4bd21268555050565b60605f805461065a90612a9c565b5f336001600160a01b037f000000000000000000000000000000000000000000000000000000000000000016146119c257604051630b96ee6960e11b815260040160405180910390fd5b6119cb83611e4d565b8511156119eb5760405163232b212d60e11b815260040160405180910390fd5b6119f4856116a4565b905081811015611a1757604051632044382560e21b815260040160405180910390fd5b611a447f000000000000000000000000000000000000000000000000000000000000000085858489612329565b949350505050565b5f611a5833848461209a565b6387a211a2600c52335f526020600c20805480841115611a7f5763f4d678b85f526004601cfd5b83810382555050825f526020600c208281540181555081602052600c5160601c335f80516020612e36833981519152602080a350600192915050565b5f80611ac5611f75565b60ff16905080611afa576106e183611adb6105c5565b611ae6906001612ae8565b6107ee6107266805345cdf77eb68f44c5490565b6106e183611b066105c5565b611b11906001612ae8565b611b1c84600a612bdb565b6805345cdf77eb68f44c546107ee9190612ae8565b5f336001600160a01b037f00000000000000000000000000000000000000000000000000000000000000001614611b7b57604051630b96ee6960e11b815260040160405180910390fd5b604051630abd048560e01b815260040160405180910390fd5b5f336001600160a01b037f00000000000000000000000000000000000000000000000000000000000000001614611bde57604051630b96ee6960e11b815260040160405180910390fd5b611be782611e4d565b841115611c075760405163232b212d60e11b815260040160405180910390fd5b611c10846116a4565b90506106e17f000000000000000000000000000000000000000000000000000000000000000084848488612329565b5f80611c49611f75565b60ff16905080611c7e576106e183611c686805345cdf77eb68f44c5490565b611c73906001612ae8565b61072c6107266105c5565b6106e183611c8d83600a612bdb565b6805345cdf77eb68f44c54611c739190612ae8565b5f6107e2610253836116c8565b6001600160a01b0386166e22d473030f116ddee9f6b43ac78ba31885191517611cdf57633f68539a5f526004601cfd5b5f611ce861064b565b8051906020012090505f611cfa612305565b905085421115611d1157631a15a3cc5f526004601cfd5b6040518960601b60601c99508860601b60601c985065383775081901600e52895f526020600c2080547f8b73c3c69bb8fe3d512ecc4cf759cc79239f7b179b0ffacaa9a75d522b39400f835284602084015283604084015246606084015230608084015260a08320602e527f6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c983528b60208401528a60408401528960608401528060808401528860a084015260c08320604e526042602c205f528760ff16602052866040528560605260208060805f60015afa8c3d5114611df95763ddafbaef5f526004601cfd5b0190556303faf4f960a51b89176040526034602c20889055888a7f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925602060608501a360405250505f60605250505050505050565b5f6107e2826116c8565b6002602052815f5260405f208181548110611e70575f80fd5b5f918252602090912060029091020180546001909101546001600160a01b039091169250905082565b5f6e22d473030f116ddee9f6b43ac78ba2196001600160a01b03831601611ec257505f196107e2565b50602052637f5e9f20600c9081525f91909152603490205490565b5f6107e282611c3f565b82820281838583041485151702611f6e575f198385098181108201900382848609835f038416828511611f215763ae47f7025f526004601cfd5b93849004938382119092035f8390038390046001010292030417600260038302811880840282030280840282030280840282030280840282030280840282030280840290910302026106e1565b0492915050565b5f6106467f00000000000000000000000000000000000000000000000000000000000000006012612dfc565b5f611fad848484611ee7565b905081838509156106e157600101806106e15763ae47f7025f526004601cfd5b60405181606052826040528360601b602c526323b872dd60601b600c5260205f6064601c5f895af18060015f51141661201857803d873b15171061201857637939f4245f526004601cfd5b505f60605260405250505050565b6120315f838361209a565b6805345cdf77eb68f44c54818101818110156120545763e5cfe9575f526004601cfd5b806805345cdf77eb68f44c5550506387a211a2600c52815f526020600c208181540181555080602052600c5160601c5f5f80516020612e36833981519152602080a35050565b5f80516020612e568339815191525f5260026020527f149af30f8bd23f55accab099aedc99217d01dad258047cc9ac481074befba49d8054156121f1575f5b81548110156121b7575f8282815481106120f5576120f5612bfa565b5f91825260209091206002909102015460405163aa709eb960e01b81523060048201526001600160a01b0388811660248301528781166044830152606482018790529091169063aa709eb9906084015f604051808303815f875af115801561215f573d5f803e3d5ffd5b505050506040513d5f823e601f3d908101601f191682016040526121869190810190612d10565b80519091506121ae578060200151604051634a84814360e11b81526004016113f091906125e5565b506001016120d9565b505f80516020612e568339815191525f526003602052437f97f17d930b298cc87b2ef7c286ab7c3efb365da5252701e362385d89271fcc3d555b50505050565b61224c6001600160a01b037f0000000000000000000000000000000000000000000000000000000000000000167f00000000000000000000000000000000000000000000000000000000000000003084611fcd565b50565b61225a825f8361209a565b6387a211a2600c52815f526020600c208054808311156122815763f4d678b85f526004601cfd5b82900390556805345cdf77eb68f44c805482900390555f8181526001600160a01b0383165f80516020612e36833981519152602083a35050565b816014528060345263a9059cbb60601b5f5260205f604460105f875af18060015f5114166122fb57803d853b1517106122fb576390b8ec185f526004601cfd5b505f603452505050565b7fc89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc690565b3068929eee149b4bd2126854036123475763ab143c065f526004601cfd5b3068929eee149b4bd2126855826001600160a01b0316856001600160a01b03161461237757612377838683612581565b612381838261224f565b61238a826121f7565b5f80516020612e168339815191525f90815260026020527ffa24723fc895b271989df6e47f150e13fcd8fe50dd4e77c49d1f311030683c63905b8154811015612498575f8282815481106123e0576123e0612bfa565b5f918252602090912060029091020154604051630c70241360e31b81526001600160a01b03909116906363812098906124259030908c908a908d908d90600401612c86565b5f604051808303815f875af1158015612440573d5f803e3d5ffd5b505050506040513d5f823e601f3d908101601f191682016040526124679190810190612d10565b805190915061248f578060200151604051634a84814360e11b81526004016113f091906125e5565b506001016123c4565b508054156124d9575f80516020612e168339815191525f526003602052437f9f0385b1ed938db9d43de8083df6144b17af73c79967d96ab37bc282abcfb5c3555b61250d6001600160a01b037f00000000000000000000000000000000000000000000000000000000000000001686856122bb565b836001600160a01b0316856001600160a01b0316876001600160a01b03167ffbde797d201c681b91056529119e0b02407c7bb96a4a2c75c01fc9667232c8db8686604051612565929190918252602082015260400190565b60405180910390a4503868929eee149b4bd21268555050505050565b6e22d473030f116ddee9f6b43ac78ba2196001600160a01b038316016125a657505050565b81602052637f5e9f20600c52825f526034600c2080548019156125de57808311156125d8576313be252b5f526004601cfd5b82810382555b5050505050565b602081525f82518060208401528060208501604085015e5f604082850101526040601f19601f83011684010191505092915050565b5f6020828403121561262a575f80fd5b5035919050565b80356001600160a01b0381168114612647575f80fd5b919050565b5f806040838503121561265d575f80fd5b61266683612631565b946020939093013593505050565b602080825282518282018190525f9190848201906040850190845b818110156126b45783516001600160a01b03168352928401929184019160010161268f565b50909695505050505050565b5f805f606084860312156126d2575f80fd5b6126db84612631565b9250602084013591506126f060408501612631565b90509250925092565b5f805f6060848603121561270b575f80fd5b61271484612631565b925061272260208501612631565b9150604084013590509250925092565b5f8083601f840112612742575f80fd5b5081356001600160401b03811115612758575f80fd5b6020830191508360208260051b8501011115612772575f80fd5b9250929050565b5f805f6040848603121561278b575f80fd5b8335925060208401356001600160401b038111156127a7575f80fd5b6127b386828701612732565b9497909650939450505050565b5f805f805f805f806080898b0312156127d7575f80fd5b88356001600160401b03808211156127ed575f80fd5b6127f98c838d01612732565b909a50985060208b0135915080821115612811575f80fd5b61281d8c838d01612732565b909850965060408b0135915080821115612835575f80fd5b6128418c838d01612732565b909650945060608b0135915080821115612859575f80fd5b506128668b828c01612732565b999c989b5096995094979396929594505050565b602080825282518282018190525f9190848201906040850190845b818110156126b457835183529284019291840191600101612895565b6001600160a01b0391909116815260200190565b5f602082840312156128d5575f80fd5b6106e182612631565b5f80604083850312156128ef575f80fd5b823591506128ff60208401612631565b90509250929050565b602080825282518282018190525f919060409081850190868401855b8281101561295257815180516001600160a01b03168552860151868501529284019290850190600101612924565b5091979650505050505050565b5f8060408385031215612970575f80fd5b50508035926020909101359150565b5f805f8060808587031215612992575f80fd5b843593506129a260208601612631565b92506129b060408601612631565b9396929550929360600135925050565b5f805f606084860312156129d2575f80fd5b833592506129e260208501612631565b91506126f060408501612631565b5f805f805f805f60e0888a031215612a06575f80fd5b612a0f88612631565b9650612a1d60208901612631565b95506040880135945060608801359350608088013560ff81168114612a40575f80fd5b9699959850939692959460a0840135945060c09093013592915050565b5f8060408385031215612a6e575f80fd5b612a7783612631565b91506128ff60208401612631565b5f60208284031215612a95575f80fd5b5051919050565b600181811c90821680612ab057607f821691505b602082108103612ace57634e487b7160e01b5f52602260045260245ffd5b50919050565b634e487b7160e01b5f52601160045260245ffd5b808201808211156107e2576107e2612ad4565b600181815b80851115612b3557815f1904821115612b1b57612b1b612ad4565b80851615612b2857918102915b93841c9390800290612b00565b509250929050565b5f82612b4b575060016107e2565b81612b5757505f6107e2565b8160018114612b6d5760028114612b7757612b93565b60019150506107e2565b60ff841115612b8857612b88612ad4565b50506001821b6107e2565b5060208310610133831016604e8410600b8410161715612bb6575081810a6107e2565b612bc08383612afb565b805f1904821115612bd357612bd3612ad4565b029392505050565b5f6106e18383612b3d565b634e487b7160e01b5f52604160045260245ffd5b634e487b7160e01b5f52603260045260245ffd5b80518015158114612647575f80fd5b5f60208284031215612c2d575f80fd5b6106e182612c0e565b602080825281018290525f6001600160fb1b03831115612c54575f80fd5b8260051b80856040850137919091016040019392505050565b60ff81811683821601908111156107e2576107e2612ad4565b6001600160a01b0395861681529385166020850152604084019290925283166060830152909116608082015260a00190565b604080519081016001600160401b0381118282101715612cda57612cda612be6565b60405290565b604051601f8201601f191681016001600160401b0381118282101715612d0857612d08612be6565b604052919050565b5f6020808385031215612d21575f80fd5b82516001600160401b0380821115612d37575f80fd5b9084019060408287031215612d4a575f80fd5b612d52612cb8565b612d5b83612c0e565b81528383015182811115612d6d575f80fd5b80840193505086601f840112612d81575f80fd5b825182811115612d9357612d93612be6565b612da5601f8201601f19168601612ce0565b92508083528785828601011115612dba575f80fd5b808585018685015e5f90830185015292830152509392505050565b818103818111156107e2576107e2612ad4565b634e487b7160e01b5f52603160045260245ffd5b60ff82811682821603908111156107e2576107e2612ad456fe5b2b3dbc0425e650141ab8840d86d0c9f083f7ddc44122d0056e9fb6b9797222ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef22e88b5eba316b174ad850666cd779e6e8ea04ebe64bb30f03875fc21841c4f0a2646970667358221220d00e0105c9418c150fd270107fa7b02741ffac89fa915218297ce69ac8d9860064736f6c63430008190033a2646970667358221220f690db39bb0767e25258a8598c3d790dbeccc1018631541fcd837785b273daea64736f6c63430008190033
Deployed Bytecode
0x6080604052600436106101af575f3560e01c80636dbf2fa0116100e65780636dbf2fa0146104405780636e551f221461046d57806374375359146104995780637b103999146104ad57806399530b06146104c15780639be918e6146104d55780639ebdf12c1461050c578063a1bf28401461052b578063a5d5db0c1461054a578063b69ef8a814610569578063b70e92421461057d578063bd81579e1461059c578063c2d41601146105ca578063c99d3a06146105ea578063d0ebdbe714610609578063da46098c14610628578063e753178114610647578063f0d2d5a81461065b575f80fd5b8062435da5146101ba578063010ec441146101ee57806305db8a691461020d57806305fe138b1461022e578063081932db1461024f578063172c48c7146102985780631f1088a0146102b757806321b1e5f8146102d65780632bf71fe9146102f55780632e144579146103145780632fdcfbd214610333578063309e9a091461035257806338d52e0f1461037157806347597d9c14610390578063481c6a75146103bd5780634c925032146103dc5780634ddde78d146103fb57806351c6590a14610421575f80fd5b366101b657005b5f80fd5b3480156101c5575f80fd5b505f546101d8906001600160a01b031681565b6040516101e591906122ab565b60405180910390f35b3480156101f9575f80fd5b506004546101d8906001600160a01b031681565b348015610218575f80fd5b5061022161067a565b6040516101e59190612302565b348015610239575f80fd5b5061024d610248366004612338565b6106da565b005b34801561025a575f80fd5b5061028861026936600461237c565b600560209081525f928352604080842090915290825290205460ff1681565b60405190151581526020016101e5565b3480156102a3575f80fd5b506101d86102b23660046123af565b610783565b3480156102c2575f80fd5b5061024d6102d13660046123c6565b6107ab565b3480156102e1575f80fd5b5061024d6102f0366004612405565b610857565b348015610300575f80fd5b5061024d61030f366004612539565b61091f565b34801561031f575f80fd5b5061024d61032e36600461260d565b6109ac565b34801561033e575f80fd5b5061024d61034d36600461260d565b6109f0565b34801561035d575f80fd5b5061024d61036c36600461264b565b610a2e565b34801561037c575f80fd5b506002546101d8906001600160a01b031681565b34801561039b575f80fd5b506103af6103aa366004612689565b610c09565b6040519081526020016101e5565b3480156103c8575f80fd5b506001546101d8906001600160a01b031681565b3480156103e7575f80fd5b5061024d6103f6366004612405565b610d01565b348015610406575f80fd5b5061040f600881565b60405160ff90911681526020016101e5565b34801561042c575f80fd5b5061024d61043b3660046123af565b610d9b565b34801561044b575f80fd5b5061045f61045a3660046126cb565b610e37565b6040516101e5929190612750565b348015610478575f80fd5b5061048c610487366004612772565b610f9f565b6040516101e5919061285d565b3480156104a4575f80fd5b506103af61131a565b3480156104b8575f80fd5b506101d86113a4565b3480156104cc575f80fd5b506103af611418565b3480156104e0575f80fd5b506102886104ef366004612405565b6001600160a01b03165f9081526006602052604090205460ff1690565b348015610517575f80fd5b506003546101d8906001600160a01b031681565b348015610536575f80fd5b506103af610545366004612405565b6114a2565b348015610555575f80fd5b5061024d61056436600461286f565b611523565b348015610574575f80fd5b506103af611571565b348015610588575f80fd5b5061024d6105973660046123af565b611726565b3480156105a7575f80fd5b506102886105b6366004612405565b60066020525f908152604090205460ff1681565b3480156105d5575f80fd5b5060025461040f90600160a01b900460ff1681565b3480156105f5575f80fd5b5061024d610604366004612405565b61178d565b348015610614575f80fd5b5061024d610623366004612405565b611811565b348015610633575f80fd5b5061024d61064236600461260d565b611960565b348015610652575f80fd5b506103af61199e565b348015610666575f80fd5b5061024d610675366004612405565b6119d7565b606060078054806020026020016040519081016040528092919081815260200182805480156106d057602002820191905f5260205f20905b81546001600160a01b031681526001909101906020018083116106b2575b5050505050905090565b6001546001600160a01b03163314610704576040516282b42960e81b815260040160405180910390fd5b6002546040516323fede4760e11b81526001600160a01b03918216600482015260248101849052908216604482015273ce4de370c7743a72b51edce92534bd98ec0246e7906347fdbc8e906064015b5f6040518083038186803b158015610769575f80fd5b505af415801561077b573d5f803e3d5ffd5b505050505050565b60078181548110610792575f80fd5b5f918252602090912001546001600160a01b0316905081565b6001546001600160a01b031633146107d5576040516282b42960e81b815260040160405180910390fd5b6040516326be1dc160e11b81526001600160a01b03808516600483015260248201849052821660448201526006606482015273ce4de370c7743a72b51edce92534bd98ec0246e790634d7c3b82906084015f6040518083038186803b15801561083c575f80fd5b505af415801561084e573d5f803e3d5ffd5b50505050505050565b6001546001600160a01b03163314610881576040516282b42960e81b815260040160405180910390fd5b5f816001600160a01b0316476040515f6040518083038185875af1925050503d805f81146108ca576040519150601f19603f3d011682016040523d82523d5f602084013e6108cf565b606091505b505090508061091b5760405162461bcd60e51b8152602060048201526013602482015272115512081d1c985b9cd9995c8819985a5b1959606a1b60448201526064015b60405180910390fd5b5050565b610930898989898989898989611a56565b8160ff1660081461093f575f80fd5b50506001600160a01b03165f818152600660205260408120805460ff191660019081179091556007805491820181559091527fa66cc928b5edb82af9bd49922954155ab7b0942694bea4ce44661d9a8736c6880180546001600160a01b0319169091179055505050505050565b6001546001600160a01b031633146109d6576040516282b42960e81b815260040160405180910390fd5b6109eb6001600160a01b038416833084611a72565b505050565b6001546001600160a01b03163314610a1a576040516282b42960e81b815260040160405180910390fd5b6109eb6001600160a01b0384168383611acb565b5f8054906101000a90046001600160a01b03166001600160a01b0316638992929c6040518163ffffffff1660e01b8152600401602060405180830381865afa158015610a7c573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190610aa09190612899565b5f5460405163145398bf60e21b81526001600160a01b039091169063514e62fc90610ad190339085906004016128b0565b602060405180830381865afa158015610aec573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190610b1091906128c9565b610b3157338160405163418b5b9d60e11b81526004016109129291906128b0565b6003546040515f9182916001600160a01b0390911690610b5490879087906128e8565b5f604051808303815f865af19150503d805f8114610b8d576040519150601f19603f3d011682016040523d82523d5f602084013e610b92565b606091505b509150915081610bb7578060405163bba5858960e01b815260040161091291906128f7565b6003546040516001600160a01b03909116907f58920bab8ebe20f458895b68243189a021c51741421c3d98eff715b8e5afe1fa90610bfa905f9089908990612931565b60405180910390a25050505050565b6001545f906001600160a01b03163314610c35576040516282b42960e81b815260040160405180910390fd5b610c3e83611b15565b610c488383611c71565b6003546001600160a01b0316639f40a7b38435610c6b60a0870160808801612405565b610c7b6060880160408901612405565b6040516001600160e01b031960e086901b16815260048101939093526001600160a01b039182166024840152166044820152602086013560648201526084016020604051808303815f875af1158015610cd6573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190610cfa9190612899565b9392505050565b6001546001600160a01b03163314610d2b576040516282b42960e81b815260040160405180910390fd5b6001600160a01b038116610d5257604051631d73770560e11b815260040160405180910390fd5b600480546001600160a01b0319166001600160a01b0383169081179091556040517fb4eda453b9dd76fa43a6adc49c2c2ae46d7d3dc55c5906261a33e0decfbec057905f90a250565b6001546001600160a01b03163314610dc5576040516282b42960e81b815260040160405180910390fd5b6002546040516256688760e81b815273ce4de370c7743a72b51edce92534bd98ec0246e791635668870091610e08916001600160a01b03169085906004016128b0565b5f6040518083038186803b158015610e1e575f80fd5b505af4158015610e30573d5f803e3d5ffd5b5050505050565b6001545f906060906001600160a01b03163314610e66576040516282b42960e81b815260040160405180910390fd5b6001600160a01b0386161580610e8457506001600160a01b03861630145b15610ea25760405163e6c4247b60e01b815260040160405180910390fd5b6003546001600160a01b0390811690871603610ed157604051631e47b9cd60e31b815260040160405180910390fd5b856001600160a01b0316858585604051610eec9291906128e8565b5f6040518083038185875af1925050503d805f8114610f26576040519150601f19603f3d011682016040523d82523d5f602084013e610f2b565b606091505b50909250905081610f51578060405163bba5858960e01b815260040161091291906128f7565b856001600160a01b03167f58920bab8ebe20f458895b68243189a021c51741421c3d98eff715b8e5afe1fa868686604051610f8e93929190612931565b60405180910390a294509492505050565b6001546060906001600160a01b03163314610fcc576040516282b42960e81b815260040160405180910390fd5b838214610fec5760405163a9854bc960e01b815260040160405180910390fd5b5f846001600160401b038111156110055761100561247d565b60405190808252806020026020018201604052801561102e578160200160208202803683370190505b5090505f856001600160401b0381111561104a5761104a61247d565b604051908082528060200260200182016040528015611073578160200160208202803683370190505b5090505f866001600160401b0381111561108f5761108f61247d565b6040519080825280602002602001820160405280156110b8578160200160208202803683370190505b5090505f876001600160401b038111156110d4576110d461247d565b6040519080825280602002602001820160405280156110fd578160200160208202803683370190505b5090505f5b888110156112945761112a8a8a8381811061111f5761111f612953565b905060c00201611b15565b6111628a8a8381811061113f5761113f612953565b905060c0020189898481811061115757611157612953565b905060600201611c71565b89898281811061117457611174612953565b905060c002015f013585828151811061118f5761118f612953565b6020026020010181815250508989828181106111ad576111ad612953565b905060c0020160800160208101906111c59190612405565b8482815181106111d7576111d7612953565b60200260200101906001600160a01b031690816001600160a01b03168152505089898281811061120957611209612953565b905060c0020160400160208101906112219190612405565b83828151811061123357611233612953565b60200260200101906001600160a01b031690816001600160a01b03168152505089898281811061126557611265612953565b905060c002016020013582828151811061128157611281612953565b6020908102919091010152600101611102565b50600354604051630cab09d560e21b81526001600160a01b03909116906332ac2754906112cb908790879087908790600401612967565b5f604051808303815f875af11580156112e6573d5f803e3d5ffd5b505050506040513d5f823e601f3d908101601f1916820160405261130d91908101906129be565b9998505050505050505050565b600254604051630303e6f960e31b81525f91737d39c84e9c009c529348c909e3d2c350b130cc079163181f37c891611360916001600160a01b03909116906004016122ab565b602060405180830381865af415801561137b573d5f803e3d5ffd5b505050506040513d601f19601f8201168201806040525081019061139f9190612899565b905090565b5f805f9054906101000a90046001600160a01b03166001600160a01b0316637b1039996040518163ffffffff1660e01b8152600401602060405180830381865afa1580156113f4573d5f803e3d5ffd5b505050506040513d601f19601f8201168201806040525081019061139f9190612a5e565b5f60045f9054906101000a90046001600160a01b03166001600160a01b0316632606a10b6040518163ffffffff1660e01b81526004015f60405180830381865afa158015611468573d5f803e3d5ffd5b505050506040513d5f823e601f3d908101601f1916820160405261148f9190810190612a79565b80602001905181019061139f9190612899565b604051630872156760e31b81525f90737d39c84e9c009c529348c909e3d2c350b130cc0790634390ab38906114de9085906006906004016128b0565b602060405180830381865af41580156114f9573d5f803e3d5ffd5b505050506040513d601f19601f8201168201806040525081019061151d9190612899565b92915050565b6040516318432c6d60e31b81526001600160a01b0383166004820152602481018290526006604482015273ce4de370c7743a72b51edce92534bd98ec0246e79063c219636890606401610753565b6004805460408051632606a10b60e01b815290515f9384936001600160a01b031692632606a10b928183019286928290030181865afa1580156115b6573d5f803e3d5ffd5b505050506040513d5f823e601f3d908101601f191682016040526115dd9190810190612a79565b8060200190518101906115f09190612899565b90505f60035f9054906101000a90046001600160a01b03166001600160a01b03166318160ddd6040518163ffffffff1660e01b8152600401602060405180830381865afa158015611643573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906116679190612899565b90505f60035f9054906101000a90046001600160a01b03166001600160a01b031663313ce5676040518163ffffffff1660e01b8152600401602060405180830381865afa1580156116ba573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906116de9190612aed565b6002549091505f90600160a01b900460ff166116fb836012612b1c565b6117059190612b35565b61171090600a612c2e565b905061171d848483611eba565b94505050505090565b6001546001600160a01b03163314611750576040516282b42960e81b815260040160405180910390fd5b60035460025461176d916001600160a01b0391821691165f611edf565b60035460025461178a916001600160a01b03918216911683611edf565b50565b6001546001600160a01b031633146117b7576040516282b42960e81b815260040160405180910390fd5b60025460405163b32c9a0d60e01b81526001600160a01b0380841660048301529091166024820152600660448201526007606482015273ce4de370c7743a72b51edce92534bd98ec0246e79063b32c9a0d90608401610e08565b5f8054906101000a90046001600160a01b03166001600160a01b0316638992929c6040518163ffffffff1660e01b8152600401602060405180830381865afa15801561185f573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906118839190612899565b5f5460405163145398bf60e21b81526001600160a01b039091169063514e62fc906118b490339085906004016128b0565b602060405180830381865afa1580156118cf573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906118f391906128c9565b61191457338160405163418b5b9d60e11b81526004016109129291906128b0565b600180546001600160a01b0319166001600160a01b03841690811790915560405181907fa860735870bed3facc9fc54c41108b1c5a273ea42e3fd7fb54f5124e990ef87b905f90a35050565b6001546001600160a01b0316331461198a576040516282b42960e81b815260040160405180910390fd5b6109eb6001600160a01b0384168383611f1f565b60405162e4194360e11b8152600760048201525f90737d39c84e9c009c529348c909e3d2c350b130cc07906301c8328690602401611360565b6001546001600160a01b03163314611a01576040516282b42960e81b815260040160405180910390fd5b604051631120af9760e01b81526001600160a01b038216600482015260066024820152600760448201526008606482015273ce4de370c7743a72b51edce92534bd98ec0246e790631120af9790608401610e08565b611a67898989898989898989611f9f565b505050505050505050565b60405181606052826040528360601b602c526323b872dd60601b600c5260205f6064601c5f895af18060015f511416611abd57803d873b151710611abd57637939f4245f526004601cfd5b505f60605260405250505050565b816014528060345263a9059cbb60601b5f5260205f604460105f875af18060015f511416611b0b57803d853b151710611b0b576390b8ec185f526004601cfd5b505f603452505050565b42611b2660c0830160a08401612c3c565b6001600160601b03161015611b4e5760405163271997a960e11b815260040160405180910390fd5b5f600581611b626060850160408601612405565b6001600160a01b0316815260208101919091526040015f90812091508190611b906080850160608601612c3c565b6001600160601b0316815260208101919091526040015f205460ff1615611bca57604051633834e53160e01b815260040160405180910390fd5b6001815f611bde6080860160608701612c3c565b6001600160601b03168152602081019190915260409081015f20805460ff191692151592909217909155611c189060608401908401612405565b6001600160a01b03167f0c5c052a8523c82e037f4c91bf81ce8a8f0df9bc89bdd872c1338e27d6676619611c526080850160608601612c3c565b6040516001600160601b03909116815260200160405180910390a25050565b5f7fe8c5e883f3410c872ea73823d1ffe6b761c739dd31959b30b204854e9ff5cc49611ca36060850160408601612405565b611cb360a0860160808701612405565b85356020870135611cca6080890160608a01612c3c565b611cda60c08a0160a08b01612c3c565b6040805160208101989098526001600160a01b0396871690880152949093166060860152608085019190915260a08401526001600160601b0390811660c08401521660e0820152610100016040516020818303038152906040528051906020012090505f611e19604080518082018252601081526f4274635661756c74537472617465677960801b602091820152815180830183526002815261563160f01b9082015281517f8b73c3c69bb8fe3d512ecc4cf759cc79239f7b179b0ffacaa9a75d522b39400f818301527f4a96b5c733663a175b63f2e78d6d14bb80066ad1db461d589ca24fd9c3ad3456818401527f4c23426613a5dc69e08fbd2787e6210aa679d4522e95a89d4dd88c4fd13a228360608201524660808201523060a0808301919091528351808303909101815260c0909101909252815191012090565b60405161190160f01b60208201526022810191909152604281018390526062016040516020818303038152906040528051906020012090505f611e7782855f016020810190611e689190612c55565b86602001358760400135612040565b9050611e896060860160408701612405565b6001600160a01b0316816001600160a01b031614610e305760405163cb1c3b0960e01b815260040160405180910390fd5b82820283158482048414178202611ed85763ad251c275f526004601cfd5b0492915050565b816014528060345263095ea7b360601b5f5260205f604460105f875af18060015f511416611b0b57803d853b151710611b0b57633e3f8f735f526004601cfd5b816014528060345263095ea7b360601b5f5260205f604460105f875af18060015f511416611b0b57803d853b151710611b0b575f60345263095ea7b360601b5f525f38604460105f885af1508160345260205f604460105f885af190508060015f511416611b0b57803d853b151710611b0b57633e3f8f735f526004601cfd5b611fb0898989898989898989612085565b5f81806020019051810190611fc59190612a5e565b90506001600160a01b038116611fee57604051631d73770560e11b815260040160405180910390fd5b600480546001600160a01b0319166001600160a01b0383169081179091556040517fb4eda453b9dd76fa43a6adc49c2c2ae46d7d3dc55c5906261a33e0decfbec057905f90a250505050505050505050565b5f604051855f5260ff851660205283604052826060526020600160805f60015afa5191503d61207657638baa579f5f526004601cfd5b5f606052604052949350505050565b600354600160a01b900460ff16156120af5760405162dc149f60e41b815260040160405180910390fd5b6003805460ff60a01b1916600160a01b1790556001600160a01b0384166120e95760405163e6c4247b60e01b815260040160405180910390fd5b6001600160a01b0383166121105760405163e6c4247b60e01b815260040160405180910390fd5b600180546001600160a01b0319166001600160a01b0386811691909117909155600280549185166001600160a81b031990921691909117600160a01b60ff85160217905561215d856121e9565b60025461217b908a908a908a908a906001600160a01b031687612257565b600380546001600160a01b0319166001600160a01b039283169081179091556002546001546040519184169316915f917f67d60251e7fb24788566a45d564dd80b11e3a68082dd61c162bb77424d11c252916121d6916122ab565b60405180910390a4505050505050505050565b6001600160a01b038116612210576040516309a53a6560e31b815260040160405180910390fd5b5f80546001600160a01b0319166001600160a01b038316908117825560405190917f6f865f6929ab0789d634d1f4591672186c120e6f5eb4f319efb07a3f894ff86d91a250565b5f86868686863060405161226a9061229e565b61227996959493929190612c70565b604051809103905ff080158015612292573d5f803e3d5ffd5b50979650505050505050565b6132c380612cbe83390190565b6001600160a01b0391909116815260200190565b5f815180845260208085019450602084015f5b838110156122f75781516001600160a01b0316875295820195908201906001016122d2565b509495945050505050565b602081525f610cfa60208301846122bf565b6001600160a01b038116811461178a575f80fd5b803561233381612314565b919050565b5f8060408385031215612349575f80fd5b82359150602083013561235b81612314565b809150509250929050565b80356001600160601b0381168114612333575f80fd5b5f806040838503121561238d575f80fd5b823561239881612314565b91506123a660208401612366565b90509250929050565b5f602082840312156123bf575f80fd5b5035919050565b5f805f606084860312156123d8575f80fd5b83356123e381612314565b92506020840135915060408401356123fa81612314565b809150509250925092565b5f60208284031215612415575f80fd5b8135610cfa81612314565b5f8083601f840112612430575f80fd5b5081356001600160401b03811115612446575f80fd5b60208301915083602082850101111561245d575f80fd5b9250929050565b60ff8116811461178a575f80fd5b803561233381612464565b634e487b7160e01b5f52604160045260245ffd5b604051601f8201601f191681016001600160401b03811182821017156124b9576124b961247d565b604052919050565b5f6001600160401b038211156124d9576124d961247d565b50601f01601f191660200190565b5f82601f8301126124f6575f80fd5b8135612509612504826124c1565b612491565b81815284602083860101111561251d575f80fd5b816020850160208301375f918101602001919091529392505050565b5f805f805f805f805f60e08a8c031215612551575f80fd5b89356001600160401b0380821115612567575f80fd5b6125738d838e01612420565b909b50995060208c013591508082111561258b575f80fd5b6125978d838e01612420565b909950975060408c013591506125ac82612314565b90955060608b0135906125be82612314565b8195506125cd60808d01612328565b94506125db60a08d01612472565b935060c08c01359150808211156125f0575f80fd5b506125fd8c828d016124e7565b9150509295985092959850929598565b5f805f6060848603121561261f575f80fd5b833561262a81612314565b9250602084013561263a81612314565b929592945050506040919091013590565b5f806020838503121561265c575f80fd5b82356001600160401b03811115612671575f80fd5b61267d85828601612420565b90969095509350505050565b5f8082840361012081121561269c575f80fd5b60c08112156126a9575f80fd5b839250606060bf19820112156126bd575f80fd5b5060c0830190509250929050565b5f805f80606085870312156126de575f80fd5b84356126e981612314565b93506020850135925060408501356001600160401b0381111561270a575f80fd5b61271687828801612420565b95989497509550505050565b5f81518084528060208401602086015e5f602082860101526020601f19601f83011685010191505092915050565b8215158152604060208201525f61276a6040830184612722565b949350505050565b5f805f8060408587031215612785575f80fd5b84356001600160401b038082111561279b575f80fd5b818701915087601f8301126127ae575f80fd5b8135818111156127bc575f80fd5b88602060c0830285010111156127d0575f80fd5b6020928301965094509086013590808211156127ea575f80fd5b818701915087601f8301126127fd575f80fd5b81358181111561280b575f80fd5b88602060608302850101111561281f575f80fd5b95989497505060200194505050565b5f815180845260208085019450602084015f5b838110156122f757815187529582019590820190600101612841565b602081525f610cfa602083018461282e565b5f8060408385031215612880575f80fd5b823561288b81612314565b946020939093013593505050565b5f602082840312156128a9575f80fd5b5051919050565b6001600160a01b03929092168252602082015260400190565b5f602082840312156128d9575f80fd5b81518015158114610cfa575f80fd5b818382375f9101908152919050565b602081525f610cfa6020830184612722565b81835281816020850137505f828201602090810191909152601f909101601f19169091010190565b838152604060208201525f61294a604083018486612909565b95945050505050565b634e487b7160e01b5f52603260045260245ffd5b608081525f612979608083018761282e565b828103602084015261298b81876122bf565b9050828103604084015261299f81866122bf565b905082810360608401526129b3818561282e565b979650505050505050565b5f60208083850312156129cf575f80fd5b82516001600160401b03808211156129e5575f80fd5b818501915085601f8301126129f8575f80fd5b815181811115612a0a57612a0a61247d565b8060051b9150612a1b848301612491565b8181529183018401918481019088841115612a34575f80fd5b938501935b83851015612a5257845182529385019390850190612a39565b98975050505050505050565b5f60208284031215612a6e575f80fd5b8151610cfa81612314565b5f60208284031215612a89575f80fd5b81516001600160401b03811115612a9e575f80fd5b8201601f81018413612aae575f80fd5b8051612abc612504826124c1565b818152856020838501011115612ad0575f80fd5b8160208401602083015e5f91810160200191909152949350505050565b5f60208284031215612afd575f80fd5b8151610cfa81612464565b634e487b7160e01b5f52601160045260245ffd5b60ff818116838216019081111561151d5761151d612b08565b60ff828116828216039081111561151d5761151d612b08565b600181815b80851115612b8857815f1904821115612b6e57612b6e612b08565b80851615612b7b57918102915b93841c9390800290612b53565b509250929050565b5f82612b9e5750600161151d565b81612baa57505f61151d565b8160018114612bc05760028114612bca57612be6565b600191505061151d565b60ff841115612bdb57612bdb612b08565b50506001821b61151d565b5060208310610133831016604e8410600b8410161715612c09575081810a61151d565b612c138383612b4e565b805f1904821115612c2657612c26612b08565b029392505050565b5f610cfa60ff841683612b90565b5f60208284031215612c4c575f80fd5b610cfa82612366565b5f60208284031215612c65575f80fd5b8135610cfa81612464565b608081525f612c8360808301888a612909565b8281036020840152612c96818789612909565b6001600160a01b03958616604085015293909416606090920191909152509594505050505056fe60e060405234801561000f575f80fd5b506040516132c33803806132c383398101604081905261002e916101a7565b83838360088484848484846001600160a01b0383166100605760405163e6c4247b60e01b815260040160405180910390fd5b6001600160a01b0381166100875760405163e6c4247b60e01b815260040160405180910390fd5b601260ff831611156100ac57604051630692acc560e51b815260040160405180910390fd5b60016100b886826102aa565b505f6100c485826102aa565b506001600160a01b0392831660805260ff90911660a0521660c052506103699950505050505050505050565b634e487b7160e01b5f52604160045260245ffd5b5f82601f830112610113575f80fd5b81516001600160401b038082111561012d5761012d6100f0565b604051601f8301601f19908116603f01168101908282118183101715610155576101556100f0565b8160405283815286602085880101111561016d575f80fd5b8360208701602083015e5f602085830101528094505050505092915050565b80516001600160a01b03811681146101a2575f80fd5b919050565b5f805f80608085870312156101ba575f80fd5b84516001600160401b03808211156101d0575f80fd5b6101dc88838901610104565b955060208701519150808211156101f1575f80fd5b506101fe87828801610104565b93505061020d6040860161018c565b915061021b6060860161018c565b905092959194509250565b600181811c9082168061023a57607f821691505b60208210810361025857634e487b7160e01b5f52602260045260245ffd5b50919050565b601f8211156102a557805f5260205f20601f840160051c810160208510156102835750805b601f840160051c820191505b818110156102a2575f815560010161028f565b50505b505050565b81516001600160401b038111156102c3576102c36100f0565b6102d7816102d18454610226565b8461025e565b602080601f83116001811461030a575f84156102f35750858301515b5f19600386901b1c1916600185901b178555610361565b5f85815260208120601f198616915b8281101561033857888601518255948401946001909101908401610319565b508582101561035557878501515f19600388901b60f8161c191681555b505060018460011b0185555b505050505050565b60805160a05160c051612eab6104185f395f8181610495015281816105c80152818161096201528181610a7901528181610be601528181610c8b01528181610fdd015281816113440152818161146b015281816115930152818161176e0152818161198401528181611a1c01528181611b3d01528181611ba001528181611c17015261222601525f8181610fac0152611f7b01525f81816115660152818161220401526124e60152612eab5ff3fe608060405234801561000f575f80fd5b5060043610610211575f3560e01c806370a082311161011e57806370a082311461040a5780637ecebe001461041d57806384e21249146104425780638f46049c1461046257806394bf804d146103f757806395d89b41146104755780639f40a7b31461047d578063a8c62e7614610490578063a9059cbb146104b7578063a9adfc2a146104ca578063b3d7f6b9146104de578063b460af94146104f1578063ba08765214610504578063c63d75b61461038a578063c6e6f59214610517578063ce96cb771461052a578063d505accf1461053d578063d905777e14610550578063dc49910914610563578063dd62ed3e14610595578063e1e158a5146105a8578063ef8b30f7146105b2575f80fd5b806301e1d1141461021557806306fdde031461023057806307a2d13a14610245578063095ea7b3146102585780630a28a4771461027b57806318160ddd1461028e5780631af05d101461029e57806322425480146102be57806323b872dd146102d1578063290d6a66146102e45780632bb77571146102f7578063313ce5671461030c578063316fe8c11461032657806332ac27541461034d5780633644e5151461036d57806338d52e0f14610375578063402d267d1461038a578063409547da1461039e578063472c2bd3146103bd5780634cdad506146103d05780635f250f69146103e35780636e553f65146103f7575b5f80fd5b61021d6105c5565b6040519081526020015b60405180910390f35b61023861064b565b60405161022791906125e5565b61021d61025336600461261a565b6106db565b61026b61026636600461264c565b610768565b6040519015158152602001610227565b61021d61028936600461261a565b6107e8565b6805345cdf77eb68f44c5461021d565b6102b16102ac36600461261a565b610855565b6040516102279190612674565b61021d6102cc3660046126c0565b610920565b61026b6102df3660046126f9565b610b06565b61021d6102f236600461264c565b610bcd565b61030a610305366004612779565b610c80565b005b610314610f9d565b60405160ff9091168152602001610227565b61021d7fa8518d2c1c36f837b794f71c4989af91cfa8e2f691ec44d51a2faeb243677cd181565b61036061035b3660046127c0565b610fd0565b604051610227919061287a565b61021d611500565b61037d611564565b60405161022791906128b1565b61021d6103983660046128c5565b505f1990565b61021d6103ac36600461261a565b60036020525f908152604090205481565b61030a6103cb3660046128de565b611588565b61021d6103de36600461261a565b6116a4565b61021d5f80516020612e1683398151915281565b61021d6104053660046128de565b6116ae565b61021d6104183660046128c5565b6116c8565b61021d61042b3660046128c5565b6338377508600c9081525f91909152602090205490565b61045561045036600461261a565b6116df565b6040516102279190612908565b61030a61047036600461295f565b611763565b61023861196a565b61021d61048b36600461297f565b611978565b61037d7f000000000000000000000000000000000000000000000000000000000000000081565b61026b6104c536600461264c565b611a4c565b61021d5f80516020612e5683398151915281565b61021d6104ec36600461261a565b611abb565b61021d6104ff3660046129c0565b611b31565b61021d6105123660046129c0565b611b94565b61021d61052536600461261a565b611c3f565b61021d6105383660046128c5565b611ca2565b61030a61054b3660046129f0565b611caf565b61021d61055e3660046128c5565b611e4d565b61057661057136600461295f565b611e57565b604080516001600160a01b039093168352602083019190915201610227565b61021d6105a3366004612a5d565b611e99565b61021d620186a081565b61021d6105c036600461261a565b611edd565b5f7f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031663b69ef8a86040518163ffffffff1660e01b8152600401602060405180830381865afa158015610622573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906106469190612a85565b905090565b60606001805461065a90612a9c565b80601f016020809104026020016040519081016040528092919081815260200182805461068690612a9c565b80156106d15780601f106106a8576101008083540402835291602001916106d1565b820191905f5260205f20905b8154815290600101906020018083116106b457829003601f168201915b5050505050905090565b5f6106e8565b9392505050565b5f6106f1611f75565b60ff16905080610731576106e1836107076105c5565b610712906001612ae8565b61072c6107266805345cdf77eb68f44c5490565b60010190565b611ee7565b6106e18361073d6105c5565b610748906001612ae8565b61075384600a612bdb565b6805345cdf77eb68f44c5461072c9190612ae8565b5f6001600160a01b0383166e22d473030f116ddee9f6b43ac78ba3188219151761079957633f68539a5f526004601cfd5b82602052637f5e9f20600c52335f52816034600c2055815f52602c5160601c337f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b92560205fa35060015b92915050565b5f6107f3565b611fa1565b5f6107fc611f75565b60ff16905080610831576106e18361081b6805345cdf77eb68f44c5490565b610826906001612ae8565b6107ee6107266105c5565b6106e18361084083600a612bdb565b6805345cdf77eb68f44c546108269190612ae8565b5f8181526002602052604081208054606092906001600160401b0381111561087f5761087f612be6565b6040519080825280602002602001820160405280156108a8578160200160208202803683370190505b5090505f5b8254811015610918578281815481106108c8576108c8612bfa565b5f91825260209091206002909102015482516001600160a01b03909116908390839081106108f8576108f8612bfa565b6001600160a01b03909216602092830291909101909101526001016108ad565b509392505050565b5f3068929eee149b4bd21268540361093f5763ab143c065f526004601cfd5b3068929eee149b4bd2126855604051634df48c7360e11b81526001600160a01b037f00000000000000000000000000000000000000000000000000000000000000001690639be918e6906109979087906004016128b1565b602060405180830381865afa1580156109b2573d5f803e3d5ffd5b505050506040513d601f19601f820116820180604052508101906109d69190612c1d565b6109f357604051633dd1b30560e01b815260040160405180910390fd5b620186a0831015610a1757604051632ca2f52b60e11b815260040160405180910390fd5b6001600160a01b038216610a3e5760405163e6c4247b60e01b815260040160405180910390fd5b610a4783611edd565b9050805f03610a6957604051639811e0c760e01b815260040160405180910390fd5b610a9e6001600160a01b038516337f000000000000000000000000000000000000000000000000000000000000000086611fcd565b610aa88282612026565b60408051848152602081018390526001600160a01b03808516929087169133917f5cc049a9dd19f9433d64bdb54e83782424e5d075e63ce9e0dab55fadde1ac10d910160405180910390a43868929eee149b4bd21268559392505050565b5f610b1284848461209a565b8360601b6e22d473030f116ddee9f6b43ac78ba33314610b665733602052637f5e9f208117600c526034600c208054801915610b635780851115610b5d576313be252b5f526004601cfd5b84810382555b50505b6387a211a28117600c526020600c20805480851115610b8c5763f4d678b85f526004601cfd5b84810382555050835f526020600c208381540181555082602052600c5160601c8160601c5f80516020612e36833981519152602080a3505060019392505050565b604051634df48c7360e11b81525f906001600160a01b037f00000000000000000000000000000000000000000000000000000000000000001690639be918e690610c1b9086906004016128b1565b602060405180830381865afa158015610c36573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190610c5a9190612c1d565b610c6557505f6107e2565b620186a0821015610c7757505f6107e2565b6106e182611edd565b336001600160a01b037f00000000000000000000000000000000000000000000000000000000000000001614610cc957604051630b96ee6960e11b815260040160405180910390fd5b3068929eee149b4bd212685403610ce75763ab143c065f526004601cfd5b3068929eee149b4bd21268555f8381526002602052604090208054828114610d225760405163b7cb4a0760e01b815260040160405180910390fd5b5f816001600160401b03811115610d3b57610d3b612be6565b604051908082528060200260200182016040528015610d7f57816020015b604080518082019091525f8082526020820152815260200190600190039081610d595790505b5090505f5b82811015610df757838181548110610d9e57610d9e612bfa565b5f9182526020918290206040805180820190915260029092020180546001600160a01b0316825260010154918101919091528251839083908110610de457610de4612bfa565b6020908102919091010152600101610d84565b505f826001600160401b03811115610e1157610e11612be6565b604051908082528060200260200182016040528015610e3a578160200160208202803683370190505b5090505f5b83811015610f4d575f878783818110610e5a57610e5a612bfa565b905060200201359050848110610e83576040516358d019df60e01b815260040160405180910390fd5b828181518110610e9557610e95612bfa565b602002602001015115610ebb57604051630286b34560e61b815260040160405180910390fd5b838181518110610ecd57610ecd612bfa565b6020026020010151868381548110610ee757610ee7612bfa565b5f91825260209182902083516002929092020180546001600160a01b0319166001600160a01b039092169190911781559101516001918201558351849083908110610f3457610f34612bfa565b9115156020928302919091019091015250600101610e3f565b50867f48aa282ab1222aa00ca8e316801496ebfa102c8347d71be019280b12351b1ea58787604051610f80929190612c36565b60405180910390a2505050503868929eee149b4bd2126855505050565b5f610fa6611f75565b610646907f0000000000000000000000000000000000000000000000000000000000000000612c6d565b6060336001600160a01b037f0000000000000000000000000000000000000000000000000000000000000000161461101b57604051630b96ee6960e11b815260040160405180910390fd5b3068929eee149b4bd2126854036110395763ab143c065f526004601cfd5b3068929eee149b4bd21268558786811415806110555750808514155b806110605750808314155b1561107e5760405163a9854bc960e01b815260040160405180910390fd5b806001600160401b0381111561109657611096612be6565b6040519080825280602002602001820160405280156110bf578160200160208202803683370190505b5091505f805b828110156111bf576110f78888838181106110e2576110e2612bfa565b905060200201602081019061055e91906128c5565b8c8c8381811061110957611109612bfa565b90506020020135111561112f5760405163232b212d60e11b815260040160405180910390fd5b5f6111518d8d8481811061114557611145612bfa565b905060200201356116a4565b905086868381811061116557611165612bfa565b9050602002013581101561118c57604051632044382560e21b815260040160405180910390fd5b8085838151811061119f5761119f612bfa565b60209081029190910101526111b48184612ae8565b9250506001016110c5565b506111c9816121f7565b5f80516020612e168339815191525f908152600260209081527ffa24723fc895b271989df6e47f150e13fcd8fe50dd4e77c49d1f311030683c638054604080518285028101850190915281815292849084015b82821015611263575f848152602090819020604080518082019091526002850290910180546001600160a01b0316825260019081015482840152908352909201910161121c565b5050505090505f5b838110156114e4575f8d8d8381811061128657611286612bfa565b9050602002013590505f8683815181106112a2576112a2612bfa565b602002602001015190505f8d8d858181106112bf576112bf612bfa565b90506020020160208101906112d491906128c5565b90505f8c8c868181106112e9576112e9612bfa565b90506020020160208101906112fe91906128c5565b905061130a818561224f565b5f5b8651811015611402575f87828151811061132857611328612bfa565b60200260200101515f01516001600160a01b03166363812098307f00000000000000000000000000000000000000000000000000000000000000008888886040518663ffffffff1660e01b8152600401611386959493929190612c86565b5f604051808303815f875af11580156113a1573d5f803e3d5ffd5b505050506040513d5f823e601f3d908101601f191682016040526113c89190810190612d10565b80519091506113f9578060200151604051634a84814360e11b81526004016113f091906125e5565b60405180910390fd5b5060010161130c565b50855115611443575f80516020612e168339815191525f526003602052437f9f0385b1ed938db9d43de8083df6144b17af73c79967d96ab37bc282abcfb5c3555b61145561144e611564565b83856122bb565b806001600160a01b0316826001600160a01b03167f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03167ffbde797d201c681b91056529119e0b02407c7bb96a4a2c75c01fc9667232c8db86886040516114cd929190918252602082015260400190565b60405180910390a45050505080600101905061126b565b505050503868929eee149b4bd212685598975050505050505050565b5f8061150a61064b565b8051906020012090505f61151c612305565b604080517f8b73c3c69bb8fe3d512ecc4cf759cc79239f7b179b0ffacaa9a75d522b39400f815260208101949094528301525046606082015230608082015260a09020919050565b7f000000000000000000000000000000000000000000000000000000000000000090565b336001600160a01b037f000000000000000000000000000000000000000000000000000000000000000016146115d157604051630b96ee6960e11b815260040160405180910390fd5b6001600160a01b0381166115f857604051637d9e044960e11b815260040160405180910390fd5b6040805180820182526001600160a01b038381168083524360208085019182525f88815260028083529681208054600180820183558284529390922087519290980290970180546001600160a01b031916919095161784559051928101929092558354929392909186917fb7913938389428c36fa6e3a9ee44f7fdf7e638f3df36e807872f51aa89d516a49161168d91612dd5565b60405190815260200160405180910390a350505050565b5f6107e2826106db565b5f604051633d03393f60e11b815260040160405180910390fd5b6387a211a2600c9081525f91909152602090205490565b606060025f8381526020019081526020015f20805480602002602001604051908101604052809291908181526020015f905b82821015611758575f848152602090819020604080518082019091526002850290910180546001600160a01b03168252600190810154828401529083529092019101611711565b505050509050919050565b336001600160a01b037f000000000000000000000000000000000000000000000000000000000000000016146117ac57604051630b96ee6960e11b815260040160405180910390fd5b3068929eee149b4bd2126854036117ca5763ab143c065f526004601cfd5b3068929eee149b4bd21268555f8281526002602052604090208054808310611805576040516374e428b760e01b815260040160405180910390fd5b5f82848154811061181857611818612bfa565b905f5260205f209060020201905060035f8681526020019081526020015f205481600101541161185b5760405163d9e9fe7f60e01b815260040160405180910390fd5b80546001600160a01b0316611871600184612dd5565b85146118e95783611883600185612dd5565b8154811061189357611893612bfa565b905f5260205f2090600202018486815481106118b1576118b1612bfa565b5f9182526020909120825460029092020180546001600160a01b0319166001600160a01b039092169190911781556001918201549101555b838054806118f9576118f9612de8565b5f8281526020812060025f199093019283020180546001600160a01b031916815560010181905591556040516001600160a01b0383169188917feda906f5392545379935f1070f3c72b72072d8b54440d8d900db211dfcc9c6059190a3505050503868929eee149b4bd21268555050565b60605f805461065a90612a9c565b5f336001600160a01b037f000000000000000000000000000000000000000000000000000000000000000016146119c257604051630b96ee6960e11b815260040160405180910390fd5b6119cb83611e4d565b8511156119eb5760405163232b212d60e11b815260040160405180910390fd5b6119f4856116a4565b905081811015611a1757604051632044382560e21b815260040160405180910390fd5b611a447f000000000000000000000000000000000000000000000000000000000000000085858489612329565b949350505050565b5f611a5833848461209a565b6387a211a2600c52335f526020600c20805480841115611a7f5763f4d678b85f526004601cfd5b83810382555050825f526020600c208281540181555081602052600c5160601c335f80516020612e36833981519152602080a350600192915050565b5f80611ac5611f75565b60ff16905080611afa576106e183611adb6105c5565b611ae6906001612ae8565b6107ee6107266805345cdf77eb68f44c5490565b6106e183611b066105c5565b611b11906001612ae8565b611b1c84600a612bdb565b6805345cdf77eb68f44c546107ee9190612ae8565b5f336001600160a01b037f00000000000000000000000000000000000000000000000000000000000000001614611b7b57604051630b96ee6960e11b815260040160405180910390fd5b604051630abd048560e01b815260040160405180910390fd5b5f336001600160a01b037f00000000000000000000000000000000000000000000000000000000000000001614611bde57604051630b96ee6960e11b815260040160405180910390fd5b611be782611e4d565b841115611c075760405163232b212d60e11b815260040160405180910390fd5b611c10846116a4565b90506106e17f000000000000000000000000000000000000000000000000000000000000000084848488612329565b5f80611c49611f75565b60ff16905080611c7e576106e183611c686805345cdf77eb68f44c5490565b611c73906001612ae8565b61072c6107266105c5565b6106e183611c8d83600a612bdb565b6805345cdf77eb68f44c54611c739190612ae8565b5f6107e2610253836116c8565b6001600160a01b0386166e22d473030f116ddee9f6b43ac78ba31885191517611cdf57633f68539a5f526004601cfd5b5f611ce861064b565b8051906020012090505f611cfa612305565b905085421115611d1157631a15a3cc5f526004601cfd5b6040518960601b60601c99508860601b60601c985065383775081901600e52895f526020600c2080547f8b73c3c69bb8fe3d512ecc4cf759cc79239f7b179b0ffacaa9a75d522b39400f835284602084015283604084015246606084015230608084015260a08320602e527f6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c983528b60208401528a60408401528960608401528060808401528860a084015260c08320604e526042602c205f528760ff16602052866040528560605260208060805f60015afa8c3d5114611df95763ddafbaef5f526004601cfd5b0190556303faf4f960a51b89176040526034602c20889055888a7f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925602060608501a360405250505f60605250505050505050565b5f6107e2826116c8565b6002602052815f5260405f208181548110611e70575f80fd5b5f918252602090912060029091020180546001909101546001600160a01b039091169250905082565b5f6e22d473030f116ddee9f6b43ac78ba2196001600160a01b03831601611ec257505f196107e2565b50602052637f5e9f20600c9081525f91909152603490205490565b5f6107e282611c3f565b82820281838583041485151702611f6e575f198385098181108201900382848609835f038416828511611f215763ae47f7025f526004601cfd5b93849004938382119092035f8390038390046001010292030417600260038302811880840282030280840282030280840282030280840282030280840282030280840290910302026106e1565b0492915050565b5f6106467f00000000000000000000000000000000000000000000000000000000000000006012612dfc565b5f611fad848484611ee7565b905081838509156106e157600101806106e15763ae47f7025f526004601cfd5b60405181606052826040528360601b602c526323b872dd60601b600c5260205f6064601c5f895af18060015f51141661201857803d873b15171061201857637939f4245f526004601cfd5b505f60605260405250505050565b6120315f838361209a565b6805345cdf77eb68f44c54818101818110156120545763e5cfe9575f526004601cfd5b806805345cdf77eb68f44c5550506387a211a2600c52815f526020600c208181540181555080602052600c5160601c5f5f80516020612e36833981519152602080a35050565b5f80516020612e568339815191525f5260026020527f149af30f8bd23f55accab099aedc99217d01dad258047cc9ac481074befba49d8054156121f1575f5b81548110156121b7575f8282815481106120f5576120f5612bfa565b5f91825260209091206002909102015460405163aa709eb960e01b81523060048201526001600160a01b0388811660248301528781166044830152606482018790529091169063aa709eb9906084015f604051808303815f875af115801561215f573d5f803e3d5ffd5b505050506040513d5f823e601f3d908101601f191682016040526121869190810190612d10565b80519091506121ae578060200151604051634a84814360e11b81526004016113f091906125e5565b506001016120d9565b505f80516020612e568339815191525f526003602052437f97f17d930b298cc87b2ef7c286ab7c3efb365da5252701e362385d89271fcc3d555b50505050565b61224c6001600160a01b037f0000000000000000000000000000000000000000000000000000000000000000167f00000000000000000000000000000000000000000000000000000000000000003084611fcd565b50565b61225a825f8361209a565b6387a211a2600c52815f526020600c208054808311156122815763f4d678b85f526004601cfd5b82900390556805345cdf77eb68f44c805482900390555f8181526001600160a01b0383165f80516020612e36833981519152602083a35050565b816014528060345263a9059cbb60601b5f5260205f604460105f875af18060015f5114166122fb57803d853b1517106122fb576390b8ec185f526004601cfd5b505f603452505050565b7fc89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc690565b3068929eee149b4bd2126854036123475763ab143c065f526004601cfd5b3068929eee149b4bd2126855826001600160a01b0316856001600160a01b03161461237757612377838683612581565b612381838261224f565b61238a826121f7565b5f80516020612e168339815191525f90815260026020527ffa24723fc895b271989df6e47f150e13fcd8fe50dd4e77c49d1f311030683c63905b8154811015612498575f8282815481106123e0576123e0612bfa565b5f918252602090912060029091020154604051630c70241360e31b81526001600160a01b03909116906363812098906124259030908c908a908d908d90600401612c86565b5f604051808303815f875af1158015612440573d5f803e3d5ffd5b505050506040513d5f823e601f3d908101601f191682016040526124679190810190612d10565b805190915061248f578060200151604051634a84814360e11b81526004016113f091906125e5565b506001016123c4565b508054156124d9575f80516020612e168339815191525f526003602052437f9f0385b1ed938db9d43de8083df6144b17af73c79967d96ab37bc282abcfb5c3555b61250d6001600160a01b037f00000000000000000000000000000000000000000000000000000000000000001686856122bb565b836001600160a01b0316856001600160a01b0316876001600160a01b03167ffbde797d201c681b91056529119e0b02407c7bb96a4a2c75c01fc9667232c8db8686604051612565929190918252602082015260400190565b60405180910390a4503868929eee149b4bd21268555050505050565b6e22d473030f116ddee9f6b43ac78ba2196001600160a01b038316016125a657505050565b81602052637f5e9f20600c52825f526034600c2080548019156125de57808311156125d8576313be252b5f526004601cfd5b82810382555b5050505050565b602081525f82518060208401528060208501604085015e5f604082850101526040601f19601f83011684010191505092915050565b5f6020828403121561262a575f80fd5b5035919050565b80356001600160a01b0381168114612647575f80fd5b919050565b5f806040838503121561265d575f80fd5b61266683612631565b946020939093013593505050565b602080825282518282018190525f9190848201906040850190845b818110156126b45783516001600160a01b03168352928401929184019160010161268f565b50909695505050505050565b5f805f606084860312156126d2575f80fd5b6126db84612631565b9250602084013591506126f060408501612631565b90509250925092565b5f805f6060848603121561270b575f80fd5b61271484612631565b925061272260208501612631565b9150604084013590509250925092565b5f8083601f840112612742575f80fd5b5081356001600160401b03811115612758575f80fd5b6020830191508360208260051b8501011115612772575f80fd5b9250929050565b5f805f6040848603121561278b575f80fd5b8335925060208401356001600160401b038111156127a7575f80fd5b6127b386828701612732565b9497909650939450505050565b5f805f805f805f806080898b0312156127d7575f80fd5b88356001600160401b03808211156127ed575f80fd5b6127f98c838d01612732565b909a50985060208b0135915080821115612811575f80fd5b61281d8c838d01612732565b909850965060408b0135915080821115612835575f80fd5b6128418c838d01612732565b909650945060608b0135915080821115612859575f80fd5b506128668b828c01612732565b999c989b5096995094979396929594505050565b602080825282518282018190525f9190848201906040850190845b818110156126b457835183529284019291840191600101612895565b6001600160a01b0391909116815260200190565b5f602082840312156128d5575f80fd5b6106e182612631565b5f80604083850312156128ef575f80fd5b823591506128ff60208401612631565b90509250929050565b602080825282518282018190525f919060409081850190868401855b8281101561295257815180516001600160a01b03168552860151868501529284019290850190600101612924565b5091979650505050505050565b5f8060408385031215612970575f80fd5b50508035926020909101359150565b5f805f8060808587031215612992575f80fd5b843593506129a260208601612631565b92506129b060408601612631565b9396929550929360600135925050565b5f805f606084860312156129d2575f80fd5b833592506129e260208501612631565b91506126f060408501612631565b5f805f805f805f60e0888a031215612a06575f80fd5b612a0f88612631565b9650612a1d60208901612631565b95506040880135945060608801359350608088013560ff81168114612a40575f80fd5b9699959850939692959460a0840135945060c09093013592915050565b5f8060408385031215612a6e575f80fd5b612a7783612631565b91506128ff60208401612631565b5f60208284031215612a95575f80fd5b5051919050565b600181811c90821680612ab057607f821691505b602082108103612ace57634e487b7160e01b5f52602260045260245ffd5b50919050565b634e487b7160e01b5f52601160045260245ffd5b808201808211156107e2576107e2612ad4565b600181815b80851115612b3557815f1904821115612b1b57612b1b612ad4565b80851615612b2857918102915b93841c9390800290612b00565b509250929050565b5f82612b4b575060016107e2565b81612b5757505f6107e2565b8160018114612b6d5760028114612b7757612b93565b60019150506107e2565b60ff841115612b8857612b88612ad4565b50506001821b6107e2565b5060208310610133831016604e8410600b8410161715612bb6575081810a6107e2565b612bc08383612afb565b805f1904821115612bd357612bd3612ad4565b029392505050565b5f6106e18383612b3d565b634e487b7160e01b5f52604160045260245ffd5b634e487b7160e01b5f52603260045260245ffd5b80518015158114612647575f80fd5b5f60208284031215612c2d575f80fd5b6106e182612c0e565b602080825281018290525f6001600160fb1b03831115612c54575f80fd5b8260051b80856040850137919091016040019392505050565b60ff81811683821601908111156107e2576107e2612ad4565b6001600160a01b0395861681529385166020850152604084019290925283166060830152909116608082015260a00190565b604080519081016001600160401b0381118282101715612cda57612cda612be6565b60405290565b604051601f8201601f191681016001600160401b0381118282101715612d0857612d08612be6565b604052919050565b5f6020808385031215612d21575f80fd5b82516001600160401b0380821115612d37575f80fd5b9084019060408287031215612d4a575f80fd5b612d52612cb8565b612d5b83612c0e565b81528383015182811115612d6d575f80fd5b80840193505086601f840112612d81575f80fd5b825182811115612d9357612d93612be6565b612da5601f8201601f19168601612ce0565b92508083528785828601011115612dba575f80fd5b808585018685015e5f90830185015292830152509392505050565b818103818111156107e2576107e2612ad4565b634e487b7160e01b5f52603160045260245ffd5b60ff82811682821603908111156107e2576107e2612ad456fe5b2b3dbc0425e650141ab8840d86d0c9f083f7ddc44122d0056e9fb6b9797222ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef22e88b5eba316b174ad850666cd779e6e8ea04ebe64bb30f03875fc21841c4f0a2646970667358221220d00e0105c9418c150fd270107fa7b02741ffac89fa915218297ce69ac8d9860064736f6c63430008190033a2646970667358221220f690db39bb0767e25258a8598c3d790dbeccc1018631541fcd837785b273daea64736f6c63430008190033
Loading...
Loading
Loading...
Loading
Net Worth in USD
$0.00
Net Worth in ETH
0
Multichain Portfolio | 34 Chains
| Chain | Token | Portfolio % | Price | Amount | Value |
|---|
Loading...
Loading
Loading...
Loading
Loading...
Loading
[ Download: CSV Export ]
[ Download: CSV Export ]
A contract address hosts a smart contract, which is a set of code stored on the blockchain that runs when predetermined conditions are met. Learn more about addresses in our Knowledge Base.