Verified contractPartialMIT
ParetoSnapshot
Address0x78d74bb1bfe713fc697bb3c8aa1985a6326a2e16
- 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,339,250
- Creation transaction
- 0x45354a4f3b…59e910cb5d
5 files
contracts/ParetoSnapshot.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.38;
import {ParetoFeeSnapshot} from "./base/ParetoFeeSnapshot.sol";
import {DynamicArrayLib} from "solady/utils/DynamicArrayLib.sol";
/// @notice One complete CDO/owner snapshot, including deposit and withdrawal lifecycles.
/// @dev Scans both owner ledgers; source facts alone are returned. Go owns reconciliation and economics.
contract ParetoSnapshot is ParetoFeeSnapshot {
struct Cursor {
uint256 phase;
uint256 next;
bool backing;
bool queueClaim;
}
struct Proofs {
uint256[] withdrawals;
uint256[] deposits;
uint256 withdrawalCount;
uint256 depositCount;
}
function snapshot(
address owner,
address cdo,
address[] calldata tokens,
address queue,
bool legacyReceipt,
bool legacyQueue,
uint256 firstEpoch
) external view returns (SourceResult[] memory) {
if (owner == address(0) || cdo == address(0) || tokens.length == 0) revert InvalidInput();
Transcript memory t = _newTranscript();
Cursor memory cursor;
if (t.cursor.length != 0) cursor = abi.decode(t.cursor, (Cursor));
address strategy = _address(_readUint(t, cdo, abi.encodeWithSignature("strategy()")));
address asset = _address(_readUint(t, cdo, abi.encodeWithSignature("token()")));
_read(t, cdo, abi.encodeWithSignature("defaulted()"), 32);
_read(t, cdo, abi.encodeWithSignature("ONE_TRANCHE_TOKEN()"), 32);
if (asset != address(0)) {
_read(t, asset, abi.encodeWithSignature("balanceOf(address)", owner), 32);
_read(t, asset, abi.encodeWithSignature("decimals()"), 32);
}
for (uint256 i = cursor.phase == 0 ? cursor.next : tokens.length; i < tokens.length; ++i) {
if (_pause(t, abi.encode(Cursor(0, i, false, false)))) return _finish(t);
_read(t, tokens[i], abi.encodeWithSignature("balanceOf(address)", owner), 32);
if (tokens[i] != strategy) {
_read(t, cdo, abi.encodeWithSignature("virtualPrice(address)", tokens[i]), 32);
if (!legacyReceipt) _readParetoFees(t, cdo, tokens[i]);
}
}
if (cursor.phase <= 1) {
if (_pause(t, abi.encode(Cursor(1, 0, false, false)))) return _finish(t);
_readClaim(t, cdo, strategy, owner, legacyReceipt);
}
if (queue != address(0)) {
_readQueue(t, owner, cdo, strategy, asset, tokens, queue, legacyReceipt, legacyQueue, firstEpoch, cursor);
}
return _finish(t);
}
function _readQueue(
Transcript memory t,
address owner,
address cdo,
address strategy,
address asset,
address[] calldata tokens,
address queue,
bool legacyReceipt,
bool legacyQueue,
uint256 firstEpoch,
Cursor memory cursor
) private view {
_read(t, queue, abi.encodeWithSignature("underlying()"), 32);
_read(t, queue, abi.encodeWithSignature("idleCDOEpoch()"), 32);
address queueStrategy = _address(_readUint(t, queue, abi.encodeWithSignature("strategy()")));
uint256 current = _readUint(t, queueStrategy, abi.encodeWithSignature("epochNumber()"));
uint256 end = current + 2;
if (firstEpoch >= end) revert InvalidInput();
bool queueClaim = cursor.queueClaim;
bool backing = cursor.backing;
uint256 start = cursor.phase == 2 ? cursor.next : firstEpoch;
uint256 epoch = cursor.phase <= 2 ? start : end;
Proofs memory proof = Proofs(DynamicArrayLib.malloc(4), DynamicArrayLib.malloc(4), 0, 0);
for (; epoch < end; ++epoch) {
uint256 scratch;
assembly ("memory-safe") { scratch := mload(0x40) }
if (_pause(t, abi.encode(Cursor(2, epoch, backing, queueClaim)))) break;
uint256 deposits =
_readUint(queue, abi.encodeWithSignature("userDepositsEpochs(address,uint256)", owner, epoch));
if (deposits != 0) {
backing = true;
uint256[6] memory depositRow = _readDeposit(t, queue, epoch, deposits, legacyQueue);
proof.deposits = _grow(proof.deposits, proof.depositCount);
uint256 depositPointer;
assembly ("memory-safe") { depositPointer := depositRow }
proof.deposits[proof.depositCount++] = depositPointer;
t.encodedBytes += 192;
}
uint256 shares =
_readUint(queue, abi.encodeWithSignature("userWithdrawalsEpochs(address,uint256)", owner, epoch));
if (shares == 0) {
// Empty epochs retain only stack scalars, so both probe buffers can be reused.
if (deposits == 0) {
assembly ("memory-safe") { mstore(0x40, scratch) }
}
continue;
}
uint256 price = _readUint(queue, abi.encodeWithSignature("epochWithdrawPrice(uint256)", epoch));
uint256 pending = _readUint(queue, abi.encodeWithSignature("epochPendingClaims(uint256)", epoch));
uint256[4] memory row = [epoch, shares, price, pending];
proof.withdrawals = _grow(proof.withdrawals, proof.withdrawalCount);
uint256 pointer;
assembly ("memory-safe") { pointer := row }
proof.withdrawals[proof.withdrawalCount++] = pointer;
t.encodedBytes += 128;
if (price == 0 && pending == 0) {
bool terminalZero;
if (!legacyQueue) {
terminalZero =
_bool(_readUint(t, queue, abi.encodeWithSignature("isEpochWithdrawZero(uint256)", epoch)));
}
if (!terminalZero) {
backing = true;
_read(t, queue, abi.encodeWithSignature("epochPendingWithdrawals(uint256)", epoch), 32);
}
} else if (price != 0 && pending != 0) {
_read(t, queue, abi.encodeWithSignature("isEpochInstant(uint256)", epoch), 32);
queueClaim = true;
}
}
if (cursor.phase <= 2 && start < epoch) {
_recordProofs(t, queue, owner, start, epoch, proof);
}
if (t.paused) return;
if (cursor.phase <= 3) {
if (_pause(t, abi.encode(Cursor(3, 0, backing, queueClaim)))) return;
if (backing) {
_read(t, queue, abi.encodeWithSignature("tranche()"), 32);
if (asset != address(0)) _read(t, asset, abi.encodeWithSignature("balanceOf(address)", queue), 32);
}
}
if (backing) {
for (uint256 i = cursor.phase == 4 ? cursor.next : 0; cursor.phase <= 4 && i < tokens.length; ++i) {
if (_pause(t, abi.encode(Cursor(4, i, backing, queueClaim)))) return;
if (tokens[i] != strategy) {
_read(t, tokens[i], abi.encodeWithSignature("balanceOf(address)", queue), 32);
}
}
}
if (queueClaim) {
if (_pause(t, abi.encode(Cursor(5, 0, backing, queueClaim)))) return;
_readClaim(t, cdo, strategy, queue, legacyReceipt);
}
}
function _grow(uint256[] memory slots, uint256 used) private pure returns (uint256[] memory grown) {
if (used < slots.length) return slots;
grown = DynamicArrayLib.malloc(slots.length * 2);
for (uint256 i; i < used; ++i) {
grown[i] = slots[i];
}
}
function _recordProofs(
Transcript memory t,
address queue,
address owner,
uint256 start,
uint256 end,
Proofs memory proof
) private pure {
DynamicArrayLib.truncate(proof.withdrawals, proof.withdrawalCount);
DynamicArrayLib.truncate(proof.deposits, proof.depositCount);
uint256[4][] memory withdrawals;
uint256[6][] memory deposits;
uint256[] memory withdrawalSlots = proof.withdrawals;
uint256[] memory depositSlots = proof.deposits;
assembly ("memory-safe") {
withdrawals := withdrawalSlots
deposits := depositSlots
}
t.encodedBytes -= proof.withdrawalCount * 128 + proof.depositCount * 192;
bool ranged = t.paused || t.cursor.length != 0;
_proof(
t,
queue,
ranged
? abi.encodeWithSignature("snapshotWithdrawals(address,uint256,uint256)", owner, start, end)
: abi.encodeWithSignature("snapshotWithdrawals(address)", owner),
abi.encode(withdrawals)
);
_proof(
t,
queue,
ranged
? abi.encodeWithSignature("snapshotDeposits(address,uint256,uint256)", owner, start, end)
: abi.encodeWithSignature("snapshotDeposits(address)", owner),
abi.encode(deposits)
);
}
function _proof(Transcript memory t, address queue, bytes memory input, bytes memory data) private pure {
_record(t, queue, input, true, data);
if (t.paused) {
SourceResult memory control = t.rows[t.used - 2];
t.rows[t.used - 2] = t.rows[t.used - 1];
t.rows[t.used - 1] = control;
}
}
function _readDeposit(Transcript memory t, address queue, uint256 epoch, uint256 deposits, bool legacy)
private
view
returns (uint256[6] memory row)
{
row[0] = epoch;
row[1] = deposits;
string[3] memory getters =
["epochPrice(uint256)", "epochPendingDeposits(uint256)", "epochPrefundedDeposits(uint256)"];
for (uint256 i; i < (legacy ? 2 : 3); ++i) {
bytes memory input = abi.encodeWithSignature(getters[i], epoch);
(bool ok, bytes memory data) = _probe(queue, input, 32);
if (ok && data.length == 32) {
row[2 + i] = abi.decode(data, (uint256));
row[5] |= uint256(1) << i;
} else {
// Failed source facts remain primitive errors; no substitute zero.
_record(t, queue, input, ok, data);
}
}
}
function _readClaim(Transcript memory t, address cdo, address strategy, address owner, bool legacy) private view {
uint256 balance = _readUint(t, strategy, abi.encodeWithSignature("balanceOf(address)", owner));
uint256 normal = _readUint(t, strategy, abi.encodeWithSignature("withdrawsRequests(address)", owner));
uint256 instant = _readUint(t, strategy, abi.encodeWithSignature("instantWithdrawsRequests(address)", owner));
if (legacy) return;
bytes memory apr = _read(t, strategy, abi.encodeWithSignature("apr0Users(address)", owner), 128);
if (apr.length != 128) revert InvalidInput();
(uint256 principal, uint256 principalEpoch, uint256 settled, uint256 interest) =
abi.decode(apr, (uint256, uint256, uint256, uint256));
uint256 postDefault = _readUint(t, strategy, abi.encodeWithSignature("postDefaultRequests(address)", owner));
// A zero balance still requires the complete owner-bound principal ledger.
if (
balance == 0 && normal == 0 && instant == 0 && principal == 0 && settled == 0 && interest == 0
&& postDefault == 0
) return;
uint256 epoch = _readUint(t, strategy, abi.encodeWithSignature("epochNumber()"));
_read(t, cdo, abi.encodeWithSignature("epochEndDate()"), 32);
bool finalized = _bool(_readUint(t, strategy, abi.encodeWithSignature("defaultRecoveryFinalized()")));
uint256 last = _readUint(t, strategy, abi.encodeWithSignature("lastWithdrawRequest(address)", owner));
uint256 loss = _readUint(t, strategy, abi.encodeWithSignature("lossRecoveryPriceByEpoch(uint256)", last));
uint256 defaultEpoch;
if (finalized) {
defaultEpoch = _readUint(t, strategy, abi.encodeWithSignature("defaultRecoveryEpoch()"));
_read(t, strategy, abi.encodeWithSignature("defaultRecoveryPrice()"), 32);
bool instantFinalized =
_bool(_readUint(t, strategy, abi.encodeWithSignature("defaultInstantWithdrawsFinalized()")));
_read(
t,
strategy,
abi.encodeWithSignature("withdrawsRequestsByEpoch(address,uint256)", owner, defaultEpoch),
32
);
if (instantFinalized) {
_read(
t,
strategy,
abi.encodeWithSignature("instantWithdrawsRequestsByEpoch(address,uint256)", owner, defaultEpoch),
32
);
}
}
if (principal != 0 && (principalEpoch < epoch || finalized && principalEpoch == defaultEpoch)) {
_read(t, strategy, abi.encodeWithSignature("apr0RateByEpoch(uint256)", principalEpoch), 32);
}
// This optional prefetch is only required by Go when that recovery route applies.
if (loss != 0) {
_read(t, strategy, abi.encodeWithSignature("withdrawsRequestsByEpoch(address,uint256)", owner, last), 32);
}
}
}
contracts/base/BoundedStaticReader.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.38;
/// @dev Static source calls and canonical scalar decoding.
abstract contract BoundedStaticReader {
error InvalidInput();
error SourceReadFailed(address target, bytes4 selector);
function _readUint(address target, bytes memory input) internal view returns (uint256 value) {
(bool success, bytes memory data) = _probe(target, input, 32);
if (!success || data.length != 32) revert SourceReadFailed(target, bytes4(input));
return abi.decode(data, (uint256));
}
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));
}
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/ParetoFeeSnapshot.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.38;
import {SnapshotReader} from "./SnapshotReader.sol";
abstract contract ParetoFeeSnapshot is SnapshotReader {
function _readParetoFees(Transcript memory t, address cdo, address tranche) internal view {
// ABI variants without this getter retain the failed fact. Go requires
// it only for deployments whose configured accounting supports the fee.
(bool usable, uint256 fee) = _readOptionalUint(t, cdo, abi.encodeWithSignature("managementFee()"));
if (!usable || fee == 0) return;
address strategyToken = _address(_readUint(t, cdo, abi.encodeWithSignature("strategyToken()")));
_readUint(t, strategyToken, abi.encodeWithSignature("balanceOf(address)", cdo));
_readUint(t, cdo, abi.encodeWithSignature("unclaimedFees()"));
_readUint(t, cdo, abi.encodeWithSignature("latestHarvestBlock()"));
_readUint(t, cdo, abi.encodeWithSignature("lastNAVAA()"));
_readUint(t, cdo, abi.encodeWithSignature("lastNAVBB()"));
_readUint(t, cdo, abi.encodeWithSignature("trancheAPRSplitRatio()"));
_readUint(t, cdo, abi.encodeWithSignature("fee()"));
_readUint(t, cdo, abi.encodeWithSignature("priceAA()"));
_readUint(t, cdo, abi.encodeWithSignature("priceBB()"));
_readUint(t, cdo, abi.encodeWithSignature("AATranche()"));
_readUint(t, cdo, abi.encodeWithSignature("BBTranche()"));
_readUint(t, tranche, abi.encodeWithSignature("totalSupply()"));
}
}
contracts/base/SnapshotReader.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.38;
import {BoundedStaticReader} from "./BoundedStaticReader.sol";
import {DynamicArrayLib} from "solady/utils/DynamicArrayLib.sol";
/// @dev Collects source replies. Consumers validate and value these facts in Go.
abstract contract SnapshotReader is BoundedStaticReader {
struct SourceResult {
address target;
bytes input;
bool success;
bytes data;
}
struct Transcript {
SourceResult[] rows;
uint256 used;
bytes cursor;
bool paused;
uint256 progress;
uint256 encodedBytes;
uint256 checkpointGas;
uint256 unitGas;
}
function page(bytes calldata snapshotCall, bytes calldata continuation)
external
view
returns (SourceResult[] memory)
{
if (msg.sender == address(this) || snapshotCall.length < 4) revert InvalidInput();
(bool success, bytes memory result) =
address(this).staticcall(abi.encodePacked(snapshotCall, continuation, uint256(continuation.length)));
if (!success) {
assembly ("memory-safe") { revert(add(result, 32), mload(result)) }
}
assembly ("memory-safe") { return(add(result, 32), mload(result)) }
}
function _newTranscript() internal view returns (Transcript memory t) {
uint256[] memory slots = DynamicArrayLib.malloc(64);
SourceResult[] memory rows;
// Only populated pointer slots are read.
assembly ("memory-safe") { rows := slots }
t.rows = rows;
t.encodedBytes = 64;
if (msg.sender == address(this)) {
uint256 length;
assembly ("memory-safe") { length := calldataload(sub(calldatasize(), 32)) }
bytes calldata continuation = msg.data[msg.data.length - 32 - length:msg.data.length - 32];
if (length != 0) {
(t.progress, t.cursor) = abi.decode(continuation, (uint256, bytes));
}
}
}
// Checkpoints precede complete resumable units; the first unit always gets an attempt.
function _pause(Transcript memory t, bytes memory nextState) internal view returns (bool) {
uint256 remaining = gasleft();
if (t.checkpointGas != 0) {
uint256 spent = t.checkpointGas > remaining ? t.checkpointGas - remaining : 0;
if (spent > t.unitGas) t.unitGas = spent;
uint256 free;
assembly ("memory-safe") { free := mload(0x40) }
// Reserve ABI construction plus the outer self-call return copy.
uint256 encodedBytes = t.encodedBytes + 352 + ((nextState.length + 31) & ~uint256(31));
uint256 words = (free + 2 * encodedBytes + 31) / 32;
uint256 currentWords = (free + 31) / 32;
uint256 memoryGas = 3 * (words - currentWords) + (words * words - currentWords * currentWords) / 512;
uint256 reserve = 30_000 + memoryGas + t.used * 500 + (encodedBytes / 32) * 24;
uint256 nextGas = t.unitGas > 10_000 ? t.unitGas : 10_000;
if (remaining <= reserve + nextGas) {
_record(
t,
address(this),
abi.encodeWithSignature("snapshotContinuation()"),
true,
abi.encode(t.progress, nextState)
);
t.paused = true;
return true;
}
}
t.checkpointGas = gasleft();
++t.progress;
return false;
}
function _record(Transcript memory t, address target, bytes memory input, bool success, bytes memory data)
internal
pure
{
if (t.used == t.rows.length) {
uint256[] memory slots = DynamicArrayLib.malloc(t.rows.length * 2);
SourceResult[] memory rows;
assembly ("memory-safe") { rows := slots }
for (uint256 i; i < t.used; ++i) {
rows[i] = t.rows[i];
}
t.rows = rows;
}
t.rows[t.used++] = SourceResult(target, input, success, data);
t.encodedBytes += 224 + ((input.length + 31) & ~uint256(31)) + ((data.length + 31) & ~uint256(31));
}
function _read(Transcript memory t, address target, bytes memory input, uint256 maxBytes)
internal
view
returns (bytes memory data)
{
bool success;
(success, data) = _probe(target, input, maxBytes);
_record(t, target, input, success, data);
}
function _readUint(Transcript memory t, address target, bytes memory input) internal view returns (uint256 value) {
(bool success, bytes memory data) = _probe(target, input, 32);
if (!success || data.length != 32) revert SourceReadFailed(target, bytes4(input));
value = abi.decode(data, (uint256));
_record(t, target, input, true, data);
}
function _finish(Transcript memory t) internal pure returns (SourceResult[] memory rows) {
rows = t.rows;
uint256 used = t.used;
assembly ("memory-safe") { mstore(rows, used) }
}
// Go owns admission and valuation. Collect exact full-amount source probes
// for its normalization, including negative probes. Cycles stop acquisition;
// Go rejects them when normalization requires the repeated asset.
function _readWrapper(Transcript memory t, address asset, uint256 amount) internal view {
if (amount == 0) return;
address[] memory seen = new address[](8);
for (uint256 depth; asset != address(0); ++depth) {
for (uint256 i; i < depth; ++i) {
if (seen[i] == asset) return;
}
if (depth == seen.length) {
address[] memory grown = new address[](seen.length * 2);
for (uint256 i; i < depth; ++i) {
grown[i] = seen[i];
}
seen = grown;
}
seen[depth] = asset;
bool ok;
address next;
(bool addressOK, uint256 addressWord) = _readOptionalUint(t, asset, abi.encodeWithSignature("asset()"));
(ok, amount) = _readOptionalUint(t, asset, abi.encodeWithSignature("previewRedeem(uint256)", amount));
if (!addressOK || addressWord == 0 || addressWord > type(uint160).max) return;
next = address(uint160(addressWord));
if (!ok) return;
asset = next;
}
}
function _readNormalization(Transcript memory t, address target, bytes memory input)
internal
view
returns (bool success, bytes memory data)
{
bytes32 key = keccak256(input);
for (uint256 i; i < t.used; ++i) {
SourceResult memory previous = t.rows[i];
if (previous.target == target && keccak256(previous.input) == key) {
return (previous.success, previous.data);
}
}
(success, data) = target.staticcall(input);
_record(t, target, input, success, data);
}
// Invalid optional facts stay in the transcript; Go decides whether they are needed.
function _readOptionalUint(Transcript memory t, address target, bytes memory input)
internal
view
returns (bool usable, uint256 value)
{
(bool success, bytes memory data) = _readNormalization(t, target, input);
if (!success || data.length != 32) return (false, 0);
return (true, abi.decode(data, (uint256)));
}
}
lib/solady/src/utils/DynamicArrayLib.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
/// @notice Library for memory arrays with automatic capacity resizing.
/// @author Solady (https://github.com/vectorized/solady/blob/main/src/utils/DynamicArrayLib.sol)
library DynamicArrayLib {
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* STRUCTS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Type to represent a dynamic array in memory.
/// You can directly assign to `data`, and the `p` function will
/// take care of the memory allocation.
///
/// Note: The capacity is a prime-checksummed word before the array. If you assign
/// an array you did not allocate (e.g. from calldata), that word can
/// masquerade as a capacity, making `p` skip reallocation and write out of bounds.
/// Arrays brought in via `wrap` carry the same risk, as `wrap` assigns to `data` too.
/// If you are not assigning to `data` or using `wrap`, you don't have to worry about it.
struct DynamicArray {
uint256[] data;
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CONSTANTS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev The constant returned when the element is not found in the array.
uint256 internal constant NOT_FOUND = type(uint256).max;
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* UINT256 ARRAY OPERATIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
// Low level minimalist uint256 array operations.
// If you don't need syntax sugar, it's recommended to use these.
// Some of these functions return the same array for function chaining.
// e.g. `array.set(0, 1).set(1, 2)`.
/// @dev Returns a uint256 array with `n` elements. The elements are not zeroized.
function malloc(uint256 n) internal pure returns (uint256[] memory result) {
/// @solidity memory-safe-assembly
assembly {
result := or(sub(0, shr(32, n)), mload(0x40))
mstore(result, n)
mstore(0x40, add(add(result, 0x20), shl(5, n)))
}
}
/// @dev Zeroizes all the elements of `a`.
function zeroize(uint256[] memory a) internal pure returns (uint256[] memory result) {
/// @solidity memory-safe-assembly
assembly {
result := a
calldatacopy(add(result, 0x20), calldatasize(), shl(5, mload(result)))
}
}
/// @dev Returns the element at `a[i]`, without bounds checking.
function get(uint256[] memory a, uint256 i) internal pure returns (uint256 result) {
/// @solidity memory-safe-assembly
assembly {
result := mload(add(add(a, 0x20), shl(5, i)))
}
}
/// @dev Returns the element at `a[i]`, without bounds checking.
function getUint256(uint256[] memory a, uint256 i) internal pure returns (uint256 result) {
/// @solidity memory-safe-assembly
assembly {
result := mload(add(add(a, 0x20), shl(5, i)))
}
}
/// @dev Returns the element at `a[i]`, without bounds checking.
function getAddress(uint256[] memory a, uint256 i) internal pure returns (address result) {
/// @solidity memory-safe-assembly
assembly {
result := mload(add(add(a, 0x20), shl(5, i)))
}
}
/// @dev Returns the element at `a[i]`, without bounds checking.
function getBool(uint256[] memory a, uint256 i) internal pure returns (bool result) {
/// @solidity memory-safe-assembly
assembly {
result := mload(add(add(a, 0x20), shl(5, i)))
}
}
/// @dev Returns the element at `a[i]`, without bounds checking.
function getBytes32(uint256[] memory a, uint256 i) internal pure returns (bytes32 result) {
/// @solidity memory-safe-assembly
assembly {
result := mload(add(add(a, 0x20), shl(5, i)))
}
}
/// @dev Sets `a.data[i]` to `data`, without bounds checking.
function set(uint256[] memory a, uint256 i, uint256 data)
internal
pure
returns (uint256[] memory result)
{
result = a;
/// @solidity memory-safe-assembly
assembly {
mstore(add(add(result, 0x20), shl(5, i)), data)
}
}
/// @dev Sets `a.data[i]` to `data`, without bounds checking.
function set(uint256[] memory a, uint256 i, address data)
internal
pure
returns (uint256[] memory result)
{
result = a;
/// @solidity memory-safe-assembly
assembly {
mstore(add(add(result, 0x20), shl(5, i)), shr(96, shl(96, data)))
}
}
/// @dev Sets `a.data[i]` to `data`, without bounds checking.
function set(uint256[] memory a, uint256 i, bool data)
internal
pure
returns (uint256[] memory result)
{
result = a;
/// @solidity memory-safe-assembly
assembly {
mstore(add(add(result, 0x20), shl(5, i)), iszero(iszero(data)))
}
}
/// @dev Sets `a.data[i]` to `data`, without bounds checking.
function set(uint256[] memory a, uint256 i, bytes32 data)
internal
pure
returns (uint256[] memory result)
{
result = a;
/// @solidity memory-safe-assembly
assembly {
mstore(add(add(result, 0x20), shl(5, i)), data)
}
}
/// @dev Casts `a` to `address[]`.
function asAddressArray(uint256[] memory a) internal pure returns (address[] memory result) {
/// @solidity memory-safe-assembly
assembly {
result := a
}
}
/// @dev Casts `a` to `bool[]`.
function asBoolArray(uint256[] memory a) internal pure returns (bool[] memory result) {
/// @solidity memory-safe-assembly
assembly {
result := a
}
}
/// @dev Casts `a` to `bytes32[]`.
function asBytes32Array(uint256[] memory a) internal pure returns (bytes32[] memory result) {
/// @solidity memory-safe-assembly
assembly {
result := a
}
}
/// @dev Casts `a` to `uint256[]`.
function toUint256Array(address[] memory a) internal pure returns (uint256[] memory result) {
/// @solidity memory-safe-assembly
assembly {
result := a
}
}
/// @dev Casts `a` to `uint256[]`.
function toUint256Array(bool[] memory a) internal pure returns (uint256[] memory result) {
/// @solidity memory-safe-assembly
assembly {
result := a
}
}
/// @dev Casts `a` to `uint256[]`.
function toUint256Array(bytes32[] memory a) internal pure returns (uint256[] memory result) {
/// @solidity memory-safe-assembly
assembly {
result := a
}
}
/// @dev Reduces the size of `a` to `n`.
/// If `n` is greater than the size of `a`, this will be a no-op.
function truncate(uint256[] memory a, uint256 n)
internal
pure
returns (uint256[] memory result)
{
/// @solidity memory-safe-assembly
assembly {
result := a
mstore(mul(lt(n, mload(result)), result), n)
}
}
/// @dev Clears the array and attempts to free the memory if possible.
function free(uint256[] memory a) internal pure returns (uint256[] memory result) {
/// @solidity memory-safe-assembly
assembly {
result := a
let n := mload(result)
mstore(shl(6, lt(iszero(n), eq(add(shl(5, add(1, n)), result), mload(0x40)))), result)
mstore(result, 0)
}
}
/// @dev Equivalent to `keccak256(abi.encodePacked(a))`.
function hash(uint256[] memory a) internal pure returns (bytes32 result) {
/// @solidity memory-safe-assembly
assembly {
result := keccak256(add(a, 0x20), shl(5, mload(a)))
}
}
/// @dev Returns a copy of `a` sliced from `start` to `end` (exclusive).
function slice(uint256[] memory a, uint256 start, uint256 end)
internal
pure
returns (uint256[] memory result)
{
/// @solidity memory-safe-assembly
assembly {
let arrayLen := mload(a)
if iszero(gt(arrayLen, end)) { end := arrayLen }
if iszero(gt(arrayLen, start)) { start := arrayLen }
if lt(start, end) {
result := mload(0x40)
let resultLen := sub(end, start)
mstore(result, resultLen)
a := add(a, shl(5, start))
// Copy the `a` one word at a time, backwards.
let o := shl(5, resultLen)
mstore(0x40, add(add(result, o), 0x20)) // Allocate memory.
for {} 1 {} {
mstore(add(result, o), mload(add(a, o)))
o := sub(o, 0x20)
if iszero(o) { break }
}
}
}
}
/// @dev Returns a copy of `a` sliced from `start` to the end of the array.
function slice(uint256[] memory a, uint256 start)
internal
pure
returns (uint256[] memory result)
{
result = slice(a, start, type(uint256).max);
}
/// @dev Returns a copy of the array.
function copy(uint256[] memory a) internal pure returns (uint256[] memory result) {
/// @solidity memory-safe-assembly
assembly {
result := mload(0x40)
let end := add(add(result, 0x20), shl(5, mload(a)))
let o := result
for { let d := sub(a, result) } 1 {} {
mstore(o, mload(add(o, d)))
o := add(0x20, o)
if eq(o, end) { break }
}
mstore(0x40, o)
}
}
/// @dev Returns if `needle` is in `a`.
function contains(uint256[] memory a, uint256 needle) internal pure returns (bool) {
return ~indexOf(a, needle, 0) != 0;
}
/// @dev Returns the first index of `needle`, scanning forward from `from`.
/// If `needle` is not in `a`, returns `NOT_FOUND`.
function indexOf(uint256[] memory a, uint256 needle, uint256 from)
internal
pure
returns (uint256 result)
{
/// @solidity memory-safe-assembly
assembly {
result := not(0)
if lt(from, mload(a)) {
let o := add(a, shl(5, from))
let end := add(shl(5, add(1, mload(a))), a)
let c := mload(end) // Cache the word after the array.
for { mstore(end, needle) } 1 {} {
o := add(o, 0x20)
if eq(mload(o), needle) { break }
}
mstore(end, c) // Restore the word after the array.
if iszero(eq(o, end)) { result := shr(5, sub(o, add(0x20, a))) }
}
}
}
/// @dev Returns the first index of `needle`.
/// If `needle` is not in `a`, returns `NOT_FOUND`.
function indexOf(uint256[] memory a, uint256 needle) internal pure returns (uint256 result) {
result = indexOf(a, needle, 0);
}
/// @dev Returns the last index of `needle`, scanning backwards from `from`.
/// If `needle` is not in `a`, returns `NOT_FOUND`.
function lastIndexOf(uint256[] memory a, uint256 needle, uint256 from)
internal
pure
returns (uint256 result)
{
/// @solidity memory-safe-assembly
assembly {
result := not(0)
let n := mload(a)
if n {
if iszero(lt(from, n)) { from := sub(n, 1) }
let o := add(shl(5, add(2, from)), a)
for { mstore(a, needle) } 1 {} {
o := sub(o, 0x20)
if eq(mload(o), needle) { break }
}
mstore(a, n) // Restore the length.
if iszero(eq(o, a)) { result := shr(5, sub(o, add(0x20, a))) }
}
}
}
/// @dev Returns the first index of `needle`.
/// If `needle` is not in `a`, returns `NOT_FOUND`.
function lastIndexOf(uint256[] memory a, uint256 needle)
internal
pure
returns (uint256 result)
{
result = lastIndexOf(a, needle, NOT_FOUND);
}
/// @dev Directly returns `a` without copying.
function directReturn(uint256[] memory a) internal pure {
/// @solidity memory-safe-assembly
assembly {
let retStart := sub(a, 0x20)
mstore(retStart, 0x20)
return(retStart, add(0x40, shl(5, mload(a))))
}
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* DYNAMIC ARRAY OPERATIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
// Some of these functions return the same array for function chaining.
// e.g. `a.p("1").p("2")`.
/// @dev Shorthand for `a.data.length`.
function length(DynamicArray memory a) internal pure returns (uint256) {
return a.data.length;
}
/// @dev Wraps `a` in a dynamic array struct.
function wrap(uint256[] memory a) internal pure returns (DynamicArray memory result) {
result.data = a;
}
/// @dev Wraps `a` in a dynamic array struct.
function wrap(address[] memory a) internal pure returns (DynamicArray memory result) {
/// @solidity memory-safe-assembly
assembly {
mstore(result, a)
}
}
/// @dev Wraps `a` in a dynamic array struct.
function wrap(bool[] memory a) internal pure returns (DynamicArray memory result) {
/// @solidity memory-safe-assembly
assembly {
mstore(result, a)
}
}
/// @dev Wraps `a` in a dynamic array struct.
function wrap(bytes32[] memory a) internal pure returns (DynamicArray memory result) {
/// @solidity memory-safe-assembly
assembly {
mstore(result, a)
}
}
/// @dev Clears the array without deallocating the memory.
function clear(DynamicArray memory a) internal pure returns (DynamicArray memory result) {
_deallocate(result);
result = a;
/// @solidity memory-safe-assembly
assembly {
mstore(mload(result), 0)
}
}
/// @dev Clears the array and attempts to free the memory if possible.
function free(DynamicArray memory a) internal pure returns (DynamicArray memory result) {
_deallocate(result);
result = a;
/// @solidity memory-safe-assembly
assembly {
let arrData := mload(result)
if iszero(eq(arrData, 0x60)) {
let prime := 8188386068317523
let cap := mload(sub(arrData, 0x20))
// Extract `cap`, initializing it to zero if it is not a multiple of `prime`.
cap := mul(div(cap, prime), iszero(mod(cap, prime)))
// If `cap` is non-zero and the memory is contiguous, we can free it.
if lt(iszero(cap), eq(mload(0x40), add(arrData, add(0x20, cap)))) {
mstore(0x40, sub(arrData, 0x20))
}
mstore(result, 0x60)
}
}
}
/// @dev Resizes the array to contain `n` elements. New elements will be zeroized.
function resize(DynamicArray memory a, uint256 n)
internal
pure
returns (DynamicArray memory result)
{
_deallocate(result);
result = a;
reserve(result, n);
/// @solidity memory-safe-assembly
assembly {
let arrData := mload(result)
let arrLen := mload(arrData)
if iszero(lt(n, arrLen)) {
calldatacopy(
add(arrData, shl(5, add(1, arrLen))),
calldatasize(),
shl(5, sub(n, arrLen))
)
}
mstore(arrData, n)
}
}
/// @dev Increases the size of `a` to `n`.
/// If `n` is less than the size of `a`, this will be a no-op.
/// This method does not zeroize any newly created elements.
function expand(DynamicArray memory a, uint256 n)
internal
pure
returns (DynamicArray memory result)
{
_deallocate(result);
result = a;
if (n >= a.data.length) {
reserve(result, n);
/// @solidity memory-safe-assembly
assembly {
mstore(mload(result), n)
}
}
}
/// @dev Reduces the size of `a` to `n`.
/// If `n` is greater than the size of `a`, this will be a no-op.
function truncate(DynamicArray memory a, uint256 n)
internal
pure
returns (DynamicArray memory result)
{
_deallocate(result);
result = a;
/// @solidity memory-safe-assembly
assembly {
mstore(mul(lt(n, mload(mload(result))), mload(result)), n)
}
}
/// @dev Reserves at least `minimum` amount of contiguous memory.
function reserve(DynamicArray memory a, uint256 minimum)
internal
pure
returns (DynamicArray memory result)
{
_deallocate(result);
result = a;
/// @solidity memory-safe-assembly
assembly {
if iszero(lt(minimum, 0xffffffff)) { invalid() } // For extra safety.
for { let arrData := mload(a) } 1 {} {
// Some random prime number to multiply `cap`, so that
// we know that the `cap` is for a dynamic array.
// Selected to be larger than any memory pointer realistically.
let prime := 8188386068317523
// Special case for `arrData` pointing to zero pointer.
if eq(arrData, 0x60) {
let newCap := shl(5, add(1, minimum))
let capSlot := mload(0x40)
mstore(capSlot, mul(prime, newCap)) // Store the capacity.
let newArrData := add(0x20, capSlot)
mstore(newArrData, 0) // Store the length.
mstore(0x40, add(newArrData, add(0x20, newCap))) // Allocate memory.
mstore(a, newArrData)
break
}
let w := not(0x1f)
let cap := mload(add(arrData, w)) // `mload(sub(arrData, w))`.
// Extract `cap`, initializing it to zero if it is not a multiple of `prime`.
cap := mul(div(cap, prime), iszero(mod(cap, prime)))
let newCap := shl(5, minimum)
// If we don't need to grow the memory.
if iszero(and(gt(minimum, mload(arrData)), gt(newCap, cap))) { break }
// If the memory is contiguous, we can simply expand it.
if eq(mload(0x40), add(arrData, add(0x20, cap))) {
mstore(add(arrData, w), mul(prime, newCap)) // Store the capacity.
mstore(0x40, add(arrData, add(0x20, newCap))) // Expand the memory allocation.
break
}
let capSlot := mload(0x40)
let newArrData := add(capSlot, 0x20)
mstore(0x40, add(newArrData, add(0x20, newCap))) // Reallocate the memory.
mstore(a, newArrData) // Store the `newArrData`.
// Copy `arrData` one word at a time, backwards.
for { let o := add(0x20, shl(5, mload(arrData))) } 1 {} {
mstore(add(newArrData, o), mload(add(arrData, o)))
o := add(o, w) // `sub(o, 0x20)`.
if iszero(o) { break }
}
mstore(capSlot, mul(prime, newCap)) // Store the capacity.
mstore(newArrData, mload(arrData)) // Store the length.
break
}
}
}
/// @dev Appends `data` to `a`.
function p(DynamicArray memory a, uint256 data)
internal
pure
returns (DynamicArray memory result)
{
_deallocate(result);
result = a;
/// @solidity memory-safe-assembly
assembly {
let arrData := mload(a)
let newArrLen := add(mload(arrData), 1)
let newArrBytesLen := shl(5, newArrLen)
// Some random prime number to multiply `cap`, so that
// we know that the `cap` is for a dynamic array.
// Selected to be larger than any memory pointer realistically.
let prime := 8188386068317523
let cap := mload(sub(arrData, 0x20))
// Extract `cap`, initializing it to zero if it is not a multiple of `prime`.
cap := mul(div(cap, prime), iszero(mod(cap, prime)))
// Expand / Reallocate memory if required.
// Note that we need to allocate an extra word for the length.
for {} iszero(lt(newArrBytesLen, cap)) {} {
// Approximately more than double the capacity to ensure more than enough space.
let newCap := add(cap, or(cap, newArrBytesLen))
// If the memory is contiguous, we can simply expand it.
if iszero(or(xor(mload(0x40), add(arrData, add(0x20, cap))), iszero(cap))) {
mstore(sub(arrData, 0x20), mul(prime, newCap)) // Store the capacity.
mstore(0x40, add(arrData, add(0x20, newCap))) // Expand the memory allocation.
break
}
// Set the `newArrData` to point to the word after `cap`.
let newArrData := add(mload(0x40), 0x20)
mstore(0x40, add(newArrData, add(0x20, newCap))) // Reallocate the memory.
mstore(a, newArrData) // Store the `newArrData`.
let w := not(0x1f)
// Copy `arrData` one word at a time, backwards.
for { let o := newArrBytesLen } 1 {} {
mstore(add(newArrData, o), mload(add(arrData, o)))
o := add(o, w) // `sub(o, 0x20)`.
if iszero(o) { break }
}
mstore(add(newArrData, w), mul(prime, newCap)) // Store the memory.
arrData := newArrData // Assign `newArrData` to `arrData`.
break
}
mstore(add(arrData, newArrBytesLen), data) // Append `data`.
mstore(arrData, newArrLen) // Store the length.
}
}
/// @dev Appends `data` to `a`.
function p(DynamicArray memory a, address data)
internal
pure
returns (DynamicArray memory result)
{
_deallocate(result);
result = p(a, uint256(uint160(data)));
}
/// @dev Appends `data` to `a`.
function p(DynamicArray memory a, bool data)
internal
pure
returns (DynamicArray memory result)
{
_deallocate(result);
result = p(a, _toUint(data));
}
/// @dev Appends `data` to `a`.
function p(DynamicArray memory a, bytes32 data)
internal
pure
returns (DynamicArray memory result)
{
_deallocate(result);
result = p(a, uint256(data));
}
/// @dev Shorthand for returning an empty array.
function p() internal pure returns (DynamicArray memory result) {}
/// @dev Shorthand for `p(p(), data)`.
function p(uint256 data) internal pure returns (DynamicArray memory result) {
p(result, uint256(data));
}
/// @dev Shorthand for `p(p(), data)`.
function p(address data) internal pure returns (DynamicArray memory result) {
p(result, uint256(uint160(data)));
}
/// @dev Shorthand for `p(p(), data)`.
function p(bool data) internal pure returns (DynamicArray memory result) {
p(result, _toUint(data));
}
/// @dev Shorthand for `p(p(), data)`.
function p(bytes32 data) internal pure returns (DynamicArray memory result) {
p(result, uint256(data));
}
/// @dev Removes and returns the last element of `a`.
/// Returns 0 and does not pop anything if the array is empty.
function pop(DynamicArray memory a) internal pure returns (uint256 result) {
/// @solidity memory-safe-assembly
assembly {
let o := mload(a)
let n := mload(o)
result := mload(add(o, shl(5, n)))
mstore(o, sub(n, iszero(iszero(n))))
}
}
/// @dev Removes and returns the last element of `a`.
/// Returns 0 and does not pop anything if the array is empty.
function popUint256(DynamicArray memory a) internal pure returns (uint256 result) {
/// @solidity memory-safe-assembly
assembly {
let o := mload(a)
let n := mload(o)
result := mload(add(o, shl(5, n)))
mstore(o, sub(n, iszero(iszero(n))))
}
}
/// @dev Removes and returns the last element of `a`.
/// Returns 0 and does not pop anything if the array is empty.
function popAddress(DynamicArray memory a) internal pure returns (address result) {
/// @solidity memory-safe-assembly
assembly {
let o := mload(a)
let n := mload(o)
result := mload(add(o, shl(5, n)))
mstore(o, sub(n, iszero(iszero(n))))
}
}
/// @dev Removes and returns the last element of `a`.
/// Returns 0 and does not pop anything if the array is empty.
function popBool(DynamicArray memory a) internal pure returns (bool result) {
/// @solidity memory-safe-assembly
assembly {
let o := mload(a)
let n := mload(o)
result := mload(add(o, shl(5, n)))
mstore(o, sub(n, iszero(iszero(n))))
}
}
/// @dev Removes and returns the last element of `a`.
/// Returns 0 and does not pop anything if the array is empty.
function popBytes32(DynamicArray memory a) internal pure returns (bytes32 result) {
/// @solidity memory-safe-assembly
assembly {
let o := mload(a)
let n := mload(o)
result := mload(add(o, shl(5, n)))
mstore(o, sub(n, iszero(iszero(n))))
}
}
/// @dev Returns the element at `a.data[i]`, without bounds checking.
function get(DynamicArray memory a, uint256 i) internal pure returns (uint256 result) {
/// @solidity memory-safe-assembly
assembly {
result := mload(add(add(mload(a), 0x20), shl(5, i)))
}
}
/// @dev Returns the element at `a.data[i]`, without bounds checking.
function getUint256(DynamicArray memory a, uint256 i) internal pure returns (uint256 result) {
/// @solidity memory-safe-assembly
assembly {
result := mload(add(add(mload(a), 0x20), shl(5, i)))
}
}
/// @dev Returns the element at `a.data[i]`, without bounds checking.
function getAddress(DynamicArray memory a, uint256 i) internal pure returns (address result) {
/// @solidity memory-safe-assembly
assembly {
result := mload(add(add(mload(a), 0x20), shl(5, i)))
}
}
/// @dev Returns the element at `a.data[i]`, without bounds checking.
function getBool(DynamicArray memory a, uint256 i) internal pure returns (bool result) {
/// @solidity memory-safe-assembly
assembly {
result := mload(add(add(mload(a), 0x20), shl(5, i)))
}
}
/// @dev Returns the element at `a.data[i]`, without bounds checking.
function getBytes32(DynamicArray memory a, uint256 i) internal pure returns (bytes32 result) {
/// @solidity memory-safe-assembly
assembly {
result := mload(add(add(mload(a), 0x20), shl(5, i)))
}
}
/// @dev Sets `a.data[i]` to `data`, without bounds checking.
function set(DynamicArray memory a, uint256 i, uint256 data)
internal
pure
returns (DynamicArray memory result)
{
_deallocate(result);
result = a;
/// @solidity memory-safe-assembly
assembly {
mstore(add(add(mload(result), 0x20), shl(5, i)), data)
}
}
/// @dev Sets `a.data[i]` to `data`, without bounds checking.
function set(DynamicArray memory a, uint256 i, address data)
internal
pure
returns (DynamicArray memory result)
{
_deallocate(result);
result = a;
/// @solidity memory-safe-assembly
assembly {
mstore(add(add(mload(result), 0x20), shl(5, i)), shr(96, shl(96, data)))
}
}
/// @dev Sets `a.data[i]` to `data`, without bounds checking.
function set(DynamicArray memory a, uint256 i, bool data)
internal
pure
returns (DynamicArray memory result)
{
_deallocate(result);
result = a;
/// @solidity memory-safe-assembly
assembly {
mstore(add(add(mload(result), 0x20), shl(5, i)), iszero(iszero(data)))
}
}
/// @dev Sets `a.data[i]` to `data`, without bounds checking.
function set(DynamicArray memory a, uint256 i, bytes32 data)
internal
pure
returns (DynamicArray memory result)
{
_deallocate(result);
result = a;
/// @solidity memory-safe-assembly
assembly {
mstore(add(add(mload(result), 0x20), shl(5, i)), data)
}
}
/// @dev Returns the underlying array as a `uint256[]`.
function asUint256Array(DynamicArray memory a) internal pure returns (uint256[] memory result) {
/// @solidity memory-safe-assembly
assembly {
result := mload(a)
}
}
/// @dev Returns the underlying array as a `address[]`.
function asAddressArray(DynamicArray memory a) internal pure returns (address[] memory result) {
/// @solidity memory-safe-assembly
assembly {
result := mload(a)
}
}
/// @dev Returns the underlying array as a `bool[]`.
function asBoolArray(DynamicArray memory a) internal pure returns (bool[] memory result) {
/// @solidity memory-safe-assembly
assembly {
result := mload(a)
}
}
/// @dev Returns the underlying array as a `bytes32[]`.
function asBytes32Array(DynamicArray memory a) internal pure returns (bytes32[] memory result) {
/// @solidity memory-safe-assembly
assembly {
result := mload(a)
}
}
/// @dev Returns a copy of `a` sliced from `start` to `end` (exclusive).
function slice(DynamicArray memory a, uint256 start, uint256 end)
internal
pure
returns (DynamicArray memory result)
{
result.data = slice(a.data, start, end);
}
/// @dev Returns a copy of `a` sliced from `start` to the end of the array.
function slice(DynamicArray memory a, uint256 start)
internal
pure
returns (DynamicArray memory result)
{
result.data = slice(a.data, start, type(uint256).max);
}
/// @dev Returns a copy of `a`.
function copy(DynamicArray memory a) internal pure returns (DynamicArray memory result) {
result.data = copy(a.data);
}
/// @dev Returns if `needle` is in `a`.
function contains(DynamicArray memory a, uint256 needle) internal pure returns (bool) {
return ~indexOf(a.data, needle, 0) != 0;
}
/// @dev Returns if `needle` is in `a`.
function contains(DynamicArray memory a, address needle) internal pure returns (bool) {
return ~indexOf(a.data, uint160(needle), 0) != 0;
}
/// @dev Returns if `needle` is in `a`.
function contains(DynamicArray memory a, bytes32 needle) internal pure returns (bool) {
return ~indexOf(a.data, uint256(needle), 0) != 0;
}
/// @dev Returns the first index of `needle`, scanning forward from `from`.
/// If `needle` is not in `a`, returns `NOT_FOUND`.
function indexOf(DynamicArray memory a, uint256 needle, uint256 from)
internal
pure
returns (uint256)
{
return indexOf(a.data, needle, from);
}
/// @dev Returns the first index of `needle`, scanning forward from `from`.
/// If `needle` is not in `a`, returns `NOT_FOUND`.
function indexOf(DynamicArray memory a, address needle, uint256 from)
internal
pure
returns (uint256)
{
return indexOf(a.data, uint160(needle), from);
}
/// @dev Returns the first index of `needle`, scanning forward from `from`.
/// If `needle` is not in `a`, returns `NOT_FOUND`.
function indexOf(DynamicArray memory a, bytes32 needle, uint256 from)
internal
pure
returns (uint256)
{
return indexOf(a.data, uint256(needle), from);
}
/// @dev Returns the first index of `needle`.
/// If `needle` is not in `a`, returns `NOT_FOUND`.
function indexOf(DynamicArray memory a, uint256 needle) internal pure returns (uint256) {
return indexOf(a.data, needle, 0);
}
/// @dev Returns the first index of `needle`.
/// If `needle` is not in `a`, returns `NOT_FOUND`.
function indexOf(DynamicArray memory a, address needle) internal pure returns (uint256) {
return indexOf(a.data, uint160(needle), 0);
}
/// @dev Returns the first index of `needle`.
/// If `needle` is not in `a`, returns `NOT_FOUND`.
function indexOf(DynamicArray memory a, bytes32 needle) internal pure returns (uint256) {
return indexOf(a.data, uint256(needle), 0);
}
/// @dev Returns the last index of `needle`, scanning backwards from `from`.
/// If `needle` is not in `a`, returns `NOT_FOUND`.
function lastIndexOf(DynamicArray memory a, uint256 needle, uint256 from)
internal
pure
returns (uint256)
{
return lastIndexOf(a.data, needle, from);
}
/// @dev Returns the last index of `needle`, scanning backwards from `from`.
/// If `needle` is not in `a`, returns `NOT_FOUND`.
function lastIndexOf(DynamicArray memory a, address needle, uint256 from)
internal
pure
returns (uint256)
{
return lastIndexOf(a.data, uint160(needle), from);
}
/// @dev Returns the last index of `needle`, scanning backwards from `from`.
/// If `needle` is not in `a`, returns `NOT_FOUND`.
function lastIndexOf(DynamicArray memory a, bytes32 needle, uint256 from)
internal
pure
returns (uint256)
{
return lastIndexOf(a.data, uint256(needle), from);
}
/// @dev Returns the last index of `needle`.
/// If `needle` is not in `a`, returns `NOT_FOUND`.
function lastIndexOf(DynamicArray memory a, uint256 needle) internal pure returns (uint256) {
return lastIndexOf(a.data, needle, NOT_FOUND);
}
/// @dev Returns the last index of `needle`.
/// If `needle` is not in `a`, returns `NOT_FOUND`.
function lastIndexOf(DynamicArray memory a, address needle) internal pure returns (uint256) {
return lastIndexOf(a.data, uint160(needle), NOT_FOUND);
}
/// @dev Returns the last index of `needle`.
/// If `needle` is not in `a`, returns `NOT_FOUND`.
function lastIndexOf(DynamicArray memory a, bytes32 needle) internal pure returns (uint256) {
return lastIndexOf(a.data, uint256(needle), NOT_FOUND);
}
/// @dev Equivalent to `keccak256(abi.encodePacked(a.data))`.
function hash(DynamicArray memory a) internal pure returns (bytes32 result) {
/// @solidity memory-safe-assembly
assembly {
result := keccak256(add(mload(a), 0x20), shl(5, mload(mload(a))))
}
}
/// @dev Directly returns `a` without copying.
function directReturn(DynamicArray memory a) internal pure {
/// @solidity memory-safe-assembly
assembly {
let arrData := mload(a)
let retStart := sub(arrData, 0x20)
mstore(retStart, 0x20)
return(retStart, add(0x40, shl(5, mload(arrData))))
}
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* PRIVATE HELPERS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Helper for deallocating an automatically allocated array pointer.
function _deallocate(DynamicArray memory result) private pure {
/// @solidity memory-safe-assembly
assembly {
mstore(0x40, result) // Deallocate, as we have already allocated.
}
}
/// @dev Casts the bool into a uint256.
function _toUint(bool b) private pure returns (uint256 result) {
/// @solidity memory-safe-assembly
assembly {
result := iszero(iszero(b))
}
}
}
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/"
],
"experimental": false
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.