Skip to content

Commit

Permalink
Implemented parity in the ERC1155Initializable contract
Browse files Browse the repository at this point in the history
  • Loading branch information
GWSzeto committed Sep 3, 2024
1 parent 630047f commit b9c9510
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 19 deletions.
1 change: 0 additions & 1 deletion src/core/token/ERC1155Core.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {BeforeTransferCallbackERC1155} from "../../callback/BeforeTransferCallba
import {UpdateMetadataCallbackERC1155} from "../../callback/UpdateMetadataCallbackERC1155.sol";

import {OnTokenURICallback} from "../../callback/OnTokenURICallback.sol";
import {console} from "forge-std/console.sol";

contract ERC1155Core is ERC1155, Core, Multicallable, EIP712 {

Expand Down
113 changes: 106 additions & 7 deletions src/core/token/ERC1155CoreInitializable.sol
Original file line number Diff line number Diff line change
@@ -1,21 +1,37 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.20;

import {OwnableRoles} from "@solady/auth/OwnableRoles.sol";
import {ERC1155} from "@solady/tokens/ERC1155.sol";

import {ECDSA} from "@solady/utils/ECDSA.sol";
import {EIP712} from "@solady/utils/EIP712.sol";
import {Initializable} from "@solady/utils/Initializable.sol";
import {Multicallable} from "@solady/utils/Multicallable.sol";

import {Core} from "../../Core.sol";
import {Role} from "../../Role.sol";

import {BeforeApproveForAllCallback} from "../../callback/BeforeApproveForAllCallback.sol";
import {BeforeBatchTransferCallbackERC1155} from "../../callback/BeforeBatchTransferCallbackERC1155.sol";
import {BeforeBurnCallbackERC1155} from "../../callback/BeforeBurnCallbackERC1155.sol";
import {BeforeMintCallbackERC1155} from "../../callback/BeforeMintCallbackERC1155.sol";
import {BeforeMintWithSignatureCallbackERC1155} from "../../callback/BeforeMintWithSignatureCallbackERC1155.sol";
import {BeforeTransferCallbackERC1155} from "../../callback/BeforeTransferCallbackERC1155.sol";
import {UpdateMetadataCallbackERC1155} from "../../callback/UpdateMetadataCallbackERC1155.sol";

import {OnTokenURICallback} from "../../callback/OnTokenURICallback.sol";

contract ERC1155CoreInitializable is ERC1155, Core, Multicallable, Initializable {
contract ERC1155CoreInitializable is ERC1155, Core, Multicallable, Initializable, EIP712 {

using ECDSA for bytes32;

/*//////////////////////////////////////////////////////////////
CONSTANTS
//////////////////////////////////////////////////////////////*/

bytes32 private constant TYPEHASH_SIGNATURE_MINT_ERC1155 =
keccak256("MintRequestERC1155(address to,uint256 tokenId,uint256 value,string baseURI,bytes data)");

/*//////////////////////////////////////////////////////////////
STORAGE
Expand All @@ -40,6 +56,12 @@ contract ERC1155CoreInitializable is ERC1155, Core, Multicallable, Initializable
/// @notice Emitted when the contract URI is updated.
event ContractURIUpdated();

/*//////////////////////////////////////////////////////////////
ERRORS
//////////////////////////////////////////////////////////////*/

error SignatureMintUnauthorized();

/*//////////////////////////////////////////////////////////////
CONSTRUCTOR & INITIALIZER
//////////////////////////////////////////////////////////////*/
Expand Down Expand Up @@ -128,29 +150,37 @@ contract ERC1155CoreInitializable is ERC1155, Core, Multicallable, Initializable
override
returns (SupportedCallbackFunction[] memory supportedCallbackFunctions)
{
supportedCallbackFunctions = new SupportedCallbackFunction[](6);
supportedCallbackFunctions = new SupportedCallbackFunction[](8);
supportedCallbackFunctions[0] = SupportedCallbackFunction({
selector: BeforeMintCallbackERC1155.beforeMintERC1155.selector,
mode: CallbackMode.REQUIRED
});
supportedCallbackFunctions[1] = SupportedCallbackFunction({
selector: BeforeMintWithSignatureCallbackERC1155.beforeMintWithSignatureERC1155.selector,
mode: CallbackMode.REQUIRED
});
supportedCallbackFunctions[2] = SupportedCallbackFunction({
selector: BeforeTransferCallbackERC1155.beforeTransferERC1155.selector,
mode: CallbackMode.OPTIONAL
});
supportedCallbackFunctions[2] = SupportedCallbackFunction({
supportedCallbackFunctions[3] = SupportedCallbackFunction({
selector: BeforeBatchTransferCallbackERC1155.beforeBatchTransferERC1155.selector,
mode: CallbackMode.OPTIONAL
});
supportedCallbackFunctions[3] = SupportedCallbackFunction({
supportedCallbackFunctions[4] = SupportedCallbackFunction({
selector: BeforeBurnCallbackERC1155.beforeBurnERC1155.selector,
mode: CallbackMode.OPTIONAL
});
supportedCallbackFunctions[4] = SupportedCallbackFunction({
supportedCallbackFunctions[5] = SupportedCallbackFunction({
selector: BeforeApproveForAllCallback.beforeApproveForAll.selector,
mode: CallbackMode.OPTIONAL
});
supportedCallbackFunctions[5] =
supportedCallbackFunctions[6] =
SupportedCallbackFunction({selector: OnTokenURICallback.onTokenURI.selector, mode: CallbackMode.REQUIRED});
supportedCallbackFunctions[7] = SupportedCallbackFunction({
selector: UpdateMetadataCallbackERC1155.updateMetadataERC1155.selector,
mode: CallbackMode.REQUIRED
});
}

/*//////////////////////////////////////////////////////////////
Expand All @@ -172,15 +202,57 @@ contract ERC1155CoreInitializable is ERC1155, Core, Multicallable, Initializable
* @param to The address to mint the token to.
* @param tokenId The tokenId to mint.
* @param value The amount of tokens to mint.
* @param baseURI The base URI for the token metadata.
* @param data ABI encoded data to pass to the beforeMint hook.
*/
function mint(address to, uint256 tokenId, uint256 value, bytes memory data) external payable {
function mint(address to, uint256 tokenId, uint256 value, string calldata baseURI, bytes memory data)
external
payable
{
if (bytes(baseURI).length > 0) {
_updateMetadata(to, tokenId, value, baseURI);
}
_beforeMint(to, tokenId, value, data);

_totalSupply[tokenId] += value;
_mint(to, tokenId, value, "");
}

/**
* @notice Mints tokens with a signature. Calls the beforeMintWithSignature hook.
* @dev Reverts if beforeMintWithSignature hook is absent or unsuccessful.
* @param to The address to mint the token to.
* @param tokenId The tokenId to mint.
* @param value The amount of tokens to mint.
* @param baseURI The base URI for the token metadata.
* @param data ABI encoded data to pass to the beforeMintWithSignature hook.
* @param signature The signature produced from signing the minting request.
*/
function mintWithSignature(
address to,
uint256 tokenId,
uint256 value,
string calldata baseURI,
bytes calldata data,
bytes memory signature
) external payable {
address signer = _hashTypedData(
keccak256(abi.encode(TYPEHASH_SIGNATURE_MINT_ERC1155, to, tokenId, value, keccak256(bytes(baseURI)), data))
).recover(signature);

if (!OwnableRoles(address(this)).hasAllRoles(signer, Role._MINTER_ROLE)) {
revert SignatureMintUnauthorized();
}

if (bytes(baseURI).length > 0) {
_updateMetadata(to, tokenId, value, baseURI);
}
_beforeMintWithSignature(to, tokenId, value, data);

_totalSupply[tokenId] += value;
_mint(to, tokenId, value, "");
}

/**
* @notice Burns given amount of tokens.
* @dev Calls the beforeBurn hook. Skips calling the hook if it doesn't exist.
Expand Down Expand Up @@ -264,6 +336,19 @@ contract ERC1155CoreInitializable is ERC1155, Core, Multicallable, Initializable
);
}

/// @dev Calls the beforeMintWithSignature hook.
function _beforeMintWithSignature(address to, uint256 tokenId, uint256 value, bytes calldata data)
internal
virtual
{
_executeCallbackFunction(
BeforeMintWithSignatureCallbackERC1155.beforeMintWithSignatureERC1155.selector,
abi.encodeCall(
BeforeMintWithSignatureCallbackERC1155.beforeMintWithSignatureERC1155, (to, tokenId, value, data)
)
);
}

/// @dev Calls the beforeTransfer hook, if installed.
function _beforeTransfer(address from, address to, uint256 tokenId, uint256 value) internal virtual {
_executeCallbackFunction(
Expand Down Expand Up @@ -307,4 +392,18 @@ contract ERC1155CoreInitializable is ERC1155, Core, Multicallable, Initializable
tokenUri = abi.decode(returndata, (string));
}

/// @dev Calls the updateMetadata hook, if installed.
function _updateMetadata(address to, uint256 tokenId, uint256 value, string calldata baseURI) internal virtual {
_executeCallbackFunction(
UpdateMetadataCallbackERC1155.updateMetadataERC1155.selector,
abi.encodeCall(UpdateMetadataCallbackERC1155.updateMetadataERC1155, (to, tokenId, value, baseURI))
);
}

/// @dev Returns the domain name and version for EIP712.
function _domainNameAndVersion() internal pure override returns (string memory name, string memory version) {
name = "ERC1155Core";
version = "1";
}

}
1 change: 0 additions & 1 deletion src/core/token/ERC721Core.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
pragma solidity ^0.8.20;

import {ERC721A, ERC721AQueryable, IERC721A} from "@erc721a/extensions/ERC721AQueryable.sol";
import {console} from "forge-std/console.sol";

import {OwnableRoles} from "@solady/auth/OwnableRoles.sol";
import {ECDSA} from "@solady/utils/ECDSA.sol";
Expand Down
1 change: 0 additions & 1 deletion src/module/token/metadata/BatchMetadataERC721.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {Role} from "../../../Role.sol";

import {UpdateMetadataCallbackERC721} from "../../../callback/UpdateMetadataCallbackERC721.sol";
import {LibString} from "@solady/utils/LibString.sol";
import {console} from "forge-std/console.sol";

library BatchMetadataStorage {

Expand Down
5 changes: 0 additions & 5 deletions src/module/token/minting/MintableERC721.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
pragma solidity ^0.8.20;

import {Module} from "../../../Module.sol";
import {console} from "forge-std/console.sol";

import {Role} from "../../../Role.sol";
import {IInstallationCallback} from "../../../interface/IInstallationCallback.sol";
Expand Down Expand Up @@ -153,7 +152,6 @@ contract MintableERC721 is
MintRequestERC721 memory _params = abi.decode(_data, (MintRequestERC721));

_mintWithSignatureERC721(_params);
console.log("gets in here");
_distributeMintPrice(msg.sender, _params.currency, _quantity * _params.pricePerUnit);
}

Expand Down Expand Up @@ -233,16 +231,13 @@ contract MintableERC721 is
}
return;
}
console.log("passes initial test");

SaleConfig memory saleConfig = _mintableStorage().saleConfig;

if (_currency == NATIVE_TOKEN_ADDRESS) {
console.log("native token detected");
if (msg.value != _price) {
revert MintableIncorrectNativeTokenSent();
}
console.log("shouldn't get here");
SafeTransferLib.safeTransferETH(saleConfig.primarySaleRecipient, _price);
} else {
if (msg.value > 0) {
Expand Down
3 changes: 0 additions & 3 deletions test/module/minting/ClaimableERC1155.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -343,9 +343,6 @@ contract ClaimableERC1155Test is Test {

vm.prank(tokenRecipient);
vm.expectRevert(abi.encodeWithSelector(ClaimableERC1155.ClaimableIncorrectNativeTokenSent.selector));
// fails here
console.log("permissionedActorAddress");
console.logAddress(permissionedActor);
core.mintWithSignature{value: (quantity * claimRequest.pricePerUnit)}(
tokenRecipient, tokenId, quantity, baseURI, abi.encode(claimRequest), sig
);
Expand Down
1 change: 0 additions & 1 deletion test/module/minting/MintableERC721.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import "lib/forge-std/src/console.sol";
import {OwnableRoles} from "@solady/auth/OwnableRoles.sol";
import {ERC20} from "@solady/tokens/ERC20.sol";
import {Test} from "forge-std/Test.sol";
import {console} from "forge-std/console.sol";

// Target contract

Expand Down

0 comments on commit b9c9510

Please sign in to comment.