Appearance
Token Ledgers Implementation Using Skuset Contract
Token Ledgers are managed on the platform using Skuset
.
Overview
The Skuset
contract is an implementation of an ERC-1155 multi-token standard, designed for managing Skucode-backed tokens within a Skuset. It supports attachments, exchange of ERC-20 tokens, and advanced minting/burning capabilities, with an emphasis on security, including pauseable operations and re-entrance protection.
Imports
ERC1155
solidityimport {ERC1155} from "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
Standard implementation of the ERC1155 multi-token standard from OpenZeppelin.
Ownable
solidityimport {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
Provides a basic access control mechanism, allowing for ownership management.
Pausable
solidityimport {Pausable} from "@openzeppelin/contracts/utils/Pausable.sol";
Allows the contract to be paused and unpaused, providing a safety mechanism during emergencies.
IERC20
solidityimport {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
Interface for ERC20 tokens, allowing interactions with other ERC20 contracts.
ReentrancyGuard
solidityimport {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
Prevents reentrant calls to functions, enhancing security against certain types of attacks.
IERC20Receiver
solidityimport {IERC20Receiver} from "./skucode.sol";
Interface for contracts that want to handle receiving ERC20 tokens.
ud
solidityimport {ud} from "@prb/math/src/UD60x18.sol";
Provides fixed-point arithmetic operations using the UD60x18 type.
Enum: SkusetType
Represents the type of Skuset.
Regular
: Standard Skuset type.Locked
: Restricts some operations to only the owner.
State Variables
skusetType
:
(SkusetType) — Defines the Skuset's operational type, eitherRegular
orLocked
.whitelistedSkucodeHashes
:
(mapping(bytes32 => bool)) — Stores the whitelisted bytecode hashes of Skucode contracts for security purposes.attachments
:
(mapping(string => address)) — Tracks attachments by storing a CID linked to the sender's address.exchangeInfo
:
(mapping(uint256 => mapping(uint256 => ExchangeInfo))) — Maintains exchange information between token IDs, storing depositor details and exchange ratios.
Struct: ExchangeInfo
Stores information about token exchange.
depositor
: (address) — Address of the user holding the tokens for exchange.exchangeRatio
: (uint256) — Ratio used to determine exchange amounts between tokens.
Constructor
Initializes the Skuset
contract with a specified owner, metadata URI, and Skuset type.
solidity
constructor(
address _initialOwner,
string memory _erc1155uri,
SkusetType _skusetType
)
- Parameters:
_initialOwner
: Address of the contract's initial owner._erc1155uri
: URI for the ERC-1155 metadata._skusetType
: Type of Skuset (Regular
orLocked
).
Events
AttachmentAdded
:
Triggered when a new attachment is added.- Parameters:
cid
(string),submitter
(address)
- Parameters:
ERC20Deposited
:
Triggered when ERC-20 tokens are deposited.- Parameters:
sender
(address),token
(address),value
(uint256)
- Parameters:
ERC20Withdrawn
:
Triggered when ERC-20 tokens are withdrawn.- Parameters:
sender
(address),token
(address),value
(uint256),to
(address)
- Parameters:
ExchangeTokensDeposited
:
Triggered when tokens are deposited for an exchange.- Parameters:
fromSkucode
(uint256),toSkucode
(uint256),user
(address),value
(uint256),exchangeRatio
(uint256)
- Parameters:
TokensExchanged
:
Triggered when tokens are successfully exchanged.- Parameters:
fromSkucode
(uint256),toSkucode
(uint256),user
(address),value
(uint256),exchangeRatio
(uint256)
- Parameters:
Public Functions
onReceiveERC20
Handles receipt of ERC-20 tokens and mints equivalent ERC-1155 tokens.solidityfunction onReceiveERC20(address from, uint256 value) external returns (bytes4)
withdraw
Burns ERC-1155 tokens and withdraws the corresponding ERC-20 tokens.solidityfunction withdraw(uint256 id, uint256 value, address to) external
addAttachment
Adds a new attachment.solidityfunction addAttachment(string calldata cid) external
exchangeDeposit
Deposits tokens for exchange.solidityfunction exchangeDeposit(address fromSkucode, address toSkucode, uint256 exchangeRatio, uint256 value) external
exchangeWithdraw
Handles the exchange of tokens and withdrawal in a single step.solidityfunction exchangeWithdraw(address fromSkucode, address toSkucode, uint256 value) external
mint
Mints ERC-1155 tokens to a specified address.solidityfunction mint(address to, uint256 id, uint256 value, bytes calldata data) public
mintBatch
Mints multiple ERC-1155 tokens in a batch.solidityfunction mintBatch(address to, uint256[] calldata ids, uint256[] calldata values, bytes calldata data) external
burn
Burns a specified amount of ERC-1155 tokens.solidityfunction burn(address from, uint256 id, uint256 value) external
burnBatch
Burns multiple ERC-1155 tokens in a batch.solidityfunction burnBatch(address from, uint256[] calldata ids, uint256[] calldata values) external
pause
/unpause
Pauses or unpauses the contract (onlyOwner).solidityfunction pause() external function unpause() external
Modifiers
onlyOwnerWhenLocked
:
Restricts operations to the owner when theSkuset
type isLocked
.
Internal Utility Functions
_getSkucodeTokenId
Computes a token ID based on the ERC-20 token address.solidityfunction _getSkucodeTokenId(address token) internal pure returns (uint256)
_uint256ToAddress
Converts auint256
to an Ethereum address.solidityfunction _uint256ToAddress(uint256 value) internal pure returns (address)
Custom Errors
InvalidTokenId
: Thrown when an invalid token ID is used.TransferFailed
: Thrown if a token transfer operation fails.UnsafeSkucode
: Thrown if the Skucode's bytecode is not whitelisted.AttachmentAlreadyAdded
: Thrown if the same attachment has been previously added.ExchangeInfoAlreadyAdded
: Thrown if exchange info already exists for a given token pair.ExchangeInfoUnavailable
: Thrown if exchange information is not available for the specified token pair.InsufficientExchangeTokens
: Thrown if there aren't enough tokens available for an exchange.InsufficientExchangeAllowance
: Thrown if the allowance for token transfers is insufficient.ContractSenderUnauthorized
: Thrown if a contract, instead of an externally owned account (EOA), attempts to interact.UnauthorizedOnLockedSkuset
: Thrown if a restricted operation is attempted on aLocked
Skuset.