Contract Source Code:
<i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: BUSL-1.1
// SPDX-FileCopyrightText: 2023 Kiln <contact@kiln.fi>
//
// ██╗ ██╗██╗██╗ ███╗ ██╗
// ██║ ██╔╝██║██║ ████╗ ██║
// █████╔╝ ██║██║ ██╔██╗ ██║
// ██╔═██╗ ██║██║ ██║╚██╗██║
// ██║ ██╗██║███████╗██║ ╚████║
// ╚═╝ ╚═╝╚═╝╚══════╝╚═╝ ╚═══╝
//
pragma solidity 0.8.17;
import "utils.sol/Fixable.sol";
import "utils.sol/NFT.sol";
import "utils.sol/Implementation.sol";
import "utils.sol/types/bool.sol";
import "openzeppelin-contracts/utils/Base64.sol";
import "./lib/LibStringify.sol";
import "./ctypes/ticket_array.sol";
import "./ctypes/cask_array.sol";
import "./interfaces/IvExitQueue.sol";
import "./interfaces/IvPool.sol";
import "./interfaces/IvFactory.sol";
/// @title Exit Queue
/// @author mortimr @ Kiln
/// @notice The exit queue stores exit requests until they are filled and claimable
///
/// ⢀⣀ ⢀⣤⣤⣤⠄⣠⣤⣤⠄⢀⣀⡀
/// ⢀⣾⣿⠏⢰⣿⣿⣿⠃⣰⣿⣿⠁⣴⣿⣿⣿⣷⡀
/// ⣾⣿⡟⢀⣿⣿⣿⡏⢠⣿⣿⡇⢰⣿⣿⣿⣿⣿⣷
/// ⢸⣿⣿⡇⢸⣿⣿⣿⡇⢸⣿⣿ ⢸⣿⣿⣿⣿⣿⣿
/// ⠈⣿⣿⡇⠸⣿⣿⣿⡇⢸⣿⣿⡆⢸⣿⣿⠟⢿⣿⣿
/// ⠘⠛⠛ ⠛⠛⠛⠃⠈⠻⠿⠧⠈⣿⡇ ⢸⣿⠃
/// ⢀⣤⠄⢀⣶⣿⣿⡟⢠⣾⣿⠇⢀⣤⣤⣄⠛⠛⠛⢁ ⣤⣤⣄ ⢀⣤⠄⢀⣶⣿⣿⡟⢠⣾⣿⠇⢀⣤⣤⣄ ⣤⣤⣄
/// ⣴⣿⡏⢠⣿⣿⣿⡟⢠⣿⣿⠃⣰⣿⣿⣿⣿⣧ ⢠⣿⣿⣿⣿⣧ ⣴⣿⡏⢠⣿⣿⣿⡟⢠⣿⣿⠃⣰⣿⣿⣿⣿⣧ ⢠⣿⣿⣿⣿⣧
/// ⢰⣿⣿ ⣾⣿⣿⣿⠁⣼⣿⡏⢠⣿⣿⣿⣿⣿⣿⡄ ⣿⣿⣿⣿⣿⣿⡇ ⢰⣿⣿ ⣾⣿⣿⣿⠁⣼⣿⡏⢠⣿⣿⣿⣿⣿⣿⡄ ⣿⣿⣿⣿⣿⣿⡇
/// ⢸⣿⣿ ⣿⣿⣿⣿ ⣿⣿⡇⢸⣿⣿⣿⣿⣿⣿⡇ ⣿⣿⣿⣿⣿⣿⡇ ⢸⣿⣿ ⣿⣿⣿⣿ ⣿⣿⡇⢸⣿⣿⣿⣿⣿⣿⡇ ⣿⣿⣿⣿⣿⣿⡇
/// ⠘⣿⣿ ⣿⣿⣿⣿⡀⢻⣿⣧⠈⣿⣿⠟⠙⣿⣿⠁ ⢿⣿⡟⠙⢿⣿⠇ ⠘⣿⣿ ⣿⣿⣿⣿⡀⢻⣿⣧⠈⣿⣿⠟⠙⣿⣿⠁ ⢿⣿⡟⠙⢿⣿⠇
/// ⠙⢿⣇⠘⢿⣿⣿⣧⠈⣿⣿⣆⠘⣿⣄⣠⣾⠃⣰⣧⠘⢿⣇⢀⣾⠏ ⠙⢿⣇⠘⢿⣿⣿⣧⠈⣿⣿⣆⠘⣿⣄⣠⣾⠃⣰⣧⠘⢿⣇⢀⣾⠏
/// ⠈⠁⠈⠛⠛⠛⠃⠈⠛⠛⠓ ⠉⠉ ⠐⠛⠛⠓ ⠉⠉⠁ ⠈⠁⠈⠛⠛⠛⠃⠈⠛⠛⠓ ⠉⠉ ⠐⠛⠛⠓ ⠉⠉⠁
///
// slither-disable-next-line naming-convention
contract vExitQueue is NFT, Fixable, Initializable, Implementation, IvExitQueue {
using LUint256 for types.Uint256;
using LAddress for types.Address;
using LBool for types.Bool;
using LString for types.String;
using LTicketArray for ctypes.TicketArray;
using LCaskArray for ctypes.CaskArray;
/// @dev Address of the associated vPool
/// @dev Slot: keccak256(bytes("exitQueue.1.pool"))) - 1
types.Address internal constant $pool = types.Address.wrap(0xdcdd87edea8fcbdc6d50bb4863c8269eed833245e48ec3e4f64dc4cd88a27283);
/// @dev Total amount of unclaimed funds in the exit queue - 1
/// @dev Slot: keccak256(bytes("exitQueue.1.unclaimedFunds")))
types.Uint256 internal constant $unclaimedFunds = types.Uint256.wrap(0x51fae72b3be6f7b8c2f4de519c1a9fb3f8624c4c7d1f85109b6659ae4958c29a);
/// @dev Flag enabling/disabling transfers
/// @dev Slot: keccak256(bytes("exitQueue.1.transferEnabled"))) - 1
types.Bool internal constant $transferEnabled = types.Bool.wrap(0xc1bfc3030aebadb3bfaa3fbc59cf364f7dee6ab92429159a4bfdf02fa88336a0);
/// @dev Token URI image URL
/// @dev Slot: keccak256(bytes("exitQueue.1.tokenUriImageUrl"))) - 1
types.String internal constant $tokenUriImageUrl = types.String.wrap(0x0f0463b3f5083af4c7135d28606a2c0eaa2bd9e3f9f62db1539e47244df8dc49);
/// @dev Array of tickets
/// @dev Slot: keccak256(bytes("exitQueue.1.tickets"))) - 1
ctypes.TicketArray internal constant $tickets =
ctypes.TicketArray.wrap(0x409fdfd8838fda00128ca5d502af2ba15c034ca4130776e2ed6d3eb7811e3481);
/// @dev Array of casks
/// @dev Slot: keccak256(bytes("exitQueue.1.casks"))) - 1
ctypes.CaskArray internal constant $casks = ctypes.CaskArray.wrap(0x39a5c864ceb6f99a196a385a148476994e3952fd6d71d040a2339a143eaeabe1);
/// @dev Resolution error code for a ticket that is out of bounds
int64 internal constant TICKET_ID_OUT_OF_BOUNDS = -1;
/// @dev Resolution error code for a ticket that has already been claimed
int64 internal constant TICKET_ALREADY_CLAIMED = -2;
/// @dev Resolution error code for a ticket that is pending fulfillment
int64 internal constant TICKET_PENDING = -3;
/// @notice Prevents calls not coming from the associated vPool
modifier onlyPool() {
if (msg.sender != $pool.get()) {
revert LibErrors.Unauthorized(msg.sender, $pool.get());
}
_;
}
/// @notice Prevents calls not coming from the vFactory admin
modifier onlyAdmin() {
{
address admin = IvFactory(_castedPool().factory()).admin();
if (msg.sender != admin) {
revert LibErrors.Unauthorized(msg.sender, admin);
}
}
_;
}
/// @inheritdoc IvExitQueue
// slither-disable-next-line missing-zero-check
function initialize(address vpool, string calldata newTokenUriImageUrl) external init(0) {
_setTokenUriImageUrl(newTokenUriImageUrl);
LibSanitize.notZeroAddress(vpool);
$pool.set(vpool);
emit SetPool(vpool);
}
/// @inheritdoc IvExitQueue
function tokenUriImageUrl() external view returns (string memory) {
return $tokenUriImageUrl.get();
}
/// @notice Get the Exit Queue name from the associated vPool -> Factory.
/// @dev The name is mutable (can be updated by the Factory admin).
/// @return The name of the Exit Queue.
function name() external view override returns (string memory) {
// slither-disable-next-line unused-return
(string memory operatorName,,) = IvFactory(_castedPool().factory()).metadata();
return string(abi.encodePacked(operatorName, " Exit Queue"));
}
/// @inheritdoc NFT
function symbol() external pure override returns (string memory) {
return "vEQ";
}
/// @inheritdoc IERC721Metadata
function tokenURI(uint256 tokenId) external view override(NFT) returns (string memory) {
_requireExists(tokenId);
uint256 tokenIdx = _getTicketIdx(tokenId);
ctypes.Ticket memory t = $tickets.get()[tokenIdx];
ctypes.Cask[] storage caskArray = $casks.get();
uint256 claimable = 0;
uint256 queueSize = 0;
{
ctypes.Cask memory c = caskArray.length > 0 ? caskArray[caskArray.length - 1] : ctypes.Cask({position: 0, size: 0, value: 0});
// | CASK |
// | TICKET |
if (c.position > t.position + t.size) {
claimable = t.size;
// | CASK |
// | TICKET |
} else if (c.position >= t.position) {
claimable = t.size - (c.position + c.size >= t.position + t.size ? 0 : (t.position + t.size) - (c.position + c.size));
// | CASK |
// | TICKET |
} else if (c.position < t.position && t.position < c.position + c.size) {
claimable = (c.position + c.size) - t.position;
}
queueSize = c.position + c.size;
}
bytes memory fullImageUrl =
abi.encodePacked($tokenUriImageUrl.get(), "/", Strings.toHexString(address(this)), "/", Strings.toString(tokenId));
return string(
abi.encodePacked(
"data:application/json;base64,",
Base64.encode(
abi.encodePacked(
"{",
"\"name\":\"Exit Ticket #",
Strings.toString(tokenIdx),
"\",",
"\"description\":\"This exit ticket can be used to claim funds from the exit queue contract once it is fulfilled.\",",
_generateAttributes(t, claimable, queueSize),
"\"image_url\":\"",
fullImageUrl,
"\"}"
)
)
)
);
}
/// @inheritdoc IvExitQueue
function transferEnabled() external view returns (bool) {
return $transferEnabled.get();
}
/// @inheritdoc IvExitQueue
function unclaimedFunds() external view returns (uint256) {
return $unclaimedFunds.get();
}
/// @inheritdoc IvExitQueue
function ticketIdAtIndex(uint32 idx) external view returns (uint256) {
return _getTicketId(idx, $tickets.get()[idx]);
}
/// @inheritdoc IvExitQueue
function ticket(uint256 id) external view returns (ctypes.Ticket memory) {
uint256 idx = _getTicketIdx(id);
ctypes.Ticket[] storage ticketArray = $tickets.get();
if (idx >= ticketArray.length) {
revert InvalidTicketId(id);
}
return ticketArray[idx];
}
/// @inheritdoc IvExitQueue
function ticketCount() external view returns (uint256) {
return $tickets.get().length;
}
/// @inheritdoc IvExitQueue
function cask(uint32 id) external view returns (ctypes.Cask memory) {
ctypes.Cask[] storage caskArray = $casks.get();
if (id >= caskArray.length) {
revert InvalidCaskId(id);
}
return caskArray[id];
}
/// @inheritdoc IvExitQueue
function caskCount() external view returns (uint256) {
return $casks.get().length;
}
/// @inheritdoc IvExitQueue
function resolve(uint256[] memory ticketIds) external view returns (int64[] memory caskIdsOrErrors) {
uint256 ticketIdsLength = ticketIds.length;
caskIdsOrErrors = new int64[](ticketIdsLength);
uint256 totalTicketCount = $tickets.get().length;
uint256 totalCaskCount = $casks.get().length;
for (uint256 idx = 0; idx < ticketIdsLength;) {
caskIdsOrErrors[idx] = _resolve(ticketIds[idx], totalTicketCount, totalCaskCount);
unchecked {
++idx;
}
}
}
/// @inheritdoc IvExitQueue
function feed(uint256 shares) external payable onlyPool {
LibSanitize.notNullValue(shares);
LibSanitize.notNullValue(msg.value);
ctypes.Cask[] storage caskArray = $casks.get();
uint256 casksLength = caskArray.length;
ctypes.Cask memory lastCask = casksLength > 0 ? caskArray[casksLength - 1] : ctypes.Cask({position: 0, size: 0, value: 0});
ctypes.Cask memory newCask =
ctypes.Cask({position: lastCask.position + lastCask.size, size: uint128(shares), value: uint128(msg.value)});
caskArray.push(newCask);
emit ReceivedCask(uint32(casksLength), newCask);
}
/// @inheritdoc IvExitQueue
function pull(uint256 max) external onlyPool {
uint256 currentUnclaimedFunds = $unclaimedFunds.get();
uint256 maxPullable = LibUint256.min(max, currentUnclaimedFunds);
if (maxPullable > 0) {
_setUnclaimedFunds(currentUnclaimedFunds - maxPullable);
emit SuppliedEther(maxPullable);
_castedPool().injectEther{value: maxPullable}();
}
}
/// @inheritdoc IvPoolSharesReceiver
//slither-disable-next-line assembly
function onvPoolSharesReceived(address, address from, uint256 amount, bytes memory data) external override onlyPool returns (bytes4) {
LibSanitize.notNullValue(amount);
if (data.length == 20) {
// If the data appears to be a packed encoded address we print a ticket to that address instead of the sender
address to;
assembly {
// After skipping the length element of data, the first element (20 bytes padded on the right to 32 bytes) is
// converted to an actual address by right shifting.
to := shr(96, mload(add(data, 32)))
}
_printTicket(uint128(amount), to);
} else {
_printTicket(uint128(amount), from);
}
return IvPoolSharesReceiver.onvPoolSharesReceived.selector;
}
/// @inheritdoc IvExitQueue
function setTokenUriImageUrl(string calldata newTokenUriImageUrl) external onlyAdmin {
_setTokenUriImageUrl(newTokenUriImageUrl);
}
/// @inheritdoc IvExitQueue
function setTransferEnabled(bool value) external onlyAdmin {
_setTransferEnabled(value);
}
struct ClaimInternalVariables {
uint256 ticketIdsLength;
uint256 totalTicketCount;
address[] recipients;
uint256[] payments;
uint256 usedRecipients;
ConsumeTicketParameters params;
}
/// @inheritdoc IvExitQueue
// slither-disable-next-line arbitrary-send-eth,calls-loop,reentrancy-events,cyclomatic-complexity
function claim(uint256[] calldata ticketIds, uint32[] calldata caskIds, uint16 maxClaimDepth)
external
returns (ClaimStatus[] memory statuses)
{
// slither-disable-next-line uninitialized-local
ClaimInternalVariables memory __;
__.ticketIdsLength = ticketIds.length;
if (__.ticketIdsLength == 0 || __.ticketIdsLength != caskIds.length) {
revert InvalidLengths();
}
__.totalTicketCount = $tickets.get().length;
__.params.totalCaskCount = $casks.get().length;
statuses = new ClaimStatus[](ticketIds.length);
__.recipients = new address[](ticketIds.length);
__.payments = new uint256[](ticketIds.length);
__.usedRecipients = 0;
// slither-disable-next-line uninitialized-local
for (uint256 idx; idx < __.ticketIdsLength;) {
__.params.ticketId = ticketIds[idx];
__.params.ticketIdx = _getTicketIdx(__.params.ticketId);
__.params.caskId = caskIds[idx];
__.params.depth = maxClaimDepth;
__.params.ethToPay = 0;
// this line reverts if the ticket id doesn't exist
address owner = ownerOf(ticketIds[idx]);
__.params.t = $tickets.get()[__.params.ticketIdx];
if (__.params.t.size == 0) {
statuses[idx] = ClaimStatus.SKIPPED;
unchecked {
++idx;
}
continue;
}
if (__.params.caskId >= __.params.totalCaskCount) {
revert InvalidCaskId(__.params.caskId);
}
__.params.c = $casks.get()[__.params.caskId];
if (!_matching(__.params.t, __.params.c)) {
revert TicketNotMatchingCask(__.params.ticketId, __.params.caskId);
}
_consumeTicket(__.params);
if (__.params.t.size > 0) {
uint256 ticketIdx = _getTicketIdx(ticketIds[idx]);
_burn(ticketIds[idx]);
uint256 newTicketId = _getTicketId(ticketIdx, $tickets.get()[ticketIdx]);
_mint(owner, newTicketId);
emit TicketIdUpdated(ticketIds[idx], newTicketId, uint32(ticketIdx));
}
statuses[idx] = __.params.t.size > 0 ? ClaimStatus.PARTIALLY_CLAIMED : ClaimStatus.CLAIMED;
if (__.params.ethToPay > 0) {
int256 ownerIndex = -1;
for (uint256 recipientIdx = 0; recipientIdx < __.usedRecipients;) {
if (__.recipients[recipientIdx] == owner) {
ownerIndex = int256(recipientIdx);
break;
}
unchecked {
++recipientIdx;
}
}
if (ownerIndex == -1) {
__.recipients[__.usedRecipients] = owner;
__.payments[__.usedRecipients] = __.params.ethToPay;
unchecked {
++__.usedRecipients;
}
} else {
__.payments[uint256(ownerIndex)] += __.params.ethToPay;
}
}
unchecked {
++idx;
}
}
for (uint256 recipientIdx = 0; recipientIdx < __.usedRecipients;) {
address recipient = __.recipients[recipientIdx];
uint256 payment = __.payments[recipientIdx];
// slither-disable-next-line missing-zero-check,low-level-calls
(bool success, bytes memory reason) = recipient.call{value: payment}("");
if (!success) {
revert ClaimTransferFailed(recipient, reason);
}
emit Payment(recipient, payment);
unchecked {
++recipientIdx;
}
}
}
/// @dev Internal utility function to retrieve the string status of a ticket
/// @param t The ticket to get the status of
/// @param claimable The amount of the ticket that is claimable
/// @return The status of the ticket
function _getStatusString(ctypes.Ticket memory t, uint256 claimable) internal pure returns (string memory) {
if (claimable == 0) {
return "Not yet claimable";
} else if (claimable < t.size) {
return "Partially claimable";
}
return "Fully claimable";
}
/// @dev Internal utility function to generate the attributes of a ticket
/// @param t The ticket to get the attributes of
/// @param claimable The amount of the ticket that is claimable
/// @return The attributes of the ticket
function _generateAttributes(ctypes.Ticket memory t, uint256 claimable, uint256 queueSize) internal pure returns (bytes memory) {
return abi.encodePacked(
"\"attributes\":[{\"trait_type\":\"Queue position\",\"value\":",
LibStringify.uintToDecimalString(t.position, 18, 3),
",\"display_type\":\"number\",\"max_value\":",
LibStringify.uintToDecimalString(queueSize, 18, 3),
"},{\"trait_type\":\"Claimable amount\",\"value\":",
LibStringify.uintToDecimalString(claimable, 18, 3),
",\"display_type\":\"number\",\"max_value\":",
LibStringify.uintToDecimalString(t.size, 18, 3),
"},{\"trait_type\":\"Status\",\"value\":\"",
_getStatusString(t, claimable),
"\"}],"
);
}
/// @dev Internal hook happening at each transfer.
/// To override.
function _onTransfer(address, address, uint256) internal view override {
if (!$transferEnabled.get()) {
revert TransferDisabled();
}
}
/// @dev Internal hook happening at each mint.
/// To override.
/// @param to The address receiving the token
/// @param tokenId The token id
function _onMint(address to, uint256 tokenId) internal override {}
/// @dev Internal hook happening at each burn.
/// To override.
/// @param tokenId The token id
function _onBurn(uint256 tokenId) internal override {}
/// @dev Internal utility to retrieve the vPool address casted to the vPool interface
/// @return The vPool address casted to the vPool interface
function _castedPool() internal view returns (IvPool) {
return IvPool($pool.get());
}
/// @dev Internal utility to check if a ticket is claimable on a cask
/// @param t The ticket to check
/// @param p The cask to check
/// @return True if the ticket is claimable on the cask
function _matching(ctypes.Ticket memory t, ctypes.Cask memory p) internal pure returns (bool) {
return (t.position < p.position + p.size && t.position >= p.position);
}
/// @dev Internal utility to perform a dichotomy search to find the cask matching a ticket
/// @param ticketIdx The index of the ticket to find the cask for
/// @return caskId The cask id matching the ticket
function _searchCaskForTicket(uint256 ticketIdx) internal view returns (uint32 caskId) {
ctypes.Cask[] storage caskArray = $casks.get();
uint32 right = uint32(caskArray.length - 1);
ctypes.Ticket memory t = $tickets.get()[ticketIdx];
if (_matching(t, caskArray[right])) {
return right;
}
uint32 left = 0;
if (_matching(t, caskArray[left])) {
return left;
}
while (left != right) {
uint32 middle = (left + right) >> 1;
ctypes.Cask memory middleC = caskArray[middle];
if (_matching(t, middleC)) {
return middle;
}
if (t.position < middleC.position) {
right = middle;
} else {
left = middle;
}
}
return left;
}
/// @dev Internal utility to resolve a ticket
/// @param ticketId The ticket to resolve
/// @param totalTicketCount The total number of tickets
/// @param totalCaskCount The total number of casks
/// @return caskIdOrError The cask id matching the ticket or an error code
function _resolve(uint256 ticketId, uint256 totalTicketCount, uint256 totalCaskCount) internal view returns (int64 caskIdOrError) {
uint256 ticketIdx = _getTicketIdx(ticketId);
if (ticketIdx >= totalTicketCount) {
return TICKET_ID_OUT_OF_BOUNDS;
}
ctypes.Ticket memory t = $tickets.get()[ticketIdx];
if (t.size == 0) {
return TICKET_ALREADY_CLAIMED;
}
if (totalCaskCount == 0 || $casks.get()[totalCaskCount - 1].position + $casks.get()[totalCaskCount - 1].size <= t.position) {
return TICKET_PENDING;
}
return int64(uint64(_searchCaskForTicket(ticketIdx)));
}
/// @dev Retrieves the ticket id from its index and size.
/// The ticket id is dynamic, every time someone performs a partial claim of the ticket, its id changes.
/// If the claim is complete, the ticket is burned. This would lower secondary market attack vectors that
/// include claiming before selling.
/// @param ticketIndex The index of the ticket
/// @param t The ticket
/// @return The ticket id
function _getTicketId(uint256 ticketIndex, ctypes.Ticket memory t) internal pure returns (uint256) {
return ticketIndex << 128 | uint256(t.size);
}
/// @dev Retrieves the ticket index from its id
/// @param ticketId The ticket id
/// @return The ticket index
function _getTicketIdx(uint256 ticketId) internal pure returns (uint256) {
return ticketId >> 128;
}
/// @dev Internal utility to create a new ticket
/// @param amount The amount of shares in the ticket
/// @param owner The owner of the ticket
function _printTicket(uint128 amount, address owner) internal {
IvPool pool = _castedPool();
uint256 totalUnderlyingSupply = pool.totalUnderlyingSupply();
uint256 totalSupply = pool.totalSupply();
ctypes.Ticket[] storage ticketArray = $tickets.get();
uint256 ticketsLength = ticketArray.length;
ctypes.Ticket memory lastTicket =
ticketArray.length > 0 ? ticketArray[ticketsLength - 1] : ctypes.Ticket({position: 0, size: 0, maxExitable: 0});
ctypes.Ticket memory newTicket = ctypes.Ticket({
position: lastTicket.position + lastTicket.size,
size: amount,
maxExitable: uint128(LibUint256.mulDiv(amount, totalUnderlyingSupply, totalSupply))
});
uint256 ticketId = _getTicketId(ticketsLength, newTicket);
ticketArray.push(newTicket);
_mint(owner, ticketId);
emit PrintedTicket(owner, uint32(ticketsLength), ticketId, newTicket);
}
/// @notice The parameters of the consume call
/// @param ticketId The ticket id
/// @param ticketIdx The index of the ticket
/// @param t The ticket itself
/// @param caskId The cask id
/// @param c The cask itself
/// @param totalCaskCount The total number of casks
/// @param depth The initial depth of the consume call
/// @param ethToPay The resulting eth to pay the user
struct ConsumeTicketParameters {
uint256 ticketId;
uint256 ticketIdx;
ctypes.Ticket t;
uint32 caskId;
ctypes.Cask c;
uint256 totalCaskCount;
uint16 depth;
uint256 ethToPay;
}
/// @dev Internal utility to consume a ticket.
/// Will call itself recursively to consume the ticket on all the casks it overlaps.
/// Recursive calls are limited to the initial depth.
/// @param params The parameters of the consume call
function _consumeTicket(ConsumeTicketParameters memory params) internal {
// we compute the end position of the cask
uint256 caskEnd = params.c.position + params.c.size;
// we compute the amount of shares and eth that overlap between the ticket and the cask
uint128 overlappingAmount = uint128(LibUint256.min(params.t.size, caskEnd - params.t.position));
uint128 overlappingEthAmount = uint128(LibUint256.mulDiv(overlappingAmount, params.c.value, params.c.size));
uint128 maxRedeemableEthAmount = uint128(LibUint256.mulDiv(overlappingAmount, params.t.maxExitable, params.t.size));
// we initialize the unclaimable amount to 0 before checking if the ticket is exceeding the capped ticket rate
uint256 unclaimableAmount = 0;
// then we check if the overlapping amount is not exceeding the capped ticket rate
// and if it's the case we adjust the amount of eth we can pay
if (maxRedeemableEthAmount < overlappingEthAmount) {
unclaimableAmount = overlappingEthAmount - maxRedeemableEthAmount;
overlappingEthAmount = maxRedeemableEthAmount;
_setUnclaimedFunds($unclaimedFunds.get() + unclaimableAmount);
}
// we update the ticket in memory
params.t.position += overlappingAmount;
params.t.size -= overlappingAmount;
params.t.maxExitable -= overlappingEthAmount;
// we update the total to pay for this ticket
params.ethToPay += overlappingEthAmount;
// we log the step
emit FilledTicket(params.ticketId, params.caskId, uint128(overlappingAmount), overlappingEthAmount, unclaimableAmount);
// if
// - the ticket is not empty
// - there are more casks to consume
// - we are not at the maximum depth
// then we call this method recursively with the next cask
// otherwise we update the ticket in storage and burn it if it's empty
if (params.t.size > 0 && params.caskId + 1 < params.totalCaskCount && params.depth > 0) {
params.caskId += 1;
params.c = $casks.get()[params.caskId];
params.depth -= 1;
_consumeTicket(params);
} else {
if (params.t.size == 0) {
_burn(params.ticketId);
}
ctypes.Ticket[] storage ticketArray = $tickets.get();
ticketArray[params.ticketIdx] = params.t;
}
}
/// @dev Internal utility to set the unclaimed funds buffer
/// @param newUnclaimedFunds The new unclaimed funds buffer
function _setUnclaimedFunds(uint256 newUnclaimedFunds) internal {
$unclaimedFunds.set(newUnclaimedFunds);
emit SetUnclaimedFunds(newUnclaimedFunds);
}
/// @dev Internal utility to set the transfer enabled flag
/// @param value The new transfer enabled flag
function _setTransferEnabled(bool value) internal {
$transferEnabled.set(value);
emit SetTransferEnabled(value);
}
/// @dev Internal utility to set the token URI image URL
/// @param newTokenUriImageUrl The new token URI image URL
function _setTokenUriImageUrl(string calldata newTokenUriImageUrl) internal {
LibSanitize.notEmptyString(newTokenUriImageUrl);
$tokenUriImageUrl.set(newTokenUriImageUrl);
emit SetTokenUriImageUrl($tokenUriImageUrl.get());
}
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: BUSL-1.1
// SPDX-FileCopyrightText: 2023 Kiln <contact@kiln.fi>
//
// ██╗ ██╗██╗██╗ ███╗ ██╗
// ██║ ██╔╝██║██║ ████╗ ██║
// █████╔╝ ██║██║ ██╔██╗ ██║
// ██╔═██╗ ██║██║ ██║╚██╗██║
// ██║ ██╗██║███████╗██║ ╚████║
// ╚═╝ ╚═╝╚═╝╚══════╝╚═╝ ╚═══╝
//
pragma solidity >=0.8.17;
import "./interfaces/IFixable.sol";
/// @title Fixable
/// @author mortimr @ Kiln
/// @dev Unstructured Storage Friendly
/// @notice The Fixable contract can be used on cubs to expose a safe noop to force a fix.
abstract contract Fixable is IFixable {
/// @inheritdoc IFixable
function fix() external {}
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.17;
import "openzeppelin-contracts/token/ERC721/IERC721Receiver.sol";
import "./types/address.sol";
import "./types/string.sol";
import "./types/mapping.sol";
import "./libs/LibSanitize.sol";
import "./Initializable.sol";
import "./interfaces/INFT.sol";
import "./uctypes/operator_approvals.sol";
/// @title NFT
/// @author mortimr @ Kiln
/// @notice NFT contract using utils.sol storage format.
// slither-disable-next-line unimplemented-functions
abstract contract NFT is INFT {
using LString for types.String;
using LUint256 for types.Uint256;
using LMapping for types.Mapping;
using LOperatorApprovalsMapping for uctypes.OperatorApprovalsMapping;
using CUint256 for uint256;
using CAddress for address;
/// @dev ERC721 name of the contract.
/// @dev Slot: keccak256(bytes("nft.1.name")) - 1
types.String internal constant $name =
types.String.wrap(0x8be0d77374e3002afd46fd09ae2c8e3afc7315322504f7f1a09d189f4925e72f);
/// @dev ERC721 symbol of the contract.
/// @dev Slot: keccak256(bytes("nft.1.symbol")) - 1
types.String internal constant $symbol =
types.String.wrap(0xddad2df2277e0186b34991db0b7ceafa36b49b76d0a1e87f6e4d44b6b17a207f);
/// @dev Internal ID counter to keep track of minted tokens.
/// @dev Slot: keccak256(bytes("nft.1.mintCounter")) - 1
types.Uint256 internal constant $mintCounter =
types.Uint256.wrap(0x3d706fc25ad0e96a2c3fb1b58cdd70ba377f331d59f761caecaf2f3a236d99a1);
/// @dev Internal burn counter used to keep track of the total supply.
/// @dev Slot: keccak256(bytes("nft.1.burnCounter")) - 1
types.Uint256 internal constant $burnCounter =
types.Uint256.wrap(0x0644144c18bf2aa8e15d5433cc3f6e2273ab9ccd122cd4f430275a2997cc0dc2);
/// @dev Internal mapping that holds the links between owners and NFT IDs.
/// @dev Type: mapping (uint256 => address)
/// @dev Slot: keccak256(bytes("nft.1.owners")) - 1
types.Mapping internal constant $owners =
types.Mapping.wrap(0xc1f66d46ebf7070ef20209d66f741219b00fb896714319503d158a28b0d103d3);
/// @dev Internal mapping that holds the balances of every owner (how many NFTs they own).
/// @dev Type: mapping (address => uint256)
/// @dev Slot: keccak256(bytes("nft.1.balances")) - 1
types.Mapping internal constant $balances =
types.Mapping.wrap(0xf9245bc1df90ea86e77b9f2423fe9cc12aa083c8ab9a55e727b285192b30d98a);
/// @dev Internal mapping that holds the token approval data.
/// @dev Type: mapping (uint256 => address)
/// @dev Slot: keccak256(bytes("nft.1.tokenApprovals")) - 1
types.Mapping internal constant $tokenApprovals =
types.Mapping.wrap(0x3790264503275ecd52e8f0b419eb5ce016ca8a1f0fbac5a9ede429d0c1732004);
/// @dev Internal mapping of operator approvals.
/// @dev Type: mapping (address => mapping (address => bool))
/// @dev Slot: keccak256(bytes("nft.1.operatorApprovals")) - 1
uctypes.OperatorApprovalsMapping internal constant $operatorApprovals =
uctypes.OperatorApprovalsMapping.wrap(0x6c716a91f6b5f5a0aa2affaf44bd88ea94ec69e363cf1fe9251e00a0fcc6c34e);
/// @dev Internal initializer to call when first deploying the contract.
// slither-disable-next-line dead-code
function initializeNFT(string memory name_, string memory symbol_) internal {
_setName(name_);
_setSymbol(symbol_);
}
/// @notice Returns the token uri for the given token id.
/// @dev To override
/// @param tokenId The token id to query
function tokenURI(uint256 tokenId) external view virtual returns (string memory);
/// @notice Internal hook happening at each transfer. Not called during mint or burn. Use _onMint and _onBurn instead.
/// The hook is called before state transitions are made.
/// @dev To override
/// @param from The address sending the token
/// @param to The address receiving the token
/// @param tokenId The token id
function _onTransfer(address from, address to, uint256 tokenId) internal virtual;
/// @notice Internal hook happening at each mint.
/// The hook is called before state transitions are made.
/// @dev To override
/// @param to The address receiving the token
/// @param tokenId The token id
function _onMint(address to, uint256 tokenId) internal virtual;
/// @notice Internal hook happening at each burn.
/// The hook is called before state transitions are made.
/// @dev To override
/// @param tokenId The token id
function _onBurn(uint256 tokenId) internal virtual;
/// @inheritdoc INFT
function totalSupply() external view returns (uint256) {
return $mintCounter.get() - $burnCounter.get();
}
/// @inheritdoc IERC721
function balanceOf(address owner) public view virtual override returns (uint256) {
LibSanitize.notZeroAddress(owner);
return $balances.get()[owner.k()];
}
/// @inheritdoc IERC721
function ownerOf(uint256 tokenId) public view virtual override returns (address) {
address owner = _ownerOf(tokenId);
if (owner == address(0)) {
revert InvalidTokenId(tokenId);
}
return owner;
}
/// @inheritdoc IERC721Metadata
function name() external view virtual returns (string memory) {
return string(abi.encodePacked($name.get()));
}
/// @inheritdoc IERC721Metadata
function symbol() external view virtual returns (string memory) {
return string(abi.encodePacked($symbol.get()));
}
/// @inheritdoc IERC165
function supportsInterface(bytes4 interfaceId) external pure returns (bool) {
return interfaceId == type(IERC721).interfaceId || interfaceId == type(IERC721Metadata).interfaceId;
}
/// @inheritdoc IERC721
function approve(address to, uint256 tokenId) public virtual override {
address owner = _ownerOf(tokenId);
if (to == owner) {
revert ApprovalToOwner(owner);
}
if (msg.sender != owner && !isApprovedForAll(owner, msg.sender)) {
revert LibErrors.Unauthorized(msg.sender, owner);
}
_approve(to, owner, tokenId);
}
/// @inheritdoc IERC721
function getApproved(uint256 tokenId) public view virtual override returns (address) {
_requireExists(tokenId);
return $tokenApprovals.get()[tokenId].toAddress();
}
/// @inheritdoc IERC721
function setApprovalForAll(address operator, bool approved) public virtual override {
_setApprovalForAll(msg.sender, operator, approved);
}
/// @inheritdoc IERC721
function isApprovedForAll(address owner, address operator) public view virtual override returns (bool) {
return $operatorApprovals.get()[owner][operator];
}
/// @inheritdoc IERC721
function transferFrom(address from, address to, uint256 tokenId) public virtual override {
if (!_isApprovedOrOwner(msg.sender, tokenId)) {
revert LibErrors.Unauthorized(msg.sender, _ownerOf(tokenId));
}
_transfer(from, to, tokenId);
}
/// @inheritdoc IERC721
function safeTransferFrom(address from, address to, uint256 tokenId) public virtual override {
safeTransferFrom(from, to, tokenId, "");
}
/// @inheritdoc IERC721
function safeTransferFrom(address from, address to, uint256 tokenId, bytes memory data) public virtual override {
if (!_isApprovedOrOwner(msg.sender, tokenId)) {
revert LibErrors.Unauthorized(msg.sender, _ownerOf(tokenId));
}
_safeTransfer(from, to, tokenId, data);
}
/// @dev Internal utility to set the ERC721 name value.
/// @param newName The new name to set
// slither-disable-next-line dead-code
function _setName(string memory newName) internal {
LibSanitize.notEmptyString(newName);
$name.set(newName);
emit SetName(newName);
}
/// @dev Internal utility to set the ERC721 symbol value.
/// @param newSymbol The new symbol to set
// slither-disable-next-line dead-code
function _setSymbol(string memory newSymbol) internal {
LibSanitize.notEmptyString(newSymbol);
$symbol.set(newSymbol);
emit SetSymbol(newSymbol);
}
/// @dev Internal utility to perform a safe transfer (transfer + extra checks on contracts).
/// @param from The address sending the token
/// @param to The address receiving the token
/// @param tokenId The ID of the token
/// @param data The extra data provided to contract callback calls
function _safeTransfer(address from, address to, uint256 tokenId, bytes memory data) internal virtual {
_transfer(from, to, tokenId);
if (!_checkOnERC721Received(from, to, tokenId, data)) {
revert NonERC721ReceiverTransfer(from, to, tokenId, data);
}
}
/// @dev Internal utility to retrieve the owner of the specified token id.
/// @param tokenId The token id to lookup
/// @return The address of the token owner
function _ownerOf(uint256 tokenId) internal view virtual returns (address) {
return $owners.get()[tokenId].toAddress();
}
/// @dev Internal utility to verify if a token id exists.
/// @param tokenId The token id to verify
/// @return True if exists
function _exists(uint256 tokenId) internal view virtual returns (bool) {
return _ownerOf(tokenId) != address(0);
}
/// @dev Internal utility to check if the specified address is either approved by the owner or the owner for the given token id.
/// @param spender The address to verify
/// @param tokenId The token id to verify
/// @return True if approved or owner
function _isApprovedOrOwner(address spender, uint256 tokenId) internal view virtual returns (bool) {
address owner = _ownerOf(tokenId);
return (spender == owner || isApprovedForAll(owner, spender) || getApproved(tokenId) == spender);
}
/// @dev Internal utility to perform a safe mint operation (mint + extra checks on contracts).
/// @param to The address receiving the token
/// @param tokenId The token id to create
/// @param data The xtra data provided to contract callback calls
// slither-disable-next-line dead-code
function _safeMint(address to, uint256 tokenId, bytes memory data) internal virtual {
_mint(to, tokenId);
if (!_checkOnERC721Received(address(0), to, tokenId, data)) {
revert NonERC721ReceiverTransfer(address(0), to, tokenId, data);
}
}
/// @dev Internal utility to mint the desired token id.
/// @param to The address that receives the token id
/// @param tokenId The token id to create
// slither-disable-next-line dead-code
function _mint(address to, uint256 tokenId) internal virtual {
if (to == address(0)) {
revert IllegalMintToZero();
}
if (_exists(tokenId)) {
revert TokenAlreadyMinted(tokenId);
}
_onMint(to, tokenId);
if (_exists(tokenId)) {
revert TokenAlreadyMinted(tokenId);
}
unchecked {
// increase owner balance
$balances.get()[to.k()] += 1;
// increase global mint counter
$mintCounter.set($mintCounter.get() + 1);
}
// set owner
$owners.get()[tokenId] = to.v();
emit Transfer(address(0), to, tokenId);
}
/// @dev Internal utility to burn the desired token id.
/// @param tokenId The token id to burn
// slither-disable-next-line dead-code
function _burn(uint256 tokenId) internal virtual {
_requireExists(tokenId);
_onBurn(tokenId);
_requireExists(tokenId);
address from = $owners.get()[tokenId].toAddress();
unchecked {
// decrease owner balance
$balances.get()[from.k()] -= 1;
// increase global burn counter
$burnCounter.set($burnCounter.get() + 1);
}
// clear owner and approvals
delete $tokenApprovals.get()[tokenId];
delete $owners.get()[tokenId];
emit Transfer(from, address(0), tokenId);
}
/// @dev Internal utility to perform a regular transfer of a token.
/// @param from The address sending the token
/// @param to The address receiving the token
/// @param tokenId The tokenId to transfer
function _transfer(address from, address to, uint256 tokenId) internal virtual {
if (to == address(0)) {
revert IllegalTransferToZero();
}
if (_ownerOf(tokenId) != from) {
revert LibErrors.Unauthorized(_ownerOf(tokenId), from);
}
_onTransfer(from, to, tokenId);
if (_ownerOf(tokenId) != from) {
revert LibErrors.Unauthorized(_ownerOf(tokenId), from);
}
// Clear approvals from the previous owner
delete $tokenApprovals.get()[tokenId];
unchecked {
$balances.get()[from.k()] -= 1;
$balances.get()[to.k()] += 1;
}
$owners.get()[tokenId] = to.v();
emit Transfer(from, to, tokenId);
}
/// @dev Internal utility to approve an account for a token id.
/// @param to The address receiving the approval
/// @param owner The owner of the token id
/// @param tokenId The token id
function _approve(address to, address owner, uint256 tokenId) internal virtual {
$tokenApprovals.get()[tokenId] = to.v();
emit Approval(owner, to, tokenId);
}
/// @dev Internal utility to approve an account for all tokens of another account.
/// @param owner The address owning the tokens
/// @param operator The address receiving the approval
/// @param approved True if approved, false otherwise
function _setApprovalForAll(address owner, address operator, bool approved) internal virtual {
if (owner == operator) {
revert ApprovalToOwner(owner);
}
$operatorApprovals.get()[owner][operator] = approved;
emit ApprovalForAll(owner, operator, approved);
}
/// @dev Internal utility to check and revert if a token doesn't exists.
/// @param tokenId The token id to verify
function _requireExists(uint256 tokenId) internal view virtual {
if (!_exists(tokenId)) {
revert InvalidTokenId(tokenId);
}
}
/// @dev Internal utility to perform checks upon transfer in the case of sending to a contract.
/// @param from The address sending the token
/// @param to The address receiving the token
/// @param data The extra data to provide in the case where to is a contract
/// @return True if all checks are good
// slither-disable-next-line variable-scope,calls-loop,unused-return
function _checkOnERC721Received(address from, address to, uint256 tokenId, bytes memory data)
private
returns (bool)
{
if (to.code.length > 0) {
try IERC721Receiver(to).onERC721Received(msg.sender, from, tokenId, data) returns (bytes4 retval) {
return retval == IERC721Receiver.onERC721Received.selector;
} catch (bytes memory reason) {
if (reason.length == 0) {
revert NonERC721ReceiverTransfer(from, to, tokenId, data);
} else {
// slither-disable-next-line assembly
assembly {
revert(add(32, reason), mload(reason))
}
}
}
} else {
return true;
}
}
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: BUSL-1.1
// SPDX-FileCopyrightText: 2023 Kiln <contact@kiln.fi>
//
// ██╗ ██╗██╗██╗ ███╗ ██╗
// ██║ ██╔╝██║██║ ████╗ ██║
// █████╔╝ ██║██║ ██╔██╗ ██║
// ██╔═██╗ ██║██║ ██║╚██╗██║
// ██║ ██╗██║███████╗██║ ╚████║
// ╚═╝ ╚═╝╚═╝╚══════╝╚═╝ ╚═══╝
//
pragma solidity >=0.8.17;
import "./types/uint256.sol";
/// @title Implementation
/// @author mortimr @ Kiln
/// @dev Unstructured Storage Friendly
/// @notice This contracts must be used on all implementation contracts. It ensures that the initializers are only callable through the proxy.
/// This will brick the implementation and make it unusable directly without using delegatecalls.
abstract contract Implementation {
using LUint256 for types.Uint256;
/// @dev The version number in storage in the initializable contract.
/// @dev Slot: keccak256(bytes("initializable.version"))) - 1
types.Uint256 internal constant $initializableVersion =
types.Uint256.wrap(0xc4c7f1ccb588f39a9aa57be6cfd798d73912e27b44cfa18e1a5eba7b34e81a76);
constructor() {
$initializableVersion.set(type(uint256).max);
}
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: BUSL-1.1
// SPDX-FileCopyrightText: 2023 Kiln <contact@kiln.fi>
//
// ██╗ ██╗██╗██╗ ███╗ ██╗
// ██║ ██╔╝██║██║ ████╗ ██║
// █████╔╝ ██║██║ ██╔██╗ ██║
// ██╔═██╗ ██║██║ ██║╚██╗██║
// ██║ ██╗██║███████╗██║ ╚████║
// ╚═╝ ╚═╝╚═╝╚══════╝╚═╝ ╚═══╝
//
pragma solidity >=0.8.17;
import "./types.sol";
library LBool {
// slither-disable-next-line dead-code
function get(types.Bool position) internal view returns (bool data) {
// slither-disable-next-line assembly
assembly {
data := sload(position)
}
}
// slither-disable-next-line dead-code
function set(types.Bool position, bool data) internal {
// slither-disable-next-line assembly
assembly {
sstore(position, data)
}
}
// slither-disable-next-line dead-code
function del(types.Bool position) internal {
// slither-disable-next-line assembly
assembly {
sstore(position, 0)
}
}
}
library CBool {
// slither-disable-next-line dead-code
function toBytes32(bool val) internal pure returns (bytes32) {
return bytes32(toUint256(val));
}
// slither-disable-next-line dead-code
function toAddress(bool val) internal pure returns (address) {
return address(uint160(toUint256(val)));
}
// slither-disable-next-line dead-code
function toUint256(bool val) internal pure returns (uint256 converted) {
// slither-disable-next-line assembly
assembly {
converted := iszero(iszero(val))
}
}
/// @dev This method should be used to convert a bool to a uint256 when used as a key in a mapping.
// slither-disable-next-line dead-code
function k(bool val) internal pure returns (uint256) {
return toUint256(val);
}
/// @dev This method should be used to convert a bool to a uint256 when used as a value in a mapping.
// slither-disable-next-line dead-code
function v(bool val) internal pure returns (uint256) {
return toUint256(val);
}
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (utils/Base64.sol)
pragma solidity ^0.8.0;
/**
* @dev Provides a set of functions to operate with Base64 strings.
*
* _Available since v4.5._
*/
library Base64 {
/**
* @dev Base64 Encoding/Decoding Table
*/
string internal constant _TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
/**
* @dev Converts a `bytes` to its Bytes64 `string` representation.
*/
function encode(bytes memory data) internal pure returns (string memory) {
/**
* Inspired by Brecht Devos (Brechtpd) implementation - MIT licence
* https://github.com/Brechtpd/base64/blob/e78d9fd951e7b0977ddca77d92dc85183770daf4/base64.sol
*/
if (data.length == 0) return "";
// Loads the table into memory
string memory table = _TABLE;
// Encoding takes 3 bytes chunks of binary data from `bytes` data parameter
// and split into 4 numbers of 6 bits.
// The final Base64 length should be `bytes` data length multiplied by 4/3 rounded up
// - `data.length + 2` -> Round up
// - `/ 3` -> Number of 3-bytes chunks
// - `4 *` -> 4 characters for each chunk
string memory result = new string(4 * ((data.length + 2) / 3));
/// @solidity memory-safe-assembly
assembly {
// Prepare the lookup table (skip the first "length" byte)
let tablePtr := add(table, 1)
// Prepare result pointer, jump over length
let resultPtr := add(result, 32)
// Run over the input, 3 bytes at a time
for {
let dataPtr := data
let endPtr := add(data, mload(data))
} lt(dataPtr, endPtr) {
} {
// Advance 3 bytes
dataPtr := add(dataPtr, 3)
let input := mload(dataPtr)
// To write each character, shift the 3 bytes (18 bits) chunk
// 4 times in blocks of 6 bits for each character (18, 12, 6, 0)
// and apply logical AND with 0x3F which is the number of
// the previous character in the ASCII table prior to the Base64 Table
// The result is then added to the table to get the character to write,
// and finally write it in the result pointer but with a left shift
// of 256 (1 byte) - 8 (1 ASCII char) = 248 bits
mstore8(resultPtr, mload(add(tablePtr, and(shr(18, input), 0x3F))))
resultPtr := add(resultPtr, 1) // Advance
mstore8(resultPtr, mload(add(tablePtr, and(shr(12, input), 0x3F))))
resultPtr := add(resultPtr, 1) // Advance
mstore8(resultPtr, mload(add(tablePtr, and(shr(6, input), 0x3F))))
resultPtr := add(resultPtr, 1) // Advance
mstore8(resultPtr, mload(add(tablePtr, and(input, 0x3F))))
resultPtr := add(resultPtr, 1) // Advance
}
// When data `bytes` is not exactly 3 bytes long
// it is padded with `=` characters at the end
switch mod(mload(data), 3)
case 1 {
mstore8(sub(resultPtr, 1), 0x3d)
mstore8(sub(resultPtr, 2), 0x3d)
}
case 2 {
mstore8(sub(resultPtr, 1), 0x3d)
}
}
return result;
}
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: BUSL-1.1
// SPDX-FileCopyrightText: 2023 Kiln <contact@kiln.fi>
//
// ██╗ ██╗██╗██╗ ███╗ ██╗
// ██║ ██╔╝██║██║ ████╗ ██║
// █████╔╝ ██║██║ ██╔██╗ ██║
// ██╔═██╗ ██║██║ ██║╚██╗██║
// ██║ ██╗██║███████╗██║ ╚████║
// ╚═╝ ╚═╝╚═╝╚══════╝╚═╝ ╚═══╝
//
pragma solidity 0.8.17;
import "utils.sol/libs/LibUint256.sol";
import "utils.sol/libs/LibBytes.sol";
import "openzeppelin-contracts/utils/Strings.sol";
/// @title Stringify Library - A library for converting numbers to strings with decimal support
library LibStringify {
/// @dev Generates a string in memory with the requested count of zeroes
/// @param count The number of zeroes to generate
/// @return The generated string
function generateZeroes(uint256 count) internal pure returns (string memory) {
bytes memory zeroes = new bytes(count);
for (uint256 idx = 0; idx < count;) {
zeroes[idx] = "0";
unchecked {
++idx;
}
}
return string(zeroes);
}
/// @dev Converts a uint256 to a string with the requested number of decimals
/// @param value The value to convert
/// @param decimals The number of decimals to include
/// @param maxIncludedDecimals The maximum number of decimals to include
/// @return The generated string
function uintToDecimalString(uint256 value, uint8 decimals, uint8 maxIncludedDecimals) internal pure returns (string memory) {
if (value == 0) {
return "0";
}
bytes memory uintToString = bytes(Strings.toString(value));
if (decimals == 0) {
return string(uintToString);
}
uint256 len = uintToString.length;
if (len > decimals) {
if (maxIncludedDecimals == 0) {
return string(LibBytes.slice(bytes(uintToString), 0, len - decimals));
}
uintToString = abi.encodePacked(
LibBytes.slice(bytes(uintToString), 0, len - decimals),
".",
LibBytes.slice(bytes(uintToString), len - decimals, LibUint256.min(decimals, maxIncludedDecimals))
);
} else {
if (maxIncludedDecimals <= decimals - len) {
return "0";
}
uintToString = abi.encodePacked(
"0.",
LibBytes.slice(
abi.encodePacked(generateZeroes(decimals - len), uintToString), 0, LibUint256.min(decimals, maxIncludedDecimals)
)
);
}
return string(uintToString);
}
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: BUSL-1.1
// SPDX-FileCopyrightText: 2023 Kiln <contact@kiln.fi>
//
// ██╗ ██╗██╗██╗ ███╗ ██╗
// ██║ ██╔╝██║██║ ████╗ ██║
// █████╔╝ ██║██║ ██╔██╗ ██║
// ██╔═██╗ ██║██║ ██║╚██╗██║
// ██║ ██╗██║███████╗██║ ╚████║
// ╚═╝ ╚═╝╚═╝╚══════╝╚═╝ ╚═══╝
//
pragma solidity 0.8.17;
import "./ctypes.sol";
/// @title Ticket Array Custom Type
library LTicketArray {
function get(ctypes.TicketArray position) internal pure returns (ctypes.Ticket[] storage data) {
// slither-disable-next-line assembly
assembly {
data.slot := position
}
}
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: BUSL-1.1
// SPDX-FileCopyrightText: 2023 Kiln <contact@kiln.fi>
//
// ██╗ ██╗██╗██╗ ███╗ ██╗
// ██║ ██╔╝██║██║ ████╗ ██║
// █████╔╝ ██║██║ ██╔██╗ ██║
// ██╔═██╗ ██║██║ ██║╚██╗██║
// ██║ ██╗██║███████╗██║ ╚████║
// ╚═╝ ╚═╝╚═╝╚══════╝╚═╝ ╚═══╝
//
pragma solidity 0.8.17;
import "./ctypes.sol";
/// @title Cask Array Custom Type
library LCaskArray {
function get(ctypes.CaskArray position) internal pure returns (ctypes.Cask[] storage data) {
// slither-disable-next-line assembly
assembly {
data.slot := position
}
}
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2023 Kiln <contact@kiln.fi>
//
// ██╗ ██╗██╗██╗ ███╗ ██╗
// ██║ ██╔╝██║██║ ████╗ ██║
// █████╔╝ ██║██║ ██╔██╗ ██║
// ██╔═██╗ ██║██║ ██║╚██╗██║
// ██║ ██╗██║███████╗██║ ╚████║
// ╚═╝ ╚═╝╚═╝╚══════╝╚═╝ ╚═══╝
//
pragma solidity 0.8.17;
import "utils.sol/interfaces/IFixable.sol";
import "./IvPoolSharesReceiver.sol";
import "../ctypes/ctypes.sol";
/// @title Exit Queue Interface
/// @author mortimr @ Kiln
/// @notice The exit queue stores exit requests until they are filled and claimable
interface IvExitQueue is IFixable, IvPoolSharesReceiver {
/// @notice Emitted when the stored Pool address is changed
/// @param pool The new pool address
event SetPool(address pool);
/// @notice Emitted when the stored token uri image url is changed
/// @param tokenUriImageUrl The new token uri image url
event SetTokenUriImageUrl(string tokenUriImageUrl);
/// @notice Emitted when the transfer enabled status is changed
/// @param enabled The new transfer enabled status
event SetTransferEnabled(bool enabled);
/// @notice Emitted when the unclaimed funds buffer is changed
/// @param unclaimedFunds The new unclaimed funds buffer
event SetUnclaimedFunds(uint256 unclaimedFunds);
/// @notice Emitted when ether was supplied to the vPool
/// @param amount The amount of ETH supplied
event SuppliedEther(uint256 amount);
/// @notice Emitted when a ticket is created
/// @param owner The address of the ticket owner
/// @param idx The index of the ticket
/// @param id The ID of the ticket
/// @param ticket The ticket details
event PrintedTicket(address indexed owner, uint32 idx, uint256 id, ctypes.Ticket ticket);
/// @notice Emitted when a cask is created
/// @param id The ID of the cask
/// @param cask The cask details
event ReceivedCask(uint32 id, ctypes.Cask cask);
/// @notice Emitted when a ticket is claimed against a cask, can happen several times for the same ticket but different casks
/// @param ticketId The ID of the ticket
/// @param caskId The ID of the cask
/// @param amountFilled The amount of shares filled
/// @param amountEthFilled The amount of ETH filled
/// @param unclaimedEth The amount of ETH that is added to the unclaimed buffer
event FilledTicket(
uint256 indexed ticketId, uint32 indexed caskId, uint128 amountFilled, uint256 amountEthFilled, uint256 unclaimedEth
);
/// @notice Emitted when a ticket is "reminted" and its external id is modified
/// @param oldTicketId The old ID of the ticket
/// @param newTicketId The new ID of the ticket
/// @param ticketIndex The index of the ticket
event TicketIdUpdated(uint256 indexed oldTicketId, uint256 indexed newTicketId, uint32 indexed ticketIndex);
/// @notice Emitted when a payment is made after a user performed a claim
/// @param recipient The address of the recipient
/// @param amount The amount of ETH paid
event Payment(address indexed recipient, uint256 amount);
/// @notice Transfer of tickets is disabled
error TransferDisabled();
/// @notice The provided ticket ID is invalid
/// @param id The ID of the ticket
error InvalidTicketId(uint256 id);
/// @notice The provided cask ID is invalid
/// @param id The ID of the cask
error InvalidCaskId(uint32 id);
/// @notice The provided ticket IDs and cask IDs are not the same length
error InvalidLengths();
/// @notice The ticket and cask are not associated
/// @param ticketId The ID of the ticket
/// @param caskId The ID of the cask
error TicketNotMatchingCask(uint256 ticketId, uint32 caskId);
/// @notice The claim transfer failed
/// @param recipient The address of the recipient
/// @param rdata The revert data
error ClaimTransferFailed(address recipient, bytes rdata);
enum ClaimStatus {
CLAIMED,
PARTIALLY_CLAIMED,
SKIPPED
}
/// @notice Initializes the ExitQueue (proxy pattern)
/// @param vpool The address of the associated vPool
/// @param newTokenUriImageUrl The token uri image url
function initialize(address vpool, string calldata newTokenUriImageUrl) external;
/// @notice Returns the token uri image url
/// @return The token uri image url
function tokenUriImageUrl() external view returns (string memory);
/// @notice Returns the transfer enabled status
/// @return True if transfers are enabled
function transferEnabled() external view returns (bool);
/// @notice Returns the unclaimed funds buffer
/// @return The unclaimed funds buffer
function unclaimedFunds() external view returns (uint256);
/// @notice Returns the id of the ticket based on the index
/// @param idx The index of the ticket
function ticketIdAtIndex(uint32 idx) external view returns (uint256);
/// @notice Returns the details about the ticket with the provided ID
/// @param id The ID of the ticket
/// @return The ticket details
function ticket(uint256 id) external view returns (ctypes.Ticket memory);
/// @notice Returns the number of tickets
/// @return The number of tickets
function ticketCount() external view returns (uint256);
/// @notice Returns the details about the cask with the provided ID
/// @param id The ID of the cask
/// @return The cask details
function cask(uint32 id) external view returns (ctypes.Cask memory);
/// @notice Returns the number of casks
/// @return The number of casks
function caskCount() external view returns (uint256);
/// @notice Resolves the provided tickets to their associated casks or provide resolution error codes
/// @dev TICKET_ID_OUT_OF_BOUNDS = -1;
/// TICKET_ALREADY_CLAIMED = -2;
/// TICKET_PENDING = -3;
/// @param ticketIds The IDs of the tickets to resolve
/// @return caskIdsOrErrors The IDs of the casks or error codes
function resolve(uint256[] memory ticketIds) external view returns (int64[] memory caskIdsOrErrors);
/// @notice Adds eth and creates a new cask
/// @dev only callbacle by the vPool
/// @param shares The amount of shares to cover with the provided eth
function feed(uint256 shares) external payable;
/// @notice Pulls eth from the unclaimed eth buffer
/// @dev Only callable by the vPool
/// @param max The maximum amount of eth to pull
function pull(uint256 max) external;
/// @notice Claims the provided tickets against their associated casks
/// @dev To retrieve the list of casks, an off-chain resolve call should be performed
/// @param ticketIds The IDs of the tickets to claim
/// @param caskIds The IDs of the casks to claim against
/// @param maxClaimDepth The maxiumum recursion depth for the claim, 0 for unlimited
function claim(uint256[] calldata ticketIds, uint32[] calldata caskIds, uint16 maxClaimDepth)
external
returns (ClaimStatus[] memory statuses);
/// @notice Sets the token uri image inside the returned token uri
/// @param newTokenUriImageUrl The new token uri image url
function setTokenUriImageUrl(string calldata newTokenUriImageUrl) external;
/// @notice Enables or disables transfers of the tickets
/// @param value True to allow transfers
function setTransferEnabled(bool value) external;
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2023 Kiln <contact@kiln.fi>
//
// ██╗ ██╗██╗██╗ ███╗ ██╗
// ██║ ██╔╝██║██║ ████╗ ██║
// █████╔╝ ██║██║ ██╔██╗ ██║
// ██╔═██╗ ██║██║ ██║╚██╗██║
// ██║ ██╗██║███████╗██║ ╚████║
// ╚═╝ ╚═╝╚═╝╚══════╝╚═╝ ╚═══╝
//
pragma solidity 0.8.17;
import "utils.sol/interfaces/IFixable.sol";
import "../ctypes/ctypes.sol";
/// @title Pool Interface
/// @author mortimr @ Kiln
/// @notice The vPool contract is in charge of pool funds and fund validators from the vFactory
interface IvPool is IFixable {
/// @notice Emitted at construction time when all contract addresses are set
/// @param factory The address of the vFactory contract
/// @param withdrawalRecipient The address of the withdrawal recipient contract
/// @param execLayerRecipient The address of the execution layer recipient contract
/// @param coverageRecipient The address of the coverage recipient contract
/// @param oracleAggregator The address of the oracle aggregator contract
/// @param exitQueue The address of the exit queue contract
event SetContractLinks(
address factory,
address withdrawalRecipient,
address execLayerRecipient,
address coverageRecipient,
address oracleAggregator,
address exitQueue
);
/// @notice Emitted when the global validator extra data is changed
/// @param extraData New extra data used on validator purchase
event SetValidatorGlobalExtraData(string extraData);
/// @notice Emitted when a depositor authorization changed
/// @param depositor The address of the depositor
/// @param allowed True if allowed to deposit
event ApproveDepositor(address depositor, bool allowed);
/// @notice Emitted when a depositor performs a deposit
/// @param sender The transaction sender
/// @param amount The deposit amount
/// @param mintedShares The amount of shares created
event Deposit(address indexed sender, uint256 amount, uint256 mintedShares);
/// @notice Emitted when the vPool purchases validators to the vFactory
/// @param validators The list of IDs (not BLS Public keys)
event PurchasedValidators(uint256[] validators);
/// @notice Emitted when new shares are created
/// @param account The account receiving the new shares
/// @param amount The amount of shares created
/// @param totalSupply The new totalSupply value
event Mint(address indexed account, uint256 amount, uint256 totalSupply);
/// @notice Emitted when shares are burned
/// @param burner The account burning shares
/// @param amount The amount of burned shares
/// @param totalSupply The new totalSupply value
event Burn(address burner, uint256 amount, uint256 totalSupply);
/// @notice Emitted when shares are transfered
/// @param from The account sending the shares
/// @param to The account receiving the shares
/// @param value The value transfered
event Transfer(address indexed from, address indexed to, uint256 value);
/// @notice Emitted when shares are approved for a spender
/// @param owner The account approving the shares
/// @param spender The account receiving the spending rights
/// @param value The value of the approval. Max uint256 means infinite (will never decrease)
event Approval(address indexed owner, address indexed spender, uint256 value);
/// @notice Emitted when shares are voided (action of burning without redeeming anything on purpose)
/// @param voider The account voiding the shares
/// @param amount The amount of voided shares
event VoidedShares(address voider, uint256 amount);
/// @notice Emitted when ether is injected into the system (outside of the deposit flow)
/// @param injecter The account injecting the ETH
/// @param amount The amount of injected ETH
event InjectedEther(address injecter, uint256 amount);
/// @notice Emitted when the report processing is finished
/// @param epoch The epoch number
/// @param report The received report structure
/// @param traces Internal traces with key figures
event ProcessedReport(uint256 indexed epoch, ctypes.ValidatorsReport report, ReportTraces traces);
/// @notice Emitted when rewards are distributed to the node operator
/// @param operatorTreasury The address receiving the rewards
/// @param sharesCount The amount of shares created to pay the rewards
/// @param sharesValue The value in ETH of the newly minted shares
/// @param totalSupply The updated totalSupply value
/// @param totalUnderlyingSupply The updated totalUnderlyingSupply value
event DistributedOperatorRewards(
address indexed operatorTreasury, uint256 sharesCount, uint256 sharesValue, uint256 totalSupply, uint256 totalUnderlyingSupply
);
/// @notice Emitted when the report bounds are updated
/// @param maxAPRUpperBound The maximum APR allowed during oracle reports
/// @param maxAPRUpperCoverageBoost The APR boost allowed only for coverage funds
/// @param maxRelativeLowerBound The max relative delta in underlying supply authorized during losses of funds
event SetReportBounds(uint64 maxAPRUpperBound, uint64 maxAPRUpperCoverageBoost, uint64 maxRelativeLowerBound);
/// @notice Emitted when the epochs per frame value is updated
/// @param epochsPerFrame The new epochs per frame value
event SetEpochsPerFrame(uint256 epochsPerFrame);
/// @notice Emitted when the consensus layer spec is updated
/// @param consensusLayerSpec The new consensus layer spec
event SetConsensusLayerSpec(ctypes.ConsensusLayerSpec consensusLayerSpec);
/// @notice Emitted when the operator fee is updated
/// @param operatorFeeBps The new operator fee value
event SetOperatorFee(uint256 operatorFeeBps);
/// @notice Emitted when the deposited ether buffer is updated
/// @param depositedEthers The new deposited ethers value
event SetDepositedEthers(uint256 depositedEthers);
/// @notice Emitted when the committed ether buffer is updated
/// @param committedEthers The new committed ethers value
event SetCommittedEthers(uint256 committedEthers);
/// @notice Emitted when the requested exits is updated
/// @param newRequestedExits The new requested exits count
event SetRequestedExits(uint32 newRequestedExits);
/// @notice The balance was too low for the requested operation
/// @param account The account trying to perform the operation
/// @param currentBalance The current account balance
/// @param requiredAmount The amount that was required to perform the operation
error BalanceTooLow(address account, uint256 currentBalance, uint256 requiredAmount);
/// @notice The allowance was too low for the requested operation
/// @param account The account trying to perform the operation
/// @param operator The account triggering the operation on behalf of the account
/// @param currentApproval The current account approval towards the operator
/// @param requiredAmount The amount that was required to perform the operation
error AllowanceTooLow(address account, address operator, uint256 currentApproval, uint256 requiredAmount);
/// @notice Thrown when approval for an account and spender is already zero.
/// @param account The account for which approval was attempted to be set to zero.
/// @param spender The spender for which approval was attempted to be set to zero.
error ApprovalAlreadyZero(address account, address spender);
/// @notice Thrown when there is an error with a share receiver.
/// @param err The error message.
error ShareReceiverError(string err);
/// @notice Thrown when there is no validator available to purchase.
error NoValidatorToPurchase();
/// @notice Thrown when the epoch of a report is too old.
/// @param epoch The epoch of the report.
/// @param expectEpoch The expected epoch for the operation.
error EpochTooOld(uint256 epoch, uint256 expectEpoch);
/// @notice Thrown when an epoch is not the first epoch of a frame.
/// @param epoch The epoch that was not the first epoch of a frame.
error EpochNotFrameFirst(uint256 epoch);
/// @notice Thrown when an epoch is not final.
/// @param epoch The epoch that was not final.
/// @param currentTimestamp The current timestamp.
/// @param finalTimestamp The final timestamp of the frame.
error EpochNotFinal(uint256 epoch, uint256 currentTimestamp, uint256 finalTimestamp);
/// @notice Thrown when the validator count is decreasing.
/// @param previousValidatorCount The previous validator count.
/// @param validatorCount The current validator count.
error DecreasingValidatorCount(uint256 previousValidatorCount, uint256 validatorCount);
/// @notice Thrown when the stopped validator count is decreasing.
/// @param previousStoppedValidatorCount The previous stopped validator count.
/// @param stoppedValidatorCount The current stopped validator count.
error DecreasingStoppedValidatorCount(uint256 previousStoppedValidatorCount, uint256 stoppedValidatorCount);
/// @notice Thrown when the slashed balance sum is decreasing.
/// @param reportedSlashedBalanceSum The reported slashed balance sum.
/// @param lastReportedSlashedBalanceSum The last reported slashed balance sum.
error DecreasingSlashedBalanceSum(uint256 reportedSlashedBalanceSum, uint256 lastReportedSlashedBalanceSum);
/// @notice Thrown when the exited balance sum is decreasing.
/// @param reportedExitedBalanceSum The reported exited balance sum.
/// @param lastReportedExitedBalanceSum The last reported exited balance sum.
error DecreasingExitedBalanceSum(uint256 reportedExitedBalanceSum, uint256 lastReportedExitedBalanceSum);
/// @notice Thrown when the skimmed balance sum is decreasing.
/// @param reportedSkimmedBalanceSum The reported skimmed balance sum.
/// @param lastReportedSkimmedBalanceSum The last reported skimmed balance sum.
error DecreasingSkimmedBalanceSum(uint256 reportedSkimmedBalanceSum, uint256 lastReportedSkimmedBalanceSum);
/// @notice Thrown when the reported validator count is higher than the total activated validators
/// @param stoppedValidatorsCount The reported stopped validator count.
/// @param maxStoppedValidatorsCount The maximum allowed stopped validator count.
error StoppedValidatorCountTooHigh(uint256 stoppedValidatorsCount, uint256 maxStoppedValidatorsCount);
/// @notice Thrown when the reported exiting balance exceeds the total validator balance on the cl
/// @param exiting The reported exiting balance.
/// @param balance The total validator balance on the cl.
error ExitingBalanceTooHigh(uint256 exiting, uint256 balance);
/// @notice Thrown when the reported validator count is higher than the deposited validator count.
/// @param reportedValidatorCount The reported validator count.
/// @param depositedValidatorCount The deposited validator count.
error ValidatorCountTooHigh(uint256 reportedValidatorCount, uint256 depositedValidatorCount);
/// @notice Thrown when the coverage is higher than the loss.
/// @param coverage The coverage.
/// @param loss The loss.
error CoverageHigherThanLoss(uint256 coverage, uint256 loss);
/// @notice Thrown when the balance increase exceeds the maximum allowed balance increase.
/// @param balanceIncrease The balance increase.
/// @param maximumAllowedBalanceIncrease The maximum allowed balance increase.
error UpperBoundCrossed(uint256 balanceIncrease, uint256 maximumAllowedBalanceIncrease);
/// @notice Thrown when the balance increase exceeds the maximum allowed balance increase or maximum allowed coverage.
/// @param balanceIncrease The balance increase.
/// @param maximumAllowedBalanceIncrease The maximum allowed balance increase.
/// @param maximumAllowedCoverage The maximum allowed coverage.
error BoostedBoundCrossed(uint256 balanceIncrease, uint256 maximumAllowedBalanceIncrease, uint256 maximumAllowedCoverage);
/// @notice Thrown when the balance decrease exceeds the maximum allowed balance decrease.
/// @param balanceDecrease The balance decrease.
/// @param maximumAllowedBalanceDecrease The maximum allowed balance decrease.
error LowerBoundCrossed(uint256 balanceDecrease, uint256 maximumAllowedBalanceDecrease);
/// @notice Thrown when the amount of shares to mint is computed to 0
error InvalidNullMint();
/// @notice Traces emitted at the end of the reporting process.
/// @param preUnderlyingSupply The pre-reporting underlying supply.
/// @param postUnderlyingSupply The post-reporting underlying supply.
/// @param preSupply The pre-reporting supply.
/// @param postSupply The post-reporting supply.
/// @param newExitedEthers The new exited ethers.
/// @param newSkimmedEthers The new skimmed ethers.
/// @param exitBoostEthers The exit boost ethers.
/// @param exitFedEthers The exit fed ethers.
/// @param exitBurnedShares The exit burned shares.
/// @param exitingProjection The exiting projection.
/// @param baseFulfillableDemand The base fulfillable demand.
/// @param extraFulfillableDemand The extra fulfillable demand.
/// @param rewards The rewards. Can be negative when there is a loss, but cannot include coverage funds.
/// @param delta The delta. Can be negative when there is a loss and include all pulled funds.
/// @param increaseLimit The increase limit.
/// @param coverageIncreaseLimit The coverage increase limit.
/// @param decreaseLimit The decrease limit.
/// @param consensusLayerDelta The consensus layer delta.
/// @param pulledCoverageFunds The pulled coverage funds.
/// @param pulledExecutionLayerRewards The pulled execution layer rewards.
/// @param pulledExitQueueUnclaimedFunds The pulled exit queue unclaimed funds.
struct ReportTraces {
// supplied
uint128 preUnderlyingSupply;
uint128 postUnderlyingSupply;
uint128 preSupply;
uint128 postSupply;
// new consensus layer funds
uint128 newExitedEthers;
uint128 newSkimmedEthers;
// exit related funds
uint128 exitBoostEthers;
uint128 exitFedEthers;
uint128 exitBurnedShares;
uint128 exitingProjection;
uint128 baseFulfillableDemand;
uint128 extraFulfillableDemand;
// rewards
int128 rewards;
// delta and details about sources of funds
int128 delta;
uint128 increaseLimit;
uint128 coverageIncreaseLimit;
uint128 decreaseLimit;
int128 consensusLayerDelta;
uint128 pulledCoverageFunds;
uint128 pulledExecutionLayerRewards;
uint128 pulledExitQueueUnclaimedFunds;
}
/// @notice Initializes the contract with the given parameters.
/// @param addrs The addresses of the dependencies (factory, withdrawal recipient, exec layer recipient,
/// coverage recipient, oracle aggregator, exit queue).
/// @param epochsPerFrame_ The number of epochs per frame.
/// @param consensusLayerSpec_ The consensus layer spec.
/// @param bounds_ The bounds for reporting.
/// @param operatorFeeBps_ The operator fee in basis points.
/// @param extraData_ The initial extra data that will be provided on each deposit
function initialize(
address[6] calldata addrs,
uint256 epochsPerFrame_,
ctypes.ConsensusLayerSpec calldata consensusLayerSpec_,
uint64[3] calldata bounds_,
uint256 operatorFeeBps_,
string calldata extraData_
) external;
/// @notice Returns the address of the factory contract.
/// @return The address of the factory contract.
function factory() external view returns (address);
/// @notice Returns the address of the execution layer recipient contract.
/// @return The address of the execution layer recipient contract.
function execLayerRecipient() external view returns (address);
/// @notice Returns the address of the coverage recipient contract.
/// @return The address of the coverage recipient contract.
function coverageRecipient() external view returns (address);
/// @notice Returns the address of the withdrawal recipient contract.
/// @return The address of the withdrawal recipient contract.
function withdrawalRecipient() external view returns (address);
/// @notice Returns the address of the oracle aggregator contract.
/// @return The address of the oracle aggregator contract.
function oracleAggregator() external view returns (address);
/// @notice Returns the address of the exit queue contract
/// @return The address of the exit queue contract
function exitQueue() external view returns (address);
/// @notice Returns the current validator global extra data
/// @return The validator global extra data value
function validatorGlobalExtraData() external view returns (string memory);
/// @notice Returns whether the given address is a depositor.
/// @param depositorAddress The address to check.
/// @return Whether the given address is a depositor.
function depositors(address depositorAddress) external view returns (bool);
/// @notice Returns the total supply of tokens.
/// @return The total supply of tokens.
function totalSupply() external view returns (uint256);
/// @notice Returns the name of the vPool
/// @return The name of the vPool
function name() external view returns (string memory);
/// @notice Returns the symbol of the vPool
/// @return The symbol of the vPool
function symbol() external view returns (string memory);
/// @notice Returns the decimals of the vPool shares
/// @return The decimal count
function decimals() external pure returns (uint8);
/// @notice Returns the total underlying supply of tokens.
/// @return The total underlying supply of tokens.
function totalUnderlyingSupply() external view returns (uint256);
/// @notice Returns the current ETH/SHARES rate based on the total underlying supply and total supply.
/// @return The current rate
function rate() external view returns (uint256);
/// @notice Returns the current requested exit count
/// @return The current requested exit count
function requestedExits() external view returns (uint32);
/// @notice Returns the balance of the given account.
/// @param account The address of the account to check.
/// @return The balance of the given account.
function balanceOf(address account) external view returns (uint256);
/// @notice Returns the allowance of the given spender for the given owner.
/// @param owner The owner of the allowance.
/// @param spender The spender of the allowance.
/// @return The allowance of the given spender for the given owner.
function allowance(address owner, address spender) external view returns (uint256);
/// @notice Returns the details about the held ethers
/// @return The structure of ethers inside the contract
function ethers() external view returns (ctypes.Ethers memory);
/// @notice Returns an array of the IDs of purchased validators.
/// @return An array of the IDs of purchased validators.
function purchasedValidators() external view returns (uint256[] memory);
/// @notice Returns the ID of the purchased validator at the given index.
/// @param idx The index of the validator.
/// @return The ID of the purchased validator at the given index.
function purchasedValidatorAtIndex(uint256 idx) external view returns (uint256);
/// @notice Returns the total number of purchased validators.
/// @return The total number of purchased validators.
function purchasedValidatorCount() external view returns (uint256);
/// @notice Returns the last epoch.
/// @return The last epoch.
function lastEpoch() external view returns (uint256);
/// @notice Returns the last validator report that was processed
/// @return The last report structure.
function lastReport() external view returns (ctypes.ValidatorsReport memory);
/// @notice Returns the total amount in ETH covered by the contract.
/// @return The total amount in ETH covered by the contract.
function totalCovered() external view returns (uint256);
/// @notice Returns the number of epochs per frame.
/// @return The number of epochs per frame.
function epochsPerFrame() external view returns (uint256);
/// @notice Returns the consensus layer spec.
/// @return The consensus layer spec.
function consensusLayerSpec() external pure returns (ctypes.ConsensusLayerSpec memory);
/// @notice Returns the report bounds.
/// @return maxAPRUpperBound The maximum APR for the upper bound.
/// @return maxAPRUpperCoverageBoost The maximum APR for the upper bound with coverage boost.
/// @return maxRelativeLowerBound The maximum relative lower bound.
function reportBounds()
external
view
returns (uint64 maxAPRUpperBound, uint64 maxAPRUpperCoverageBoost, uint64 maxRelativeLowerBound);
/// @notice Returns the operator fee.
/// @return The operator fee.
function operatorFee() external view returns (uint256);
/// @notice Returns whether the given epoch is valid.
/// @param epoch The epoch to check.
/// @return Whether the given epoch is valid.
function isValidEpoch(uint256 epoch) external view returns (bool);
/// @notice Reverts if given epoch is invalid, with an explicit custom error based on the issue
/// @param epoch The epoch to check.
function onlyValidEpoch(uint256 epoch) external view;
/// @notice Allows or disallows the given depositor to deposit.
/// @param depositorAddress The address of the depositor.
/// @param allowed Whether the depositor is allowed to deposit.
function allowDepositor(address depositorAddress, bool allowed) external;
/// @notice Transfers the given amount of shares to the given address.
/// @param to The address to transfer the shares to.
/// @param amount The amount of shares to transfer.
/// @param data Additional data for the transfer.
/// @return Whether the transfer was successful.
function transferShares(address to, uint256 amount, bytes calldata data) external returns (bool);
/// @notice Increases the allowance for the given spender by the given amount.
/// @param spender The spender to increase the allowance for.
/// @param amount The amount to increase the allowance by.
/// @return Whether the increase was successful.
function increaseAllowance(address spender, uint256 amount) external returns (bool);
/// @notice Decreases the allowance of a spender by the given amount.
/// @param spender The address of the spender.
/// @param amount The amount to decrease the allowance by.
/// @return Whether the allowance was successfully decreased.
function decreaseAllowance(address spender, uint256 amount) external returns (bool);
/// @notice Voids the allowance of a spender.
/// @param spender The address of the spender.
/// @return Whether the allowance was successfully voided.
function voidAllowance(address spender) external returns (bool);
/// @notice Transfers shares from one account to another.
/// @param from The address of the account to transfer shares from.
/// @param to The address of the account to transfer shares to.
/// @param amount The amount of shares to transfer.
/// @param data Optional data to include with the transaction.
/// @return Whether the transfer was successful.
function transferSharesFrom(address from, address to, uint256 amount, bytes calldata data) external returns (bool);
/// @notice Deposits ether into the contract.
/// @return The number of shares minted on deposit
function deposit() external payable returns (uint256);
/// @notice Purchases the maximum number of validators allowed.
/// @param max The maximum number of validators to purchase.
function purchaseValidators(uint256 max) external;
/// @notice Sets the operator fee.
/// @param operatorFeeBps The new operator fee, in basis points.
function setOperatorFee(uint256 operatorFeeBps) external;
/// @notice Sets the number of epochs per frame.
/// @param newEpochsPerFrame The new number of epochs per frame.
function setEpochsPerFrame(uint256 newEpochsPerFrame) external;
/// @notice Sets the consensus layer spec.
/// @param consensusLayerSpec_ The new consensus layer spec.
function setConsensusLayerSpec(ctypes.ConsensusLayerSpec calldata consensusLayerSpec_) external;
/// @notice Sets the global validator extra data
/// @param extraData The new extra data to use
function setValidatorGlobalExtraData(string calldata extraData) external;
/// @notice Sets the bounds for reporting.
/// @param maxAPRUpperBound The maximum APR for the upper bound.
/// @param maxAPRUpperCoverageBoost The maximum APR for the upper coverage boost.
/// @param maxRelativeLowerBound The maximum relative value for the lower bound.
function setReportBounds(uint64 maxAPRUpperBound, uint64 maxAPRUpperCoverageBoost, uint64 maxRelativeLowerBound) external;
/// @notice Injects ether into the contract.
function injectEther() external payable;
/// @notice Voids the given amount of shares.
/// @param amount The amount of shares to void.
function voidShares(uint256 amount) external;
/// @notice Reports the validator data for the given epoch.
/// @param rprt The consensus layer report to process
function report(ctypes.ValidatorsReport calldata rprt) external;
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2023 Kiln <contact@kiln.fi>
//
// ██╗ ██╗██╗██╗ ███╗ ██╗
// ██║ ██╔╝██║██║ ████╗ ██║
// █████╔╝ ██║██║ ██╔██╗ ██║
// ██╔═██╗ ██║██║ ██║╚██╗██║
// ██║ ██╗██║███████╗██║ ╚████║
// ╚═╝ ╚═╝╚═╝╚══════╝╚═╝ ╚═══╝
//
pragma solidity 0.8.17;
import "utils.sol/interfaces/IAdministrable.sol";
import "utils.sol/interfaces/IDepositor.sol";
import "utils.sol/interfaces/IFixable.sol";
/// @title Factory Interface
/// @author mortimr @ Kiln
/// @notice The vFactory contract is in charge of depositing validators to the consensus layer
interface IvFactory is IAdministrable, IDepositor, IFixable {
/// @notice The provided array is empty
error EmptyArray();
/// @notice The provided arrays do not have matching lengths
error InvalidArrayLengths();
/// @notice The withdrawal attempt was made on a validator that collected no funds
error EmptyWithdrawalRecipient();
/// @notice The provided key concatenation is empty
/// @param index The index of the invalid key concatenation in the calldata parameters
error EmptyKeyPayload(uint256 index);
/// @notice The provided validator id is invalid
/// @param id The invalid id
error InvalidValidatorId(uint256 id);
/// @notice The provided key concatenation is invalid
/// @param index The index of the invalid key concatenation in the calldata parameters
error InvalidKeyPayload(uint256 index);
/// @notice The provided indexes array if empty
/// @param index The index of the invalid index array in the calldata parameters
error EmptyIndexesArray(uint256 index);
/// @notice The provided indexes array is unsorted
/// @param index The index of the invalid index array in the calldata parameters
error UnsortedIndexArray(uint256 index);
/// @notice The withdrawal call performed on the minimal recipient reverted
/// @param rdata The resulting error return data
error MinimalRecipientExecutionError(bytes rdata);
/// @notice The provided withdrawal channel is invalid
/// @param withdrawalChannel The invalid withdrawal channel
error InvalidWithdrawalChannel(bytes32 withdrawalChannel);
/// @notice The provided message value in ether is invalid
/// @param received The provided amount
/// @param expected The expected amount
error InvalidMessageValue(uint256 received, uint256 expected);
/// @notice The requested validator count is too high
/// @param requested The count of validators requested
/// @param available The count of available validators
error NotEnoughValidators(uint256 requested, uint256 available);
/// @notice The provided validator index is out of bounds
/// @param index The indexes array index in the calldata
/// @param validatorIndex The invalid validator index
error ValidatorIndexOutOfBounds(uint256 index, uint256 validatorIndex);
/// @notice A funded validator removal was attempted
/// @param index The indexes array index in the calldata
/// @param validatorIndex The funded validator index
error FundedValidatorRemovalAttempt(uint256 index, uint256 validatorIndex);
/// @notice Error raised when the requested total exits on a custom channel is higher than the total funded count
/// @param withdrawalChannel The withdrawal channel
/// @param requestedTotal The total requested exits
/// @param maxFundedCount The count of funded validators on the channel
error ExitTotalTooHigh(bytes32 withdrawalChannel, uint32 requestedTotal, uint32 maxFundedCount);
/// @notice Error raised when the requested limit on a withdrawal channel is higher than the validators count.
/// @param withdrawalChannel The withdrawal channel
/// @param limit The limit requested
/// @param validatorCount The count of validators on the channel
error LimitExceededValidatorCount(bytes32 withdrawalChannel, uint256 limit, uint256 validatorCount);
/// @notice Emitted when the minimal recipient implementation is set
/// @param minimalRecipientImplementation The address of the implementation
event SetMinimalRecipientImplementation(address minimalRecipientImplementation);
/// @notice Emitted when hatcher registry is set
/// @param hatcherRegistry The address of the hatcher registry
event SetHatcherRegistry(address hatcherRegistry);
/// @notice Emitted when the operator changed
/// @param operator The new operator address
event ChangedOperator(address operator);
/// @notice Emitted when the treasury changed
/// @param treasury The new treasury address
event ChangedTreasury(address treasury);
/// @notice Emitted when an exit request was made
/// @param withdrawalChannel The withdrawal channel that received the exit request
/// @param publicKey The public key of the validator that requested the exit
/// @param id The id of the validator that requested the exit
event ExitValidator(bytes32 indexed withdrawalChannel, bytes publicKey, uint256 id);
/// @notice Emitted when the owner of a validator is changed
/// @param id The id of the validator
/// @param owner The new owner address
event SetValidatorOwner(uint256 indexed id, address owner);
/// @notice Emitted when the metadata of the vFactory is changed
/// @param name The operator name
/// @param url The operator shared url
/// @param iconUrl The operator icon
event SetMetadata(string name, string url, string iconUrl);
/// @notice Emitted when a depositor authorization changed
/// @param depositor The address of the depositor
/// @param wc The withdrawal channel
/// @param allowed True if allowed to deposit
event ApproveDepositor(address indexed depositor, bytes32 indexed wc, bool allowed);
/// @notice Emitted when new keys are added to a withdrawal channel
/// @param withdrawalChannel The withdrawal channel that received new keys
/// @param keys The keys that were added
event AddedValidators(bytes32 indexed withdrawalChannel, bytes keys);
/// @notice Emitted when the staking limit has been changed for a withdrawal channel
/// @param withdrawalChannel The withdrawal channel that had its limit updated
/// @param limit The new staking limit of the withdrawal channel
event UpdatedLimit(bytes32 indexed withdrawalChannel, uint256 limit);
/// @notice Emitted when funds have been withdrawn from a validator withdrawal recipient
/// @param id The id of the validator
/// @param recipient The address receiving the funds
/// @param value The value that was withdrawn
event Withdraw(uint256 indexed id, address recipient, uint256 value);
/// @notice Emitted when a validator extra data is changed
/// @param id The id of the validator
/// @param extraData The new extra data value
event SetValidatorExtraData(uint256 indexed id, string extraData);
/// @notice Emitted when a validator fee recipient is changed
/// @param id The id of the validator
/// @param feeRecipient The new fee recipient address
event SetValidatorFeeRecipient(uint256 indexed id, address feeRecipient);
/// @notice Emitted when keys are requested on a withdrawal channel
/// @param withdrawalChannel The withdrawal channel where keys have been requested
/// @param total The expect total key count of the channel
event ValidatorRequest(bytes32 indexed withdrawalChannel, uint256 total);
/// @notice Emitted when a channel exit request is above the funded count
/// @param funded The count of funded validators on the channel
/// @param requestedTotal The total requested exits
event ExitRequestAboveFunded(uint32 funded, uint32 requestedTotal);
/// @notice Emitted when a validator key has been removed from a withdrawal channel
/// @param withdrawalChannel The withdrawal channel where the key has been removed
/// @param publicKey The public key that has been removed
/// @param validatorIndex The index of the removed validator key
event RemovedValidator(bytes32 indexed withdrawalChannel, bytes publicKey, uint256 validatorIndex);
/// @notice Emitted when a validator key is funded
/// @param withdrawalChannel The withdrawal channel where the validator got funded
/// @param depositor The address of the depositor bringing the funds for the validator
/// @param withdrawalAddress The address of the withdrawal recipient
/// @param publicKey The BLS Public key of the funded validator
/// @param id The unique id of the validator
/// @param validatorIndex The index of the funded validator in the withdrawal channel
event FundedValidator(
bytes32 indexed withdrawalChannel,
address indexed depositor,
address indexed withdrawalAddress,
bytes publicKey,
uint256 id,
uint256 validatorIndex
);
/// @notice Emitted when the total exit for a custom withdrawal channel is changed
/// @param withdrawalChannel The withdrawal channel where the exit count is changed
/// @param totalExited The new total exited value
event SetExitTotal(bytes32 indexed withdrawalChannel, uint32 totalExited);
/// @notice Emitted when the last edit is after the snapshot (when editing the limit). The snapshot limit is staled.
/// @param withdrawalChannel The withdrawal channel
/// @param limit The limit requested
event LastEditAfterSnapshot(bytes32 indexed withdrawalChannel, uint256 limit);
/// @notice Initializes the vFactory
/// @dev Can only be called once
/// @param depositContract Address of the deposit contract to use
/// @param admin Address of the contract admin
/// @param operator_ Address of the contract operator
/// @param treasury_ Address of the treasury
/// @param minimalRecipientImplementation Address used by the clones as implementation for the withdrawal recipients
/// @param hatcherRegistry Contract holding the hatcher registry
function initialize(
string memory name,
address depositContract,
address admin,
address operator_,
address treasury_,
address minimalRecipientImplementation,
address hatcherRegistry
) external;
/// @notice Retrieve the current operator address
/// @return The operator address
function operator() external view returns (address);
/// @notice Retrieve the current treasury address
/// @return The treasury address
function treasury() external view returns (address);
/// @notice Retrieve the depositor status
/// @param depositor Address to verify
/// @param wc Withdrawal channel to verify
/// @return Status of the depositor
function depositors(address depositor, bytes32 wc) external view returns (bool);
/// @notice Retrieve the details of a validator by its unique id
/// @param id ID of the validator
/// @return found True if the ID matches a validator
/// @return funded True if the validator is funded
/// @return wc The withdrawal channel of the validator
/// @return index The index of the validator in the withdrawal channel
/// @return publicKey The BLS public key of the validator
/// @return signature The BLS signature of the validator
/// @return owner The address owning the validator
/// @return withdrawalRecipient The address where the withdrawal rewards will go to
/// @return feeRecipient The address where the execution layer fees are expected to go to
function validator(uint256 id)
external
view
returns (
bool found,
bool funded,
bytes32 wc,
uint256 index,
bytes memory publicKey,
bytes memory signature,
address owner,
address withdrawalRecipient,
address feeRecipient
);
/// @notice Retrieve the details of a validator by its unique id
/// @param ids IDs of the validators
/// @return Public keys of the provided IDs
function publicKeys(uint256[] calldata ids) external view returns (bytes[] memory);
/// @notice Retrieve the details of a key in a withdrawalChannel
/// @param wc The withdrawal channel the key is stored in
/// @param index The index of the key in the withdrawal channel
/// @return found True if there's a key at the given index in the withdrawal channel
/// @return publicKey The BLS public key of the validator
/// @return signature The BLS signature of the validator
/// @return withdrawalRecipient The address where the withdrawal rewards will go to
function key(bytes32 wc, uint256 index)
external
view
returns (bool found, bytes memory publicKey, bytes memory signature, address withdrawalRecipient);
/// @notice Retrieve the number of validators owned by an account in a specific withdrawal channel
/// @param wc The withdrawal channel to inspect
/// @param owner The account owning the validators
/// @return The number of owned validators in the withdrawal channel
function balance(bytes32 wc, address owner) external view returns (uint256);
/// @notice Retrieve the key details of the withdrawal channel
/// @param wc The withdrawal channel to inspect
/// @return total The total count of deposited keys
/// @return limit The staking limit of the channel
/// @return funded The count of funded validators
function withdrawalChannel(bytes32 wc) external view returns (uint32 total, uint32 limit, uint32 funded);
/// @notice Retrieve the operator public metadata
/// @return name The operator name. Cannot be empty.
/// @return url The operator shared url. Can be empty.
/// @return iconUrl The operator icon url
function metadata() external view returns (string memory name, string memory url, string memory iconUrl);
/// @notice Retrieve the withdrawal address for the specified public key
/// @dev This is only useful on the null withdrawal channel where the vFactory spawns
/// minimal clones deterministically as the withdrawal recipients of each validator.
/// @param publicKey The BLS Public Key of the validator
/// @return The address where the minimal clone will be deployed to retrieve the consensus layer rewards
function withdrawalAddress(bytes calldata publicKey) external view returns (address);
/// @notice Retrieve the count of fundable validators on a withdrawal channel
/// @param wc The withdrawal channel to inspect
/// @return The count of fundable validators
function availableValidators(bytes32 wc) external view returns (uint256);
/// @notice Changes the operator address
/// @dev Only callable by the admin
/// @param newOperator New operator address
function setOperator(address newOperator) external;
/// @notice Changes the operator public metadata
/// @param name The operator name. Cannot be empty.
/// @param url The operator shared url. Can be empty.
/// @param iconUrl The operator icon url
function setMetadata(string calldata name, string calldata url, string calldata iconUrl) external;
/// @notice Add or remove depositor
/// @dev Callable by the admin of the factory or the nexus
/// @param depositor The address to add or remove
/// @param wc The withdrawal channel to add or remove the depositor from
/// @param allowed True to allow as depositor
function allowDepositor(address depositor, bytes32 wc, bool allowed) external;
/// @notice Emits an event signaling a request in keys on a specific withdrawal channel
/// @param wc The withdrawal channel to perform the request on
/// @param amount The amount of keys that should be added to the channel
function request(bytes32 wc, uint256 amount) external;
/// @notice Adds keys to several withdrawal channels
/// @dev It's expected that the provided withdrawalChannels and _keys have the same length.
/// For each withdrawalChannel, a concatenation of [S1,P1,S2,P2...,SN,PN] is expected.
/// S = BLS Signature and P = BLS Public Key. Signature should come first in each pair.
/// @param withdrawalChannels The list of withdrawal channels to add keys on
/// @param keys The list of key concatenations to add to the withdrawal channels
function addValidators(bytes32[] calldata withdrawalChannels, bytes[] calldata keys) external;
/// @notice Removes keys from several withdrawal channels
/// @dev It's expected that the provided withdrawalChannels and _indexes have the same length.
/// For each withdrawalChannel, an array of indexes is expected. These indexes should be sorted in descending order.
/// Each array should not contain any duplicate index.
/// @param withdrawalChannels The list of withdrawal channels to add keys on
/// @param indexes The list of lists of indexes to remove from the withdrawal channels
function removeValidators(bytes32[] calldata withdrawalChannels, uint256[][] calldata indexes) external;
/// @notice Modifies the staking limits of several withdrawal channels
/// @dev It's expected that the provided withdrawalChannels, _limits and _snapshots have the same length
/// For each withdrawalChannel, a new limit is provided alongside a snapshot block number.
/// If the new limit value decreases the current one, no extra check if performed and the limit is decreased.
/// If the new limit value increases the current one, we check that no key modifictions have been done after
/// the provided snapshot block. If it's the case, we don't update the limit and we don't revert, we simply
/// emit an event alerting that the last key edition happened after the snapshot. Otherwise the limit is increased.
/// @param withdrawalChannels The list of withdrawal channels to update the limits
/// @param limits The list of new staking limits values
/// @param snapshots The list of block snapshots to respect if the limit is increased
function approve(bytes32[] calldata withdrawalChannels, uint256[] calldata limits, uint256[] calldata snapshots) external;
/// @notice Deposits _count validators on the provided withdrawal channel
/// @dev This call reverts if the count of available keys is too low on the withdrawal channel
/// @param wc The withdrawal channel to fund keys on
/// @param count The amount of keys to fund
/// @param feeRecipient The fee recipient to set all the funded keys on
/// @param owner The address owning the validators
/// @param extradata The extra data to transmit to the node operator
/// @return An array of unique IDs identifying the funded validators
function deposit(bytes32 wc, uint256 count, address feeRecipient, address owner, string calldata extradata)
external
payable
returns (uint256[] memory);
/// @notice Changes the fee recipient of several validators
/// @dev Only callable by the owner of the validators
/// @param ids The list of validator IDs
/// @param newFeeRecipient The new fee recipient address
function setFeeRecipient(uint256[] calldata ids, address newFeeRecipient) external;
/// @notice Changes the owner of several validators
/// @dev Only callable by the owner of the validators
/// @param ids The list of validator IDs
/// @param newOwner The new owner address
function setOwner(uint256[] calldata ids, address newOwner) external;
/// @notice Changes the extradata of several validators
/// @dev Only callable by the owner of the validators
/// @param ids The list of validator IDs
/// @param newExtradata The new validator extra data
function setExtraData(uint256[] calldata ids, string calldata newExtradata) external;
/// @notice Emits an exit request event for several validators
/// @dev Only callable by the owner of the validators
/// @param ids The list of validator IDs
function exit(uint256[] calldata ids) external;
/// @notice Perform a consensus layer withdrawal on several validators
/// @dev Only callable by the owner of the validators and on funded validators from the null withdrawal channel
/// @param ids The list of validator IDs
/// @param recipient The address that should receive the funds, that implements the WithdrawRecipientLike interface
function withdraw(uint256[] calldata ids, address recipient) external;
/// @notice Requests a new total exited validator count for the withdrawal recipient calling the method
/// @dev This endpoint is callable by any address, it's up to the operator to properly filter the calls
/// based on existing withdrawal channels only.
/// @param totalExited The new total exited validator count for the withdrawal channel
/// @return The new total exited validator count for the withdrawal channel
function exitTotal(uint32 totalExited) external returns (uint32);
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2023 Kiln <contact@kiln.fi>
//
// ██╗ ██╗██╗██╗ ███╗ ██╗
// ██║ ██╔╝██║██║ ████╗ ██║
// █████╔╝ ██║██║ ██╔██╗ ██║
// ██╔═██╗ ██║██║ ██║╚██╗██║
// ██║ ██╗██║███████╗██║ ╚████║
// ╚═╝ ╚═╝╚═╝╚══════╝╚═╝ ╚═══╝
//
pragma solidity >=0.8.17;
/// @title Fixable Interface
/// @author mortimr @ Kiln
/// @dev Unstructured Storage Friendly
/// @notice The Fixable contract can be used on cubs to expose a safe noop to force a fix.
interface IFixable {
/// @notice Noop method to force a global fix to be applied.
function fix() external;
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.6.0) (token/ERC721/IERC721Receiver.sol)
pragma solidity ^0.8.0;
/**
* @title ERC721 token receiver interface
* @dev Interface for any contract that wants to support safeTransfers
* from ERC721 asset contracts.
*/
interface IERC721Receiver {
/**
* @dev Whenever an {IERC721} `tokenId` token is transferred to this contract via {IERC721-safeTransferFrom}
* by `operator` from `from`, this function is called.
*
* It must return its Solidity selector to confirm the token transfer.
* If any other value is returned or the interface is not implemented by the recipient, the transfer will be reverted.
*
* The selector can be obtained in Solidity with `IERC721Receiver.onERC721Received.selector`.
*/
function onERC721Received(
address operator,
address from,
uint256 tokenId,
bytes calldata data
) external returns (bytes4);
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: BUSL-1.1
// SPDX-FileCopyrightText: 2023 Kiln <contact@kiln.fi>
//
// ██╗ ██╗██╗██╗ ███╗ ██╗
// ██║ ██╔╝██║██║ ████╗ ██║
// █████╔╝ ██║██║ ██╔██╗ ██║
// ██╔═██╗ ██║██║ ██║╚██╗██║
// ██║ ██╗██║███████╗██║ ╚████║
// ╚═╝ ╚═╝╚═╝╚══════╝╚═╝ ╚═══╝
//
pragma solidity >=0.8.17;
import "./types.sol";
/// @notice Library Address - Address slot utilities.
library LAddress {
// slither-disable-next-line dead-code, assembly
function get(types.Address position) internal view returns (address data) {
// slither-disable-next-line assembly
assembly {
data := sload(position)
}
}
// slither-disable-next-line dead-code
function set(types.Address position, address data) internal {
// slither-disable-next-line assembly
assembly {
sstore(position, data)
}
}
// slither-disable-next-line dead-code
function del(types.Address position) internal {
// slither-disable-next-line assembly
assembly {
sstore(position, 0)
}
}
}
library CAddress {
// slither-disable-next-line dead-code
function toUint256(address val) internal pure returns (uint256) {
return uint256(uint160(val));
}
// slither-disable-next-line dead-code
function toBytes32(address val) internal pure returns (bytes32) {
return bytes32(uint256(uint160(val)));
}
// slither-disable-next-line dead-code
function toBool(address val) internal pure returns (bool converted) {
// slither-disable-next-line assembly
assembly {
converted := gt(val, 0)
}
}
/// @notice This method should be used to convert an address to a uint256 when used as a key in a mapping.
// slither-disable-next-line dead-code
function k(address val) internal pure returns (uint256) {
return toUint256(val);
}
/// @notice This method should be used to convert an address to a uint256 when used as a value in a mapping.
// slither-disable-next-line dead-code
function v(address val) internal pure returns (uint256) {
return toUint256(val);
}
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: BUSL-1.1
// SPDX-FileCopyrightText: 2023 Kiln <contact@kiln.fi>
//
// ██╗ ██╗██╗██╗ ███╗ ██╗
// ██║ ██╔╝██║██║ ████╗ ██║
// █████╔╝ ██║██║ ██╔██╗ ██║
// ██╔═██╗ ██║██║ ██║╚██╗██║
// ██║ ██╗██║███████╗██║ ╚████║
// ╚═╝ ╚═╝╚═╝╚══════╝╚═╝ ╚═══╝
//
pragma solidity >=0.8.17;
import "./types.sol";
library LString {
struct StringStorage {
string value;
}
// slither-disable-next-line dead-code
function get(types.String position) internal view returns (string memory) {
StringStorage storage ss;
// slither-disable-next-line assembly
assembly {
ss.slot := position
}
return ss.value;
}
// slither-disable-next-line dead-code
function set(types.String position, string memory value) internal {
StringStorage storage ss;
// slither-disable-next-line assembly
assembly {
ss.slot := position
}
ss.value = value;
}
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: BUSL-1.1
// SPDX-FileCopyrightText: 2023 Kiln <contact@kiln.fi>
//
// ██╗ ██╗██╗██╗ ███╗ ██╗
// ██║ ██╔╝██║██║ ████╗ ██║
// █████╔╝ ██║██║ ██╔██╗ ██║
// ██╔═██╗ ██║██║ ██║╚██╗██║
// ██║ ██╗██║███████╗██║ ╚████║
// ╚═╝ ╚═╝╚═╝╚══════╝╚═╝ ╚═══╝
//
pragma solidity >=0.8.17;
import "./types.sol";
library LMapping {
// slither-disable-next-line dead-code
function get(types.Mapping position) internal pure returns (mapping(uint256 => uint256) storage data) {
// slither-disable-next-line assembly
assembly {
data.slot := position
}
}
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: BUSL-1.1
// SPDX-FileCopyrightText: 2023 Kiln <contact@kiln.fi>
//
// ██╗ ██╗██╗██╗ ███╗ ██╗
// ██║ ██╔╝██║██║ ████╗ ██║
// █████╔╝ ██║██║ ██╔██╗ ██║
// ██╔═██╗ ██║██║ ██║╚██╗██║
// ██║ ██╗██║███████╗██║ ╚████║
// ╚═╝ ╚═╝╚═╝╚══════╝╚═╝ ╚═══╝
//
pragma solidity >=0.8.17;
import "./LibErrors.sol";
import "./LibConstant.sol";
/// @title Lib Sanitize
/// @dev This library helps sanitizing inputs.
library LibSanitize {
/// @dev Internal utility to sanitize an address and ensure its value is not 0.
/// @param addressValue The address to verify
// slither-disable-next-line dead-code
function notZeroAddress(address addressValue) internal pure {
if (addressValue == address(0)) {
revert LibErrors.InvalidZeroAddress();
}
}
/// @dev Internal utility to sanitize an uint256 value and ensure its value is not 0.
/// @param value The value to verify
// slither-disable-next-line dead-code
function notNullValue(uint256 value) internal pure {
if (value == 0) {
revert LibErrors.InvalidNullValue();
}
}
/// @dev Internal utility to sanitize a bps value and ensure it's <= 100%.
/// @param value The bps value to verify
// slither-disable-next-line dead-code
function notInvalidBps(uint256 value) internal pure {
if (value > LibConstant.BASIS_POINTS_MAX) {
revert LibErrors.InvalidBPSValue();
}
}
/// @dev Internal utility to sanitize a string value and ensure it's not empty.
/// @param stringValue The string value to verify
// slither-disable-next-line dead-code
function notEmptyString(string memory stringValue) internal pure {
if (bytes(stringValue).length == 0) {
revert LibErrors.InvalidEmptyString();
}
}
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: BUSL-1.1
// SPDX-FileCopyrightText: 2023 Kiln <contact@kiln.fi>
//
// ██╗ ██╗██╗██╗ ███╗ ██╗
// ██║ ██╔╝██║██║ ████╗ ██║
// █████╔╝ ██║██║ ██╔██╗ ██║
// ██╔═██╗ ██║██║ ██║╚██╗██║
// ██║ ██╗██║███████╗██║ ╚████║
// ╚═╝ ╚═╝╚═╝╚══════╝╚═╝ ╚═══╝
//
pragma solidity >=0.8.17;
import "./types/uint256.sol";
/// @title Initializable
/// @author mortimr @ Kiln
/// @dev Unstructured Storage Friendly
/// @notice This contracts helps upgradeable contracts handle an internal
/// version value to prevent initializer replays.
abstract contract Initializable {
using LUint256 for types.Uint256;
/// @notice The version has been initialized.
/// @param version The version number initialized
/// @param cdata The calldata used for the call
event Initialized(uint256 version, bytes cdata);
/// @notice The init modifier has already been called on the given version number.
/// @param version The provided version number
/// @param currentVersion The stored version number
error AlreadyInitialized(uint256 version, uint256 currentVersion);
/// @dev The version number in storage.
/// @dev Slot: keccak256(bytes("initializable.version"))) - 1
types.Uint256 internal constant $version =
types.Uint256.wrap(0xc4c7f1ccb588f39a9aa57be6cfd798d73912e27b44cfa18e1a5eba7b34e81a76);
/// @dev The modifier to use on initializers.
/// @dev Do not provide _version dynamically, make sure the value is hard-coded each
/// time the modifier is used.
/// @param _version The version to initialize
// slither-disable-next-line incorrect-modifier
modifier init(uint256 _version) {
if (_version == $version.get()) {
$version.set(_version + 1);
emit Initialized(_version, msg.data);
_;
} else {
revert AlreadyInitialized(_version, $version.get());
}
}
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.17;
import "openzeppelin-contracts/token/ERC721/extensions/IERC721Metadata.sol";
/// @title NFT
/// @author mortimr @ Kiln
/// @notice NFT contract using utils.sol storage format.
interface INFT is IERC721Metadata {
/// @notice Emitted when name is changed.
/// @param name The new ERC721 contract name
event SetName(string name);
/// @notice Emitted when symbol is changed.
/// @param symbol The new ERC721 contract symbol
event SetSymbol(string symbol);
/// @notice Thrown when the token is already minted when it shouldn't.
/// @param tokenId The id of the already existing token
error TokenAlreadyMinted(uint256 tokenId);
/// @notice Thrown when a mint operation to address zero is attempted.
error IllegalMintToZero();
/// @notice Thrown when a transfer operation to address zero is attempted.
error IllegalTransferToZero();
/// @notice Thrown when approval to self is made.
/// @param owner Address attempting approval to self
error ApprovalToOwner(address owner);
/// @notice Thrown when provided token id is invalid.
/// @param tokenId The invalid token id
error InvalidTokenId(uint256 tokenId);
/// @notice Thrown when the receiving contract is not able to receive the token.
/// @param from The address sending the token
/// @param to The address (contract) receiving the token and failing to properly receive it
/// @param tokenId The token id
/// @param data The extra data provided to the call
error NonERC721ReceiverTransfer(address from, address to, uint256 tokenId, bytes data);
/// @notice Throw when an nft transfer was attempted while the nft is frozen.
/// NFTs get frozen for ever once the exit request is made.
/// NFTs get frozen for 6 hours when a withdrawal is made.
/// @param tokenId The frozen token id
/// @param currentTimestamp The timestamp where the transfer was attempted
/// @param freezeTimestamp The timestamp until which the token is frozen
error IllegalTransferWhileFrozen(uint256 tokenId, uint256 currentTimestamp, uint256 freezeTimestamp);
/// @notice Retrieve the total count of validator created with this contract.
/// @return The total count of NFT validators of this contract
function totalSupply() external view returns (uint256);
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
//SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.17;
import "./uctypes.sol";
/// @title Operator Approvals Custom Type
library LOperatorApprovalsMapping {
function get(uctypes.OperatorApprovalsMapping position)
internal
pure
returns (mapping(address => mapping(address => bool)) storage data)
{
// slither-disable-next-line assembly
assembly {
data.slot := position
}
}
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: BUSL-1.1
// SPDX-FileCopyrightText: 2023 Kiln <contact@kiln.fi>
//
// ██╗ ██╗██╗██╗ ███╗ ██╗
// ██║ ██╔╝██║██║ ████╗ ██║
// █████╔╝ ██║██║ ██╔██╗ ██║
// ██╔═██╗ ██║██║ ██║╚██╗██║
// ██║ ██╗██║███████╗██║ ╚████║
// ╚═╝ ╚═╝╚═╝╚══════╝╚═╝ ╚═══╝
//
pragma solidity >=0.8.17;
import "./types.sol";
library LUint256 {
// slither-disable-next-line dead-code
function get(types.Uint256 position) internal view returns (uint256 data) {
// slither-disable-next-line assembly
assembly {
data := sload(position)
}
}
// slither-disable-next-line dead-code
function set(types.Uint256 position, uint256 data) internal {
// slither-disable-next-line assembly
assembly {
sstore(position, data)
}
}
// slither-disable-next-line dead-code
function del(types.Uint256 position) internal {
// slither-disable-next-line assembly
assembly {
sstore(position, 0)
}
}
}
library CUint256 {
// slither-disable-next-line dead-code
function toBytes32(uint256 val) internal pure returns (bytes32) {
return bytes32(val);
}
// slither-disable-next-line dead-code
function toAddress(uint256 val) internal pure returns (address) {
return address(uint160(val));
}
// slither-disable-next-line dead-code
function toBool(uint256 val) internal pure returns (bool) {
return (val & 1) == 1;
}
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: BUSL-1.1
// SPDX-FileCopyrightText: 2023 Kiln <contact@kiln.fi>
//
// ██╗ ██╗██╗██╗ ███╗ ██╗
// ██║ ██╔╝██║██║ ████╗ ██║
// █████╔╝ ██║██║ ██╔██╗ ██║
// ██╔═██╗ ██║██║ ██║╚██╗██║
// ██║ ██╗██║███████╗██║ ╚████║
// ╚═╝ ╚═╝╚═╝╚══════╝╚═╝ ╚═══╝
//
pragma solidity >=0.8.17;
/// @dev Library holding bytes32 custom types
// slither-disable-next-line naming-convention
library types {
type Uint256 is bytes32;
type Address is bytes32;
type Bytes32 is bytes32;
type Bool is bytes32;
type String is bytes32;
type Mapping is bytes32;
type Array is bytes32;
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: BUSL-1.1
// SPDX-FileCopyrightText: 2023 Kiln <contact@kiln.fi>
//
// ██╗ ██╗██╗██╗ ███╗ ██╗
// ██║ ██╔╝██║██║ ████╗ ██║
// █████╔╝ ██║██║ ██╔██╗ ██║
// ██╔═██╗ ██║██║ ██║╚██╗██║
// ██║ ██╗██║███████╗██║ ╚████║
// ╚═╝ ╚═╝╚═╝╚══════╝╚═╝ ╚═══╝
//
pragma solidity >=0.8.17;
import "prb-math/PRBMath.sol";
library LibUint256 {
// slither-disable-next-line dead-code
function min(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
// slither-disable-next-line assembly
assembly {
z := xor(x, mul(xor(x, y), lt(y, x)))
}
}
/// @custom:author Vectorized/solady#58681e79de23082fd3881a76022e0842f5c08db8
// slither-disable-next-line dead-code
function max(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
// slither-disable-next-line assembly
assembly {
z := xor(x, mul(xor(x, y), gt(y, x)))
}
}
// slither-disable-next-line dead-code
function mulDiv(uint256 a, uint256 b, uint256 c) internal pure returns (uint256) {
return PRBMath.mulDiv(a, b, c);
}
// slither-disable-next-line dead-code
function ceil(uint256 num, uint256 den) internal pure returns (uint256) {
return (num / den) + (num % den > 0 ? 1 : 0);
}
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: BUSL-1.1
// SPDX-FileCopyrightText: 2023 Kiln <contact@kiln.fi>
//
// ██╗ ██╗██╗██╗ ███╗ ██╗
// ██║ ██╔╝██║██║ ████╗ ██║
// █████╔╝ ██║██║ ██╔██╗ ██║
// ██╔═██╗ ██║██║ ██║╚██╗██║
// ██║ ██╗██║███████╗██║ ╚████║
// ╚═╝ ╚═╝╚═╝╚══════╝╚═╝ ╚═══╝
//
pragma solidity >=0.8.17;
/// @title Lib Bytes
/// @dev This library helps manipulating bytes.
library LibBytes {
/// @dev The length overflows an uint.
error SliceOverflow();
/// @dev The slice is outside of the initial bytes bounds.
error SliceOutOfBounds();
/// @dev Slices the provided bytes.
/// @param bytes_ Bytes to slice
/// @param start The starting index of the slice
/// @param length The length of the slice
/// @return The slice of _bytes starting at _start of length _length
// slither-disable-next-line dead-code
function slice(bytes memory bytes_, uint256 start, uint256 length) internal pure returns (bytes memory) {
unchecked {
if (length + 31 < length) {
revert SliceOverflow();
}
}
if (bytes_.length < start + length) {
revert SliceOutOfBounds();
}
bytes memory tempBytes;
// slither-disable-next-line assembly
assembly {
switch iszero(length)
case 0 {
// Get a location of some free memory and store it in tempBytes as
// Solidity does for memory variables.
tempBytes := mload(0x40)
// The first word of the slice result is potentially a partial
// word read from the original array. To read it, we calculate
// the length of that partial word and start copying that many
// bytes into the array. The first word we copy will start with
// data we don't care about, but the last `lengthmod` bytes will
// land at the beginning of the contents of the new array. When
// we're done copying, we overwrite the full first word with
// the actual length of the slice.
let lengthmod := and(length, 31)
// The multiplication in the next line is necessary
// because when slicing multiples of 32 bytes (lengthmod == 0)
// the following copy loop was copying the origin's length
// and then ending prematurely not copying everything it should.
let mc := add(add(tempBytes, lengthmod), mul(0x20, iszero(lengthmod)))
let end := add(mc, length)
for {
// The multiplication in the next line has the same exact purpose
// as the one above.
let cc := add(add(add(bytes_, lengthmod), mul(0x20, iszero(lengthmod))), start)
} lt(mc, end) {
mc := add(mc, 0x20)
cc := add(cc, 0x20)
} { mstore(mc, mload(cc)) }
mstore(tempBytes, length)
//update free-memory pointer
//allocating the array padded to 32 bytes like the compiler does now
mstore(0x40, and(add(mc, 31), not(31)))
}
//if we want a zero-length slice let's just return a zero-length array
default {
tempBytes := mload(0x40)
//zero out the 32 bytes slice we are about to return
//we need to do it because Solidity does not garbage collect
mstore(tempBytes, 0)
mstore(0x40, add(tempBytes, 0x20))
}
}
return tempBytes;
}
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.0) (utils/Strings.sol)
pragma solidity ^0.8.0;
import "./math/Math.sol";
import "./math/SignedMath.sol";
/**
* @dev String operations.
*/
library Strings {
bytes16 private constant _SYMBOLS = "0123456789abcdef";
uint8 private constant _ADDRESS_LENGTH = 20;
/**
* @dev Converts a `uint256` to its ASCII `string` decimal representation.
*/
function toString(uint256 value) internal pure returns (string memory) {
unchecked {
uint256 length = Math.log10(value) + 1;
string memory buffer = new string(length);
uint256 ptr;
/// @solidity memory-safe-assembly
assembly {
ptr := add(buffer, add(32, length))
}
while (true) {
ptr--;
/// @solidity memory-safe-assembly
assembly {
mstore8(ptr, byte(mod(value, 10), _SYMBOLS))
}
value /= 10;
if (value == 0) break;
}
return buffer;
}
}
/**
* @dev Converts a `int256` to its ASCII `string` decimal representation.
*/
function toString(int256 value) internal pure returns (string memory) {
return string(abi.encodePacked(value < 0 ? "-" : "", toString(SignedMath.abs(value))));
}
/**
* @dev Converts a `uint256` to its ASCII `string` hexadecimal representation.
*/
function toHexString(uint256 value) internal pure returns (string memory) {
unchecked {
return toHexString(value, Math.log256(value) + 1);
}
}
/**
* @dev Converts a `uint256` to its ASCII `string` hexadecimal representation with fixed length.
*/
function toHexString(uint256 value, uint256 length) internal pure returns (string memory) {
bytes memory buffer = new bytes(2 * length + 2);
buffer[0] = "0";
buffer[1] = "x";
for (uint256 i = 2 * length + 1; i > 1; --i) {
buffer[i] = _SYMBOLS[value & 0xf];
value >>= 4;
}
require(value == 0, "Strings: hex length insufficient");
return string(buffer);
}
/**
* @dev Converts an `address` with fixed length of 20 bytes to its not checksummed ASCII `string` hexadecimal representation.
*/
function toHexString(address addr) internal pure returns (string memory) {
return toHexString(uint256(uint160(addr)), _ADDRESS_LENGTH);
}
/**
* @dev Returns true if the two strings are equal.
*/
function equal(string memory a, string memory b) internal pure returns (bool) {
return keccak256(bytes(a)) == keccak256(bytes(b));
}
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: BUSL-1.1
// SPDX-FileCopyrightText: 2023 Kiln <contact@kiln.fi>
//
// ██╗ ██╗██╗██╗ ███╗ ██╗
// ██║ ██╔╝██║██║ ████╗ ██║
// █████╔╝ ██║██║ ██╔██╗ ██║
// ██╔═██╗ ██║██║ ██║╚██╗██║
// ██║ ██╗██║███████╗██║ ╚████║
// ╚═╝ ╚═╝╚═╝╚══════╝╚═╝ ╚═══╝
//
pragma solidity 0.8.17;
import "utils.sol/libs/LibPublicKey.sol";
import "utils.sol/libs/LibSignature.sol";
/// @title Custom Types
// slither-disable-next-line naming-convention
library ctypes {
/// @notice Structure representing a validator in the factory
/// @param publicKey The public key of the validator
/// @param signature The signature used for the deposit
/// @param feeRecipient The address receiving the exec layer fees
struct Validator {
LibPublicKey.PublicKey publicKey;
LibSignature.Signature signature;
address feeRecipient;
}
/// @notice Structure representing a withdrawal channel in the factory
/// @param validators The validators in the channel
/// @param lastEdit The last time the channel was edited (in blocks)
/// @param limit The staking limit of the channel. Always <= validators.length
/// @param funded The amount of funded validators in the channel
struct WithdrawalChannel {
Validator[] validators;
uint256 lastEdit;
uint32 limit;
uint32 funded;
}
/// @notice Structure representing a deposit in the factory
/// @param index The index of the deposit in the withdrawal channel
/// @param withdrawalChannel The withdrawal channel of the validator
/// @param owner The owner of the deposited validator
struct Deposit {
uint256 index;
bytes32 withdrawalChannel;
address owner;
}
/// @notice Structure representing the operator metadata in the factory
/// @param name The name of the operator
/// @param url The url of the operator
/// @param iconUrl The icon url of the operator
struct Metadata {
string name;
string url;
string iconUrl;
}
/// @notice Structure representing the global consensus layer spec held in the global consensus layer spec holder
/// @param genesisTimestamp The timestamp of the genesis of the consensus layer (slot 0 timestamp)
/// @param epochsUntilFinal The number of epochs until a block is considered final by the vsuite
/// @param slotsPerEpoch The number of slots per epoch (32 on mainnet)
/// @param secondsPerSlot The number of seconds per slot (12 on mainnet)
struct ConsensusLayerSpec {
uint64 genesisTimestamp;
uint64 epochsUntilFinal;
uint64 slotsPerEpoch;
uint64 secondsPerSlot;
}
/// @notice Structure representing the report bounds held in the pools
/// @param maxAPRUpperBound The maximum APR upper bound, representing the maximum increase in underlying balance checked at each oracle report
/// @param maxAPRUpperCoverageBoost The maximum APR upper coverage boost, representing the additional increase allowed when pulling coverage funds
/// @param maxRelativeLowerBound The maximum relative lower bound, representing the maximum decrease in underlying balance checked at each oracle report
struct ReportBounds {
uint64 maxAPRUpperBound;
uint64 maxAPRUpperCoverageBoost;
uint64 maxRelativeLowerBound;
}
/// @notice Structure representing the consensus layer report submitted by oracle members
/// @param balanceSum sum of all the balances of all validators that have been activated by the vPool
/// this means that as long as the validator was activated, no matter its current status, its balance is taken
/// into account
/// @param exitedSum sum of all the ether that has been exited by the validators that have been activated by the vPool
/// to compute this value, we look for withdrawal events inside the block bodies that have happened at an epoch
/// that is greater or equal to the withdrawable epoch of a validator purchased by the pool
/// when we detect any, we take min(amount,32 eth) into account as exited balance
/// @param skimmedSum sum of all the ether that has been skimmed by the validators that have been activated by the vPool
/// similar to the exitedSum, we look for withdrawal events. If the epochs is lower than the withdrawable epoch
/// we take into account the full withdrawal amount, otherwise we take amount - min(amount, 32 eth) into account
/// @param slashedSum sum of all the ether that has been slashed by the validators that have been activated by the vPool
/// to compute this value, we look for validators that are of have been in the slashed state
/// then we take the balance of the validator at the epoch prior to its slashing event
/// we then add the delta between this old balance and the current balance (or balance just before withdrawal)
/// @param exiting amount of currently exiting eth, that will soon hit the withdrawal recipient
/// this value is computed by taking the balance of any validator in the exit or slashed state or after
/// @param maxExitable maximum amount that can get requested for exits during report processing
/// this value is determined by the oracle. its calculation logic can be updated but all members need to agree and reach
/// consensus on the new calculation logic. Its role is to control the rate at which exit requests are performed
/// @param maxCommittable maximum amount that can get committed for deposits during report processing
/// positive value means commit happens before possible exit boosts, negative after
/// similar to the mexExitable, this value is determined by the oracle. its calculation logic can be updated but all
/// members need to agree and reach consensus on the new calculation logic. Its role is to control the rate at which
/// deposit are made. Committed funds are funds that are always a multiple of 32 eth and that cannot be used for
/// anything else than purchasing validator, as opposed to the deposited funds that can still be used to fuel the
/// exit queue in some cases.
/// @param epoch epoch at which the report was crafter
/// @param activatedCount current count of validators that have been activated by the vPool
/// no matter the current state of the validator, if it has been activated, it has to be accounted inside this value
/// @param stoppedCount current count of validators that have been stopped (being in the exit queue, exited or slashed)
struct ValidatorsReport {
uint128 balanceSum;
uint128 exitedSum;
uint128 skimmedSum;
uint128 slashedSum;
uint128 exiting;
uint128 maxExitable;
int256 maxCommittable;
uint64 epoch;
uint32 activatedCount;
uint32 stoppedCount;
}
/// @notice Structure representing the ethers held in the pools
/// @param deposited The amount of deposited ethers, that can either be used to boost exits or get committed
/// @param committed The amount of committed ethers, that can only be used to purchase validators
struct Ethers {
uint128 deposited;
uint128 committed;
}
/// @notice Structure representing a ticket in the exit queue
/// @param position The position of the ticket in the exit queue (equal to the position + size of the previous ticket)
/// @param size The size of the ticket in the exit queue (in pool shares)
/// @param maxExitable The maximum amount of ethers that can be exited by the ticket owner (no more rewards in the exit queue, losses are still mutualized)
struct Ticket {
uint128 position;
uint128 size;
uint128 maxExitable;
}
/// @notice Structure representing a cask in the exit queue. This entity is created by the pool upon oracle reports, when exit liquidity is available to feed the exit queue
/// @param position The position of the cask in the exit queue (equal to the position + size of the previous cask)
/// @param size The size of the cask in the exit queue (in pool shares)
/// @param value The value of the cask in the exit queue (in ethers)
struct Cask {
uint128 position;
uint128 size;
uint128 value;
}
type DepositMapping is bytes32;
type WithdrawalChannelMapping is bytes32;
type BalanceMapping is bytes32;
type MetadataStruct is bytes32;
type ConsensusLayerSpecStruct is bytes32;
type ReportBoundsStruct is bytes32;
type ApprovalsMapping is bytes32;
type ValidatorsReportStruct is bytes32;
type EthersStruct is bytes32;
type TicketArray is bytes32;
type CaskArray is bytes32;
type FactoryDepositorMapping is bytes32;
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2023 Kiln <contact@kiln.fi>
//
// ██╗ ██╗██╗██╗ ███╗ ██╗
// ██║ ██╔╝██║██║ ████╗ ██║
// █████╔╝ ██║██║ ██╔██╗ ██║
// ██╔═██╗ ██║██║ ██║╚██╗██║
// ██║ ██╗██║███████╗██║ ╚████║
// ╚═╝ ╚═╝╚═╝╚══════╝╚═╝ ╚═══╝
//
pragma solidity 0.8.17;
/// @title Pool Shares Receiver Interface
/// @author mortimr @ Kiln
/// @notice Interface that needs to be implemented for a contract to be able to receive shares
interface IvPoolSharesReceiver {
/// @notice Callback used by the vPool to notify contracts of shares being transfered
/// @param operator The address of the operator of the transfer
/// @param from The address sending the funds
/// @param amount The amount of shares received
/// @param data The attached data
/// @return selector Should return its own selector if everything went well
function onvPoolSharesReceived(address operator, address from, uint256 amount, bytes memory data) external returns (bytes4 selector);
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2023 Kiln <contact@kiln.fi>
//
// ██╗ ██╗██╗██╗ ███╗ ██╗
// ██║ ██╔╝██║██║ ████╗ ██║
// █████╔╝ ██║██║ ██╔██╗ ██║
// ██╔═██╗ ██║██║ ██║╚██╗██║
// ██║ ██╗██║███████╗██║ ╚████║
// ╚═╝ ╚═╝╚═╝╚══════╝╚═╝ ╚═══╝
//
pragma solidity >=0.8.17;
/// @title Administrable Interface
/// @author mortimr @ Kiln
/// @dev Unstructured Storage Friendly
/// @notice This contract provides all the utilities to handle the administration and its transfer.
interface IAdministrable {
/// @notice The admin address has been changed.
/// @param admin The new admin address
event SetAdmin(address admin);
/// @notice The pending admin address has been changed.
/// @param pendingAdmin The pending admin has been changed
event SetPendingAdmin(address pendingAdmin);
/// @notice Retrieve the admin address.
/// @return adminAddress The admin address
function admin() external view returns (address adminAddress);
/// @notice Retrieve the pending admin address.
/// @return pendingAdminAddress The pending admin address
function pendingAdmin() external view returns (address pendingAdminAddress);
/// @notice Propose a new admin.
/// @dev Only callable by the admin
/// @param _newAdmin The new admin to propose
function transferAdmin(address _newAdmin) external;
/// @notice Accept an admin transfer.
/// @dev Only callable by the pending admin
function acceptAdmin() external;
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2023 Kiln <contact@kiln.fi>
//
// ██╗ ██╗██╗██╗ ███╗ ██╗
// ██║ ██╔╝██║██║ ████╗ ██║
// █████╔╝ ██║██║ ██╔██╗ ██║
// ██╔═██╗ ██║██║ ██║╚██╗██║
// ██║ ██╗██║███████╗██║ ╚████║
// ╚═╝ ╚═╝╚═╝╚══════╝╚═╝ ╚═══╝
//
pragma solidity >=0.8.17;
/// @title Depositor Interface
/// @author mortimr @ Kiln
/// @dev Unstructured Storage Friendly
/// @notice The Depositor contract adds deposit capabilities to easily fund
/// validators and activate them on the Consensus Layer.
interface IDepositor {
/// @notice The provided public key is not 48 bytes long.
error InvalidPublicKeyLength();
/// @notice The provided signature is not 96 bytes long.
error InvalidSignatureLength();
/// @notice The balance is too low for the deposit.
error InvalidDepositSize();
/// @notice An error occured during the deposit.
error DepositError();
/// @notice The deposit contract address has been updated.
/// @param depositContract The new deposit contract address
event SetDepositContract(address depositContract);
/// @notice Retrieve the deposit contract address.
/// @return depositContractAddress The deposit contract address
function depositContract() external view returns (address depositContractAddress);
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: BUSL-1.1
// SPDX-FileCopyrightText: 2023 Kiln <contact@kiln.fi>
//
// ██╗ ██╗██╗██╗ ███╗ ██╗
// ██║ ██╔╝██║██║ ████╗ ██║
// █████╔╝ ██║██║ ██╔██╗ ██║
// ██╔═██╗ ██║██║ ██║╚██╗██║
// ██║ ██╗██║███████╗██║ ╚████║
// ╚═╝ ╚═╝╚═╝╚══════╝╚═╝ ╚═══╝
//
pragma solidity >=0.8.17;
library LibErrors {
error Unauthorized(address account, address expected);
error InvalidZeroAddress();
error InvalidNullValue();
error InvalidBPSValue();
error InvalidEmptyString();
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: BUSL-1.1
// SPDX-FileCopyrightText: 2023 Kiln <contact@kiln.fi>
//
// ██╗ ██╗██╗██╗ ███╗ ██╗
// ██║ ██╔╝██║██║ ████╗ ██║
// █████╔╝ ██║██║ ██╔██╗ ██║
// ██╔═██╗ ██║██║ ██║╚██╗██║
// ██║ ██╗██║███████╗██║ ╚████║
// ╚═╝ ╚═╝╚═╝╚══════╝╚═╝ ╚═══╝
//
pragma solidity >=0.8.17;
library LibConstant {
/// @dev The basis points value representing 100%.
uint256 internal constant BASIS_POINTS_MAX = 10_000;
/// @dev The size of a deposit to activate a validator.
uint256 internal constant DEPOSIT_SIZE = 32 ether;
/// @dev The minimum freeze timeout before freeze is active.
uint256 internal constant MINIMUM_FREEZE_TIMEOUT = 100 days;
/// @dev Address used to represent ETH when an address is required to identify an asset.
address internal constant ETHER = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (token/ERC721/extensions/IERC721Metadata.sol)
pragma solidity ^0.8.0;
import "../IERC721.sol";
/**
* @title ERC-721 Non-Fungible Token Standard, optional metadata extension
* @dev See https://eips.ethereum.org/EIPS/eip-721
*/
interface IERC721Metadata is IERC721 {
/**
* @dev Returns the token collection name.
*/
function name() external view returns (string memory);
/**
* @dev Returns the token collection symbol.
*/
function symbol() external view returns (string memory);
/**
* @dev Returns the Uniform Resource Identifier (URI) for `tokenId` token.
*/
function tokenURI(uint256 tokenId) external view returns (string memory);
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
//SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.17;
/// @title Uitls Custom Types
// slither-disable-next-line naming-convention
library uctypes {
type OperatorApprovalsMapping is bytes32;
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: Unlicense
pragma solidity >=0.8.4;
/// @notice Emitted when the result overflows uint256.
error PRBMath__MulDivFixedPointOverflow(uint256 prod1);
/// @notice Emitted when the result overflows uint256.
error PRBMath__MulDivOverflow(uint256 prod1, uint256 denominator);
/// @notice Emitted when one of the inputs is type(int256).min.
error PRBMath__MulDivSignedInputTooSmall();
/// @notice Emitted when the intermediary absolute result overflows int256.
error PRBMath__MulDivSignedOverflow(uint256 rAbs);
/// @notice Emitted when the input is MIN_SD59x18.
error PRBMathSD59x18__AbsInputTooSmall();
/// @notice Emitted when ceiling a number overflows SD59x18.
error PRBMathSD59x18__CeilOverflow(int256 x);
/// @notice Emitted when one of the inputs is MIN_SD59x18.
error PRBMathSD59x18__DivInputTooSmall();
/// @notice Emitted when one of the intermediary unsigned results overflows SD59x18.
error PRBMathSD59x18__DivOverflow(uint256 rAbs);
/// @notice Emitted when the input is greater than 133.084258667509499441.
error PRBMathSD59x18__ExpInputTooBig(int256 x);
/// @notice Emitted when the input is greater than 192.
error PRBMathSD59x18__Exp2InputTooBig(int256 x);
/// @notice Emitted when flooring a number underflows SD59x18.
error PRBMathSD59x18__FloorUnderflow(int256 x);
/// @notice Emitted when converting a basic integer to the fixed-point format overflows SD59x18.
error PRBMathSD59x18__FromIntOverflow(int256 x);
/// @notice Emitted when converting a basic integer to the fixed-point format underflows SD59x18.
error PRBMathSD59x18__FromIntUnderflow(int256 x);
/// @notice Emitted when the product of the inputs is negative.
error PRBMathSD59x18__GmNegativeProduct(int256 x, int256 y);
/// @notice Emitted when multiplying the inputs overflows SD59x18.
error PRBMathSD59x18__GmOverflow(int256 x, int256 y);
/// @notice Emitted when the input is less than or equal to zero.
error PRBMathSD59x18__LogInputTooSmall(int256 x);
/// @notice Emitted when one of the inputs is MIN_SD59x18.
error PRBMathSD59x18__MulInputTooSmall();
/// @notice Emitted when the intermediary absolute result overflows SD59x18.
error PRBMathSD59x18__MulOverflow(uint256 rAbs);
/// @notice Emitted when the intermediary absolute result overflows SD59x18.
error PRBMathSD59x18__PowuOverflow(uint256 rAbs);
/// @notice Emitted when the input is negative.
error PRBMathSD59x18__SqrtNegativeInput(int256 x);
/// @notice Emitted when the calculating the square root overflows SD59x18.
error PRBMathSD59x18__SqrtOverflow(int256 x);
/// @notice Emitted when addition overflows UD60x18.
error PRBMathUD60x18__AddOverflow(uint256 x, uint256 y);
/// @notice Emitted when ceiling a number overflows UD60x18.
error PRBMathUD60x18__CeilOverflow(uint256 x);
/// @notice Emitted when the input is greater than 133.084258667509499441.
error PRBMathUD60x18__ExpInputTooBig(uint256 x);
/// @notice Emitted when the input is greater than 192.
error PRBMathUD60x18__Exp2InputTooBig(uint256 x);
/// @notice Emitted when converting a basic integer to the fixed-point format format overflows UD60x18.
error PRBMathUD60x18__FromUintOverflow(uint256 x);
/// @notice Emitted when multiplying the inputs overflows UD60x18.
error PRBMathUD60x18__GmOverflow(uint256 x, uint256 y);
/// @notice Emitted when the input is less than 1.
error PRBMathUD60x18__LogInputTooSmall(uint256 x);
/// @notice Emitted when the calculating the square root overflows UD60x18.
error PRBMathUD60x18__SqrtOverflow(uint256 x);
/// @notice Emitted when subtraction underflows UD60x18.
error PRBMathUD60x18__SubUnderflow(uint256 x, uint256 y);
/// @dev Common mathematical functions used in both PRBMathSD59x18 and PRBMathUD60x18. Note that this shared library
/// does not always assume the signed 59.18-decimal fixed-point or the unsigned 60.18-decimal fixed-point
/// representation. When it does not, it is explicitly mentioned in the NatSpec documentation.
library PRBMath {
/// STRUCTS ///
struct SD59x18 {
int256 value;
}
struct UD60x18 {
uint256 value;
}
/// STORAGE ///
/// @dev How many trailing decimals can be represented.
uint256 internal constant SCALE = 1e18;
/// @dev Largest power of two divisor of SCALE.
uint256 internal constant SCALE_LPOTD = 262144;
/// @dev SCALE inverted mod 2^256.
uint256 internal constant SCALE_INVERSE =
78156646155174841979727994598816262306175212592076161876661_508869554232690281;
/// FUNCTIONS ///
/// @notice Calculates the binary exponent of x using the binary fraction method.
/// @dev Has to use 192.64-bit fixed-point numbers.
/// See https://ethereum.stackexchange.com/a/96594/24693.
/// @param x The exponent as an unsigned 192.64-bit fixed-point number.
/// @return result The result as an unsigned 60.18-decimal fixed-point number.
function exp2(uint256 x) internal pure returns (uint256 result) {
unchecked {
// Start from 0.5 in the 192.64-bit fixed-point format.
result = 0x800000000000000000000000000000000000000000000000;
// Multiply the result by root(2, 2^-i) when the bit at position i is 1. None of the intermediary results overflows
// because the initial result is 2^191 and all magic factors are less than 2^65.
if (x & 0x8000000000000000 > 0) {
result = (result * 0x16A09E667F3BCC909) >> 64;
}
if (x & 0x4000000000000000 > 0) {
result = (result * 0x1306FE0A31B7152DF) >> 64;
}
if (x & 0x2000000000000000 > 0) {
result = (result * 0x1172B83C7D517ADCE) >> 64;
}
if (x & 0x1000000000000000 > 0) {
result = (result * 0x10B5586CF9890F62A) >> 64;
}
if (x & 0x800000000000000 > 0) {
result = (result * 0x1059B0D31585743AE) >> 64;
}
if (x & 0x400000000000000 > 0) {
result = (result * 0x102C9A3E778060EE7) >> 64;
}
if (x & 0x200000000000000 > 0) {
result = (result * 0x10163DA9FB33356D8) >> 64;
}
if (x & 0x100000000000000 > 0) {
result = (result * 0x100B1AFA5ABCBED61) >> 64;
}
if (x & 0x80000000000000 > 0) {
result = (result * 0x10058C86DA1C09EA2) >> 64;
}
if (x & 0x40000000000000 > 0) {
result = (result * 0x1002C605E2E8CEC50) >> 64;
}
if (x & 0x20000000000000 > 0) {
result = (result * 0x100162F3904051FA1) >> 64;
}
if (x & 0x10000000000000 > 0) {
result = (result * 0x1000B175EFFDC76BA) >> 64;
}
if (x & 0x8000000000000 > 0) {
result = (result * 0x100058BA01FB9F96D) >> 64;
}
if (x & 0x4000000000000 > 0) {
result = (result * 0x10002C5CC37DA9492) >> 64;
}
if (x & 0x2000000000000 > 0) {
result = (result * 0x1000162E525EE0547) >> 64;
}
if (x & 0x1000000000000 > 0) {
result = (result * 0x10000B17255775C04) >> 64;
}
if (x & 0x800000000000 > 0) {
result = (result * 0x1000058B91B5BC9AE) >> 64;
}
if (x & 0x400000000000 > 0) {
result = (result * 0x100002C5C89D5EC6D) >> 64;
}
if (x & 0x200000000000 > 0) {
result = (result * 0x10000162E43F4F831) >> 64;
}
if (x & 0x100000000000 > 0) {
result = (result * 0x100000B1721BCFC9A) >> 64;
}
if (x & 0x80000000000 > 0) {
result = (result * 0x10000058B90CF1E6E) >> 64;
}
if (x & 0x40000000000 > 0) {
result = (result * 0x1000002C5C863B73F) >> 64;
}
if (x & 0x20000000000 > 0) {
result = (result * 0x100000162E430E5A2) >> 64;
}
if (x & 0x10000000000 > 0) {
result = (result * 0x1000000B172183551) >> 64;
}
if (x & 0x8000000000 > 0) {
result = (result * 0x100000058B90C0B49) >> 64;
}
if (x & 0x4000000000 > 0) {
result = (result * 0x10000002C5C8601CC) >> 64;
}
if (x & 0x2000000000 > 0) {
result = (result * 0x1000000162E42FFF0) >> 64;
}
if (x & 0x1000000000 > 0) {
result = (result * 0x10000000B17217FBB) >> 64;
}
if (x & 0x800000000 > 0) {
result = (result * 0x1000000058B90BFCE) >> 64;
}
if (x & 0x400000000 > 0) {
result = (result * 0x100000002C5C85FE3) >> 64;
}
if (x & 0x200000000 > 0) {
result = (result * 0x10000000162E42FF1) >> 64;
}
if (x & 0x100000000 > 0) {
result = (result * 0x100000000B17217F8) >> 64;
}
if (x & 0x80000000 > 0) {
result = (result * 0x10000000058B90BFC) >> 64;
}
if (x & 0x40000000 > 0) {
result = (result * 0x1000000002C5C85FE) >> 64;
}
if (x & 0x20000000 > 0) {
result = (result * 0x100000000162E42FF) >> 64;
}
if (x & 0x10000000 > 0) {
result = (result * 0x1000000000B17217F) >> 64;
}
if (x & 0x8000000 > 0) {
result = (result * 0x100000000058B90C0) >> 64;
}
if (x & 0x4000000 > 0) {
result = (result * 0x10000000002C5C860) >> 64;
}
if (x & 0x2000000 > 0) {
result = (result * 0x1000000000162E430) >> 64;
}
if (x & 0x1000000 > 0) {
result = (result * 0x10000000000B17218) >> 64;
}
if (x & 0x800000 > 0) {
result = (result * 0x1000000000058B90C) >> 64;
}
if (x & 0x400000 > 0) {
result = (result * 0x100000000002C5C86) >> 64;
}
if (x & 0x200000 > 0) {
result = (result * 0x10000000000162E43) >> 64;
}
if (x & 0x100000 > 0) {
result = (result * 0x100000000000B1721) >> 64;
}
if (x & 0x80000 > 0) {
result = (result * 0x10000000000058B91) >> 64;
}
if (x & 0x40000 > 0) {
result = (result * 0x1000000000002C5C8) >> 64;
}
if (x & 0x20000 > 0) {
result = (result * 0x100000000000162E4) >> 64;
}
if (x & 0x10000 > 0) {
result = (result * 0x1000000000000B172) >> 64;
}
if (x & 0x8000 > 0) {
result = (result * 0x100000000000058B9) >> 64;
}
if (x & 0x4000 > 0) {
result = (result * 0x10000000000002C5D) >> 64;
}
if (x & 0x2000 > 0) {
result = (result * 0x1000000000000162E) >> 64;
}
if (x & 0x1000 > 0) {
result = (result * 0x10000000000000B17) >> 64;
}
if (x & 0x800 > 0) {
result = (result * 0x1000000000000058C) >> 64;
}
if (x & 0x400 > 0) {
result = (result * 0x100000000000002C6) >> 64;
}
if (x & 0x200 > 0) {
result = (result * 0x10000000000000163) >> 64;
}
if (x & 0x100 > 0) {
result = (result * 0x100000000000000B1) >> 64;
}
if (x & 0x80 > 0) {
result = (result * 0x10000000000000059) >> 64;
}
if (x & 0x40 > 0) {
result = (result * 0x1000000000000002C) >> 64;
}
if (x & 0x20 > 0) {
result = (result * 0x10000000000000016) >> 64;
}
if (x & 0x10 > 0) {
result = (result * 0x1000000000000000B) >> 64;
}
if (x & 0x8 > 0) {
result = (result * 0x10000000000000006) >> 64;
}
if (x & 0x4 > 0) {
result = (result * 0x10000000000000003) >> 64;
}
if (x & 0x2 > 0) {
result = (result * 0x10000000000000001) >> 64;
}
if (x & 0x1 > 0) {
result = (result * 0x10000000000000001) >> 64;
}
// We're doing two things at the same time:
//
// 1. Multiply the result by 2^n + 1, where "2^n" is the integer part and the one is added to account for
// the fact that we initially set the result to 0.5. This is accomplished by subtracting from 191
// rather than 192.
// 2. Convert the result to the unsigned 60.18-decimal fixed-point format.
//
// This works because 2^(191-ip) = 2^ip / 2^191, where "ip" is the integer part "2^n".
result *= SCALE;
result >>= (191 - (x >> 64));
}
}
/// @notice Finds the zero-based index of the first one in the binary representation of x.
/// @dev See the note on msb in the "Find First Set" Wikipedia article https://en.wikipedia.org/wiki/Find_first_set
/// @param x The uint256 number for which to find the index of the most significant bit.
/// @return msb The index of the most significant bit as an uint256.
function mostSignificantBit(uint256 x) internal pure returns (uint256 msb) {
if (x >= 2**128) {
x >>= 128;
msb += 128;
}
if (x >= 2**64) {
x >>= 64;
msb += 64;
}
if (x >= 2**32) {
x >>= 32;
msb += 32;
}
if (x >= 2**16) {
x >>= 16;
msb += 16;
}
if (x >= 2**8) {
x >>= 8;
msb += 8;
}
if (x >= 2**4) {
x >>= 4;
msb += 4;
}
if (x >= 2**2) {
x >>= 2;
msb += 2;
}
if (x >= 2**1) {
// No need to shift x any more.
msb += 1;
}
}
/// @notice Calculates floor(x*y÷denominator) with full precision.
///
/// @dev Credit to Remco Bloemen under MIT license https://xn--2-umb.com/21/muldiv.
///
/// Requirements:
/// - The denominator cannot be zero.
/// - The result must fit within uint256.
///
/// Caveats:
/// - This function does not work with fixed-point numbers.
///
/// @param x The multiplicand as an uint256.
/// @param y The multiplier as an uint256.
/// @param denominator The divisor as an uint256.
/// @return result The result as an uint256.
function mulDiv(
uint256 x,
uint256 y,
uint256 denominator
) internal pure returns (uint256 result) {
// 512-bit multiply [prod1 prod0] = x * y. Compute the product mod 2^256 and mod 2^256 - 1, then use
// use the Chinese Remainder Theorem to reconstruct the 512 bit result. The result is stored in two 256
// variables such that product = prod1 * 2^256 + prod0.
uint256 prod0; // Least significant 256 bits of the product
uint256 prod1; // Most significant 256 bits of the product
assembly {
let mm := mulmod(x, y, not(0))
prod0 := mul(x, y)
prod1 := sub(sub(mm, prod0), lt(mm, prod0))
}
// Handle non-overflow cases, 256 by 256 division.
if (prod1 == 0) {
unchecked {
result = prod0 / denominator;
}
return result;
}
// Make sure the result is less than 2^256. Also prevents denominator == 0.
if (prod1 >= denominator) {
revert PRBMath__MulDivOverflow(prod1, denominator);
}
///////////////////////////////////////////////
// 512 by 256 division.
///////////////////////////////////////////////
// Make division exact by subtracting the remainder from [prod1 prod0].
uint256 remainder;
assembly {
// Compute remainder using mulmod.
remainder := mulmod(x, y, denominator)
// Subtract 256 bit number from 512 bit number.
prod1 := sub(prod1, gt(remainder, prod0))
prod0 := sub(prod0, remainder)
}
// Factor powers of two out of denominator and compute largest power of two divisor of denominator. Always >= 1.
// See https://cs.stackexchange.com/q/138556/92363.
unchecked {
// Does not overflow because the denominator cannot be zero at this stage in the function.
uint256 lpotdod = denominator & (~denominator + 1);
assembly {
// Divide denominator by lpotdod.
denominator := div(denominator, lpotdod)
// Divide [prod1 prod0] by lpotdod.
prod0 := div(prod0, lpotdod)
// Flip lpotdod such that it is 2^256 / lpotdod. If lpotdod is zero, then it becomes one.
lpotdod := add(div(sub(0, lpotdod), lpotdod), 1)
}
// Shift in bits from prod1 into prod0.
prod0 |= prod1 * lpotdod;
// Invert denominator mod 2^256. Now that denominator is an odd number, it has an inverse modulo 2^256 such
// that denominator * inv = 1 mod 2^256. Compute the inverse by starting with a seed that is correct for
// four bits. That is, denominator * inv = 1 mod 2^4.
uint256 inverse = (3 * denominator) ^ 2;
// Use the 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.
inverse *= 2 - denominator * inverse; // inverse mod 2^8
inverse *= 2 - denominator * inverse; // inverse mod 2^16
inverse *= 2 - denominator * inverse; // inverse mod 2^32
inverse *= 2 - denominator * inverse; // inverse mod 2^64
inverse *= 2 - denominator * inverse; // inverse mod 2^128
inverse *= 2 - denominator * inverse; // inverse mod 2^256
// Because the division is now exact we can divide by multiplying with the modular inverse of denominator.
// This will give us the correct result modulo 2^256. Since the preconditions guarantee that the outcome is
// less than 2^256, this is the final result. We don't need to compute the high bits of the result and prod1
// is no longer required.
result = prod0 * inverse;
return result;
}
}
/// @notice Calculates floor(x*y÷1e18) with full precision.
///
/// @dev Variant of "mulDiv" with constant folding, i.e. in which the denominator is always 1e18. Before returning the
/// final result, we add 1 if (x * y) % SCALE >= HALF_SCALE. Without this, 6.6e-19 would be truncated to 0 instead of
/// being rounded to 1e-18. See "Listing 6" and text above it at https://accu.org/index.php/journals/1717.
///
/// Requirements:
/// - The result must fit within uint256.
///
/// Caveats:
/// - The body is purposely left uncommented; see the NatSpec comments in "PRBMath.mulDiv" to understand how this works.
/// - It is assumed that the result can never be type(uint256).max when x and y solve the following two equations:
/// 1. x * y = type(uint256).max * SCALE
/// 2. (x * y) % SCALE >= SCALE / 2
///
/// @param x The multiplicand as an unsigned 60.18-decimal fixed-point number.
/// @param y The multiplier as an unsigned 60.18-decimal fixed-point number.
/// @return result The result as an unsigned 60.18-decimal fixed-point number.
function mulDivFixedPoint(uint256 x, uint256 y) internal pure returns (uint256 result) {
uint256 prod0;
uint256 prod1;
assembly {
let mm := mulmod(x, y, not(0))
prod0 := mul(x, y)
prod1 := sub(sub(mm, prod0), lt(mm, prod0))
}
if (prod1 >= SCALE) {
revert PRBMath__MulDivFixedPointOverflow(prod1);
}
uint256 remainder;
uint256 roundUpUnit;
assembly {
remainder := mulmod(x, y, SCALE)
roundUpUnit := gt(remainder, 499999999999999999)
}
if (prod1 == 0) {
unchecked {
result = (prod0 / SCALE) + roundUpUnit;
return result;
}
}
assembly {
result := add(
mul(
or(
div(sub(prod0, remainder), SCALE_LPOTD),
mul(sub(prod1, gt(remainder, prod0)), add(div(sub(0, SCALE_LPOTD), SCALE_LPOTD), 1))
),
SCALE_INVERSE
),
roundUpUnit
)
}
}
/// @notice Calculates floor(x*y÷denominator) with full precision.
///
/// @dev An extension of "mulDiv" for signed numbers. Works by computing the signs and the absolute values separately.
///
/// Requirements:
/// - None of the inputs can be type(int256).min.
/// - The result must fit within int256.
///
/// @param x The multiplicand as an int256.
/// @param y The multiplier as an int256.
/// @param denominator The divisor as an int256.
/// @return result The result as an int256.
function mulDivSigned(
int256 x,
int256 y,
int256 denominator
) internal pure returns (int256 result) {
if (x == type(int256).min || y == type(int256).min || denominator == type(int256).min) {
revert PRBMath__MulDivSignedInputTooSmall();
}
// Get hold of the absolute values of x, y and the denominator.
uint256 ax;
uint256 ay;
uint256 ad;
unchecked {
ax = x < 0 ? uint256(-x) : uint256(x);
ay = y < 0 ? uint256(-y) : uint256(y);
ad = denominator < 0 ? uint256(-denominator) : uint256(denominator);
}
// Compute the absolute value of (x*y)÷denominator. The result must fit within int256.
uint256 rAbs = mulDiv(ax, ay, ad);
if (rAbs > uint256(type(int256).max)) {
revert PRBMath__MulDivSignedOverflow(rAbs);
}
// Get the signs of x, y and the denominator.
uint256 sx;
uint256 sy;
uint256 sd;
assembly {
sx := sgt(x, sub(0, 1))
sy := sgt(y, sub(0, 1))
sd := sgt(denominator, sub(0, 1))
}
// XOR over sx, sy and sd. This is checking whether there are one or three negative signs in the inputs.
// If yes, the result should be negative.
result = sx ^ sy ^ sd == 0 ? -int256(rAbs) : int256(rAbs);
}
/// @notice Calculates the square root of x, rounding down.
/// @dev Uses the Babylonian method https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method.
///
/// Caveats:
/// - This function does not work with fixed-point numbers.
///
/// @param x The uint256 number for which to calculate the square root.
/// @return result The result as an uint256.
function sqrt(uint256 x) internal pure returns (uint256 result) {
if (x == 0) {
return 0;
}
// Set the initial guess to the least power of two that is greater than or equal to sqrt(x).
uint256 xAux = uint256(x);
result = 1;
if (xAux >= 0x100000000000000000000000000000000) {
xAux >>= 128;
result <<= 64;
}
if (xAux >= 0x10000000000000000) {
xAux >>= 64;
result <<= 32;
}
if (xAux >= 0x100000000) {
xAux >>= 32;
result <<= 16;
}
if (xAux >= 0x10000) {
xAux >>= 16;
result <<= 8;
}
if (xAux >= 0x100) {
xAux >>= 8;
result <<= 4;
}
if (xAux >= 0x10) {
xAux >>= 4;
result <<= 2;
}
if (xAux >= 0x8) {
result <<= 1;
}
// The operations can never overflow because the result is max 2^127 when it enters this block.
unchecked {
result = (result + x / result) >> 1;
result = (result + x / result) >> 1;
result = (result + x / result) >> 1;
result = (result + x / result) >> 1;
result = (result + x / result) >> 1;
result = (result + x / result) >> 1;
result = (result + x / result) >> 1; // Seven iterations should be enough
uint256 roundedDownResult = x / result;
return result >= roundedDownResult ? roundedDownResult : result;
}
}
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.0) (utils/math/Math.sol)
pragma solidity ^0.8.0;
/**
* @dev Standard math utilities missing in the Solidity language.
*/
library Math {
enum Rounding {
Down, // Toward negative infinity
Up, // Toward infinity
Zero // Toward zero
}
/**
* @dev Returns the largest of two numbers.
*/
function max(uint256 a, uint256 b) internal pure returns (uint256) {
return a > b ? a : b;
}
/**
* @dev Returns the smallest of two numbers.
*/
function min(uint256 a, uint256 b) internal pure returns (uint256) {
return a < b ? a : b;
}
/**
* @dev Returns the average of two numbers. The result is rounded towards
* zero.
*/
function average(uint256 a, uint256 b) internal pure returns (uint256) {
// (a + b) / 2 can overflow.
return (a & b) + (a ^ b) / 2;
}
/**
* @dev Returns the ceiling of the division of two numbers.
*
* This differs from standard division with `/` in that it rounds up instead
* of rounding down.
*/
function ceilDiv(uint256 a, uint256 b) internal pure returns (uint256) {
// (a + b - 1) / b can overflow on addition, so we distribute.
return a == 0 ? 0 : (a - 1) / b + 1;
}
/**
* @notice Calculates floor(x * y / denominator) with full precision. Throws if result overflows a uint256 or denominator == 0
* @dev Original credit to Remco Bloemen under MIT license (https://xn--2-umb.com/21/muldiv)
* with further edits by Uniswap Labs also under MIT license.
*/
function mulDiv(uint256 x, uint256 y, uint256 denominator) internal pure returns (uint256 result) {
unchecked {
// 512-bit multiply [prod1 prod0] = x * y. Compute the product mod 2^256 and mod 2^256 - 1, then use
// use the Chinese Remainder Theorem to reconstruct the 512 bit result. The result is stored in two 256
// variables such that product = prod1 * 2^256 + prod0.
uint256 prod0; // Least significant 256 bits of the product
uint256 prod1; // Most significant 256 bits of the product
assembly {
let mm := mulmod(x, y, not(0))
prod0 := mul(x, y)
prod1 := sub(sub(mm, prod0), lt(mm, prod0))
}
// Handle non-overflow cases, 256 by 256 division.
if (prod1 == 0) {
return prod0 / denominator;
}
// Make sure the result is less than 2^256. Also prevents denominator == 0.
require(denominator > prod1, "Math: mulDiv overflow");
///////////////////////////////////////////////
// 512 by 256 division.
///////////////////////////////////////////////
// Make division exact by subtracting the remainder from [prod1 prod0].
uint256 remainder;
assembly {
// Compute remainder using mulmod.
remainder := mulmod(x, y, denominator)
// Subtract 256 bit number from 512 bit number.
prod1 := sub(prod1, gt(remainder, prod0))
prod0 := sub(prod0, remainder)
}
// Factor powers of two out of denominator and compute largest power of two divisor of denominator. Always >= 1.
// See https://cs.stackexchange.com/q/138556/92363.
// Does not overflow because the denominator cannot be zero at this stage in the function.
uint256 twos = denominator & (~denominator + 1);
assembly {
// Divide denominator by twos.
denominator := div(denominator, twos)
// Divide [prod1 prod0] by twos.
prod0 := div(prod0, twos)
// Flip twos such that it is 2^256 / twos. If twos is zero, then it becomes one.
twos := add(div(sub(0, twos), twos), 1)
}
// Shift in bits from prod1 into prod0.
prod0 |= prod1 * twos;
// Invert denominator mod 2^256. Now that denominator is an odd number, it has an inverse modulo 2^256 such
// that denominator * inv = 1 mod 2^256. Compute the inverse by starting with a seed that is correct for
// four bits. That is, denominator * inv = 1 mod 2^4.
uint256 inverse = (3 * denominator) ^ 2;
// Use the 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.
inverse *= 2 - denominator * inverse; // inverse mod 2^8
inverse *= 2 - denominator * inverse; // inverse mod 2^16
inverse *= 2 - denominator * inverse; // inverse mod 2^32
inverse *= 2 - denominator * inverse; // inverse mod 2^64
inverse *= 2 - denominator * inverse; // inverse mod 2^128
inverse *= 2 - denominator * inverse; // inverse mod 2^256
// Because the division is now exact we can divide by multiplying with the modular inverse of denominator.
// This will give us the correct result modulo 2^256. Since the preconditions guarantee that the outcome is
// less than 2^256, this is the final result. We don't need to compute the high bits of the result and prod1
// is no longer required.
result = prod0 * inverse;
return result;
}
}
/**
* @notice Calculates x * y / denominator with full precision, following the selected rounding direction.
*/
function mulDiv(uint256 x, uint256 y, uint256 denominator, Rounding rounding) internal pure returns (uint256) {
uint256 result = mulDiv(x, y, denominator);
if (rounding == Rounding.Up && mulmod(x, y, denominator) > 0) {
result += 1;
}
return result;
}
/**
* @dev Returns the square root of a number. If the number is not a perfect square, the value is rounded down.
*
* Inspired by Henry S. Warren, Jr.'s "Hacker's Delight" (Chapter 11).
*/
function sqrt(uint256 a) internal pure returns (uint256) {
if (a == 0) {
return 0;
}
// For our first guess, we get the biggest power of 2 which is smaller than the square root of the target.
//
// We know that the "msb" (most significant bit) of our target number `a` is a power of 2 such that we have
// `msb(a) <= a < 2*msb(a)`. This value can be written `msb(a)=2**k` with `k=log2(a)`.
//
// This can be rewritten `2**log2(a) <= a < 2**(log2(a) + 1)`
// → `sqrt(2**k) <= sqrt(a) < sqrt(2**(k+1))`
// → `2**(k/2) <= sqrt(a) < 2**((k+1)/2) <= 2**(k/2 + 1)`
//
// Consequently, `2**(log2(a) / 2)` is a good first approximation of `sqrt(a)` with at least 1 correct bit.
uint256 result = 1 << (log2(a) >> 1);
// At this point `result` is an estimation with one bit of precision. We know the true value is a uint128,
// since it is the square root of a uint256. Newton's method converges quadratically (precision doubles at
// every iteration). We thus need at most 7 iteration to turn our partial result with one bit of precision
// into the expected uint128 result.
unchecked {
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
return min(result, a / result);
}
}
/**
* @notice Calculates sqrt(a), following the selected rounding direction.
*/
function sqrt(uint256 a, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = sqrt(a);
return result + (rounding == Rounding.Up && result * result < a ? 1 : 0);
}
}
/**
* @dev Return the log in base 2, rounded down, of a positive value.
* Returns 0 if given 0.
*/
function log2(uint256 value) internal pure returns (uint256) {
uint256 result = 0;
unchecked {
if (value >> 128 > 0) {
value >>= 128;
result += 128;
}
if (value >> 64 > 0) {
value >>= 64;
result += 64;
}
if (value >> 32 > 0) {
value >>= 32;
result += 32;
}
if (value >> 16 > 0) {
value >>= 16;
result += 16;
}
if (value >> 8 > 0) {
value >>= 8;
result += 8;
}
if (value >> 4 > 0) {
value >>= 4;
result += 4;
}
if (value >> 2 > 0) {
value >>= 2;
result += 2;
}
if (value >> 1 > 0) {
result += 1;
}
}
return result;
}
/**
* @dev Return the log in base 2, following the selected rounding direction, of a positive value.
* Returns 0 if given 0.
*/
function log2(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log2(value);
return result + (rounding == Rounding.Up && 1 << result < value ? 1 : 0);
}
}
/**
* @dev Return the log in base 10, rounded down, of a positive value.
* Returns 0 if given 0.
*/
function log10(uint256 value) internal pure returns (uint256) {
uint256 result = 0;
unchecked {
if (value >= 10 ** 64) {
value /= 10 ** 64;
result += 64;
}
if (value >= 10 ** 32) {
value /= 10 ** 32;
result += 32;
}
if (value >= 10 ** 16) {
value /= 10 ** 16;
result += 16;
}
if (value >= 10 ** 8) {
value /= 10 ** 8;
result += 8;
}
if (value >= 10 ** 4) {
value /= 10 ** 4;
result += 4;
}
if (value >= 10 ** 2) {
value /= 10 ** 2;
result += 2;
}
if (value >= 10 ** 1) {
result += 1;
}
}
return result;
}
/**
* @dev Return the log in base 10, following the selected rounding direction, of a positive value.
* Returns 0 if given 0.
*/
function log10(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log10(value);
return result + (rounding == Rounding.Up && 10 ** result < value ? 1 : 0);
}
}
/**
* @dev Return the log in base 256, rounded down, of a positive value.
* Returns 0 if given 0.
*
* Adding one to the result gives the number of pairs of hex symbols needed to represent `value` as a hex string.
*/
function log256(uint256 value) internal pure returns (uint256) {
uint256 result = 0;
unchecked {
if (value >> 128 > 0) {
value >>= 128;
result += 16;
}
if (value >> 64 > 0) {
value >>= 64;
result += 8;
}
if (value >> 32 > 0) {
value >>= 32;
result += 4;
}
if (value >> 16 > 0) {
value >>= 16;
result += 2;
}
if (value >> 8 > 0) {
result += 1;
}
}
return result;
}
/**
* @dev Return the log in base 256, following the selected rounding direction, of a positive value.
* Returns 0 if given 0.
*/
function log256(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log256(value);
return result + (rounding == Rounding.Up && 1 << (result << 3) < value ? 1 : 0);
}
}
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.0) (utils/math/SignedMath.sol)
pragma solidity ^0.8.0;
/**
* @dev Standard signed math utilities missing in the Solidity language.
*/
library SignedMath {
/**
* @dev Returns the largest of two signed numbers.
*/
function max(int256 a, int256 b) internal pure returns (int256) {
return a > b ? a : b;
}
/**
* @dev Returns the smallest of two signed numbers.
*/
function min(int256 a, int256 b) internal pure returns (int256) {
return a < b ? a : b;
}
/**
* @dev Returns the average of two signed numbers without overflow.
* The result is rounded towards zero.
*/
function average(int256 a, int256 b) internal pure returns (int256) {
// Formula from the book "Hacker's Delight"
int256 x = (a & b) + ((a ^ b) >> 1);
return x + (int256(uint256(x) >> 255) & (a ^ b));
}
/**
* @dev Returns the absolute unsigned value of a signed value.
*/
function abs(int256 n) internal pure returns (uint256) {
unchecked {
// must be unchecked in order to support `n = type(int256).min`
return uint256(n >= 0 ? n : -n);
}
}
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: BUSL-1.1
// SPDX-FileCopyrightText: 2023 Kiln <contact@kiln.fi>
//
// ██╗ ██╗██╗██╗ ███╗ ██╗
// ██║ ██╔╝██║██║ ████╗ ██║
// █████╔╝ ██║██║ ██╔██╗ ██║
// ██╔═██╗ ██║██║ ██║╚██╗██║
// ██║ ██╗██║███████╗██║ ╚████║
// ╚═╝ ╚═╝╚═╝╚══════╝╚═╝ ╚═══╝
//
pragma solidity >=0.8.17;
library LibPublicKey {
// slither-disable-next-line unused-state
uint256 constant PUBLIC_KEY_LENGTH = 48;
// slither-disable-next-line unused-state
bytes constant PADDING = hex"00000000000000000000000000000000";
struct PublicKey {
bytes32 A;
bytes16 B;
}
// slither-disable-next-line dead-code
function toBytes(PublicKey memory publicKey) internal pure returns (bytes memory) {
return abi.encodePacked(publicKey.A, publicKey.B);
}
// slither-disable-next-line dead-code
function fromBytes(bytes memory publicKey) internal pure returns (PublicKey memory ret) {
publicKey = bytes.concat(publicKey, PADDING);
(bytes32 A, bytes32 B_prime) = abi.decode(publicKey, (bytes32, bytes32));
bytes16 B = bytes16(uint128(uint256(B_prime) >> 128));
ret.A = A;
ret.B = B;
}
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: BUSL-1.1
// SPDX-FileCopyrightText: 2023 Kiln <contact@kiln.fi>
//
// ██╗ ██╗██╗██╗ ███╗ ██╗
// ██║ ██╔╝██║██║ ████╗ ██║
// █████╔╝ ██║██║ ██╔██╗ ██║
// ██╔═██╗ ██║██║ ██║╚██╗██║
// ██║ ██╗██║███████╗██║ ╚████║
// ╚═╝ ╚═╝╚═╝╚══════╝╚═╝ ╚═══╝
//
pragma solidity >=0.8.17;
library LibSignature {
// slither-disable-next-line unused-state
uint256 constant SIGNATURE_LENGTH = 96;
struct Signature {
bytes32 A;
bytes32 B;
bytes32 C;
}
// slither-disable-next-line dead-code
function toBytes(Signature memory signature) internal pure returns (bytes memory) {
return abi.encodePacked(signature.A, signature.B, signature.C);
}
// slither-disable-next-line dead-code
function fromBytes(bytes memory signature) internal pure returns (Signature memory ret) {
(ret) = abi.decode(signature, (Signature));
}
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.0) (token/ERC721/IERC721.sol)
pragma solidity ^0.8.0;
import "../../utils/introspection/IERC165.sol";
/**
* @dev Required interface of an ERC721 compliant contract.
*/
interface IERC721 is IERC165 {
/**
* @dev Emitted when `tokenId` token is transferred from `from` to `to`.
*/
event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);
/**
* @dev Emitted when `owner` enables `approved` to manage the `tokenId` token.
*/
event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId);
/**
* @dev Emitted when `owner` enables or disables (`approved`) `operator` to manage all of its assets.
*/
event ApprovalForAll(address indexed owner, address indexed operator, bool approved);
/**
* @dev Returns the number of tokens in ``owner``'s account.
*/
function balanceOf(address owner) external view returns (uint256 balance);
/**
* @dev Returns the owner of the `tokenId` token.
*
* Requirements:
*
* - `tokenId` must exist.
*/
function ownerOf(uint256 tokenId) external view returns (address owner);
/**
* @dev Safely transfers `tokenId` token from `from` to `to`.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `tokenId` token must exist and be owned by `from`.
* - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
* - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
*
* Emits a {Transfer} event.
*/
function safeTransferFrom(address from, address to, uint256 tokenId, bytes calldata data) external;
/**
* @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients
* are aware of the ERC721 protocol to prevent tokens from being forever locked.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `tokenId` token must exist and be owned by `from`.
* - If the caller is not `from`, it must have been allowed to move this token by either {approve} or {setApprovalForAll}.
* - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
*
* Emits a {Transfer} event.
*/
function safeTransferFrom(address from, address to, uint256 tokenId) external;
/**
* @dev Transfers `tokenId` token from `from` to `to`.
*
* WARNING: Note that the caller is responsible to confirm that the recipient is capable of receiving ERC721
* or else they may be permanently lost. Usage of {safeTransferFrom} prevents loss, though the caller must
* understand this adds an external call which potentially creates a reentrancy vulnerability.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `tokenId` token must be owned by `from`.
* - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
*
* Emits a {Transfer} event.
*/
function transferFrom(address from, address to, uint256 tokenId) external;
/**
* @dev Gives permission to `to` to transfer `tokenId` token to another account.
* The approval is cleared when the token is transferred.
*
* Only a single account can be approved at a time, so approving the zero address clears previous approvals.
*
* Requirements:
*
* - The caller must own the token or be an approved operator.
* - `tokenId` must exist.
*
* Emits an {Approval} event.
*/
function approve(address to, uint256 tokenId) external;
/**
* @dev Approve or remove `operator` as an operator for the caller.
* Operators can call {transferFrom} or {safeTransferFrom} for any token owned by the caller.
*
* Requirements:
*
* - The `operator` cannot be the caller.
*
* Emits an {ApprovalForAll} event.
*/
function setApprovalForAll(address operator, bool approved) external;
/**
* @dev Returns the account approved for `tokenId` token.
*
* Requirements:
*
* - `tokenId` must exist.
*/
function getApproved(uint256 tokenId) external view returns (address operator);
/**
* @dev Returns if the `operator` is allowed to manage all of the assets of `owner`.
*
* See {setApprovalForAll}
*/
function isApprovedForAll(address owner, address operator) external view returns (bool);
} <i class='far fa-question-circle text-muted ms-2' data-bs-trigger='hover' data-bs-toggle='tooltip' data-bs-html='true' data-bs-title='Click on the check box to select individual contract to compare. Only 1 contract can be selected from each side.'></i>
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/introspection/IERC165.sol)
pragma solidity ^0.8.0;
/**
* @dev Interface of the ERC165 standard, as defined in the
* https://eips.ethereum.org/EIPS/eip-165[EIP].
*
* Implementers can declare support of contract interfaces, which can then be
* queried by others ({ERC165Checker}).
*
* For an implementation, see {ERC165}.
*/
interface IERC165 {
/**
* @dev Returns true if this contract implements the interface defined by
* `interfaceId`. See the corresponding
* https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[EIP section]
* to learn more about how these ids are created.
*
* This function call must use less than 30 000 gas.
*/
function supportsInterface(bytes4 interfaceId) external view returns (bool);
}