From 909cf618131c82827cabc5e8ad16656d66cb142d Mon Sep 17 00:00:00 2001 From: KONFeature Date: Thu, 7 Sep 2023 17:04:00 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Add=20direct=20mint=20meth?= =?UTF-8?q?od's?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contracts/minter/IMinter.sol | 6 +++ contracts/minter/Minter.sol | 72 ++++++++++++++++++++++++++++++++++++ test/minter/Minter.t.sol | 34 +++++++++++++++++ 3 files changed, 112 insertions(+) diff --git a/contracts/minter/IMinter.sol b/contracts/minter/IMinter.sol index b65bb8e..62c8060 100644 --- a/contracts/minter/IMinter.sol +++ b/contracts/minter/IMinter.sol @@ -54,6 +54,12 @@ interface IMinter { payable returns (ContentId contentId); + /// @dev Add a new auto minted content in our system + function addAutoMintedContent(address autoMintHolder) external payable returns (ContentId contentId); + + /// @dev Add a content when a creator asked for it + function addContentForCreator(address contentOwnerAddress) external payable returns (ContentId contentId); + /** * @notice Mint a new fraktion for the given amount and user * @dev Will compute the fraktion price, ensure the user have enough Frk to buy it, if try, perform the transfer diff --git a/contracts/minter/Minter.sol b/contracts/minter/Minter.sol index ee907d5..3d15bcd 100644 --- a/contracts/minter/Minter.sol +++ b/contracts/minter/Minter.sol @@ -168,6 +168,78 @@ contract Minter is IMinter, FrakAccessControlUpgradeable, FraktionCostBadges, Mu } } + /// @dev Add an autominted content holder + function addAutoMintedContent(address autoMintHolder) + external + payable + override + onlyRole(FrakRoles.MINTER) + returns (ContentId contentId) + { + // Each typplies to types array + uint256[] memory suppliesToType; + assembly { + // Check owner address + if iszero(autoMintHolder) { + mstore(0x00, _INVALID_ADDRESS_SELECTOR) + revert(0x1c, 0x04) + } + // Init our array's + suppliesToType := mload(0x40) + // Update our free mem pointer + mstore(0x40, add(suppliesToType, 0x40)) + // Init our array's length + mstore(suppliesToType, 1) + // Store the fraktionTypes (only 1 supply of common fraktion) + mstore(add(suppliesToType, 0x20), or(shl(4, 1), 3)) + } + // Try to mint the new content + contentId = fraktionTokens.mintNewContent(autoMintHolder, suppliesToType); + assembly { + // Emit the content minted event + mstore(0, contentId) + log2(0, 0x20, _CONTENT_MINTED_EVENT_SELECTOR, autoMintHolder) + } + } + + /// @dev Add a content for a creator + function addContentForCreator(address contentOwnerAddress) + external + payable + override + onlyRole(FrakRoles.MINTER) + returns (ContentId contentId) + { + // Each typplies to types array + uint256[] memory suppliesToType; + assembly { + // Check owner address + if iszero(contentOwnerAddress) { + mstore(0x00, _INVALID_ADDRESS_SELECTOR) + revert(0x1c, 0x04) + } + // Init our array's + suppliesToType := mload(0x40) + // Update our free mem pointer + mstore(0x40, add(suppliesToType, 0x100)) + // Init our array's length + mstore(suppliesToType, 4) + // Store the fraktionTypes + // We can keep shifting since it will be replaced by constant by the compiler + mstore(add(suppliesToType, 0x20), or(shl(4, 20), 3)) + mstore(add(suppliesToType, 0x40), or(shl(4, 7), 4)) + mstore(add(suppliesToType, 0x60), or(shl(4, 3), 5)) + mstore(add(suppliesToType, 0x80), or(shl(4, 1), 6)) + } + // Try to mint the new content + contentId = fraktionTokens.mintNewContent(contentOwnerAddress, suppliesToType); + assembly { + // Emit the content minted event + mstore(0, contentId) + log2(0, 0x20, _CONTENT_MINTED_EVENT_SELECTOR, contentOwnerAddress) + } + } + /** * @notice Mint a new fraktion for the given amount and user * @dev Will compute the fraktion price, ensure the user have enough Frk to buy it, if try, perform the transfer diff --git a/test/minter/Minter.t.sol b/test/minter/Minter.t.sol index 8ccc99a..650b916 100644 --- a/test/minter/Minter.t.sol +++ b/test/minter/Minter.t.sol @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GNU GPLv3 pragma solidity 0.8.21; +import "forge-std/console.sol"; import { FrakTest } from "../FrakTest.sol"; import { NotAuthorized, InvalidArray } from "contracts/utils/FrakErrors.sol"; import { FraktionTokens } from "contracts/fraktions/FraktionTokens.sol"; @@ -71,6 +72,39 @@ contract MinterTest is FrakTest { minter.addContent(contentOwner, 20, 7, 3, 21); } + /// @dev Different mint method's benchmark + function test_benchmarkAddContent_ok() public asDeployer { + // Warm up storage + minter.addAutoMintedContent(contentOwner); + minter.addContent(contentOwner, 1, 0, 0, 0); + + uint256 gasLeft = gasleft(); + minter.addAutoMintedContent(contentOwner); + uint256 gasUsed = gasLeft - gasleft(); + + console.log("- Automint"); + console.log("-- Automint method used: %d", gasUsed); + + // Build supply for 1 common only + gasLeft = gasleft(); + minter.addContent(contentOwner, 1, 0, 0, 0); + gasUsed = gasLeft - gasleft(); + console.log("-- Classic mint method : %d", gasUsed); + + // Creator mint test + console.log("- Creator"); + gasLeft = gasleft(); + minter.addContentForCreator(contentOwner); + gasUsed = gasLeft - gasleft(); + console.log("-- Creator method used: %d", gasUsed); + + // Build supply for 20, 7, 3, 1 + gasLeft = gasleft(); + minter.addContent(contentOwner, 20, 7, 3, 1); + gasUsed = gasLeft - gasleft(); + console.log("-- Classic mint method : %d", gasUsed); + } + /* -------------------------------------------------------------------------- */ /* Free fraktion mint */ /* -------------------------------------------------------------------------- */