Skip to content

Commit

Permalink
⚡️ Add direct mint method's
Browse files Browse the repository at this point in the history
  • Loading branch information
KONFeature committed Sep 7, 2023
1 parent a0a3f82 commit 909cf61
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
6 changes: 6 additions & 0 deletions contracts/minter/IMinter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
72 changes: 72 additions & 0 deletions contracts/minter/Minter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions test/minter/Minter.t.sol
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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 */
/* -------------------------------------------------------------------------- */
Expand Down

0 comments on commit 909cf61

Please sign in to comment.