// SPDX-License-Identifier: MIT// OpenZeppelin Contracts v4.4.1 (utils/Context.sol)pragmasolidity ^0.8.0;/**
* @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.
*/abstractcontractContext{
function_msgSender() internalviewvirtualreturns (address) {
returnmsg.sender;
}
function_msgData() internalviewvirtualreturns (bytescalldata) {
returnmsg.data;
}
}
Contract Source Code
File 3 of 8: ERC404Plus.sol
//SPDX-License-Identifier: UNLICENSEDpragmasolidity ^0.8.0;import"./Ownable.sol";
import"./ERC721Receiver.sol";
abstractcontractERC404PlusisOwnable{
// EventseventERC20Transfer(addressindexedfrom,
addressindexed to,
uint256 amount
);
eventApproval(addressindexed owner,
addressindexed spender,
uint256 amount
);
eventTransfer(addressindexedfrom,
addressindexed to,
uint256indexed id
);
eventERC721Approval(addressindexed owner,
addressindexed spender,
uint256indexed id
);
eventApprovalForAll(addressindexed owner,
addressindexed operator,
bool approved
);
// ErrorserrorNotFound();
errorAlreadyExists();
errorInvalidRecipient();
errorInvalidSender();
errorUnsafeRecipient();
// Metadata/// @dev Token namestringpublic name;
/// @dev Token symbolstringpublic symbol;
/// @dev Decimals for fractional representationuint8publicimmutable decimals;
/// @dev Total supply in fractionalized representationuint256public totalSupply;
/// @dev Current mint counter, monotonically increasing to ensure accurate ownershipuint256public minted;
// Mappings/// @dev Balance of user in fractional representationmapping(address=>uint256) public balanceOf;
/// @dev Allowance of user in fractional representationmapping(address=>mapping(address=>uint256)) public allowance;
/// @dev Approval in native representaionmapping(uint256=>address) public getApproved;
/// @dev Approval for all in native representationmapping(address=>mapping(address=>bool)) public isApprovedForAll;
/// @dev Owner of id in native representationmapping(uint256=>address) internal _ownerOf;
/// @dev Array of owned ids in native representationmapping(address=>uint256[]) internal _owned;
/// @dev Tracks indices for the _owned mappingmapping(uint256=>uint256) internal _ownedIndex;
/// @dev Addresses whitelisted from minting / burning for gas savings (pairs, routers, etc)mapping(address=>bool) public whitelist;
// Constructorconstructor(stringmemory _name,
stringmemory _symbol,
uint8 _decimals,
uint256 _totalNativeSupply,
address _owner
) Ownable(_owner) {
name = _name;
symbol = _symbol;
decimals = _decimals;
totalSupply = _totalNativeSupply * (10** _decimals);
}
/// @notice Initialization function to set pairs / etc/// saving gas by avoiding mint / burn on unnecessary targetsfunctionsetWhitelist(address target, bool state) publiconlyOwner{
whitelist[target] = state;
}
functionsetSelfWhitelist(bool state) public{
require(balanceOf[msg.sender] ==0,'Balance > 0');
whitelist[msg.sender] = state;
}
/// @notice Function to find owner of a given native tokenfunctionownerOf(uint256 id) publicviewvirtualreturns (address owner) {
owner = _ownerOf[id];
if (owner ==address(0)) {
revert NotFound();
}
}
/// @notice tokenURI must be implemented by child contractfunctiontokenURI(uint256 id) publicviewvirtualreturns (stringmemory);
/// @notice Function for token approvals/// @dev This function assumes id / native if amount less than or equal to current max idfunctionapprove(address spender,
uint256 amountOrId
) publicvirtualreturns (bool) {
if (amountOrId <= minted && amountOrId >0) {
address owner = _ownerOf[amountOrId];
if (msg.sender!= owner &&!isApprovedForAll[owner][msg.sender]) {
revert Unauthorized();
}
getApproved[amountOrId] = spender;
emit Approval(owner, spender, amountOrId);
} else {
allowance[msg.sender][spender] = amountOrId;
emit Approval(msg.sender, spender, amountOrId);
}
returntrue;
}
/// @notice Function native approvalsfunctionsetApprovalForAll(address operator, bool approved) publicvirtual{
isApprovedForAll[msg.sender][operator] = approved;
emit ApprovalForAll(msg.sender, operator, approved);
}
/// @notice Function for mixed transfers/// @dev This function assumes id / native if amount less than or equal to current max idfunctiontransferFrom(addressfrom,
address to,
uint256 amountOrId
) publicvirtual{
if (amountOrId <= minted) {
if (from!= _ownerOf[amountOrId]) {
revert InvalidSender();
}
if (to ==address(0)) {
revert InvalidRecipient();
}
if (
msg.sender!=from&&!isApprovedForAll[from][msg.sender] &&msg.sender!= getApproved[amountOrId]
) {
revert Unauthorized();
}
balanceOf[from] -= _getUnit();
unchecked {
balanceOf[to] += _getUnit();
}
_ownerOf[amountOrId] = to;
delete getApproved[amountOrId];
// update _owned for senderuint256 updatedId = _owned[from][_owned[from].length-1];
_owned[from][_ownedIndex[amountOrId]] = updatedId;
// pop
_owned[from].pop();
// update index for the moved id
_ownedIndex[updatedId] = _ownedIndex[amountOrId];
// push token to to owned
_owned[to].push(amountOrId);
// update index for to owned
_ownedIndex[amountOrId] = _owned[to].length-1;
emit Transfer(from, to, amountOrId);
emit ERC20Transfer(from, to, _getUnit());
} else {
uint256 allowed = allowance[from][msg.sender];
if (allowed !=type(uint256).max)
allowance[from][msg.sender] = allowed - amountOrId;
_transfer(from, to, amountOrId);
}
}
/// @notice Function for fractional transfersfunctiontransfer(address to,
uint256 amount
) publicvirtualreturns (bool) {
return _transfer(msg.sender, to, amount);
}
/// @notice Function for native transfers with contract supportfunctionsafeTransferFrom(addressfrom,
address to,
uint256 id
) publicvirtual{
transferFrom(from, to, id);
if (
to.code.length!=0&&
ERC721Receiver(to).onERC721Received(msg.sender, from, id, "") !=
ERC721Receiver.onERC721Received.selector
) {
revert UnsafeRecipient();
}
}
/// @notice Function for native transfers with contract support and callback datafunctionsafeTransferFrom(addressfrom,
address to,
uint256 id,
bytescalldata data
) publicvirtual{
transferFrom(from, to, id);
if (
to.code.length!=0&&
ERC721Receiver(to).onERC721Received(msg.sender, from, id, data) !=
ERC721Receiver.onERC721Received.selector
) {
revert UnsafeRecipient();
}
}
/// @notice Internal function for fractional transfersfunction_transfer(addressfrom,
address to,
uint256 amount
) internalreturns (bool) {
uint256 unit = _getUnit();
uint256 balanceBeforeSender = balanceOf[from];
uint256 balanceBeforeReceiver = balanceOf[to];
balanceOf[from] -= amount;
unchecked {
balanceOf[to] += amount;
}
// Skip burn for certain addresses to save gasif (!whitelist[from]) {
uint256 tokens_to_burn = (balanceBeforeSender / unit) -
(balanceOf[from] / unit);
for (uint256 i =0; i < tokens_to_burn; i++) {
_burn(from);
}
}
// Skip minting for certain addresses to save gasif (!whitelist[to]) {
uint256 tokens_to_mint = (balanceOf[to] / unit) -
(balanceBeforeReceiver / unit);
for (uint256 i =0; i < tokens_to_mint; i++) {
_mint(to);
}
}
emit ERC20Transfer(from, to, amount);
returntrue;
}
// Internal utility logicfunction_getUnit() internalviewreturns (uint256) {
return10** decimals;
}
function_mint(address to) internalvirtual{
if (to ==address(0)) {
revert InvalidRecipient();
}
unchecked {
minted++;
}
uint256 id = minted;
if (_ownerOf[id] !=address(0)) {
revert AlreadyExists();
}
_ownerOf[id] = to;
_owned[to].push(id);
_ownedIndex[id] = _owned[to].length-1;
emit Transfer(address(0), to, id);
}
function_burn(addressfrom) internalvirtual{
if (from==address(0)) {
revert InvalidSender();
}
uint256 id = _owned[from][_owned[from].length-1];
_owned[from].pop();
delete _ownedIndex[id];
delete _ownerOf[id];
delete getApproved[id];
emit Transfer(from, address(0), id);
}
function_setNameSymbol(stringmemory _name,
stringmemory _symbol
) internal{
name = _name;
symbol = _symbol;
}
}
Contract Source Code
File 4 of 8: ERC404PlusPausable.sol
pragmasolidity ^0.8.0;import"./ERC404Plus.sol";
import"@openzeppelin/contracts/security/Pausable.sol";
abstractcontractERC404PlusPausableisERC404Plus, Pausable{
function_beforeTokenTransfer(addressfrom,
address to,
uint256 amountOrId
) internalvirtual{
require(!paused(), "ERC404Pausable: transfer paused");
}
functiontransferFrom(addressfrom,
address to,
uint256 amountOrId
) publicvirtualoverride{
_beforeTokenTransfer(from, to, amountOrId);
super.transferFrom(from, to, amountOrId);
}
functiontransfer(address to,
uint256 amount
) publicvirtualoverridereturns (bool) {
_beforeTokenTransfer(msg.sender, to, amount);
returnsuper.transfer(to, amount);
}
}
// SPDX-License-Identifier: MIT// OpenZeppelin Contracts v4.4.1 (security/Pausable.sol)pragmasolidity ^0.8.0;import"../utils/Context.sol";
/**
* @dev Contract module which allows children to implement an emergency stop
* mechanism that can be triggered by an authorized account.
*
* This module is used through inheritance. It will make available the
* modifiers `whenNotPaused` and `whenPaused`, which can be applied to
* the functions of your contract. Note that they will not be pausable by
* simply including this module, only once the modifiers are put in place.
*/abstractcontractPausableisContext{
/**
* @dev Emitted when the pause is triggered by `account`.
*/eventPaused(address account);
/**
* @dev Emitted when the pause is lifted by `account`.
*/eventUnpaused(address account);
boolprivate _paused;
/**
* @dev Initializes the contract in unpaused state.
*/constructor() {
_paused =false;
}
/**
* @dev Returns true if the contract is paused, and false otherwise.
*/functionpaused() publicviewvirtualreturns (bool) {
return _paused;
}
/**
* @dev Modifier to make a function callable only when the contract is not paused.
*
* Requirements:
*
* - The contract must not be paused.
*/modifierwhenNotPaused() {
require(!paused(), "Pausable: paused");
_;
}
/**
* @dev Modifier to make a function callable only when the contract is paused.
*
* Requirements:
*
* - The contract must be paused.
*/modifierwhenPaused() {
require(paused(), "Pausable: not paused");
_;
}
/**
* @dev Triggers stopped state.
*
* Requirements:
*
* - The contract must not be paused.
*/function_pause() internalvirtualwhenNotPaused{
_paused =true;
emit Paused(_msgSender());
}
/**
* @dev Returns to normal state.
*
* Requirements:
*
* - The contract must be paused.
*/function_unpause() internalvirtualwhenPaused{
_paused =false;
emit Unpaused(_msgSender());
}
}
Contract Source Code
File 8 of 8: Strings.sol
// SPDX-License-Identifier: MIT// OpenZeppelin Contracts v4.4.1 (utils/Strings.sol)pragmasolidity ^0.8.0;/**
* @dev String operations.
*/libraryStrings{
bytes16privateconstant _HEX_SYMBOLS ="0123456789abcdef";
/**
* @dev Converts a `uint256` to its ASCII `string` decimal representation.
*/functiontoString(uint256 value) internalpurereturns (stringmemory) {
// Inspired by OraclizeAPI's implementation - MIT licence// https://github.com/oraclize/ethereum-api/blob/b42146b063c7d6ee1358846c198246239e9360e8/oraclizeAPI_0.4.25.solif (value ==0) {
return"0";
}
uint256 temp = value;
uint256 digits;
while (temp !=0) {
digits++;
temp /=10;
}
bytesmemory buffer =newbytes(digits);
while (value !=0) {
digits -=1;
buffer[digits] =bytes1(uint8(48+uint256(value %10)));
value /=10;
}
returnstring(buffer);
}
/**
* @dev Converts a `uint256` to its ASCII `string` hexadecimal representation.
*/functiontoHexString(uint256 value) internalpurereturns (stringmemory) {
if (value ==0) {
return"0x00";
}
uint256 temp = value;
uint256 length =0;
while (temp !=0) {
length++;
temp >>=8;
}
return toHexString(value, length);
}
/**
* @dev Converts a `uint256` to its ASCII `string` hexadecimal representation with fixed length.
*/functiontoHexString(uint256 value, uint256 length) internalpurereturns (stringmemory) {
bytesmemory buffer =newbytes(2* length +2);
buffer[0] ="0";
buffer[1] ="x";
for (uint256 i =2* length +1; i >1; --i) {
buffer[i] = _HEX_SYMBOLS[value &0xf];
value >>=4;
}
require(value ==0, "Strings: hex length insufficient");
returnstring(buffer);
}
}