Verified contractPartialMIT
LagoonSnapshot
Address0x2f0d4f97db3503a43b00f5bda7bd6bb5e7ea3cc9
- Balance
0 ETH
- Transactions
1
- Compiler
solc 0.8.38
- Same code
Only this contract
Details
- Compiler
- solc 0.8.38+commit.1835d247
- Language
- solidity
- Optimization
- Yes, 200 runs
- EVM version
- cancun
- Via IR
- yes
- License
- MIT
- Match
- Partialcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 26,444,141
- Creation transaction
- 0x7e7697a89c…c7929be332
contracts/LagoonSnapshot.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.38;
import {IERC4626Source} from "./base/SourceInterfaces.sol";
import {PagedReader} from "./base/PagedReader.sol";
contract LagoonSnapshot is PagedReader {
uint256 private constant SNAPSHOT_FRAME_BYTES = 10 * 32 + 2 * 32;
uint256 private constant QUOTE_FRAME_BYTES = 2 * 32 + 2 * 32;
struct Quote {
address asset;
uint256 assets;
}
struct Facts {
address asset;
uint8 state;
uint256 held;
uint256 pendingDeposit;
uint256 pendingRedeem;
uint256 heldAssets;
uint256 pendingRedeemAssets;
uint256 claimableDepositShares;
uint256 claimableDepositAssets;
uint256 claimableRedeemAssets;
}
function snapshot(address vault, address owner, address denomination)
external
view
returns (Facts memory f, bytes memory)
{
_requireResponseBudget(SNAPSHOT_FRAME_BYTES);
f.asset = _asset(vault);
bool paused = _readBool(vault, abi.encodeWithSignature("paused()"));
f.held = _balanceOf(vault, owner);
f.pendingDeposit = _readUint(vault, abi.encodeWithSignature("pendingDepositRequest(uint256,address)", 0, owner));
bool claimableDeposit =
_readUint(vault, abi.encodeWithSignature("claimableDepositRequest(uint256,address)", 0, owner)) != 0;
f.pendingRedeem = _readUint(vault, abi.encodeWithSignature("pendingRedeemRequest(uint256,address)", 0, owner));
bool claimableRedeem =
_readUint(vault, abi.encodeWithSignature("claimableRedeemRequest(uint256,address)", 0, owner)) != 0;
if ((f.pendingDeposit != 0 && claimableDeposit) || (f.pendingRedeem != 0 && claimableRedeem)) {
revert InvalidInput();
}
f.state = (paused ? 1 : 0) | (claimableDeposit ? 2 : 0) | (claimableRedeem ? 4 : 0);
if (f.held > 0 && denomination != vault) f.heldAssets = _convertToAssets(vault, f.held);
if (f.pendingRedeem > 0) f.pendingRedeemAssets = _convertToAssets(vault, f.pendingRedeem);
if (!paused && claimableDeposit) {
f.claimableDepositShares = _readUint(vault, abi.encodeCall(IERC4626Source.maxMint, (owner)));
if (f.claimableDepositShares > 0) {
f.claimableDepositAssets = _convertToAssets(vault, f.claimableDepositShares);
}
}
if (!paused && claimableRedeem) {
f.claimableRedeemAssets = _readUint(vault, abi.encodeCall(IERC4626Source.maxWithdraw, (owner)));
}
return (f, "");
}
function quote(address vault, uint256 shares) external view returns (Quote memory f, bytes memory) {
_requireResponseBudget(QUOTE_FRAME_BYTES);
return (Quote(_asset(vault), _convertToAssets(vault, shares)), "");
}
function _asset(address vault) private view returns (address asset) {
asset = _readAsset(vault);
if (asset == address(0) || asset == vault) revert InvalidInput();
}
}
contracts/base/BoundedStaticReader.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.38;
import {LowLevelCall} from "openzeppelin/utils/LowLevelCall.sol";
import {IERC20Source, IDecimalSource, IERC4626Source} from "./SourceInterfaces.sol";
/// @dev Static source calls and canonical scalar decoding.
abstract contract BoundedStaticReader {
error InvalidInput();
error SourceReadFailed(address target, bytes4 selector);
struct IndexedRead {
address target;
bytes input;
bytes reply;
}
function _indexedRead(address target, bytes4 selector, uint256 replyWidth)
internal
pure
returns (IndexedRead memory)
{
return IndexedRead(target, abi.encodeWithSelector(selector, uint256(0)), new bytes(replyWidth));
}
/// @dev The caller decodes the reply before advancing to the next row.
function _read(IndexedRead memory source, uint256 index) internal view returns (bytes memory reply) {
address target = source.target;
if (target == address(0)) revert InvalidInput();
bytes memory input = source.input;
reply = source.reply;
bool success;
assembly ("memory-safe") {
let payload := add(input, 32)
mstore(add(payload, 4), index)
success := staticcall(gas(), target, payload, mload(input), add(reply, 32), mload(reply))
}
if (!success || LowLevelCall.returnDataSize() != reply.length) {
revert SourceReadFailed(target, bytes4(input));
}
}
function _balanceOf(address token, address owner) internal view returns (uint256) {
return _readUint(token, abi.encodeCall(IERC20Source.balanceOf, (owner)));
}
function _decimals(address token) internal view returns (uint8) {
return uint8(_uint(_readUint(token, abi.encodeCall(IDecimalSource.decimals, ())), 8));
}
function _readAsset(address vault) internal view returns (address) {
return _readAddress(vault, abi.encodeCall(IERC4626Source.asset, ()));
}
function _readAddress(address target, bytes memory input) internal view returns (address) {
return _address(_readUint(target, input));
}
function _readBool(address target, bytes memory input) internal view returns (bool) {
return _bool(_readUint(target, input));
}
function _convertToAssets(address vault, uint256 shares) internal view returns (uint256) {
return _readUint(vault, abi.encodeCall(IERC4626Source.convertToAssets, (shares)));
}
function _previewRedeem(address vault, uint256 shares) internal view returns (uint256) {
return _readUint(vault, abi.encodeCall(IERC4626Source.previewRedeem, (shares)));
}
function _readUint(address target, bytes memory input) internal view returns (uint256 value) {
if (target == address(0) || input.length < 4) revert InvalidInput();
(bool success, bytes32 first,) = LowLevelCall.staticcallReturn64Bytes(target, input);
if (!success || LowLevelCall.returnDataSize() != 32) revert SourceReadFailed(target, bytes4(input));
return uint256(first);
}
function _probe(address target, bytes memory input, uint256 maxBytes)
internal
view
returns (bool success, bytes memory data)
{
if (target == address(0) || input.length < 4) revert InvalidInput();
(success, data) = target.staticcall(input);
if (success && data.length > maxBytes) revert SourceReadFailed(target, bytes4(input));
}
/// @dev Required reply of exactly `size` bytes.
function _read(address target, bytes memory input, uint256 size) internal view returns (bytes memory data) {
bool ok;
(ok, data) = _try(target, input, size);
if (!ok) revert SourceReadFailed(target, bytes4(input));
}
/// @dev Required variable-width reply; the caller's ABI decoding checks its shape.
function _read(address target, bytes memory input) internal view returns (bytes memory data) {
bool ok;
(ok, data) = target.staticcall(input);
if (!ok) revert SourceReadFailed(target, bytes4(input));
}
/// @dev Optional exact-width reply: a failed or short reply is unavailable; an oversized success still fails.
function _try(address target, bytes memory input, uint256 size) internal view returns (bool ok, bytes memory data) {
(ok, data) = _probe(target, input, size);
ok = ok && data.length == size;
}
function _tryUint(address target, bytes memory input) internal view returns (bool ok, uint256 value) {
bytes memory data;
(ok, data) = _try(target, input, 32);
if (ok) value = abi.decode(data, (uint256));
}
function _address(uint256 value) internal pure returns (address) {
if (value > type(uint160).max) revert InvalidInput();
return address(uint160(value));
}
function _bool(uint256 value) internal pure returns (bool) {
if (value > 1) revert InvalidInput();
return value == 1;
}
function _uint(uint256 value, uint256 bits) internal pure returns (uint256) {
if (bits < 256 && value >> bits != 0) revert InvalidInput();
return value;
}
}
contracts/base/PagedReader.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.38;
import {LowLevelCall} from "openzeppelin/utils/LowLevelCall.sol";
import {BoundedStaticReader} from "./BoundedStaticReader.sol";
/// @dev Paging is transport state; Go values the completed native facts.
abstract contract PagedReader is BoundedStaticReader {
// 100,000-byte CRE Await envelope minus 106 protobuf bytes, rounded to ABI words.
uint256 internal constant MAX_RESPONSE_BYTES = 99_872;
uint256 internal constant DEFAULT_PAGE_BYTES = MAX_RESPONSE_BYTES - 192;
error ResponseBudgetTooSmall(uint256 required);
error ResponseTooLarge(uint256 actual, uint256 budget);
struct PageState {
bytes cursor;
uint256 checkpointGas;
uint256 unitGas;
uint256 encodedBytes;
uint256 responseBudget;
bytes nextCursor;
}
function page(bytes calldata snapshotCall, bytes calldata continuation) external view {
if (msg.sender == address(this) || snapshotCall.length < 4) revert InvalidInput();
_page(snapshotCall, continuation, DEFAULT_PAGE_BYTES);
}
/// @dev The outer batch supplies its remaining response capacity before acquisition.
function read(bytes calldata snapshotCall, uint256 budget) external view {
if (msg.sender == address(this) || snapshotCall.length < 4) revert InvalidInput();
if (bytes4(snapshotCall[:4]) == this.page.selector) {
(bytes memory original, bytes memory continuation) = abi.decode(snapshotCall[4:], (bytes, bytes));
_page(original, continuation, budget);
} else {
_page(snapshotCall, bytes(""), budget);
}
}
function _page(bytes memory snapshotCall, bytes memory continuation, uint256 budget) private view {
(bool success, bytes memory result) =
address(this).staticcall(abi.encodePacked(snapshotCall, continuation, continuation.length, budget));
if (!success) LowLevelCall.bubbleRevert(result);
if (result.length > budget) revert ResponseTooLarge(result.length, budget);
assembly ("memory-safe") { return(add(result, 32), mload(result)) }
}
/// @dev A fixed reply needs the batch's budget check without allocating page state.
function _requireResponseBudget(uint256 required) internal view {
if (msg.sender != address(this)) return;
_checkResponseBudget(required, uint256(bytes32(msg.data[msg.data.length - 32:])));
}
function _checkResponseBudget(uint256 required, uint256 budget) private pure {
if (required > budget && budget < DEFAULT_PAGE_BYTES) revert ResponseBudgetTooSmall(required);
}
function _newPage(uint256 terminalBytes, uint256 maxUnitBytes, uint256 maxCursorBytes)
internal
view
returns (PageState memory state)
{
state.encodedBytes = terminalBytes;
state.responseBudget = DEFAULT_PAGE_BYTES;
if (msg.sender == address(this)) {
uint256 footer = msg.data.length - 64;
uint256 length = uint256(bytes32(msg.data[footer:]));
state.responseBudget = uint256(bytes32(msg.data[msg.data.length - 32:]));
state.cursor = msg.data[footer - length:footer];
}
// The cursor is an ABI tuple measured in full words.
_checkResponseBudget(terminalBytes + maxUnitBytes + maxCursorBytes, state.responseBudget);
}
function _yieldAtIndex(PageState memory state, uint256 index, uint256 nextUnitBytes) internal view returns (bool) {
if (!_shouldYield(state, 32, nextUnitBytes)) return false;
state.nextCursor = abi.encode(index);
return true;
}
// Always attempt the first indivisible unit. Reserve encoding/return memory
// using actual output size and the greatest observed unit cost on this page.
// The caller encodes the cursor only when yielding.
function _shouldYield(PageState memory state, uint256 cursorBytes, uint256 nextUnitBytes)
internal
view
returns (bool)
{
uint256 remaining = gasleft();
if (state.checkpointGas != 0) {
uint256 spent = state.checkpointGas > remaining ? state.checkpointGas - remaining : 0;
if (spent > state.unitGas) state.unitGas = spent;
uint256 free;
assembly ("memory-safe") { free := mload(0x40) }
uint256 encoded = state.encodedBytes + cursorBytes;
uint256 cursorAllocation = 32 + cursorBytes;
uint256 words = (free + cursorAllocation + 2 * encoded + 31) / 32;
uint256 current = (free + 31) / 32;
uint256 memoryGas = 3 * (words - current) + (words * words - current * current) / 512;
uint256 reserve = 40_000 + memoryGas + (encoded / 32) * 24;
if (
encoded + nextUnitBytes > state.responseBudget
|| remaining <= reserve + (state.unitGas > 10_000 ? state.unitGas : 10_000)
) {
return true;
}
}
state.checkpointGas = gasleft();
return false;
}
}
contracts/base/SourceInterfaces.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.38;
interface IERC20Source {
function balanceOf(address owner) external view returns (uint256);
function totalSupply() external view returns (uint256);
}
interface IDecimalSource {
function decimals() external view returns (uint8);
}
interface IERC4626Source {
function asset() external view returns (address);
function convertToAssets(uint256 shares) external view returns (uint256);
function previewRedeem(uint256 shares) external view returns (uint256);
function maxMint(address owner) external view returns (uint256);
function maxWithdraw(address owner) external view returns (uint256);
}
interface IPriceFeedSource {
function latestRoundData() external view returns (uint80, int256, uint256, uint256, uint80);
function getRoundData(uint80 roundId) external view returns (uint80, int256, uint256, uint256, uint80);
}
lib/openzeppelin/contracts/utils/LowLevelCall.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.6.0) (utils/LowLevelCall.sol)
pragma solidity ^0.8.20;
/**
* @dev Library of low level call functions that implement different calling strategies to deal with the return data.
*
* WARNING: Using this library requires an advanced understanding of Solidity and how the EVM works. It is recommended
* to use the {Address} library instead.
*/
library LowLevelCall {
/// @dev Performs a Solidity function call using a low level `call` and ignoring the return data.
function callNoReturn(address target, bytes memory data) internal returns (bool success) {
return callNoReturn(target, 0, data);
}
/// @dev Same as {callNoReturn-address-bytes}, but allows specifying the value to be sent in the call.
function callNoReturn(address target, uint256 value, bytes memory data) internal returns (bool success) {
assembly ("memory-safe") {
success := call(gas(), target, value, add(data, 0x20), mload(data), 0x00, 0x00)
}
}
/// @dev Performs a Solidity function call using a low level `call` and returns the first 64 bytes of the result
/// in the scratch space of memory. Useful for functions that return a tuple with two single-word values.
///
/// WARNING: Do not assume that the results are zero if `success` is false. Memory can be already allocated
/// and this function doesn't zero it out.
function callReturn64Bytes(
address target,
bytes memory data
) internal returns (bool success, bytes32 result1, bytes32 result2) {
return callReturn64Bytes(target, 0, data);
}
/// @dev Same as {callReturn64Bytes-address-bytes}, but allows specifying the value to be sent in the call.
function callReturn64Bytes(
address target,
uint256 value,
bytes memory data
) internal returns (bool success, bytes32 result1, bytes32 result2) {
assembly ("memory-safe") {
success := call(gas(), target, value, add(data, 0x20), mload(data), 0x00, 0x40)
result1 := mload(0x00)
result2 := mload(0x20)
}
}
/// @dev Performs a Solidity function call using a low level `staticcall` and ignoring the return data.
function staticcallNoReturn(address target, bytes memory data) internal view returns (bool success) {
assembly ("memory-safe") {
success := staticcall(gas(), target, add(data, 0x20), mload(data), 0x00, 0x00)
}
}
/// @dev Performs a Solidity function call using a low level `staticcall` and returns the first 64 bytes of the result
/// in the scratch space of memory. Useful for functions that return a tuple with two single-word values.
///
/// WARNING: Do not assume that the results are zero if `success` is false. Memory can be already allocated
/// and this function doesn't zero it out.
function staticcallReturn64Bytes(
address target,
bytes memory data
) internal view returns (bool success, bytes32 result1, bytes32 result2) {
assembly ("memory-safe") {
success := staticcall(gas(), target, add(data, 0x20), mload(data), 0x00, 0x40)
result1 := mload(0x00)
result2 := mload(0x20)
}
}
/// @dev Performs a Solidity function call using a low level `delegatecall` and ignoring the return data.
function delegatecallNoReturn(address target, bytes memory data) internal returns (bool success) {
assembly ("memory-safe") {
success := delegatecall(gas(), target, add(data, 0x20), mload(data), 0x00, 0x00)
}
}
/// @dev Performs a Solidity function call using a low level `delegatecall` and returns the first 64 bytes of the result
/// in the scratch space of memory. Useful for functions that return a tuple with two single-word values.
///
/// WARNING: Do not assume that the results are zero if `success` is false. Memory can be already allocated
/// and this function doesn't zero it out.
function delegatecallReturn64Bytes(
address target,
bytes memory data
) internal returns (bool success, bytes32 result1, bytes32 result2) {
assembly ("memory-safe") {
success := delegatecall(gas(), target, add(data, 0x20), mload(data), 0x00, 0x40)
result1 := mload(0x00)
result2 := mload(0x20)
}
}
/// @dev Returns the size of the return data buffer.
function returnDataSize() internal pure returns (uint256 size) {
assembly ("memory-safe") {
size := returndatasize()
}
}
/// @dev Returns a buffer containing the return data from the last call.
function returnData() internal pure returns (bytes memory result) {
assembly ("memory-safe") {
result := mload(0x40)
mstore(result, returndatasize())
returndatacopy(add(result, 0x20), 0x00, returndatasize())
mstore(0x40, add(result, add(0x20, returndatasize())))
}
}
/// @dev Revert with the return data from the last call.
function bubbleRevert() internal pure {
assembly ("memory-safe") {
let fmp := mload(0x40)
returndatacopy(fmp, 0x00, returndatasize())
revert(fmp, returndatasize())
}
}
function bubbleRevert(bytes memory returndata) internal pure {
assembly ("memory-safe") {
revert(add(returndata, 0x20), mload(returndata))
}
}
}
Compiler settingsShow
{
"viaIR": true,
"metadata": {
"appendCBOR": true,
"bytecodeHash": "none",
"useLiteralContent": false
},
"libraries": {},
"optimizer": {
"runs": 200,
"enabled": true
},
"viaSSACFG": false,
"evmVersion": "cancun",
"remappings": [
"solady/=lib/solady/src/",
"openzeppelin/=lib/openzeppelin/contracts/"
],
"experimental": false
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.