Address Contract Verified
Address
0x4c00E0Bc0ebd5ab685e2d6fA567C25b11F5F1CD1
Balance
0 ETH
Nonce
1
Code Size
6578 bytes
Creator
0x6D59A39A...D2F0 at tx 0xf86fb53d...38035d
Indexed Transactions
0
Contract Bytecode
6578 bytes
0x608060405234801561001057600080fd5b506004361061018e5760003560e01c80638da5cb5b116100de578063c884ef8311610097578063e30c397811610071578063e30c397814610405578063e3e4b15a14610416578063f2fde38b14610429578063f73410f21461043c57600080fd5b8063c884ef83146103b5578063d44641af146103d5578063d54ad2a1146103fc57600080fd5b80638da5cb5b146102d757806399d90c38146102fc5780639f11853614610323578063a2d7f5e314610340578063aa6e52f014610367578063bf7b766d1461038e57600080fd5b806356539f391161014b578063715018a611610125578063715018a61461028d57806379ba5097146102955780638218df621461029d578063839006f2146102c457600080fd5b806356539f391461024b578063603d13eb1461027257806366aa56c51461028557600080fd5b806309836ef5146101935780631e83409a146101ae578063227f02d3146101c357806325b0fedc146101ea578063420d4a021461021157806353add66114610224575b600080fd5b61019b610451565b6040519081526020015b60405180910390f35b6101c16101bc366004611705565b610461565b005b61019b7f0000000000000000000000000000000000000000000000000000000067e397e081565b61019b7f000000000000000000000000000000000000000000000000000000000000000681565b61019b61021f366004611705565b6105c3565b61019b7f00000000000000000000000000000000000000000000a968163f0a57b400000081565b61019b7f000000000000000000000000000000000000000000000000000000000000000181565b61019b610280366004611720565b6105d5565b6101c16107b2565b6101c1610894565b6101c16108a6565b61019b7f00000000000000000000000000000000000000000002a5a058fc295ed000000081565b6101c16102d2366004611705565b6108ec565b6000546001600160a01b03165b6040516001600160a01b0390911681526020016101a5565b6102e47f000000000000000000000000ef640ebaa42b41d1dcc5108d05dfb7d4f8c5eaa981565b6004546103309060ff1681565b60405190151581526020016101a5565b61019b7f000000000000000000000000000000000000000000034f086f3b33b68400000081565b61019b7f000000000000000000000000000000000000000000000000000000000000001281565b61019b7f0000000000000000000000000000000000000000000000000000000067e5058081565b61019b6103c3366004611705565b60026020526000908152604090205481565b6102e47f000000000000000000000000e2616122ed554bd693335e9143c47df187a86ef381565b61019b60035481565b6001546001600160a01b03166102e4565b61019b61042436600461174c565b610aa5565b6101c1610437366004611705565b610ccd565b610444610d3e565b6040516101a59190611765565b600061045c42610aa5565b905090565b60045460ff1661048457604051636a5221d360e01b815260040160405180910390fd5b600061048f826105c3565b6001600160a01b03831660009081526002602052604090205490915081116104b5575050565b6001600160a01b0382166000908152600260205260408120546104d890836117be565b604080516001600160a01b037f000000000000000000000000e2616122ed554bd693335e9143c47df187a86ef38116825260208201849052929350918516917ff7a40077ff7a04c7e61f6f26fb13774259ddf1b6bce9ecf26a8276cdd3992683910160405180910390a26001600160a01b0383166000908152600260205260408120805483929061056a9084906117d1565b92505081905550806003600082825461058391906117d1565b909155506105bd90506001600160a01b037f000000000000000000000000e2616122ed554bd693335e9143c47df187a86ef3168483610e51565b50505b50565b60006105cf42836105d5565b92915050565b6000806105e184610aa5565b60405163a2088a1760e01b81527f000000000000000000000000000000000000000000000000000000000000000160048201527f0000000000000000000000000000000000000000000000000000000067e397e060248201529091506000906001600160a01b037f000000000000000000000000ef640ebaa42b41d1dcc5108d05dfb7d4f8c5eaa9169063a2088a1790604401602060405180830381865afa158015610691573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906106b591906117e4565b604051634d6fb77560e01b81526001600160a01b0386811660048301527f000000000000000000000000000000000000000000000000000000000000000160248301527f0000000000000000000000000000000000000000000000000000000067e397e060448301529192506000917f000000000000000000000000ef640ebaa42b41d1dcc5108d05dfb7d4f8c5eaa91690634d6fb77590606401602060405180830381865afa15801561076d573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061079191906117e4565b90508161079e82856117fd565b6107a8919061182a565b9695505050505050565b6107ba610eb0565b60045460ff16156107de57604051631b6c056760e11b815260040160405180910390fd5b6040517f0000000000000000000000000000000000000000000000000000000067e5058081527e6e0c97de781a7389d44ba8fd35d1467cabb17ed04d038d166d34ab819213f39060200160405180910390a16004805460ff191660011790556108926001600160a01b037f000000000000000000000000e2616122ed554bd693335e9143c47df187a86ef31633307f000000000000000000000000000000000000000000034f086f3b33b684000000610edd565b565b61089c610eb0565b6108926000610f1c565b60015433906001600160a01b031681146108e35760405163118cdaa760e01b81526001600160a01b03821660048201526024015b60405180910390fd5b6105c081610f1c565b6108f4610eb0565b6040516370a0823160e01b81523060048201526000906001600160a01b038316906370a0823190602401602060405180830381865afa15801561093b573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061095f91906117e4565b90507f000000000000000000000000e2616122ed554bd693335e9143c47df187a86ef36001600160a01b0316826001600160a01b03161480156109a4575060045460ff165b15610a33577f000000000000000000000000000000000000000000034f086f3b33b684000000600354826109d891906117d1565b10156109f757604051631e9acf1760e31b815260040160405180910390fd5b7f000000000000000000000000000000000000000000034f086f3b33b68400000060035482610a2691906117d1565b610a3091906117be565b90505b8015610aa157604080516001600160a01b0384168152602081018390527f8aec0ce3dadffacf4b7a963e0fed1ff2e6151b4c95d4a65acafa9d1299630402910160405180910390a1610aa1610a906000546001600160a01b031690565b6001600160a01b0384169083610e51565b5050565b60007f0000000000000000000000000000000000000000000000000000000067e50580821015610ad757506000919050565b610b217f0000000000000000000000000000000000000000000000000000000067e505807f0000000000000000000000000000000000000000000000000000000000000006610f35565b821015610b4f57507f00000000000000000000000000000000000000000000a968163f0a57b4000000919050565b610bc37f0000000000000000000000000000000000000000000000000000000000000012610bbd7f0000000000000000000000000000000000000000000000000000000067e505807f0000000000000000000000000000000000000000000000000000000000000006610f35565b90610f35565b8210610bf057507f000000000000000000000000000000000000000000034f086f3b33b684000000919050565b6000610c45610c3f7f0000000000000000000000000000000000000000000000000000000067e505807f0000000000000000000000000000000000000000000000000000000000000006610f35565b84611043565b90507f0000000000000000000000000000000000000000000000000000000000000012610c92827f00000000000000000000000000000000000000000002a5a058fc295ed00000006117fd565b610c9c919061182a565b610cc6907f00000000000000000000000000000000000000000000a968163f0a57b40000006117d1565b9392505050565b610cd5610eb0565b600180546001600160a01b0383166001600160a01b03199091168117909155610d066000546001600160a01b031690565b6001600160a01b03167f38d16b8cac22d99fc7c124b9cd0de2d3fa1faef420bfe791d8c362d765e2270060405160405180910390a350565b606060007f000000000000000000000000000000000000000000000000000000000000001267ffffffffffffffff811115610d7b57610d7b61183e565b604051908082528060200260200182016040528015610da4578160200160208202803683370190505b50905060005b7f0000000000000000000000000000000000000000000000000000000000000012811015610e4b576000610e2282610bbd7f0000000000000000000000000000000000000000000000000000000067e505807f0000000000000000000000000000000000000000000000000000000000000006610f35565b905080838381518110610e3757610e37611854565b602090810291909101015250600101610daa565b50919050565b6040516001600160a01b038381166024830152604482018390526105bd91859182169063a9059cbb906064015b604051602081830303815290604052915060e01b6020820180516001600160e01b0383818316178352505050506110a6565b6000546001600160a01b031633146108925760405163118cdaa760e01b81523360048201526024016108da565b6040516001600160a01b038481166024830152838116604483015260648201839052610f169186918216906323b872dd90608401610e7e565b50505050565b600180546001600160a01b03191690556105c081611109565b6000808080610f4f610f4a620151808861182a565b611159565b91945092509050610f6085836117d1565b9150600c610f6f6001846117be565b610f79919061182a565b610f8390846117d1565b9250600c610f926001846117be565b610f9c919061186a565b610fa79060016117d1565b91506000610fb584846112cd565b905080821115610fc3578091505b610fd0620151808861186a565b62015180610fdf868686611353565b610fe991906117fd565b610ff391906117d1565b9450868510156110395760405162461bcd60e51b81526020600482015260116024820152700696e76616c69642074696d657374616d7607c1b60448201526064016108da565b5050505092915050565b6000818311156110665760405163b7d0949760e01b815260040160405180910390fd5b600061107284846114d3565b90508261107f8583610f35565b1115611093576110906001826117be565b90505b61109e8160016117d1565b949350505050565b60006110bb6001600160a01b0384168361158c565b905080516000141580156110e05750808060200190518101906110de919061187e565b155b156105bd57604051635274afe760e01b81526001600160a01b03841660048201526024016108da565b600080546001600160a01b038381166001600160a01b0319831681178455604051919092169283917f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e09190a35050565b60008080838162253d8c6111708362010bd96118a0565b61117a91906118a0565b9050600062023ab161118d8360046118c8565b61119791906118f8565b905060046111a88262023ab16118c8565b6111b39060036118a0565b6111bd91906118f8565b6111c79083611926565b9150600062164b096111da8460016118a0565b6111e690610fa06118c8565b6111f091906118f8565b90506004611200826105b56118c8565b61120a91906118f8565b6112149084611926565b61121f90601f6118a0565b9250600061098f6112318560506118c8565b61123b91906118f8565b90506000605061124d8361098f6118c8565b61125791906118f8565b6112619086611926565b905061126e600b836118f8565b945061127b85600c6118c8565b6112868360026118a0565b6112909190611926565b9150848361129f603187611926565b6112aa9060646118c8565b6112b491906118a0565b6112be91906118a0565b9a919950975095505050505050565b600081600114806112de5750816003145b806112e95750816005145b806112f45750816007145b806112ff5750816008145b8061130a575081600a145b80611315575081600c145b156113225750601f6105cf565b816002146113325750601e6105cf565b61133b8361159a565b61134657601c611349565b601d5b60ff169392505050565b60006107b28410156113a75760405162461bcd60e51b815260206004820152601960248201527f79656172206e6f206561726c696572207468616e20313937300000000000000060448201526064016108da565b838383600062253d8c60046064600c6113c1600e88611926565b6113cb91906118f8565b6113d7886113246118a0565b6113e191906118a0565b6113eb91906118f8565b6113f69060036118c8565b61140091906118f8565b600c8061140e600e88611926565b61141891906118f8565b61142390600c6118c8565b61142e600288611926565b6114389190611926565b6114449061016f6118c8565b61144e91906118f8565b6004600c61145d600e89611926565b61146791906118f8565b611473896112c06118a0565b61147d91906118a0565b611489906105b56118c8565b61149391906118f8565b61149f617d4b87611926565b6114a991906118a0565b6114b391906118a0565b6114bd9190611926565b6114c79190611926565b98975050505050505050565b6000818311156115195760405162461bcd60e51b81526020600482015260116024820152700696e76616c69642074696d657374616d7607c1b60448201526064016108da565b60008061152c610f4a620151808761182a565b509092509050600080611545610f4a620151808861182a565b5090925090508261155785600c6117fd565b8261156385600c6117fd565b61156d91906117d1565b61157791906117be565b61158191906117be565b979650505050505050565b6060610cc6838360006115d6565b60006115a760048361186a565b1580156115bd57506115ba60648361186a565b15155b806105cf57506115cf6101908361186a565b1592915050565b6060814710156115fb5760405163cd78605960e01b81523060048201526024016108da565b600080856001600160a01b03168486604051611617919061194d565b60006040518083038185875af1925050503d8060008114611654576040519150601f19603f3d011682016040523d82523d6000602084013e611659565b606091505b50915091506107a886838360608261167957611674826116c0565b610cc6565b815115801561169057506001600160a01b0384163b155b156116b957604051639996b31560e01b81526001600160a01b03851660048201526024016108da565b5080610cc6565b8051156116d05780518082602001fd5b604051630a12f52160e11b815260040160405180910390fd5b80356001600160a01b038116811461170057600080fd5b919050565b60006020828403121561171757600080fd5b610cc6826116e9565b6000806040838503121561173357600080fd5b82359150611743602084016116e9565b90509250929050565b60006020828403121561175e57600080fd5b5035919050565b602080825282518282018190526000918401906040840190835b8181101561179d57835183526020938401939092019160010161177f565b509095945050505050565b634e487b7160e01b600052601160045260246000fd5b818103818111156105cf576105cf6117a8565b808201808211156105cf576105cf6117a8565b6000602082840312156117f657600080fd5b5051919050565b80820281158282048414176105cf576105cf6117a8565b634e487b7160e01b600052601260045260246000fd5b60008261183957611839611814565b500490565b634e487b7160e01b600052604160045260246000fd5b634e487b7160e01b600052603260045260246000fd5b60008261187957611879611814565b500690565b60006020828403121561189057600080fd5b81518015158114610cc657600080fd5b80820182811260008312801582168215821617156118c0576118c06117a8565b505092915050565b80820260008212600160ff1b841416156118e4576118e46117a8565b81810583148215176105cf576105cf6117a8565b60008261190757611907611814565b600160ff1b821460001984141615611921576119216117a8565b500590565b8181036000831280158383131683831282161715611946576119466117a8565b5092915050565b6000825160005b8181101561196e5760208186018101518583015201611954565b50600092019182525091905056fea2646970667358221220e12205f3bc7b11401887c9204833b7e5190b72dd043313175ce70e4a698a5a9864736f6c634300081a0033
Verified Source Code Full Match
Compiler: v0.8.26+commit.8a97fa7a
EVM: paris
Optimization: Yes (200 runs)
IAlphastarter1155.sol 14 lines
// SPDX-License-Identifier: MIT
// Compatible with OpenZeppelin Contracts ^5.0.0
pragma solidity 0.8.26;
import {IERC1155} from "../libs/@openzeppelin/contracts/token/ERC1155/IERC1155.sol";
interface IAlphastarter1155 is IERC1155 {
function mint(address account, uint256 id, uint256 amount, bytes memory data) external;
function burn(address account, uint256 id, uint256 value) external;
function finalize(uint256 id, bool flag) external;
function isFinalized(uint256 id) external view returns (bool);
function getPastVotes(address account, uint256 id, uint256 timepoint) external view returns (uint256);
function getPastTotalSupply(uint256 id, uint256 timepoint) external view returns (uint256);
}
Ownable.sol 100 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (access/Ownable.sol)
pragma solidity ^0.8.20;
import {Context} from "../utils/Context.sol";
/**
* @dev Contract module which provides a basic access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
* specific functions.
*
* The initial owner is set to the address provided by the deployer. This can
* later be changed with {transferOwnership}.
*
* This module is used through inheritance. It will make available the modifier
* `onlyOwner`, which can be applied to your functions to restrict their use to
* the owner.
*/
abstract contract Ownable is Context {
address private _owner;
/**
* @dev The caller account is not authorized to perform an operation.
*/
error OwnableUnauthorizedAccount(address account);
/**
* @dev The owner is not a valid owner account. (eg. `address(0)`)
*/
error OwnableInvalidOwner(address owner);
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/**
* @dev Initializes the contract setting the address provided by the deployer as the initial owner.
*/
constructor(address initialOwner) {
if (initialOwner == address(0)) {
revert OwnableInvalidOwner(address(0));
}
_transferOwnership(initialOwner);
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
_checkOwner();
_;
}
/**
* @dev Returns the address of the current owner.
*/
function owner() public view virtual returns (address) {
return _owner;
}
/**
* @dev Throws if the sender is not the owner.
*/
function _checkOwner() internal view virtual {
if (owner() != _msgSender()) {
revert OwnableUnauthorizedAccount(_msgSender());
}
}
/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby disabling any functionality that is only available to the owner.
*/
function renounceOwnership() public virtual onlyOwner {
_transferOwnership(address(0));
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/
function transferOwnership(address newOwner) public virtual onlyOwner {
if (newOwner == address(0)) {
revert OwnableInvalidOwner(address(0));
}
_transferOwnership(newOwner);
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Internal function without access restriction.
*/
function _transferOwnership(address newOwner) internal virtual {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
Ownable2Step.sol 59 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (access/Ownable2Step.sol)
pragma solidity ^0.8.20;
import {Ownable} from "./Ownable.sol";
/**
* @dev Contract module which provides access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
* specific functions.
*
* The initial owner is specified at deployment time in the constructor for `Ownable`. This
* can later be changed with {transferOwnership} and {acceptOwnership}.
*
* This module is used through inheritance. It will make available all functions
* from parent (Ownable).
*/
abstract contract Ownable2Step is Ownable {
address private _pendingOwner;
event OwnershipTransferStarted(address indexed previousOwner, address indexed newOwner);
/**
* @dev Returns the address of the pending owner.
*/
function pendingOwner() public view virtual returns (address) {
return _pendingOwner;
}
/**
* @dev Starts the ownership transfer of the contract to a new account. Replaces the pending transfer if there is one.
* Can only be called by the current owner.
*/
function transferOwnership(address newOwner) public virtual override onlyOwner {
_pendingOwner = newOwner;
emit OwnershipTransferStarted(owner(), newOwner);
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`) and deletes any pending owner.
* Internal function without access restriction.
*/
function _transferOwnership(address newOwner) internal virtual override {
delete _pendingOwner;
super._transferOwnership(newOwner);
}
/**
* @dev The new owner accepts the ownership transfer.
*/
function acceptOwnership() public virtual {
address sender = _msgSender();
if (pendingOwner() != sender) {
revert OwnableUnauthorizedAccount(sender);
}
_transferOwnership(sender);
}
}
IERC1155.sol 127 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.1) (token/ERC1155/IERC1155.sol)
pragma solidity ^0.8.20;
import {IERC165} from "../../utils/introspection/IERC165.sol";
/**
* @dev Required interface of an ERC1155 compliant contract, as defined in the
* https://eips.ethereum.org/EIPS/eip-1155[EIP].
*/
interface IERC1155 is IERC165 {
/**
* @dev Emitted when `value` amount of tokens of type `id` are transferred from `from` to `to` by `operator`.
*/
event TransferSingle(address indexed operator, address indexed from, address indexed to, uint256 id, uint256 value);
/**
* @dev Equivalent to multiple {TransferSingle} events, where `operator`, `from` and `to` are the same for all
* transfers.
*/
event TransferBatch(
address indexed operator,
address indexed from,
address indexed to,
uint256[] ids,
uint256[] values
);
/**
* @dev Emitted when `account` grants or revokes permission to `operator` to transfer their tokens, according to
* `approved`.
*/
event ApprovalForAll(address indexed account, address indexed operator, bool approved);
/**
* @dev Emitted when the URI for token type `id` changes to `value`, if it is a non-programmatic URI.
*
* If an {URI} event was emitted for `id`, the standard
* https://eips.ethereum.org/EIPS/eip-1155#metadata-extensions[guarantees] that `value` will equal the value
* returned by {IERC1155MetadataURI-uri}.
*/
event URI(string value, uint256 indexed id);
/**
* @dev Returns the value of tokens of token type `id` owned by `account`.
*
* Requirements:
*
* - `account` cannot be the zero address.
*/
function balanceOf(address account, uint256 id) external view returns (uint256);
/**
* @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {balanceOf}.
*
* Requirements:
*
* - `accounts` and `ids` must have the same length.
*/
function balanceOfBatch(
address[] calldata accounts,
uint256[] calldata ids
) external view returns (uint256[] memory);
/**
* @dev Grants or revokes permission to `operator` to transfer the caller's tokens, according to `approved`,
*
* Emits an {ApprovalForAll} event.
*
* Requirements:
*
* - `operator` cannot be the caller.
*/
function setApprovalForAll(address operator, bool approved) external;
/**
* @dev Returns true if `operator` is approved to transfer ``account``'s tokens.
*
* See {setApprovalForAll}.
*/
function isApprovedForAll(address account, address operator) external view returns (bool);
/**
* @dev Transfers a `value` amount of tokens of type `id` from `from` to `to`.
*
* WARNING: This function can potentially allow a reentrancy attack when transferring tokens
* to an untrusted contract, when invoking {onERC1155Received} on the receiver.
* Ensure to follow the checks-effects-interactions pattern and consider employing
* reentrancy guards when interacting with untrusted contracts.
*
* Emits a {TransferSingle} event.
*
* Requirements:
*
* - `to` cannot be the zero address.
* - If the caller is not `from`, it must have been approved to spend ``from``'s tokens via {setApprovalForAll}.
* - `from` must have a balance of tokens of type `id` of at least `value` amount.
* - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155Received} and return the
* acceptance magic value.
*/
function safeTransferFrom(address from, address to, uint256 id, uint256 value, bytes calldata data) external;
/**
* @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {safeTransferFrom}.
*
* WARNING: This function can potentially allow a reentrancy attack when transferring tokens
* to an untrusted contract, when invoking {onERC1155BatchReceived} on the receiver.
* Ensure to follow the checks-effects-interactions pattern and consider employing
* reentrancy guards when interacting with untrusted contracts.
*
* Emits either a {TransferSingle} or a {TransferBatch} event, depending on the length of the array arguments.
*
* Requirements:
*
* - `ids` and `values` must have the same length.
* - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155BatchReceived} and return the
* acceptance magic value.
*/
function safeBatchTransferFrom(
address from,
address to,
uint256[] calldata ids,
uint256[] calldata values,
bytes calldata data
) external;
}
IERC20Permit.sol 90 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC20/extensions/IERC20Permit.sol)
pragma solidity ^0.8.20;
/**
* @dev Interface of the ERC20 Permit extension allowing approvals to be made via signatures, as defined in
* https://eips.ethereum.org/EIPS/eip-2612[EIP-2612].
*
* Adds the {permit} method, which can be used to change an account's ERC20 allowance (see {IERC20-allowance}) by
* presenting a message signed by the account. By not relying on {IERC20-approve}, the token holder account doesn't
* need to send a transaction, and thus is not required to hold Ether at all.
*
* ==== Security Considerations
*
* There are two important considerations concerning the use of `permit`. The first is that a valid permit signature
* expresses an allowance, and it should not be assumed to convey additional meaning. In particular, it should not be
* considered as an intention to spend the allowance in any specific way. The second is that because permits have
* built-in replay protection and can be submitted by anyone, they can be frontrun. A protocol that uses permits should
* take this into consideration and allow a `permit` call to fail. Combining these two aspects, a pattern that may be
* generally recommended is:
*
* ```solidity
* function doThingWithPermit(..., uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s) public {
* try token.permit(msg.sender, address(this), value, deadline, v, r, s) {} catch {}
* doThing(..., value);
* }
*
* function doThing(..., uint256 value) public {
* token.safeTransferFrom(msg.sender, address(this), value);
* ...
* }
* ```
*
* Observe that: 1) `msg.sender` is used as the owner, leaving no ambiguity as to the signer intent, and 2) the use of
* `try/catch` allows the permit to fail and makes the code tolerant to frontrunning. (See also
* {SafeERC20-safeTransferFrom}).
*
* Additionally, note that smart contract wallets (such as Argent or Safe) are not able to produce permit signatures, so
* contracts should have entry points that don't rely on permit.
*/
interface IERC20Permit {
/**
* @dev Sets `value` as the allowance of `spender` over ``owner``'s tokens,
* given ``owner``'s signed approval.
*
* IMPORTANT: The same issues {IERC20-approve} has related to transaction
* ordering also apply here.
*
* Emits an {Approval} event.
*
* Requirements:
*
* - `spender` cannot be the zero address.
* - `deadline` must be a timestamp in the future.
* - `v`, `r` and `s` must be a valid `secp256k1` signature from `owner`
* over the EIP712-formatted function arguments.
* - the signature must use ``owner``'s current nonce (see {nonces}).
*
* For more information on the signature format, see the
* https://eips.ethereum.org/EIPS/eip-2612#specification[relevant EIP
* section].
*
* CAUTION: See Security Considerations above.
*/
function permit(
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) external;
/**
* @dev Returns the current nonce for `owner`. This value must be
* included whenever a signature is generated for {permit}.
*
* Every successful call to {permit} increases ``owner``'s nonce by one. This
* prevents a signature from being used multiple times.
*/
function nonces(address owner) external view returns (uint256);
/**
* @dev Returns the domain separator used in the encoding of the signature for {permit}, as defined by {EIP712}.
*/
// solhint-disable-next-line func-name-mixedcase
function DOMAIN_SEPARATOR() external view returns (bytes32);
}
IERC20.sol 79 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC20/IERC20.sol)
pragma solidity ^0.8.20;
/**
* @dev Interface of the ERC20 standard as defined in the EIP.
*/
interface IERC20 {
/**
* @dev Emitted when `value` tokens are moved from one account (`from`) to
* another (`to`).
*
* Note that `value` may be zero.
*/
event Transfer(address indexed from, address indexed to, uint256 value);
/**
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
* a call to {approve}. `value` is the new allowance.
*/
event Approval(address indexed owner, address indexed spender, uint256 value);
/**
* @dev Returns the value of tokens in existence.
*/
function totalSupply() external view returns (uint256);
/**
* @dev Returns the value of tokens owned by `account`.
*/
function balanceOf(address account) external view returns (uint256);
/**
* @dev Moves a `value` amount of tokens from the caller's account to `to`.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transfer(address to, uint256 value) external returns (bool);
/**
* @dev Returns the remaining number of tokens that `spender` will be
* allowed to spend on behalf of `owner` through {transferFrom}. This is
* zero by default.
*
* This value changes when {approve} or {transferFrom} are called.
*/
function allowance(address owner, address spender) external view returns (uint256);
/**
* @dev Sets a `value` amount of tokens as the allowance of `spender` over the
* caller's tokens.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* IMPORTANT: Beware that changing an allowance with this method brings the risk
* that someone may use both the old and the new allowance by unfortunate
* transaction ordering. One possible solution to mitigate this race
* condition is to first reduce the spender's allowance to 0 and set the
* desired value afterwards:
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
*
* Emits an {Approval} event.
*/
function approve(address spender, uint256 value) external returns (bool);
/**
* @dev Moves a `value` amount of tokens from `from` to `to` using the
* allowance mechanism. `value` is then deducted from the caller's
* allowance.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transferFrom(address from, address to, uint256 value) external returns (bool);
}
SafeERC20.sol 118 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC20/utils/SafeERC20.sol)
pragma solidity ^0.8.20;
import {IERC20} from "../IERC20.sol";
import {IERC20Permit} from "../extensions/IERC20Permit.sol";
import {Address} from "../../../utils/Address.sol";
/**
* @title SafeERC20
* @dev Wrappers around ERC20 operations that throw on failure (when the token
* contract returns false). Tokens that return no value (and instead revert or
* throw on failure) are also supported, non-reverting calls are assumed to be
* successful.
* To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract,
* which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
*/
library SafeERC20 {
using Address for address;
/**
* @dev An operation with an ERC20 token failed.
*/
error SafeERC20FailedOperation(address token);
/**
* @dev Indicates a failed `decreaseAllowance` request.
*/
error SafeERC20FailedDecreaseAllowance(address spender, uint256 currentAllowance, uint256 requestedDecrease);
/**
* @dev Transfer `value` amount of `token` from the calling contract to `to`. If `token` returns no value,
* non-reverting calls are assumed to be successful.
*/
function safeTransfer(IERC20 token, address to, uint256 value) internal {
_callOptionalReturn(token, abi.encodeCall(token.transfer, (to, value)));
}
/**
* @dev Transfer `value` amount of `token` from `from` to `to`, spending the approval given by `from` to the
* calling contract. If `token` returns no value, non-reverting calls are assumed to be successful.
*/
function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal {
_callOptionalReturn(token, abi.encodeCall(token.transferFrom, (from, to, value)));
}
/**
* @dev Increase the calling contract's allowance toward `spender` by `value`. If `token` returns no value,
* non-reverting calls are assumed to be successful.
*/
function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal {
uint256 oldAllowance = token.allowance(address(this), spender);
forceApprove(token, spender, oldAllowance + value);
}
/**
* @dev Decrease the calling contract's allowance toward `spender` by `requestedDecrease`. If `token` returns no
* value, non-reverting calls are assumed to be successful.
*/
function safeDecreaseAllowance(IERC20 token, address spender, uint256 requestedDecrease) internal {
unchecked {
uint256 currentAllowance = token.allowance(address(this), spender);
if (currentAllowance < requestedDecrease) {
revert SafeERC20FailedDecreaseAllowance(spender, currentAllowance, requestedDecrease);
}
forceApprove(token, spender, currentAllowance - requestedDecrease);
}
}
/**
* @dev Set the calling contract's allowance toward `spender` to `value`. If `token` returns no value,
* non-reverting calls are assumed to be successful. Meant to be used with tokens that require the approval
* to be set to zero before setting it to a non-zero value, such as USDT.
*/
function forceApprove(IERC20 token, address spender, uint256 value) internal {
bytes memory approvalCall = abi.encodeCall(token.approve, (spender, value));
if (!_callOptionalReturnBool(token, approvalCall)) {
_callOptionalReturn(token, abi.encodeCall(token.approve, (spender, 0)));
_callOptionalReturn(token, approvalCall);
}
}
/**
* @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
* on the return value: the return value is optional (but if data is returned, it must not be false).
* @param token The token targeted by the call.
* @param data The call data (encoded using abi.encode or one of its variants).
*/
function _callOptionalReturn(IERC20 token, bytes memory data) private {
// We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
// we're implementing it ourselves. We use {Address-functionCall} to perform this call, which verifies that
// the target address contains contract code and also asserts for success in the low-level call.
bytes memory returndata = address(token).functionCall(data);
if (returndata.length != 0 && !abi.decode(returndata, (bool))) {
revert SafeERC20FailedOperation(address(token));
}
}
/**
* @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
* on the return value: the return value is optional (but if data is returned, it must not be false).
* @param token The token targeted by the call.
* @param data The call data (encoded using abi.encode or one of its variants).
*
* This is a variant of {_callOptionalReturn} that silents catches all reverts and returns a bool instead.
*/
function _callOptionalReturnBool(IERC20 token, bytes memory data) private returns (bool) {
// We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
// we're implementing it ourselves. We cannot use {Address-functionCall} here since this should return false
// and not revert is the subcall reverts.
(bool success, bytes memory returndata) = address(token).call(data);
return success && (returndata.length == 0 || abi.decode(returndata, (bool))) && address(token).code.length > 0;
}
}
Address.sol 159 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (utils/Address.sol)
pragma solidity ^0.8.20;
/**
* @dev Collection of functions related to the address type
*/
library Address {
/**
* @dev The ETH balance of the account is not enough to perform the operation.
*/
error AddressInsufficientBalance(address account);
/**
* @dev There's no code at `target` (it is not a contract).
*/
error AddressEmptyCode(address target);
/**
* @dev A call to an address target failed. The target may have reverted.
*/
error FailedInnerCall();
/**
* @dev Replacement for Solidity's `transfer`: sends `amount` wei to
* `recipient`, forwarding all available gas and reverting on errors.
*
* https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
* of certain opcodes, possibly making contracts go over the 2300 gas limit
* imposed by `transfer`, making them unable to receive funds via
* `transfer`. {sendValue} removes this limitation.
*
* https://consensys.net/diligence/blog/2019/09/stop-using-soliditys-transfer-now/[Learn more].
*
* IMPORTANT: because control is transferred to `recipient`, care must be
* taken to not create reentrancy vulnerabilities. Consider using
* {ReentrancyGuard} or the
* https://solidity.readthedocs.io/en/v0.8.20/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
*/
function sendValue(address payable recipient, uint256 amount) internal {
if (address(this).balance < amount) {
revert AddressInsufficientBalance(address(this));
}
(bool success, ) = recipient.call{value: amount}("");
if (!success) {
revert FailedInnerCall();
}
}
/**
* @dev Performs a Solidity function call using a low level `call`. A
* plain `call` is an unsafe replacement for a function call: use this
* function instead.
*
* If `target` reverts with a revert reason or custom error, it is bubbled
* up by this function (like regular Solidity function calls). However, if
* the call reverted with no returned reason, this function reverts with a
* {FailedInnerCall} error.
*
* Returns the raw returned data. To convert to the expected return value,
* use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
*
* Requirements:
*
* - `target` must be a contract.
* - calling `target` with `data` must not revert.
*/
function functionCall(address target, bytes memory data) internal returns (bytes memory) {
return functionCallWithValue(target, data, 0);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but also transferring `value` wei to `target`.
*
* Requirements:
*
* - the calling contract must have an ETH balance of at least `value`.
* - the called Solidity function must be `payable`.
*/
function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
if (address(this).balance < value) {
revert AddressInsufficientBalance(address(this));
}
(bool success, bytes memory returndata) = target.call{value: value}(data);
return verifyCallResultFromTarget(target, success, returndata);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but performing a static call.
*/
function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
(bool success, bytes memory returndata) = target.staticcall(data);
return verifyCallResultFromTarget(target, success, returndata);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but performing a delegate call.
*/
function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
(bool success, bytes memory returndata) = target.delegatecall(data);
return verifyCallResultFromTarget(target, success, returndata);
}
/**
* @dev Tool to verify that a low level call to smart-contract was successful, and reverts if the target
* was not a contract or bubbling up the revert reason (falling back to {FailedInnerCall}) in case of an
* unsuccessful call.
*/
function verifyCallResultFromTarget(
address target,
bool success,
bytes memory returndata
) internal view returns (bytes memory) {
if (!success) {
_revert(returndata);
} else {
// only check if target is a contract if the call was successful and the return data is empty
// otherwise we already know that it was a contract
if (returndata.length == 0 && target.code.length == 0) {
revert AddressEmptyCode(target);
}
return returndata;
}
}
/**
* @dev Tool to verify that a low level call was successful, and reverts if it wasn't, either by bubbling the
* revert reason or with a default {FailedInnerCall} error.
*/
function verifyCallResult(bool success, bytes memory returndata) internal pure returns (bytes memory) {
if (!success) {
_revert(returndata);
} else {
return returndata;
}
}
/**
* @dev Reverts with returndata if present. Otherwise reverts with {FailedInnerCall}.
*/
function _revert(bytes memory returndata) private pure {
// Look for revert reason and bubble it up if present
if (returndata.length > 0) {
// The easiest way to bubble the revert reason is using memory via assembly
/// @solidity memory-safe-assembly
assembly {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert FailedInnerCall();
}
}
}
Context.sol 28 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.1) (utils/Context.sol)
pragma solidity ^0.8.20;
/**
* @dev Provides information about the current execution context, including the
* sender of the transaction and its data. While these are generally available
* via msg.sender and msg.data, they should not be accessed in such a direct
* manner, since when dealing with meta-transactions the account sending and
* paying for execution may not be the actual sender (as far as an application
* is concerned).
*
* This contract is only required for intermediate, library-like contracts.
*/
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
function _contextSuffixLength() internal view virtual returns (uint256) {
return 0;
}
}
IERC165.sol 25 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (utils/introspection/IERC165.sol)
pragma solidity ^0.8.20;
/**
* @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);
}
DateTime.sol 378 lines
// SPDX-License-Identifier: MIT
pragma solidity 0.8.26;
// ----------------------------------------------------------------------------
// BokkyPooBah's DateTime Library v1.01
//
// A gas-efficient Solidity date and time library
//
// https://github.com/bokkypoobah/BokkyPooBahsDateTimeLibrary
//
// Tested date range 1970/01/01 to 2345/12/31
//
// Conventions:
// Unit | Range | Notes
// :-------- |:-------------:|:-----
// timestamp | >= 0 | Unix timestamp, number of seconds since 1970/01/01 00:00:00 UTC
// year | 1970 ... 2345 |
// month | 1 ... 12 |
// day | 1 ... 31 |
// hour | 0 ... 23 |
// minute | 0 ... 59 |
// second | 0 ... 59 |
// dayOfWeek | 1 ... 7 | 1 = Monday, ..., 7 = Sunday
//
//
// Enjoy. (c) BokkyPooBah / Bok Consulting Pty Ltd 2018-2019. The MIT Licence.
// ----------------------------------------------------------------------------
library DateTime {
uint256 private constant SECONDS_PER_DAY = 24 * 60 * 60;
uint256 private constant SECONDS_PER_HOUR = 60 * 60;
uint256 private constant SECONDS_PER_MINUTE = 60;
int256 private constant OFFSET19700101 = 2440588;
uint256 private constant DOW_MON = 1;
uint256 private constant DOW_TUE = 2;
uint256 private constant DOW_WED = 3;
uint256 private constant DOW_THU = 4;
uint256 private constant DOW_FRI = 5;
uint256 private constant DOW_SAT = 6;
uint256 private constant DOW_SUN = 7;
// ------------------------------------------------------------------------
// Calculate the number of days from 1970/01/01 to year/month/day using
// the date conversion algorithm from
// https://aa.usno.navy.mil/faq/JD_formula.html
// and subtracting the offset 2440588 so that 1970/01/01 is day 0
//
// days = day
// - 32075
// + 1461 * (year + 4800 + (month - 14) / 12) / 4
// + 367 * (month - 2 - (month - 14) / 12 * 12) / 12
// - 3 * ((year + 4900 + (month - 14) / 12) / 100) / 4
// - offset
// ------------------------------------------------------------------------
function _daysFromDate(uint256 year, uint256 month, uint256 day) internal pure returns (uint256 _days) {
// solhint-disable-next-line custom-errors, gas-custom-errors
require(year >= 1970, "year no earlier than 1970");
int256 _year = int256(year);
int256 _month = int256(month);
int256 _day = int256(day);
int256 __days = _day -
32075 +
(1461 * (_year + 4800 + (_month - 14) / 12)) /
4 +
(367 * (_month - 2 - ((_month - 14) / 12) * 12)) /
12 -
(3 * ((_year + 4900 + (_month - 14) / 12) / 100)) /
4 -
OFFSET19700101;
_days = uint256(__days);
}
// ------------------------------------------------------------------------
// Calculate year/month/day from the number of days since 1970/01/01 using
// the date conversion algorithm from
// http://aa.usno.navy.mil/faq/docs/JD_Formula.php
// and adding the offset 2440588 so that 1970/01/01 is day 0
//
// int L = days + 68569 + offset
// int N = 4 * L / 146097
// L = L - (146097 * N + 3) / 4
// year = 4000 * (L + 1) / 1461001
// L = L - 1461 * year / 4 + 31
// month = 80 * L / 2447
// dd = L - 2447 * month / 80
// L = month / 11
// month = month + 2 - 12 * L
// year = 100 * (N - 49) + year + L
// ------------------------------------------------------------------------
function _daysToDate(uint256 _days) internal pure returns (uint256 year, uint256 month, uint256 day) {
int256 __days = int256(_days);
// solhint-disable-next-line var-name-mixedcase
int256 L = __days + 68569 + OFFSET19700101;
// solhint-disable-next-line var-name-mixedcase
int256 N = (4 * L) / 146097;
L = L - (146097 * N + 3) / 4;
int256 _year = (4000 * (L + 1)) / 1461001;
L = L - (1461 * _year) / 4 + 31;
int256 _month = (80 * L) / 2447;
int256 _day = L - (2447 * _month) / 80;
L = _month / 11;
_month = _month + 2 - 12 * L;
_year = 100 * (N - 49) + _year + L;
year = uint256(_year);
month = uint256(_month);
day = uint256(_day);
}
function timestampFromDate(uint256 year, uint256 month, uint256 day) internal pure returns (uint256 timestamp) {
timestamp = _daysFromDate(year, month, day) * SECONDS_PER_DAY;
}
function timestampFromDateTime(
uint256 year,
uint256 month,
uint256 day,
uint256 hour,
uint256 minute,
uint256 second
) internal pure returns (uint256 timestamp) {
timestamp =
_daysFromDate(year, month, day) *
SECONDS_PER_DAY +
hour *
SECONDS_PER_HOUR +
minute *
SECONDS_PER_MINUTE +
second;
}
function timestampToDate(uint256 timestamp) internal pure returns (uint256 year, uint256 month, uint256 day) {
(year, month, day) = _daysToDate(timestamp / SECONDS_PER_DAY);
}
function timestampToDateTime(
uint256 timestamp
) internal pure returns (uint256 year, uint256 month, uint256 day, uint256 hour, uint256 minute, uint256 second) {
(year, month, day) = _daysToDate(timestamp / SECONDS_PER_DAY);
uint256 secs = timestamp % SECONDS_PER_DAY;
hour = secs / SECONDS_PER_HOUR;
secs = secs % SECONDS_PER_HOUR;
minute = secs / SECONDS_PER_MINUTE;
second = secs % SECONDS_PER_MINUTE;
}
function isValidDate(uint256 year, uint256 month, uint256 day) internal pure returns (bool valid) {
if (year >= 1970 && month > 0 && month <= 12) {
uint256 daysInMonth = _getDaysInMonth(year, month);
if (day > 0 && day <= daysInMonth) {
valid = true;
}
}
}
function isValidDateTime(
uint256 year,
uint256 month,
uint256 day,
uint256 hour,
uint256 minute,
uint256 second
) internal pure returns (bool valid) {
if (isValidDate(year, month, day)) {
if (hour < 24 && minute < 60 && second < 60) {
valid = true;
}
}
}
function isLeapYear(uint256 timestamp) internal pure returns (bool leapYear) {
(uint256 year, , ) = _daysToDate(timestamp / SECONDS_PER_DAY);
leapYear = _isLeapYear(year);
}
function _isLeapYear(uint256 year) internal pure returns (bool leapYear) {
leapYear = ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
}
function isWeekDay(uint256 timestamp) internal pure returns (bool weekDay) {
weekDay = getDayOfWeek(timestamp) <= DOW_FRI;
}
function isWeekEnd(uint256 timestamp) internal pure returns (bool weekEnd) {
weekEnd = getDayOfWeek(timestamp) >= DOW_SAT;
}
function getDaysInMonth(uint256 timestamp) internal pure returns (uint256 daysInMonth) {
(uint256 year, uint256 month, ) = _daysToDate(timestamp / SECONDS_PER_DAY);
daysInMonth = _getDaysInMonth(year, month);
}
function _getDaysInMonth(uint256 year, uint256 month) internal pure returns (uint256 daysInMonth) {
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
daysInMonth = 31;
} else if (month != 2) {
daysInMonth = 30;
} else {
daysInMonth = _isLeapYear(year) ? 29 : 28;
}
}
// 1 = Monday, 7 = Sunday
function getDayOfWeek(uint256 timestamp) internal pure returns (uint256 dayOfWeek) {
uint256 _days = timestamp / SECONDS_PER_DAY;
dayOfWeek = ((_days + 3) % 7) + 1;
}
function getYear(uint256 timestamp) internal pure returns (uint256 year) {
(year, , ) = _daysToDate(timestamp / SECONDS_PER_DAY);
}
function getMonth(uint256 timestamp) internal pure returns (uint256 month) {
(, month, ) = _daysToDate(timestamp / SECONDS_PER_DAY);
}
function getDay(uint256 timestamp) internal pure returns (uint256 day) {
(, , day) = _daysToDate(timestamp / SECONDS_PER_DAY);
}
function getHour(uint256 timestamp) internal pure returns (uint256 hour) {
uint256 secs = timestamp % SECONDS_PER_DAY;
hour = secs / SECONDS_PER_HOUR;
}
function getMinute(uint256 timestamp) internal pure returns (uint256 minute) {
uint256 secs = timestamp % SECONDS_PER_HOUR;
minute = secs / SECONDS_PER_MINUTE;
}
function getSecond(uint256 timestamp) internal pure returns (uint256 second) {
second = timestamp % SECONDS_PER_MINUTE;
}
function addYears(uint256 timestamp, uint256 _years) internal pure returns (uint256 newTimestamp) {
(uint256 year, uint256 month, uint256 day) = _daysToDate(timestamp / SECONDS_PER_DAY);
year += _years;
uint256 daysInMonth = _getDaysInMonth(year, month);
if (day > daysInMonth) {
day = daysInMonth;
}
newTimestamp = _daysFromDate(year, month, day) * SECONDS_PER_DAY + (timestamp % SECONDS_PER_DAY);
// solhint-disable-next-line custom-errors, gas-custom-errors
require(newTimestamp >= timestamp, "invalid timestamp");
}
function addMonths(uint256 timestamp, uint256 _months) internal pure returns (uint256 newTimestamp) {
(uint256 year, uint256 month, uint256 day) = _daysToDate(timestamp / SECONDS_PER_DAY);
month += _months;
year += (month - 1) / 12;
month = ((month - 1) % 12) + 1;
uint256 daysInMonth = _getDaysInMonth(year, month);
if (day > daysInMonth) {
day = daysInMonth;
}
newTimestamp = _daysFromDate(year, month, day) * SECONDS_PER_DAY + (timestamp % SECONDS_PER_DAY);
// solhint-disable-next-line custom-errors, gas-custom-errors
require(newTimestamp >= timestamp, "invalid timestamp");
}
function addDays(uint256 timestamp, uint256 _days) internal pure returns (uint256 newTimestamp) {
newTimestamp = timestamp + _days * SECONDS_PER_DAY;
// solhint-disable-next-line custom-errors, gas-custom-errors
require(newTimestamp >= timestamp, "invalid timestamp");
}
function addHours(uint256 timestamp, uint256 _hours) internal pure returns (uint256 newTimestamp) {
newTimestamp = timestamp + _hours * SECONDS_PER_HOUR;
// solhint-disable-next-line custom-errors, gas-custom-errors
require(newTimestamp >= timestamp, "invalid timestamp");
}
function addMinutes(uint256 timestamp, uint256 _minutes) internal pure returns (uint256 newTimestamp) {
newTimestamp = timestamp + _minutes * SECONDS_PER_MINUTE;
// solhint-disable-next-line custom-errors, gas-custom-errors
require(newTimestamp >= timestamp, "invalid timestamp");
}
function addSeconds(uint256 timestamp, uint256 _seconds) internal pure returns (uint256 newTimestamp) {
newTimestamp = timestamp + _seconds;
// solhint-disable-next-line custom-errors, gas-custom-errors
require(newTimestamp >= timestamp, "invalid timestamp");
}
function subYears(uint256 timestamp, uint256 _years) internal pure returns (uint256 newTimestamp) {
(uint256 year, uint256 month, uint256 day) = _daysToDate(timestamp / SECONDS_PER_DAY);
year -= _years;
uint256 daysInMonth = _getDaysInMonth(year, month);
if (day > daysInMonth) {
day = daysInMonth;
}
newTimestamp = _daysFromDate(year, month, day) * SECONDS_PER_DAY + (timestamp % SECONDS_PER_DAY);
// solhint-disable-next-line custom-errors, gas-custom-errors
require(newTimestamp <= timestamp, "invalid timestamp");
}
function subMonths(uint256 timestamp, uint256 _months) internal pure returns (uint256 newTimestamp) {
(uint256 year, uint256 month, uint256 day) = _daysToDate(timestamp / SECONDS_PER_DAY);
uint256 yearMonth = year * 12 + (month - 1) - _months;
year = yearMonth / 12;
month = (yearMonth % 12) + 1;
uint256 daysInMonth = _getDaysInMonth(year, month);
if (day > daysInMonth) {
day = daysInMonth;
}
newTimestamp = _daysFromDate(year, month, day) * SECONDS_PER_DAY + (timestamp % SECONDS_PER_DAY);
// solhint-disable-next-line custom-errors, gas-custom-errors
require(newTimestamp <= timestamp, "invalid timestamp");
}
function subDays(uint256 timestamp, uint256 _days) internal pure returns (uint256 newTimestamp) {
newTimestamp = timestamp - _days * SECONDS_PER_DAY;
// solhint-disable-next-line custom-errors, gas-custom-errors
require(newTimestamp <= timestamp, "invalid timestamp");
}
function subHours(uint256 timestamp, uint256 _hours) internal pure returns (uint256 newTimestamp) {
newTimestamp = timestamp - _hours * SECONDS_PER_HOUR;
// solhint-disable-next-line custom-errors, gas-custom-errors
require(newTimestamp <= timestamp, "invalid timestamp");
}
function subMinutes(uint256 timestamp, uint256 _minutes) internal pure returns (uint256 newTimestamp) {
newTimestamp = timestamp - _minutes * SECONDS_PER_MINUTE;
// solhint-disable-next-line custom-errors, gas-custom-errors
require(newTimestamp <= timestamp, "invalid timestamp");
}
function subSeconds(uint256 timestamp, uint256 _seconds) internal pure returns (uint256 newTimestamp) {
newTimestamp = timestamp - _seconds;
// solhint-disable-next-line custom-errors, gas-custom-errors
require(newTimestamp <= timestamp, "invalid timestamp");
}
function diffYears(uint256 fromTimestamp, uint256 toTimestamp) internal pure returns (uint256 _years) {
// solhint-disable-next-line custom-errors, gas-custom-errors
require(fromTimestamp <= toTimestamp, "invalid timestamp");
(uint256 fromYear, , ) = _daysToDate(fromTimestamp / SECONDS_PER_DAY);
(uint256 toYear, , ) = _daysToDate(toTimestamp / SECONDS_PER_DAY);
_years = toYear - fromYear;
}
function diffMonths(uint256 fromTimestamp, uint256 toTimestamp) internal pure returns (uint256 _months) {
// solhint-disable-next-line custom-errors, gas-custom-errors
require(fromTimestamp <= toTimestamp, "invalid timestamp");
(uint256 fromYear, uint256 fromMonth, ) = _daysToDate(fromTimestamp / SECONDS_PER_DAY);
(uint256 toYear, uint256 toMonth, ) = _daysToDate(toTimestamp / SECONDS_PER_DAY);
_months = toYear * 12 + toMonth - fromYear * 12 - fromMonth;
}
function diffDays(uint256 fromTimestamp, uint256 toTimestamp) internal pure returns (uint256 _days) {
// solhint-disable-next-line custom-errors, gas-custom-errors
require(fromTimestamp <= toTimestamp, "invalid timestamp");
_days = (toTimestamp - fromTimestamp) / SECONDS_PER_DAY;
}
function diffHours(uint256 fromTimestamp, uint256 toTimestamp) internal pure returns (uint256 _hours) {
// solhint-disable-next-line custom-errors, gas-custom-errors
require(fromTimestamp <= toTimestamp, "invalid timestamp");
_hours = (toTimestamp - fromTimestamp) / SECONDS_PER_HOUR;
}
function diffMinutes(uint256 fromTimestamp, uint256 toTimestamp) internal pure returns (uint256 _minutes) {
// solhint-disable-next-line custom-errors, gas-custom-errors
require(fromTimestamp <= toTimestamp, "invalid timestamp");
_minutes = (toTimestamp - fromTimestamp) / SECONDS_PER_MINUTE;
}
function diffSeconds(uint256 fromTimestamp, uint256 toTimestamp) internal pure returns (uint256 _seconds) {
// solhint-disable-next-line custom-errors, gas-custom-errors
require(fromTimestamp <= toTimestamp, "invalid timestamp");
_seconds = toTimestamp - fromTimestamp;
}
}
TimeLock.sol 276 lines
// SPDX-License-Identifier: MIT
// Compatible with OpenZeppelin Contracts ^5.0.0
pragma solidity 0.8.26;
import {Ownable, Ownable2Step} from "./libs/@openzeppelin/contracts/access/Ownable2Step.sol";
import {IERC20, SafeERC20} from "./libs/@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {DateTime} from "./libs/DateTime.sol";
import {IAlphastarter1155} from "./interfaces/IAlphastarter1155.sol";
contract TimeLock is Ownable2Step {
using DateTime for uint256;
using SafeERC20 for IERC20;
event Started(uint256 startAt);
event Claimed(address indexed beneficiary, address token, uint256 amount);
event Rescued(address token, uint256 amount);
error AlreadyInitiated();
error InsufficientBalance();
error InvalidAddress();
error InvalidTimestamp();
error InvalidTotalVotingPower();
error MustNotZero();
error NotInitiated();
/**
* @notice the address of the ERC1155 token contract for deciding the stake of time-locked tokens
*/
address public immutable ALPHASTARTER1155;
/**
* @notice the project id of the AlphaStarter projects
*/
uint256 public immutable PROJECT_ID;
/**
* @notice the timestamp of the snapshot for the alphaStarter1155's voting power
*/
uint256 public immutable SNAPSHOT_AT;
/**
* @notice the address of the ERC20 token
*/
address public immutable ERC20TOKEN;
/**
* @notice the amount of tokens to be distributed initially when the contract is started
*/
uint256 public immutable INITIAL_RELEASE_AMOUNT;
/**
* @notice the amount of tokens to be distributed along with the vesting schedule
*/
uint256 public immutable SCHEDULED_VESTING_AMOUNT;
/**
* @notice the total amount of tokens to be distributed including the initial release and the vesting schedule
*/
uint256 public immutable TOTAL_AMOUNT;
/**
* @notice the start timestamp of the cliff period and the release of initial released tokens
*/
uint256 public immutable START_AT;
/**
* @notice the cliff period in months
*/
uint256 public immutable CLIFF_MONTH;
/**
* @notice the vesting period in months
*/
uint256 public immutable VESTING_MONTH;
/**
* @notice the amount of tokens claimed by each beneficiary
*/
mapping(address => uint256) public claimed;
/**
* @notice the total amount of tokens claimed by all beneficiaries
*/
uint256 public totalClaimed;
/**
* @notice the initiation status of the contract
*/
bool public initiated;
modifier onlyInitiated() {
if (!initiated) {
revert NotInitiated();
}
_;
}
constructor(
address initialOwner,
address alphaStarter1155,
uint256 projectId,
uint256 snapshotAt,
address erc20Token,
uint256 initialReleaseAmount,
uint256 scheduledVestingAmount,
uint256 startAt,
uint256 cliffMonth,
uint256 vestingMonth
) Ownable(initialOwner) {
if (alphaStarter1155 == address(0) || erc20Token == address(0)) {
revert InvalidAddress();
}
// total voting power must be greater than 0 at the snapshot
// it must be confirmed because burn 1155 tokens will decrease the total voting power
// if the total voting power is 0, it means no one can claim the locked tokens
if (IAlphastarter1155(alphaStarter1155).getPastTotalSupply(projectId, snapshotAt) == 0) {
revert InvalidTotalVotingPower();
}
// vesting month must be at least 1 month
// total amount must be greater than 0
if (vestingMonth == 0 || initialReleaseAmount == 0 || scheduledVestingAmount == 0) {
revert MustNotZero();
}
ALPHASTARTER1155 = alphaStarter1155;
PROJECT_ID = projectId;
SNAPSHOT_AT = snapshotAt;
ERC20TOKEN = erc20Token;
INITIAL_RELEASE_AMOUNT = initialReleaseAmount;
SCHEDULED_VESTING_AMOUNT = scheduledVestingAmount;
TOTAL_AMOUNT = initialReleaseAmount + scheduledVestingAmount;
START_AT = startAt;
CLIFF_MONTH = cliffMonth;
VESTING_MONTH = vestingMonth;
}
/**
* @notice initiate the contract by transferring the tokens to the contract
*/
function initiate() external onlyOwner {
if (initiated) {
revert AlreadyInitiated();
}
emit Started(START_AT);
initiated = true;
IERC20(ERC20TOKEN).safeTransferFrom(msg.sender, address(this), TOTAL_AMOUNT);
}
/**
* @notice get the estimated unlock dates
*/
function getEstimatedUnlockTimestamps() public view returns (uint256[] memory) {
uint256[] memory unlockTimestamps = new uint256[](VESTING_MONTH);
for (uint256 i = 0; i < VESTING_MONTH; i++) {
uint256 timestamp = START_AT.addMonths(CLIFF_MONTH).addMonths(i);
unlockTimestamps[i] = timestamp;
}
return unlockTimestamps;
}
/**
* @notice get the unlock amount at the given timestamp
*/
function getUnlockedAmountTotalAt(uint256 timestamp) public view returns (uint256) {
if (timestamp < START_AT) {
// before start
return 0;
} else if (timestamp < START_AT.addMonths(CLIFF_MONTH)) {
// after time lock starts and before cliff ends
return INITIAL_RELEASE_AMOUNT;
} else if (timestamp >= START_AT.addMonths(CLIFF_MONTH).addMonths(VESTING_MONTH)) {
// completed
return TOTAL_AMOUNT;
}
// in vesting period
uint256 months = _getFullMonthsBetween(START_AT.addMonths(CLIFF_MONTH), timestamp);
return INITIAL_RELEASE_AMOUNT + (SCHEDULED_VESTING_AMOUNT * months) / VESTING_MONTH;
}
/**
* @notice get the unlock amount at the given timestamp for the beneficiary
*/
function getUnlockedAmountAt(uint256 timestamp, address beneficiary) public view returns (uint256) {
uint256 unlockedAmount = getUnlockedAmountTotalAt(timestamp);
uint256 totalVotePower = IAlphastarter1155(ALPHASTARTER1155).getPastTotalSupply(PROJECT_ID, SNAPSHOT_AT);
uint256 votePower = IAlphastarter1155(ALPHASTARTER1155).getPastVotes(beneficiary, PROJECT_ID, SNAPSHOT_AT);
return (unlockedAmount * votePower) / totalVotePower;
}
/**
* @notice get the unlock amount at current block timestamp
*/
function getUnlockedAmountTotal() public view returns (uint256) {
return getUnlockedAmountTotalAt(block.timestamp);
}
/**
* @notice get the unlock amount at current block timestamp for the beneficiary
*/
function getUnlockedAmount(address beneficiary) public view returns (uint256) {
return getUnlockedAmountAt(block.timestamp, beneficiary);
}
/**
* @notice get the token unlocked at the given timestamp for the beneficiary
*/
function claim(address beneficiary) external onlyInitiated {
uint256 unlocked = getUnlockedAmount(beneficiary);
if (claimed[beneficiary] >= unlocked) {
return;
}
uint256 claimable = unlocked - claimed[beneficiary];
emit Claimed(beneficiary, ERC20TOKEN, claimable);
claimed[beneficiary] += claimable;
totalClaimed += claimable;
IERC20(ERC20TOKEN).safeTransfer(beneficiary, claimable);
}
/**
* @notice save locked token just in case
*/
function rescue(address targetToken) external onlyOwner {
uint256 amount = IERC20(targetToken).balanceOf(address(this));
if (targetToken == ERC20TOKEN && initiated) {
// if initiated, the contract should have the total amount of tokens
// or should have the amount of tokens that are not claimed yet and the claimed amount
if (amount + totalClaimed < TOTAL_AMOUNT) {
// this can't happen and shouldn't happen.
revert InsufficientBalance();
}
amount = amount + totalClaimed - TOTAL_AMOUNT;
}
if (amount > 0) {
emit Rescued(targetToken, amount);
IERC20(targetToken).safeTransfer(owner(), amount);
}
}
/**
* @dev get the number of full months between two timestamps
*/
function _getFullMonthsBetween(uint256 fromTimestamp, uint256 toTimestamp) internal pure returns (uint256) {
if (fromTimestamp > toTimestamp) {
// this can't happen
revert InvalidTimestamp();
}
uint256 monthDifference = fromTimestamp.diffMonths(toTimestamp);
// if adding the monthDifference to fromTimestamp exceeds toTimestamp, reduce by one more
if (fromTimestamp.addMonths(monthDifference) > toTimestamp) {
monthDifference -= 1;
}
// unlock will happen at the start of the month
return monthDifference + 1;
}
}
Read Contract
ALPHASTARTER1155 0x99d90c38 → address
CLIFF_MONTH 0x25b0fedc → uint256
ERC20TOKEN 0xd44641af → address
INITIAL_RELEASE_AMOUNT 0x53add661 → uint256
PROJECT_ID 0x56539f39 → uint256
SCHEDULED_VESTING_AMOUNT 0x8218df62 → uint256
SNAPSHOT_AT 0x227f02d3 → uint256
START_AT 0xbf7b766d → uint256
TOTAL_AMOUNT 0xa2d7f5e3 → uint256
VESTING_MONTH 0xaa6e52f0 → uint256
claimed 0xc884ef83 → uint256
getEstimatedUnlockTimestamps 0xf73410f2 → uint256[]
getUnlockedAmount 0x420d4a02 → uint256
getUnlockedAmountAt 0x603d13eb → uint256
getUnlockedAmountTotal 0x09836ef5 → uint256
getUnlockedAmountTotalAt 0xe3e4b15a → uint256
initiated 0x9f118536 → bool
owner 0x8da5cb5b → address
pendingOwner 0xe30c3978 → address
totalClaimed 0xd54ad2a1 → uint256
Write Contract 6 functions
These functions modify contract state and require a wallet transaction to execute.
acceptOwnership 0x79ba5097
No parameters
claim 0x1e83409a
address beneficiary
initiate 0x66aa56c5
No parameters
renounceOwnership 0x715018a6
No parameters
rescue 0x839006f2
address targetToken
transferOwnership 0xf2fde38b
address newOwner
Recent Transactions
No transactions found for this address