Description:
Multi-signature wallet contract requiring multiple confirmations for transaction execution.
Blockchain: Ethereum
Source Code: View Code On The Blockchain
Solidity Source Code:
{{
"language": "Solidity",
"sources": {
"src/GlyphBotsArtifacts.sol": {
"content": "// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
import {ERC1155} from "solady/tokens/ERC1155.sol";
import {Ownable} from "solady/auth/Ownable.sol";
import {EIP712} from "solady/utils/EIP712.sol";
import {SignatureCheckerLib} from "solady/utils/SignatureCheckerLib.sol";
import {SafeTransferLib} from "solady/utils/SafeTransferLib.sol";
import {ReentrancyGuard} from "solady/utils/ReentrancyGuard.sol";
/// @title GlyphBots Artifacts - ERC-1155 Contract
/// @notice Mints unique artifacts with payment splitting and EIP-712 authorization
/// @author Ryan Ghods
contract GlyphBotsArtifacts is ERC1155, Ownable, EIP712, ReentrancyGuard {
using SafeTransferLib for address;
// ---------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------
uint256 internal constant BASIS_POINTS_DENOMINATOR = 10_000;
// Typehashes computed via keccak256 of the canonical type strings.
bytes32 internal constant MINT_PAYMENT_SHARE_TYPEHASH = keccak256("MintPaymentShare(address recipient,uint16 bps)");
bytes32 internal constant MINT_ETH_TYPEHASH = keccak256(
"MintEth(address to,uint256 quantity,string base64JsonUri,MintPaymentShare[] shares,uint256 ethAmount,uint256 nonce,uint256 expirationTimestamp)MintPaymentShare(address recipient,uint16 bps)"
);
bytes32 internal constant MINT_ERC20_TYPEHASH = keccak256(
"MintErc20(address to,uint256 quantity,string base64JsonUri,MintPaymentShare[] shares,address erc20,uint256 erc20Amount,uint256 nonce,uint256 expirationTimestamp)MintPaymentShare(address recipient,uint16 bps)"
);
// ---------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------
/// @notice Payment share structure for mint-time payment splitting
struct MintPaymentShare {
address recipient; // cannot be zero address
uint16 bps; // share in basis points; sum(shares.bps) <= 10_000
}
// Contract state
address private _signerAddress;
uint256 private _nextTokenId;
// ---------------------------------------------------------------------
// Storage
// ---------------------------------------------------------------------
mapping(uint256 => string) private _tokenUriBase64;
mapping(uint256 => uint256) public totalSupply; // supply per id
mapping(bytes32 => bool) public usedHashes; // digest -> consumed
// ---------------------------------------------------------------------
// Events
// ---------------------------------------------------------------------
/// @notice Emitted when a new artifact is minted with ETH payment
event MintedEth(uint256 indexed tokenId, address indexed to, uint256 quantity, uint256 value);
/// @notice Emitted when a new artifact is minted with ERC-20 payment
event MintedErc20(
uint256 indexed tokenId, address indexed to, uint256 quantity, address indexed erc20, uint256 amount
);
/// @notice Emitted when a payment share is distributed
event PaymentSharePaid(uint256 indexed tokenId, address indexed recipient, address indexed asset, uint256 amount);
/// @notice Emitted when the signer address is updated
event SignerUpdated(address indexed oldSigner, address indexed newSigner);
/// @notice Emitted when ETH is withdrawn
event Withdrawn(address indexed to, uint256 amount);
/// @notice Emitted when ERC-20 tokens are withdrawn
event WithdrawnErc20(address indexed erc20, address indexed to, uint256 amount);
// ---------------------------------------------------------------------
// Errors
// ---------------------------------------------------------------------
/// @dev Signature failed verification against the configured signer.
error InvalidSignature();
error SignatureExpired(uint256 currentTime, uint256 expirationTime);
error InvalidRecipient();
error InvalidQuantity();
error SharesBpsTooHigh(uint256 totalBps);
error EthValueMismatch(uint256 expected, uint256 actual);
error Erc20TransferFailed(address token, uint256 amount);
error ZeroAddress();
error InsufficientPayment(uint256 required, uint256 provided);
error InvalidBasisPoints(uint256 bps);
/// @dev The signature digest has already been used (replay).
error SignatureAlreadyUsed(bytes32 digest);
error InvalidTokenId(uint256 tokenId);
error TransferFailed();
error InvalidArrayLength();
error DuplicateRecipient();
error InvalidURI(uint256 maxLength);
// ---------------------------------------------------------------------
// Constructor & Metadata
// ---------------------------------------------------------------------
constructor(address signer_) {
_initializeOwner(msg.sender);
_signerAddress = signer_;
_nextTokenId = 1;
}
function name() public pure returns (string memory) {
return "GlyphBots Artifacts";
}
function symbol() public pure returns (string memory) {
return unicode"⦿⦿◈";
}
function signerAddress() public view returns (address) {
return _signerAddress;
}
function nextTokenId() public view returns (uint256) {
return _nextTokenId;
}
/// @notice Returns the total number of minted token IDs (i.e. last assigned id - 0)
function totalMintedIds() external view returns (uint256) {
return _nextTokenId - 1; // starts at 1
}
// ---------------------------------------------------------------------
// EIP-712 Domain
// ---------------------------------------------------------------------
function _domainNameAndVersion() internal pure override returns (string memory, string memory) {
return ("GlyphBots Artifacts", "1");
}
// ---------------------------------------------------------------------
// ERC-1155 Metadata
// ---------------------------------------------------------------------
function uri(uint256 id) public view override returns (string memory) {
_validateTokenId(id);
return string(abi.encodePacked("data:application/json;base64,", _tokenUriBase64[id]));
}
// ---------------------------------------------------------------------
// External API
// ---------------------------------------------------------------------
/// @notice Mints a new artifact with ETH payment and payment splitting
function mint(
address to,
uint256 quantity,
string calldata base64JsonUri,
MintPaymentShare[] calldata shares,
uint256 ethAmount,
uint256 nonce,
uint256 expirationTimestamp,
bytes calldata signature
) external payable nonReentrant {
_validateRecipient(to);
_validateQuantity(quantity);
_validateExpiration(expirationTimestamp);
_validateURI(base64JsonUri);
_validatePaymentShares(shares);
if (msg.value != ethAmount) revert EthValueMismatch(ethAmount, msg.value);
// Build the EIP-712 digest with the provided parameters.
bytes32 digest = _hashTypedData(
keccak256(
abi.encode(
MINT_ETH_TYPEHASH,
to,
quantity,
keccak256(bytes(base64JsonUri)),
_hashSharesArray(shares),
ethAmount,
nonce,
expirationTimestamp
)
)
);
_enforceFreshAndConsumeSignature(digest, nonce, signature);
uint256 tokenId = _nextTokenId;
unchecked {
_nextTokenId = tokenId + 1;
}
_tokenUriBase64[tokenId] = base64JsonUri;
totalSupply[tokenId] = quantity;
_mint(to, tokenId, quantity, "");
// Payment splitting (if any)
if (ethAmount > 0) {
_splitEth(tokenId, ethAmount, shares);
}
emit MintedEth(tokenId, to, quantity, ethAmount);
}
/// @notice Mints a new artifact with ERC-20 payment and payment splitting
function mintWithErc20(
address to,
uint256 quantity,
string calldata base64JsonUri,
MintPaymentShare[] calldata shares,
address erc20,
uint256 erc20Amount,
uint256 nonce,
uint256 expirationTimestamp,
bytes calldata signature
) external nonReentrant {
_validateRecipient(to);
_validateQuantity(quantity);
_validateExpiration(expirationTimestamp);
_validateURI(base64JsonUri);
_validatePaymentShares(shares);
if (erc20Amount > 0 && erc20 == address(0)) revert ZeroAddress();
bytes32 digest = _hashTypedData(
keccak256(
abi.encode(
MINT_ERC20_TYPEHASH,
to,
quantity,
keccak256(bytes(base64JsonUri)),
_hashSharesArray(shares),
erc20,
erc20Amount,
nonce,
expirationTimestamp
)
)
);
_enforceFreshAndConsumeSignature(digest, nonce, signature);
if (erc20Amount > 0) {
// Pull the ERC-20 into this contract first.
bool ok = SafeTransferLib.trySafeTransferFrom(erc20, msg.sender, address(this), erc20Amount);
if (!ok) revert Erc20TransferFailed(erc20, erc20Amount);
}
uint256 tokenId = _nextTokenId;
unchecked {
_nextTokenId = tokenId + 1;
}
_tokenUriBase64[tokenId] = base64JsonUri;
totalSupply[tokenId] = quantity;
_mint(to, tokenId, quantity, "");
if (erc20Amount > 0) {
_splitErc20(tokenId, erc20, erc20Amount, shares);
}
emit MintedErc20(tokenId, to, quantity, erc20, erc20Amount);
}
/// @notice Batch mints multiple artifacts with ETH payments
function batchMint(
address[] calldata recipients,
uint256[] calldata quantities,
string[] calldata base58URIs,
MintPaymentShare[][] calldata sharesArray,
uint256[] calldata ethAmounts,
uint256[] calldata nonces,
uint256[] calldata expirationTimestamps,
bytes[] calldata signatures
) external payable nonReentrant {
uint256 n = recipients.length;
if (
n == 0 || n != quantities.length || n != base58URIs.length || n != sharesArray.length
|| n != ethAmounts.length || n != nonces.length || n != expirationTimestamps.length
|| n != signatures.length
) revert InvalidArrayLength();
uint256 total;
for (uint256 i = 0; i < n; i++) {
total += ethAmounts[i];
}
if (msg.value != total) revert EthValueMismatch(total, msg.value);
for (uint256 i = 0; i < n; i++) {
// Perform a single ETH mint using the provided slice of arrays.
_batchMintSingleEth(
recipients[i],
quantities[i],
base58URIs[i],
sharesArray[i],
ethAmounts[i],
nonces[i],
expirationTimestamps[i],
signatures[i]
);
}
}
/// @notice Batch mints multiple artifacts with ERC-20 payments
function batchMintWithErc20(
address[] calldata recipients,
uint256[] calldata quantities,
string[] calldata base58URIs,
MintPaymentShare[][] calldata sharesArray,
address[] calldata erc20Tokens,
uint256[] calldata erc20Amounts,
uint256[] calldata nonces,
uint256[] calldata expirationTimestamps,
bytes[] calldata signatures
) external nonReentrant {
uint256 n = recipients.length;
if (
n == 0 || n != quantities.length || n != base58URIs.length || n != sharesArray.length
|| n != erc20Tokens.length || n != erc20Amounts.length || n != nonces.length
|| n != expirationTimestamps.length || n != signatures.length
) revert InvalidArrayLength();
for (uint256 i = 0; i < n; i++) {
_batchMintSingleErc20(
recipients[i],
quantities[i],
base58URIs[i],
sharesArray[i],
erc20Tokens[i],
erc20Amounts[i],
nonces[i],
expirationTimestamps[i],
signatures[i]
);
}
}
/// @notice Withdraws all ETH balance to the owner
function withdraw() external onlyOwner nonReentrant {
address payable to = payable(owner());
uint256 amount = address(this).balance;
if (amount == 0) {
emit Withdrawn(to, 0);
return;
}
// Use force send to support recipients without payable fallback.
SafeTransferLib.forceSafeTransferAllETH(to);
emit Withdrawn(to, amount);
}
/// @notice Withdraws all ERC-20 balance to the owner
function withdrawErc20(address erc20) external onlyOwner nonReentrant {
address payable to = payable(owner());
uint256 amount = SafeTransferLib.safeTransferAll(erc20, to);
emit WithdrawnErc20(erc20, to, amount);
}
/// @notice Updates the authorized signer address
function setSignerAddress(address newSigner) external onlyOwner {
address old = _signerAddress;
_signerAddress = newSigner; // allow zero to effectively pause authorization
emit SignerUpdated(old, newSigner);
}
// ---------------------------------------------------------------------
// Internal: Single Mint Helpers for Batch
// ---------------------------------------------------------------------
function _batchMintSingleEth(
address to,
uint256 quantity,
string calldata base64JsonUri,
MintPaymentShare[] calldata shares,
uint256 ethAmount,
uint256 nonce,
uint256 expirationTimestamp,
bytes calldata signature
) internal {
_validateRecipient(to);
_validateQuantity(quantity);
_validateExpiration(expirationTimestamp);
_validateURI(base64JsonUri);
_validatePaymentShares(shares);
bytes32 digest = _hashTypedData(
keccak256(
abi.encode(
MINT_ETH_TYPEHASH,
to,
quantity,
keccak256(bytes(base64JsonUri)),
_hashSharesArray(shares),
ethAmount,
nonce,
expirationTimestamp
)
)
);
_enforceFreshAndConsumeSignature(digest, nonce, signature);
uint256 tokenId = _nextTokenId;
unchecked {
_nextTokenId = tokenId + 1;
}
_tokenUriBase64[tokenId] = base64JsonUri;
totalSupply[tokenId] = quantity;
_mint(to, tokenId, quantity, "");
if (ethAmount > 0) _splitEth(tokenId, ethAmount, shares);
emit MintedEth(tokenId, to, quantity, ethAmount);
}
function _batchMintSingleErc20(
address to,
uint256 quantity,
string calldata base64JsonUri,
MintPaymentShare[] calldata shares,
address erc20,
uint256 erc20Amount,
uint256 nonce,
uint256 expirationTimestamp,
bytes calldata signature
) internal {
_validateRecipient(to);
_validateQuantity(quantity);
_validateExpiration(expirationTimestamp);
_validateURI(base64JsonUri);
_validatePaymentShares(shares);
if (erc20Amount > 0 && erc20 == address(0)) revert ZeroAddress();
bytes32 digest = _hashTypedData(
keccak256(
abi.encode(
MINT_ERC20_TYPEHASH,
to,
quantity,
keccak256(bytes(base64JsonUri)),
_hashSharesArray(shares),
erc20,
erc20Amount,
nonce,
expirationTimestamp
)
)
);
_enforceFreshAndConsumeSignature(digest, nonce, signature);
if (erc20Amount > 0) {
bool ok = SafeTransferLib.trySafeTransferFrom(erc20, msg.sender, address(this), erc20Amount);
if (!ok) revert Erc20TransferFailed(erc20, erc20Amount);
}
uint256 tokenId = _nextTokenId;
unchecked {
_nextTokenId = tokenId + 1;
}
_tokenUriBase64[tokenId] = base64JsonUri;
totalSupply[tokenId] = quantity;
_mint(to, tokenId, quantity, "");
if (erc20Amount > 0) _splitErc20(tokenId, erc20, erc20Amount, shares);
emit MintedErc20(tokenId, to, quantity, erc20, erc20Amount);
}
// ---------------------------------------------------------------------
// Internal: Validation & Sig/Nonce
// ---------------------------------------------------------------------
function _validateRecipient(address to) internal pure {
if (to == address(0)) revert InvalidRecipient();
}
function _validateQuantity(uint256 quantity) internal pure {
if (quantity == 0) revert InvalidQuantity();
}
function _validateTokenId(uint256 tokenId) internal view {
if (tokenId == 0 || tokenId >= _nextTokenId) revert InvalidTokenId(tokenId);
}
function _validateExpiration(uint256 expirationTimestamp) internal view {
if (block.timestamp > expirationTimestamp) revert SignatureExpired(block.timestamp, expirationTimestamp);
}
function _enforceFreshAndConsumeSignature(bytes32 digest, uint256 nonce, bytes calldata signature) internal {
if (usedHashes[digest]) revert SignatureAlreadyUsed(digest);
bool ok = SignatureCheckerLib.isValidSignatureNowCalldata(_signerAddress, digest, signature);
if (!ok) revert InvalidSignature();
usedHashes[digest] = true;
}
function _validatePaymentShares(MintPaymentShare[] calldata shares) internal pure returns (uint256 totalBps) {
if (shares.length == 0) return 0;
for (uint256 i = 0; i < shares.length; i++) {
MintPaymentShare calldata s = shares[i];
if (s.recipient == address(0)) revert InvalidRecipient();
if (s.bps == 0 || s.bps > BASIS_POINTS_DENOMINATOR) revert InvalidBasisPoints(s.bps);
// duplicate check O(n^2) acceptable for small arrays
for (uint256 j = 0; j < i; j++) {
if (shares[j].recipient == s.recipient) revert DuplicateRecipient();
}
totalBps += s.bps;
}
if (totalBps > BASIS_POINTS_DENOMINATOR) revert SharesBpsTooHigh(totalBps);
}
function _validateURI(string calldata s_) internal pure {
bytes memory b = bytes(s_);
if (b.length == 0 || b.length > 2048) revert InvalidURI(2048);
}
// ---------------------------------------------------------------------
// Internal: Payment Splitters
// ---------------------------------------------------------------------
function _splitEth(uint256 tokenId, uint256 totalAmount, MintPaymentShare[] calldata shares) internal {
if (totalAmount == 0) return;
uint256 distributed;
for (uint256 i = 0; i < shares.length; i++) {
uint256 amt = (totalAmount * shares[i].bps) / BASIS_POINTS_DENOMINATOR;
if (amt > 0) {
SafeTransferLib.safeTransferETH(shares[i].recipient, amt);
emit PaymentSharePaid(tokenId, shares[i].recipient, address(0), amt);
distributed += amt;
}
}
// Remainder stays in contract for owner withdrawal.
}
function _splitErc20(uint256 tokenId, address erc20, uint256 totalAmount, MintPaymentShare[] calldata shares)
internal
{
if (totalAmount == 0) return;
uint256 distributed;
for (uint256 i = 0; i < shares.length; i++) {
uint256 amt = (totalAmount * shares[i].bps) / BASIS_POINTS_DENOMINATOR;
if (amt > 0) {
SafeTransferLib.safeTransfer(erc20, shares[i].recipient, amt);
emit PaymentSharePaid(tokenId, shares[i].recipient, erc20, amt);
distributed += amt;
}
}
// Remainder stays in contract for owner withdrawal.
}
// ---------------------------------------------------------------------
// Internal: EIP-712 Helpers
// ---------------------------------------------------------------------
function _hashSharesArray(MintPaymentShare[] calldata shares) internal pure returns (bytes32) {
if (shares.length == 0) return keccak256("");
bytes32[] memory hashes = new bytes32[](shares.length);
for (uint256 i = 0; i < shares.length; i++) {
hashes[i] = keccak256(abi.encode(MINT_PAYMENT_SHARE_TYPEHASH, shares[i].recipient, shares[i].bps));
}
return keccak256(abi.encodePacked(hashes));
}
}
"
},
"lib/solady/src/tokens/ERC1155.sol": {
"content": "// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
/// @notice Simple ERC1155 implementation.
/// @author Solady (https://github.com/vectorized/solady/blob/main/src/tokens/ERC1155.sol)
/// @author Modified from Solmate (https://github.com/transmissions11/solmate/blob/main/src/tokens/ERC1155.sol)
/// @author Modified from OpenZeppelin (https://github.com/OpenZeppelin/openzeppelin-contracts/tree/master/contracts/token/ERC1155/ERC1155.sol)
///
/// @dev Note:
/// - The ERC1155 standard allows for self-approvals.
/// For performance, this implementation WILL NOT revert for such actions.
/// Please add any checks with overrides if desired.
/// - The transfer functions use the identity precompile (0x4)
/// to copy memory internally.
///
/// If you are overriding:
/// - Make sure all variables written to storage are properly cleaned
// (e.g. the bool value for `isApprovedForAll` MUST be either 1 or 0 under the hood).
/// - Check that the overridden function is actually used in the function you want to
/// change the behavior of. Much of the code has been manually inlined for performance.
abstract contract ERC1155 {
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CUSTOM ERRORS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev The lengths of the input arrays are not the same.
error ArrayLengthsMismatch();
/// @dev Cannot mint or transfer to the zero address.
error TransferToZeroAddress();
/// @dev The recipient's balance has overflowed.
error AccountBalanceOverflow();
/// @dev Insufficient balance.
error InsufficientBalance();
/// @dev Only the token owner or an approved account can manage the tokens.
error NotOwnerNorApproved();
/// @dev Cannot safely transfer to a contract that does not implement
/// the ERC1155Receiver interface.
error TransferToNonERC1155ReceiverImplementer();
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* EVENTS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Emitted when `amount` of token `id` is transferred
/// from `from` to `to` by `operator`.
event TransferSingle(
address indexed operator,
address indexed from,
address indexed to,
uint256 id,
uint256 amount
);
/// @dev Emitted when `amounts` of token `ids` are transferred
/// from `from` to `to` by `operator`.
event TransferBatch(
address indexed operator,
address indexed from,
address indexed to,
uint256[] ids,
uint256[] amounts
);
/// @dev Emitted when `owner` enables or disables `operator` to manage all of their tokens.
event ApprovalForAll(address indexed owner, address indexed operator, bool isApproved);
/// @dev Emitted when the Uniform Resource Identifier (URI) for token `id`
/// is updated to `value`. This event is not used in the base contract.
/// You may need to emit this event depending on your URI logic.
///
/// See: https://eips.ethereum.org/EIPS/eip-1155#metadata
event URI(string value, uint256 indexed id);
/// @dev `keccak256(bytes("TransferSingle(address,address,address,uint256,uint256)"))`.
uint256 private constant _TRANSFER_SINGLE_EVENT_SIGNATURE =
0xc3d58168c5ae7397731d063d5bbf3d657854427343f4c083240f7aacaa2d0f62;
/// @dev `keccak256(bytes("TransferBatch(address,address,address,uint256[],uint256[])"))`.
uint256 private constant _TRANSFER_BATCH_EVENT_SIGNATURE =
0x4a39dc06d4c0dbc64b70af90fd698a233a518aa5d07e595d983b8c0526c8f7fb;
/// @dev `keccak256(bytes("ApprovalForAll(address,address,bool)"))`.
uint256 private constant _APPROVAL_FOR_ALL_EVENT_SIGNATURE =
0x17307eab39ab6107e8899845ad3d59bd9653f200f220920489ca2b5937696c31;
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* STORAGE */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev The `ownerSlotSeed` of a given owner is given by.
/// ```
/// let ownerSlotSeed := or(_ERC1155_MASTER_SLOT_SEED, shl(96, owner))
/// ```
///
/// The balance slot of `owner` is given by.
/// ```
/// mstore(0x20, ownerSlotSeed)
/// mstore(0x00, id)
/// let balanceSlot := keccak256(0x00, 0x40)
/// ```
///
/// The operator approval slot of `owner` is given by.
/// ```
/// mstore(0x20, ownerSlotSeed)
/// mstore(0x00, operator)
/// let operatorApprovalSlot := keccak256(0x0c, 0x34)
/// ```
uint256 private constant _ERC1155_MASTER_SLOT_SEED = 0x9a31110384e0b0c9;
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* ERC1155 METADATA */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Returns the URI for token `id`.
///
/// You can either return the same templated URI for all token IDs,
/// (e.g. "https://example.com/api/{id}.json"),
/// or return a unique URI for each `id`.
///
/// See: https://eips.ethereum.org/EIPS/eip-1155#metadata
function uri(uint256 id) public view virtual returns (string memory);
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* ERC1155 */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Returns the amount of `id` owned by `owner`.
function balanceOf(address owner, uint256 id) public view virtual returns (uint256 result) {
/// @solidity memory-safe-assembly
assembly {
mstore(0x20, _ERC1155_MASTER_SLOT_SEED)
mstore(0x14, owner)
mstore(0x00, id)
result := sload(keccak256(0x00, 0x40))
}
}
/// @dev Returns whether `operator` is approved to manage the tokens of `owner`.
function isApprovedForAll(address owner, address operator)
public
view
virtual
returns (bool result)
{
/// @solidity memory-safe-assembly
assembly {
mstore(0x20, _ERC1155_MASTER_SLOT_SEED)
mstore(0x14, owner)
mstore(0x00, operator)
result := sload(keccak256(0x0c, 0x34))
}
}
/// @dev Sets whether `operator` is approved to manage the tokens of the caller.
///
/// Emits a {ApprovalForAll} event.
function setApprovalForAll(address operator, bool isApproved) public virtual {
/// @solidity memory-safe-assembly
assembly {
// Convert to 0 or 1.
isApproved := iszero(iszero(isApproved))
// Update the `isApproved` for (`msg.sender`, `operator`).
mstore(0x20, _ERC1155_MASTER_SLOT_SEED)
mstore(0x14, caller())
mstore(0x00, operator)
sstore(keccak256(0x0c, 0x34), isApproved)
// Emit the {ApprovalForAll} event.
mstore(0x00, isApproved)
// forgefmt: disable-next-line
log3(0x00, 0x20, _APPROVAL_FOR_ALL_EVENT_SIGNATURE, caller(), shr(96, shl(96, operator)))
}
}
/// @dev Transfers `amount` of `id` from `from` to `to`.
///
/// Requirements:
/// - `to` cannot be the zero address.
/// - `from` must have at least `amount` of `id`.
/// - If the caller is not `from`,
/// it must be approved to manage the tokens of `from`.
/// - If `to` refers to a smart contract, it must implement
/// {ERC1155-onERC1155Received}, which is called upon a batch transfer.
///
/// Emits a {TransferSingle} event.
function safeTransferFrom(
address from,
address to,
uint256 id,
uint256 amount,
bytes calldata data
) public virtual {
if (_useBeforeTokenTransfer()) {
_beforeTokenTransfer(from, to, _single(id), _single(amount), data);
}
/// @solidity memory-safe-assembly
assembly {
let fromSlotSeed := or(_ERC1155_MASTER_SLOT_SEED, shl(96, from))
let toSlotSeed := or(_ERC1155_MASTER_SLOT_SEED, shl(96, to))
mstore(0x20, fromSlotSeed)
// Clear the upper 96 bits.
from := shr(96, fromSlotSeed)
to := shr(96, toSlotSeed)
// Revert if `to` is the zero address.
if iszero(to) {
mstore(0x00, 0xea553b34) // `TransferToZeroAddress()`.
revert(0x1c, 0x04)
}
// If the caller is not `from`, do the authorization check.
if iszero(eq(caller(), from)) {
mstore(0x00, caller())
if iszero(sload(keccak256(0x0c, 0x34))) {
mstore(0x00, 0x4b6e7f18) // `NotOwnerNorApproved()`.
revert(0x1c, 0x04)
}
}
// Subtract and store the updated balance of `from`.
{
mstore(0x00, id)
let fromBalanceSlot := keccak256(0x00, 0x40)
let fromBalance := sload(fromBalanceSlot)
if gt(amount, fromBalance) {
mstore(0x00, 0xf4d678b8) // `InsufficientBalance()`.
revert(0x1c, 0x04)
}
sstore(fromBalanceSlot, sub(fromBalance, amount))
}
// Increase and store the updated balance of `to`.
{
mstore(0x20, toSlotSeed)
let toBalanceSlot := keccak256(0x00, 0x40)
let toBalanceBefore := sload(toBalanceSlot)
let toBalanceAfter := add(toBalanceBefore, amount)
if lt(toBalanceAfter, toBalanceBefore) {
mstore(0x00, 0x01336cea) // `AccountBalanceOverflow()`.
revert(0x1c, 0x04)
}
sstore(toBalanceSlot, toBalanceAfter)
}
// Emit a {TransferSingle} event.
mstore(0x20, amount)
log4(0x00, 0x40, _TRANSFER_SINGLE_EVENT_SIGNATURE, caller(), from, to)
}
if (_useAfterTokenTransfer()) {
_afterTokenTransfer(from, to, _single(id), _single(amount), data);
}
/// @solidity memory-safe-assembly
assembly {
// Do the {onERC1155Received} check if `to` is a smart contract.
if extcodesize(to) {
// Prepare the calldata.
let m := mload(0x40)
// `onERC1155Received(address,address,uint256,uint256,bytes)`.
mstore(m, 0xf23a6e61)
mstore(add(m, 0x20), caller())
mstore(add(m, 0x40), from)
mstore(add(m, 0x60), id)
mstore(add(m, 0x80), amount)
mstore(add(m, 0xa0), 0xa0)
mstore(add(m, 0xc0), data.length)
calldatacopy(add(m, 0xe0), data.offset, data.length)
// Revert if the call reverts.
if iszero(call(gas(), to, 0, add(m, 0x1c), add(0xc4, data.length), m, 0x20)) {
if returndatasize() {
// Bubble up the revert if the call reverts.
returndatacopy(m, 0x00, returndatasize())
revert(m, returndatasize())
}
}
// Load the returndata and compare it with the function selector.
if iszero(eq(mload(m), shl(224, 0xf23a6e61))) {
mstore(0x00, 0x9c05499b) // `TransferToNonERC1155ReceiverImplementer()`.
revert(0x1c, 0x04)
}
}
}
}
/// @dev Transfers `amounts` of `ids` from `from` to `to`.
///
/// Requirements:
/// - `to` cannot be the zero address.
/// - `from` must have at least `amount` of `id`.
/// - `ids` and `amounts` must have the same length.
/// - If the caller is not `from`,
/// it must be approved to manage the tokens of `from`.
/// - If `to` refers to a smart contract, it must implement
/// {ERC1155-onERC1155BatchReceived}, which is called upon a batch transfer.
///
/// Emits a {TransferBatch} event.
function safeBatchTransferFrom(
address from,
address to,
uint256[] calldata ids,
uint256[] calldata amounts,
bytes calldata data
) public virtual {
if (_useBeforeTokenTransfer()) {
_beforeTokenTransfer(from, to, ids, amounts, data);
}
/// @solidity memory-safe-assembly
assembly {
if iszero(eq(ids.length, amounts.length)) {
mstore(0x00, 0x3b800a46) // `ArrayLengthsMismatch()`.
revert(0x1c, 0x04)
}
let fromSlotSeed := or(_ERC1155_MASTER_SLOT_SEED, shl(96, from))
let toSlotSeed := or(_ERC1155_MASTER_SLOT_SEED, shl(96, to))
mstore(0x20, fromSlotSeed)
// Clear the upper 96 bits.
from := shr(96, fromSlotSeed)
to := shr(96, toSlotSeed)
// Revert if `to` is the zero address.
if iszero(to) {
mstore(0x00, 0xea553b34) // `TransferToZeroAddress()`.
revert(0x1c, 0x04)
}
// If the caller is not `from`, do the authorization check.
if iszero(eq(caller(), from)) {
mstore(0x00, caller())
if iszero(sload(keccak256(0x0c, 0x34))) {
mstore(0x00, 0x4b6e7f18) // `NotOwnerNorApproved()`.
revert(0x1c, 0x04)
}
}
// Loop through all the `ids` and update the balances.
{
for { let i := shl(5, ids.length) } i {} {
i := sub(i, 0x20)
let amount := calldataload(add(amounts.offset, i))
// Subtract and store the updated balance of `from`.
{
mstore(0x20, fromSlotSeed)
mstore(0x00, calldataload(add(ids.offset, i)))
let fromBalanceSlot := keccak256(0x00, 0x40)
let fromBalance := sload(fromBalanceSlot)
if gt(amount, fromBalance) {
mstore(0x00, 0xf4d678b8) // `InsufficientBalance()`.
revert(0x1c, 0x04)
}
sstore(fromBalanceSlot, sub(fromBalance, amount))
}
// Increase and store the updated balance of `to`.
{
mstore(0x20, toSlotSeed)
let toBalanceSlot := keccak256(0x00, 0x40)
let toBalanceBefore := sload(toBalanceSlot)
let toBalanceAfter := add(toBalanceBefore, amount)
if lt(toBalanceAfter, toBalanceBefore) {
mstore(0x00, 0x01336cea) // `AccountBalanceOverflow()`.
revert(0x1c, 0x04)
}
sstore(toBalanceSlot, toBalanceAfter)
}
}
}
// Emit a {TransferBatch} event.
{
let m := mload(0x40)
// Copy the `ids`.
mstore(m, 0x40)
let n := shl(5, ids.length)
mstore(add(m, 0x40), ids.length)
calldatacopy(add(m, 0x60), ids.offset, n)
// Copy the `amounts`.
mstore(add(m, 0x20), add(0x60, n))
let o := add(add(m, n), 0x60)
mstore(o, ids.length)
calldatacopy(add(o, 0x20), amounts.offset, n)
// Do the emit.
log4(m, add(add(n, n), 0x80), _TRANSFER_BATCH_EVENT_SIGNATURE, caller(), from, to)
}
}
if (_useAfterTokenTransfer()) {
_afterTokenTransferCalldata(from, to, ids, amounts, data);
}
/// @solidity memory-safe-assembly
assembly {
// Do the {onERC1155BatchReceived} check if `to` is a smart contract.
if extcodesize(to) {
mstore(0x00, to) // Cache `to` to prevent stack too deep.
let m := mload(0x40)
// Prepare the calldata.
// `onERC1155BatchReceived(address,address,uint256[],uint256[],bytes)`.
mstore(m, 0xbc197c81)
mstore(add(m, 0x20), caller())
mstore(add(m, 0x40), from)
// Copy the `ids`.
mstore(add(m, 0x60), 0xa0)
let n := shl(5, ids.length)
mstore(add(m, 0xc0), ids.length)
calldatacopy(add(m, 0xe0), ids.offset, n)
// Copy the `amounts`.
mstore(add(m, 0x80), add(0xc0, n))
let o := add(add(m, n), 0xe0)
mstore(o, ids.length)
calldatacopy(add(o, 0x20), amounts.offset, n)
// Copy the `data`.
mstore(add(m, 0xa0), add(add(0xe0, n), n))
o := add(add(o, n), 0x20)
mstore(o, data.length)
calldatacopy(add(o, 0x20), data.offset, data.length)
let nAll := add(0x104, add(data.length, add(n, n)))
// Revert if the call reverts.
if iszero(call(gas(), mload(0x00), 0, add(mload(0x40), 0x1c), nAll, m, 0x20)) {
if returndatasize() {
// Bubble up the revert if the call reverts.
returndatacopy(m, 0x00, returndatasize())
revert(m, returndatasize())
}
}
// Load the returndata and compare it with the function selector.
if iszero(eq(mload(m), shl(224, 0xbc197c81))) {
mstore(0x00, 0x9c05499b) // `TransferToNonERC1155ReceiverImplementer()`.
revert(0x1c, 0x04)
}
}
}
}
/// @dev Returns the amounts of `ids` for `owners.
///
/// Requirements:
/// - `owners` and `ids` must have the same length.
function balanceOfBatch(address[] calldata owners, uint256[] calldata ids)
public
view
virtual
returns (uint256[] memory balances)
{
/// @solidity memory-safe-assembly
assembly {
if iszero(eq(ids.length, owners.length)) {
mstore(0x00, 0x3b800a46) // `ArrayLengthsMismatch()`.
revert(0x1c, 0x04)
}
balances := mload(0x40)
mstore(balances, ids.length)
let o := add(balances, 0x20)
let i := shl(5, ids.length)
mstore(0x40, add(i, o))
// Loop through all the `ids` and load the balances.
for {} i {} {
i := sub(i, 0x20)
let owner := calldataload(add(owners.offset, i))
mstore(0x20, or(_ERC1155_MASTER_SLOT_SEED, shl(96, owner)))
mstore(0x00, calldataload(add(ids.offset, i)))
mstore(add(o, i), sload(keccak256(0x00, 0x40)))
}
}
}
/// @dev Returns true if this contract implements the interface defined by `interfaceId`.
/// See: https://eips.ethereum.org/EIPS/eip-165
/// This function call must use less than 30000 gas.
function supportsInterface(bytes4 interfaceId) public view virtual returns (bool result) {
/// @solidity memory-safe-assembly
assembly {
let s := shr(224, interfaceId)
// ERC165: 0x01ffc9a7, ERC1155: 0xd9b67a26, ERC1155MetadataURI: 0x0e89341c.
result := or(or(eq(s, 0x01ffc9a7), eq(s, 0xd9b67a26)), eq(s, 0x0e89341c))
}
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* INTERNAL MINT FUNCTIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Mints `amount` of `id` to `to`.
///
/// Requirements:
/// - `to` cannot be the zero address.
/// - If `to` refers to a smart contract, it must implement
/// {ERC1155-onERC1155Received}, which is called upon a batch transfer.
///
/// Emits a {TransferSingle} event.
function _mint(address to, uint256 id, uint256 amount, bytes memory data) internal virtual {
if (_useBeforeTokenTransfer()) {
_beforeTokenTransfer(address(0), to, _single(id), _single(amount), data);
}
/// @solidity memory-safe-assembly
assembly {
let to_ := shl(96, to)
// Revert if `to` is the zero address.
if iszero(to_) {
mstore(0x00, 0xea553b34) // `TransferToZeroAddress()`.
revert(0x1c, 0x04)
}
// Increase and store the updated balance of `to`.
{
mstore(0x20, _ERC1155_MASTER_SLOT_SEED)
mstore(0x14, to)
mstore(0x00, id)
let toBalanceSlot := keccak256(0x00, 0x40)
let toBalanceBefore := sload(toBalanceSlot)
let toBalanceAfter := add(toBalanceBefore, amount)
if lt(toBalanceAfter, toBalanceBefore) {
mstore(0x00, 0x01336cea) // `AccountBalanceOverflow()`.
revert(0x1c, 0x04)
}
sstore(toBalanceSlot, toBalanceAfter)
}
// Emit a {TransferSingle} event.
mstore(0x20, amount)
log4(0x00, 0x40, _TRANSFER_SINGLE_EVENT_SIGNATURE, caller(), 0, shr(96, to_))
}
if (_useAfterTokenTransfer()) {
_afterTokenTransfer(address(0), to, _single(id), _single(amount), data);
}
if (_hasCode(to)) _checkOnERC1155Received(address(0), to, id, amount, data);
}
/// @dev Mints `amounts` of `ids` to `to`.
///
/// Requirements:
/// - `to` cannot be the zero address.
/// - `ids` and `amounts` must have the same length.
/// - If `to` refers to a smart contract, it must implement
/// {ERC1155-onERC1155BatchReceived}, which is called upon a batch transfer.
///
/// Emits a {TransferBatch} event.
function _batchMint(
address to,
uint256[] memory ids,
uint256[] memory amounts,
bytes memory data
) internal virtual {
if (_useBeforeTokenTransfer()) {
_beforeTokenTransfer(address(0), to, ids, amounts, data);
}
/// @solidity memory-safe-assembly
assembly {
if iszero(eq(mload(ids), mload(amounts))) {
mstore(0x00, 0x3b800a46) // `ArrayLengthsMismatch()`.
revert(0x1c, 0x04)
}
let to_ := shl(96, to)
// Revert if `to` is the zero address.
if iszero(to_) {
mstore(0x00, 0xea553b34) // `TransferToZeroAddress()`.
revert(0x1c, 0x04)
}
// Loop through all the `ids` and update the balances.
{
mstore(0x20, or(_ERC1155_MASTER_SLOT_SEED, to_))
for { let i := shl(5, mload(ids)) } i { i := sub(i, 0x20) } {
let amount := mload(add(amounts, i))
// Increase and store the updated balance of `to`.
{
mstore(0x00, mload(add(ids, i)))
let toBalanceSlot := keccak256(0x00, 0x40)
let toBalanceBefore := sload(toBalanceSlot)
let toBalanceAfter := add(toBalanceBefore, amount)
if lt(toBalanceAfter, toBalanceBefore) {
mstore(0x00, 0x01336cea) // `AccountBalanceOverflow()`.
revert(0x1c, 0x04)
}
sstore(toBalanceSlot, toBalanceAfter)
}
}
}
// Emit a {TransferBatch} event.
{
let m := mload(0x40)
// Copy the `ids`.
mstore(m, 0x40)
let n := add(0x20, shl(5, mload(ids)))
let o := add(m, 0x40)
pop(staticcall(gas(), 4, ids, n, o, n))
// Copy the `amounts`.
mstore(add(m, 0x20), add(0x40, returndatasize()))
o := add(o, returndatasize())
n := add(0x20, shl(5, mload(amounts)))
pop(staticcall(gas(), 4, amounts, n, o, n))
n := sub(add(o, returndatasize()), m)
// Do the emit.
log4(m, n, _TRANSFER_BATCH_EVENT_SIGNATURE, caller(), 0, shr(96, to_))
}
}
if (_useAfterTokenTransfer()) {
_afterTokenTransfer(address(0), to, ids, amounts, data);
}
if (_hasCode(to)) _checkOnERC1155BatchReceived(address(0), to, ids, amounts, data);
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* INTERNAL BURN FUNCTIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Equivalent to `_burn(address(0), from, id, amount)`.
function _burn(address from, uint256 id, uint256 amount) internal virtual {
_burn(address(0), from, id, amount);
}
/// @dev Destroys `amount` of `id` from `from`.
///
/// Requirements:
/// - `from` must have at least `amount` of `id`.
/// - If `by` is not the zero address, it must be either `from`,
/// or approved to manage the tokens of `from`.
///
/// Emits a {TransferSingle} event.
function _burn(address by, address from, uint256 id, uint256 amount) internal virtual {
if (_useBeforeTokenTransfer()) {
_beforeTokenTransfer(from, address(0), _single(id), _single(amount), "");
}
/// @solidity memory-safe-assembly
assembly {
let from_ := shl(96, from)
mstore(0x20, or(_ERC1155_MASTER_SLOT_SEED, from_))
// If `by` is not the zero address, and not equal to `from`,
// check if it is approved to manage all the tokens of `from`.
if iszero(or(iszero(shl(96, by)), eq(shl(96, by), from_))) {
mstore(0x00, by)
if iszero(sload(keccak256(0x0c, 0x34))) {
mstore(0x00, 0x4b6e7f18) // `NotOwnerNorApproved()`.
revert(0x1c, 0x04)
}
}
// Decrease and store the updated balance of `from`.
{
mstore(0x00, id)
let fromBalanceSlot := keccak256(0x00, 0x40)
let fromBalance := sload(fromBalanceSlot)
if gt(amount, fromBalance) {
mstore(0x00, 0xf4d678b8) // `InsufficientBalance()`.
revert(0x1c, 0x04)
}
sstore(fromBalanceSlot, sub(fromBalance, amount))
}
// Emit a {TransferSingle} event.
mstore(0x20, amount)
log4(0x00, 0x40, _TRANSFER_SINGLE_EVENT_SIGNATURE, caller(), shr(96, from_), 0)
}
if (_useAfterTokenTransfer()) {
_afterTokenTransfer(from, address(0), _single(id), _single(amount), "");
}
}
/// @dev Equivalent to `_batchBurn(address(0), from, ids, amounts)`.
function _batchBurn(address from, uint256[] memory ids, uint256[] memory amounts)
internal
virtual
{
_batchBurn(address(0), from, ids, amounts);
}
/// @dev Destroys `amounts` of `ids` from `from`.
///
/// Requirements:
/// - `ids` and `amounts` must have the same length.
/// - `from` must have at least `amounts` of `ids`.
/// - If `by` is not the zero address, it must be either `from`,
/// or approved to manage the tokens of `from`.
///
/// Emits a {TransferBatch} event.
function _batchBurn(address by, address from, uint256[] memory ids, uint256[] memory amounts)
internal
virtual
{
if (_useBeforeTokenTransfer()) {
_beforeTokenTransfer(from, address(0), ids, amounts, "");
}
/// @solidity memory-safe-assembly
assembly {
if iszero(eq(mload(ids), mload(amounts))) {
mstore(0x00, 0x3b800a46) // `ArrayLengthsMismatch()`.
revert(0x1c, 0x04)
}
let from_ := shl(96, from)
mstore(0x20, or(_ERC1155_MASTER_SLOT_SEED, from_))
// If `by` is not the zero address, and not equal to `from`,
// check if it is approved to manage all the tokens of `from`.
let by_ := shl(96, by)
if iszero(or(iszero(by_), eq(by_, from_))) {
mstore(0x00, by)
if iszero(sload(keccak256(0x0c, 0x34))) {
mstore(0x00, 0x4b6e7f18) // `NotOwnerNorApproved()`.
revert(0x1c, 0x04)
}
}
// Loop through all the `ids` and update the balances.
{
for { let i := shl(5, mload(ids)) } i { i := sub(i, 0x20) } {
let amount := mload(add(amounts, i))
// Decrease and store the updated balance of `from`.
{
mstore(0x00, mload(add(ids, i)))
let fromBalanceSlot := keccak256(0x00, 0x40)
let fromBalance := sload(fromBalanceSlot)
if gt(amount, fromBalance) {
mstore(0x00, 0xf4d678b8) // `InsufficientBalance()`.
revert(0x1c, 0x04)
}
sstore(fromBalanceSlot, sub(fromBalance, amount))
}
}
}
// Emit a {TransferBatch} event.
{
let m := mload(0x40)
// Copy the `ids`.
mstore(m, 0x40)
let n := add(0x20, shl(5, mload(ids)))
let o := add(m, 0x40)
pop(staticcall(gas(), 4, ids, n, o, n))
// Copy the `amounts`.
mstore(add(m, 0x20), add(0x40, returndatasize()))
o := add(o, returndatasize())
n := add(0x20, shl(5, mload(amounts)))
pop(staticcall(gas(), 4, amounts, n, o, n))
n := sub(add(o, returndatasize()), m)
// Do the emit.
log4(m, n, _TRANSFER_BATCH_EVENT_SIGNATURE, caller(), shr(96, from_), 0)
}
}
if (_useAfterTokenTransfer()) {
_afterTokenTransfer(from, address(0), ids, amounts, "");
}
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* INTERNAL APPROVAL FUNCTIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Approve or remove the `operator` as an operator for `by`,
/// without authorization checks.
///
/// Emits a {ApprovalForAll} event.
function _setApprovalForAll(address by, address operator, bool isApproved) internal virtual {
/// @solidity memory-safe-assembly
assembly {
// Convert to 0 or 1.
isApproved := iszero(iszero(isApproved))
// Update the `isApproved` for (`by`, `operator`).
mstore(0x20, _ERC1155_MASTER_SLOT_SEED)
mstore(0x14, by)
mstore(0x00, operator)
sstore(keccak256(0x0c, 0x34), isApproved)
// Emit the {ApprovalForAll} event.
mstore(0x00, isApproved)
let m := shr(96, not(0))
log3(0x00, 0x20, _APPROVAL_FOR_ALL_EVENT_SIGNATURE, and(m, by), and(m, operator))
}
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* INTERNAL TRANSFER FUNCTIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Equivalent to `_safeTransfer(address(0), from, to, id, amount, data)`.
function _safeTransfer(address from, address to, uint256 id, uint256 amount, bytes memory data)
internal
virtual
{
_safeTransfer(address(0), from, to, id, amount, data);
}
/// @dev Transfers `amount` of `id` from `from` to `to`.
///
/// Requirements:
/// - `to` cannot be the zero address.
/// - `from` must have at least `amount` of `id`.
/// - If `by` is not the zero address, it must be either `from`,
/// or approved to manage the tokens of `from`.
/// - If `to` refers to a smart contract, it must implement
/// {ERC1155-onERC1155Received}, which is called upon a batch transfer.
///
/// Emits a {TransferSingle} event.
function _safeTransfer(
address by,
address from,
address to,
uint256 id,
uint256 amount,
bytes memory data
) internal virtual {
if (_useBeforeTokenTransfer()) {
_beforeTokenTransfer(from, to, _single(id), _single(amount), data);
}
/// @solidity memory-safe-assembly
assembly {
let from_ := shl(96, from)
let to_ := shl(96, to)
// Revert if `to` is the zero address.
if iszero(to_) {
mstore(0x00, 0xea553b34) // `TransferToZeroAddress()`.
revert(0x1c, 0x04)
}
mstore(0x20, or(_ERC1155_MASTER_SLOT_SEED, from_))
// If `by` is not the zero address, and not equal to `from`,
// check if it is approved to manage all the tokens of `from`.
let by_ := shl(96, by)
if iszero(or(iszero(by_), eq(by_, from_))) {
mstore(0x00, by)
if iszero(sload(keccak256(0x0c, 0x34))) {
mstore(0x00, 0x4b6e7f18) // `NotOwnerNorApproved()`.
revert(0x1c, 0x04)
}
}
// Subtract and store the updated balance of `from`.
{
mstore(0x00, id)
let fromBalanceSlot := keccak256(0x00, 0x40)
let fromBalance := sload(fromBalanceSlot)
if gt(amount, fromBalance) {
mstore(0x00, 0xf4d678b8) // `InsufficientBalance()`.
revert(0x1c, 0x04)
}
sstore(fromBalanceSlot, sub(fromBalance, amount))
}
// Increase and store the updated balance of `to`.
{
mstore(0x20, or(_ERC1155_MASTER_SLOT_SEED, to_))
let toBalanceSlot := keccak256(0x00, 0x40)
let toBalanceBefore := sload(toBalanceSlot)
let toBalanceAfter := add(toBalanceBefore, amount)
if lt(toBalanceAfter, toBalanceBefore) {
mstore(0x00, 0x01336cea) // `AccountBalanceOverflow()`.
revert(0x1c, 0x04)
}
sstore(toBalanceSlot, toBalanceAfter)
}
// Emit a {TransferSingle} event.
mstore(0x20, amount)
// forgefmt: disable-next-line
log4(0x00, 0x40, _TRANSFER_SINGLE_EVENT_SIGNATURE, caller(), shr(96, from_), shr(96, to_))
}
if (_useAfterTokenTransfer()) {
_afterTokenTransfer(from, to, _single(id), _single(amount), data);
}
if (_hasCode(to)) _checkOnERC1155Received(from, to, id, amount, data);
}
/// @dev Equivalent to `_safeBatchTransfer(address(0), from, to, ids, amounts, data)`.
function _safeBatchTransfer(
address from,
address to,
uint256[] memory ids,
uint256[] memory amounts,
bytes memory data
) internal virtual {
_safeBatchTransfer(address(0), from, to, ids, amounts, data);
}
/// @dev Transfers `amounts` of `ids` from `from` to `to`.
///
/// Requirements:
/// - `to` cannot be the zero address.
/// - `ids` and `amounts` must have the same length.
/// - `from` must have at least `amounts` of `ids`.
/// - If `by` is not the zero address, it must be either `from`,
/// or approved to manage the tokens of `from`.
/// - If `to` refers to a smart contract, it must implement
/// {ERC1155-onERC1155BatchReceived}, which is called upon a batch transfer.
///
/// Emits a {TransferBatch} event.
function _safeBatchTransfer(
address by,
address from,
address to,
uint256[] memory ids,
uint256[] memory amounts,
bytes memory data
) internal virtual {
if (_useBeforeTokenTransfer()) {
_beforeTokenTransfer(from, to, ids, amounts, data);
}
/// @solidity memory-safe-assembly
assembly {
if iszero(eq(mload(ids), mload(amounts))) {
mstore(0x00, 0x3b800a46) // `ArrayLengthsMismatch()`.
revert(0x1c, 0x04)
}
let from_ := shl(96, from)
let to_ := shl(96, to)
// Revert if `to` is the zero address.
if iszero(to_) {
mstore(0x00, 0xea553b34) // `TransferToZeroAddress()`.
revert(0x1c, 0x04)
}
let fromSlotSeed := or(_ERC1155_MASTER_SLOT_SEED, from_)
let toSlotSeed := or(_ERC1155_MASTER_SLOT_SEED, to_)
mstore(0x20, fromSlotSeed)
// If `by` is not the zero address, and not equal to `from`,
// check if it is approved to manage all the tokens of `from`.
let by_ := shl(96, by)
if iszero(or(iszero(by_), eq(by_, from_))) {
mstore(0x00, by)
if iszero(sload(keccak256(0x0c, 0x34))) {
mstore(0x00, 0x4b6e7f18) // `NotOwnerNorApproved()`.
revert(0x1c, 0x04)
}
}
// Loop through all the `ids` and update the balances.
{
for { let i := shl(5, mload(ids)) } i { i := sub(i, 0x20) } {
let amount := mload(add(amounts, i))
// Subtract and store the updated balance of `from`.
{
mstore(0x20, fromSlotSeed)
mstore(0x00, mload(add(ids, i)))
let fromBalanceSlot := keccak256(0x00, 0x40)
let fromBalance := sload(fromBalanceSlot)
if gt(amount, fromBalance) {
mstore(0x00, 0xf4d678b8) // `InsufficientBalance()`.
revert(0x1c, 0x04)
}
sstore(fromBalanceSlot, sub(fromBalance, amount))
}
// Increase and store the updated balance of `to`.
{
mstore(0x20, toSlotSeed)
let toBalanceSlot := keccak256(0x00, 0x40)
let toBalanceBefore := sload(toBalanceSlot)
let toBalanceAfter := add(toBalanceBefore, amount)
if lt(toBalanceAfter, toBalanceBefore) {
mstore(0x00, 0x01336cea) // `AccountBalanceOverflow()`.
revert(0x1c, 0x04)
}
sstore(toBalanceSlot, toBalanceAfter)
}
}
}
// Emit a {TransferBatch} event.
{
let m := mload(0x40)
// Copy the `ids`.
mstore(m, 0x40)
let n := add(0x20, shl(5, mload(ids)))
let o := add(m, 0x40)
pop(staticcall(gas(), 4, ids, n, o, n))
// Copy the `amounts`.
mstore(add(m, 0x20), add(0x40, returndatasize()))
o := add(o, returndatasize())
n := add(0x20, shl(5, mload(amounts)))
pop(staticcall(gas(), 4, amounts, n, o, n))
n := sub(add(o, returndatasize()), m)
// Do the emit.
log4(m, n, _TRANSFER_BATCH_EVENT_SIGNATURE, caller(), shr(96, from_), shr(96, to_))
}
}
if (_useAfterTokenTransfer()) {
_afterTokenTransfer(from, to, ids, amounts, data);
}
if (_hasCode(to)) _checkOnERC1155BatchReceived(from, to, ids, amounts, data);
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* HOOKS FOR OVERRIDING */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Override this function to return true if `_beforeTokenTransfer` is used.
/// This is to help the compiler avoid producing dead bytecode.
function _useBeforeTokenTransfer() internal view virtual returns (bool) {
return false;
}
/// @dev Hook that is called before any token transfer.
/// This includes minting and burning, as well as batched variants.
///
/// The same hook is called on both single and batched variants.
/// For single transfers, the length of the `id` and `amount` arrays are 1.
function _beforeTokenTransfer(
address from,
address to,
uint256[] memory ids,
uint256[] memo
Submitted on: 2025-10-12 11:22:20
Comments
Log in to comment.
No comments yet.